From 1943629bb434b2595ff57bdf3f43716e2d07ab84 Mon Sep 17 00:00:00 2001 From: Ivan Davidov Date: Wed, 29 Nov 2017 22:05:34 +0200 Subject: [PATCH] The ISO image generation logic has been split in multiple scripts. --- src/11_generate_overlay.sh | 92 +++++++++ src/12_generate_iso.sh | 175 ------------------ ...{11_get_syslinux.sh => 12_get_syslinux.sh} | 2 +- src/13_prepare_iso.sh | 62 +++++++ src/14_generate_iso.sh | 45 +++++ ...generate_image.sh => 15_generate_image.sh} | 0 src/rebuild.sh | 6 +- 7 files changed, 204 insertions(+), 178 deletions(-) create mode 100755 src/11_generate_overlay.sh delete mode 100755 src/12_generate_iso.sh rename src/{11_get_syslinux.sh => 12_get_syslinux.sh} (95%) create mode 100755 src/13_prepare_iso.sh create mode 100755 src/14_generate_iso.sh rename src/{13_generate_image.sh => 15_generate_image.sh} (100%) diff --git a/src/11_generate_overlay.sh b/src/11_generate_overlay.sh new file mode 100755 index 000000000..73b7bf3d5 --- /dev/null +++ b/src/11_generate_overlay.sh @@ -0,0 +1,92 @@ +#!/bin/sh + +set -e + +echo "*** GENERATE OVERLAY BEGIN ***" + +SRC_DIR=$(pwd) + +# Remove the old ISO generation area if it exists. +echo "Removing old overlay area. This may take a while." +rm -rf work/isoimage_overlay +mkdir -p work/isoimage_overlay +cd work/isoimage_overlay + +# Read the 'OVERLAY_TYPE' property from '.config' +OVERLAY_TYPE="$(grep -i ^OVERLAY_TYPE $SRC_DIR/.config | cut -f2 -d'=')" + +# Read the 'OVERLAY_LOCATION' property from '.config' +OVERLAY_LOCATION="$(grep -i ^OVERLAY_LOCATION $SRC_DIR/.config | cut -f2 -d'=')" + +if [ "$OVERLAY_LOCATION" = "iso" \ + -a "$OVERLAY_TYPE" = "sparse" \ + -a -d $SRC_DIR/work/overlay_rootfs \ + -a "$(id -u)" = "0" ] ; then + + # Use sparse file as storage place. The above check guarantees that the whole + # script is executed with root permissions or otherwise this block is skipped. + # All files and folders located in the folder 'minimal_overlay' will be merged + # with the root folder on boot. + + echo "Using sparse file for overlay." + + # This is the BusyBox executable that we have already generated. + BUSYBOX=../rootfs/bin/busybox + + # Create sparse image file with 1MB size. Note that this increases the ISO + # image size. + $BUSYBOX truncate -s 1M minimal.img + + # Find available loop device. + LOOP_DEVICE=$($BUSYBOX losetup -f) + + # Associate the available loop device with the sparse image file. + $BUSYBOX losetup $LOOP_DEVICE minimal.img + + # Format the sparse image file with Ext2 file system. + $BUSYBOX mkfs.ext2 $LOOP_DEVICE + + # Mount the sparse file in folder 'sparse". + mkdir sparse + $BUSYBOX mount minimal.img sparse + + # Create the overlay folders. + mkdir -p sparse/rootfs + mkdir -p sparse/work + + # Copy the overlay content. + cp -r $SRC_DIR/overlay_rootfs/* sparse/rootfs + cp -r $SRC_DIR/minimal_overlay/rootfs/* sparse/rootfs + + # Unmount the sparse file and delete the temporary folder. + $BUSYBOX umount sparse + rm -rf sparse + + # Detach the loop device since we no longer need it. + $BUSYBOX losetup -d $LOOP_DEVICE + + echo "Applying original ownership to all affected files and folders." + chown -R $(logname) . +elif [ "$OVERLAY_LOCATION" = "iso" \ + -a "$OVERLAY_TYPE" = "folder" \ + -a -d $SRC_DIR/work/overlay_rootfs ] ; then + + # Use normal folder structure for overlay. All files and folders located in + # the folder 'minimal_overlay' will be merged with the root folder on boot. + + echo "Using folder structure for overlay." + + # Create the overlay folders. + mkdir -p minimal/rootfs + mkdir -p minimal/work + + # Copy the overlay content. + cp -rf $SRC_DIR/work/overlay_rootfs/* minimal/rootfs + cp -r $SRC_DIR/minimal_overlay/rootfs/* minimal/rootfs +else + echo "The ISO image will have no overlay structure." +fi + +cd $SRC_DIR + +echo "*** GENERATE OVERLAY END ***" diff --git a/src/12_generate_iso.sh b/src/12_generate_iso.sh deleted file mode 100755 index a1d2f387e..000000000 --- a/src/12_generate_iso.sh +++ /dev/null @@ -1,175 +0,0 @@ -#!/bin/sh - -set -e - -# TODO - this shell script file needs serios refactoring since right now it does -# too many things: -# -# 1) Create proper overlay structure. -# 2) Prepare the actual ISO structure. -# 3) Generate the actual ISO image. -# -# Probably it's best to create separate shell scripts for each functionality. - -echo "*** GENERATE ISO BEGIN ***" - -SRC_DIR=$(pwd) - -# Save the kernel installation directory. -KERNEL_INSTALLED=$SRC_DIR/work/kernel/kernel_installed - -# Find the Syslinux build directory. -cd work/syslinux -cd $(ls -d *) -WORK_SYSLINUX_DIR=$(pwd) -cd $SRC_DIR - -# Remove the old ISO file if it exists. -rm -f minimal_linux_live.iso -echo "Old ISO image file has been removed." - -# Remove the old ISO generation area if it exists. -echo "Removing old ISO image work area. This may take a while..." -rm -rf work/isoimage - -# This is the root folder of the ISO image. -mkdir work/isoimage -echo "Prepared new ISO image work area." - -# Read the 'COPY_SOURCE_ISO' property from '.config' -COPY_SOURCE_ISO="$(grep -i ^COPY_SOURCE_ISO .config | cut -f2 -d'=')" - -cd work/isoimage - -# Now we copy the kernel. -cp $KERNEL_INSTALLED/kernel ./kernel.xz - -# Now we copy the root file system. -cp ../rootfs.cpio.xz ./rootfs.xz - -# Read the 'OVERLAY_TYPE' property from '.config' -OVERLAY_TYPE="$(grep -i ^OVERLAY_TYPE $SRC_DIR/.config | cut -f2 -d'=')" - -# Read the 'OVERLAY_LOCATION' property from '.config' -OVERLAY_LOCATION="$(grep -i ^OVERLAY_LOCATION $SRC_DIR/.config | cut -f2 -d'=')" - -if [ "$OVERLAY_LOCATION" = "iso" \ - -a "$OVERLAY_TYPE" = "sparse" \ - -a -d $SRC_DIR/work/overlay_rootfs \ - -a "$(id -u)" = "0" ] ; then - - # Use sparse file as storage place. The above check guarantees that the whole - # script is executed with root permissions or otherwise this block is skipped. - # All files and folders located in the folder 'minimal_overlay' will be merged - # with the root folder on boot. - - echo "Using sparse file for overlay." - - # This is the BusyBox executable that we have already generated. - BUSYBOX=../rootfs/bin/busybox - - # Create sparse image file with 1MB size. Note that this increases the ISO - # image size. - $BUSYBOX truncate -s 1M minimal.img - - # Find available loop device. - LOOP_DEVICE=$($BUSYBOX losetup -f) - - # Associate the available loop device with the sparse image file. - $BUSYBOX losetup $LOOP_DEVICE minimal.img - - # Format the sparse image file with Ext2 file system. - $BUSYBOX mkfs.ext2 $LOOP_DEVICE - - # Mount the sparse file in folder 'sparse". - mkdir sparse - $BUSYBOX mount minimal.img sparse - - # Create the overlay folders. - mkdir -p sparse/rootfs - mkdir -p sparse/work - - # Copy the overlay content. - cp -r $SRC_DIR/overlay_rootfs/* sparse/rootfs - cp -r $SRC_DIR/minimal_overlay/rootfs/* sparse/rootfs - - # Unmount the sparse file and delete the temporary folder. - $BUSYBOX umount sparse - rm -rf sparse - - # Detach the loop device since we no longer need it. - $BUSYBOX losetup -d $LOOP_DEVICE -elif [ "$OVERLAY_LOCATION" = "iso" \ - -a "$OVERLAY_TYPE" = "folder" \ - -a -d $SRC_DIR/work/overlay_rootfs ] ; then - - # Use normal folder structure for overlay. All files and folders located in - # the folder 'minimal_overlay' will be merged with the root folder on boot. - - echo "Using folder structure for overlay." - - # Create the overlay folders. - mkdir -p minimal/rootfs - mkdir -p minimal/work - - # Copy the overlay content. - cp -rf $SRC_DIR/work/overlay_rootfs/* minimal/rootfs - cp -r $SRC_DIR/minimal_overlay/rootfs/* minimal/rootfs -else - echo "The ISO image will have no overlay structure." -fi - -# Copy the precompiled files 'isolinux.bin' and 'ldlinux.c32' in the ISO image -# root folder. -cp $WORK_SYSLINUX_DIR/bios/core/isolinux.bin . -cp $WORK_SYSLINUX_DIR/bios/com32/elflink/ldlinux/ldlinux.c32 . - -# Create the ISOLINUX configuration file. -echo 'default kernel.xz initrd=rootfs.xz vga=ask' > ./syslinux.cfg - -# Create UEFI start script. -mkdir -p efi/boot -cat << CEOF > ./efi/boot/startup.nsh -echo -off -echo Minimal Linux Live is starting... -\\kernel.xz initrd=\\rootfs.xz -CEOF - -# Now we generate the ISO image file. -xorriso \ - -as mkisofs \ - -R \ - -r \ - -o ../minimal_linux_live.iso \ - -b isolinux.bin \ - -c boot.cat \ - -input-charset UTF-8 \ - -no-emul-boot \ - -boot-load-size 4 \ - -boot-info-table \ - ./ - -# Copy the ISO image to the root project folder. -cp ../minimal_linux_live.iso ../../ - -if [ "$(id -u)" = "0" ] ; then - # Apply ownership back to original owner for all affected files. - chown $(logname) ../../minimal_linux_live.iso - chown $(logname) ../../work/minimal_linux_live.iso - chown -R $(logname) . - echo "Applied original ownership to all affected files and folders." -fi - -cd $SRC_DIR - -cat << CEOF - - ################################################################# - # # - # ISO image file 'minimal_linux_live.iso' has been generated. # - # # - ################################################################# - -CEOF - -echo "*** GENERATE ISO END ***" diff --git a/src/11_get_syslinux.sh b/src/12_get_syslinux.sh similarity index 95% rename from src/11_get_syslinux.sh rename to src/12_get_syslinux.sh index ef3c09ae4..9deb0e1fe 100755 --- a/src/11_get_syslinux.sh +++ b/src/12_get_syslinux.sh @@ -30,7 +30,7 @@ else fi # Delete folder with previously extracted Syslinux. -echo "Removing SYSLINUX work area. This may take a while..." +echo "Removing SYSLINUX work area. This may take a while." rm -rf ../work/syslinux mkdir ../work/syslinux diff --git a/src/13_prepare_iso.sh b/src/13_prepare_iso.sh new file mode 100755 index 000000000..bb4bb0df1 --- /dev/null +++ b/src/13_prepare_iso.sh @@ -0,0 +1,62 @@ +#!/bin/sh + +set -e + +echo "*** PREPARE ISO BEGIN ***" + +SRC_DIR=$(pwd) + +# Save the kernel installation directory. +KERNEL_INSTALLED=$SRC_DIR/work/kernel/kernel_installed + +# Find the Syslinux build directory. +cd work/syslinux +cd $(ls -d *) +WORK_SYSLINUX_DIR=$(pwd) +cd $SRC_DIR + +# Remove the old ISO generation area if it exists. +echo "Removing old ISO image work area. This may take a while." +rm -rf work/isoimage + +# This is the root folder of the ISO image. +echo "Preparing new ISO image work area." +mkdir work/isoimage + +cd work/isoimage + +# Now we copy the kernel. +cp $KERNEL_INSTALLED/kernel ./kernel.xz + +# Now we copy the root file system. +cp ../rootfs.cpio.xz ./rootfs.xz + +# Now we copy the overlay content if it exists +if [ -d $SRC_DIR/work/isoimage_overlay \ + -a ! "`ls $SRC_DIR/work/isoimage_overlay`" = "" ] ; then + + echo "The ISO image will have overlay structure." + cp -r $SRC_DIR/work/isoimage_overlay/* . +else + echo "The ISO image will have no overlay structure." +fi + +# Copy the precompiled files 'isolinux.bin' and 'ldlinux.c32' in the ISO image +# root folder. +cp $WORK_SYSLINUX_DIR/bios/core/isolinux.bin . +cp $WORK_SYSLINUX_DIR/bios/com32/elflink/ldlinux/ldlinux.c32 . + +# Create the ISOLINUX configuration file. +echo 'default kernel.xz initrd=rootfs.xz vga=ask' > ./syslinux.cfg + +# Create UEFI start script. +mkdir -p efi/boot +cat << CEOF > ./efi/boot/startup.nsh +echo -off +echo Minimal Linux Live is starting. +\\kernel.xz initrd=\\rootfs.xz +CEOF + +cd $SRC_DIR + +echo "*** PREPARE ISO END ***" diff --git a/src/14_generate_iso.sh b/src/14_generate_iso.sh new file mode 100755 index 000000000..d2b0a10a3 --- /dev/null +++ b/src/14_generate_iso.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +set -e + +echo "*** GENERATE ISO BEGIN ***" + +SRC_DIR=$(pwd) + +if [ ! -d $SRC_DIR/work/isoimage ] ; then + echo "Cannot locate ISO image work folder. Cannot continue." + exit 1 +fi + +cd $SRC_DIR/work/isoimage + +# Now we generate the ISO image file. +xorriso \ + -as mkisofs \ + -R \ + -r \ + -o ../minimal_linux_live.iso \ + -b isolinux.bin \ + -c boot.cat \ + -input-charset UTF-8 \ + -no-emul-boot \ + -boot-load-size 4 \ + -boot-info-table \ + ./ + +# Copy the ISO image to the root project folder. +cp ../minimal_linux_live.iso ../../ + +cd $SRC_DIR + +cat << CEOF + + ################################################################# + # # + # ISO image file 'minimal_linux_live.iso' has been generated. # + # # + ################################################################# + +CEOF + +echo "*** GENERATE ISO END ***" diff --git a/src/13_generate_image.sh b/src/15_generate_image.sh similarity index 100% rename from src/13_generate_image.sh rename to src/15_generate_image.sh diff --git a/src/rebuild.sh b/src/rebuild.sh index ede001def..5c6d3949b 100755 --- a/src/rebuild.sh +++ b/src/rebuild.sh @@ -10,5 +10,7 @@ set -e ./08_prepare_bundles.sh ./09_generate_rootfs.sh ./10_pack_rootfs.sh -./12_generate_iso.sh -./13_generate_image.sh +./11_generate_overlay.sh +./13_prepare_iso.sh +./14_generate_iso.sh +./15_generate_image.sh