From 2f4fdc8cb4a84ca33b6131c608b978dd780dfb7c Mon Sep 17 00:00:00 2001 From: Ivan Davidov Date: Sun, 10 Apr 2016 04:09:27 +0300 Subject: [PATCH] Added overlay support which mounts /minimal/rootfs if it is found on the boot media. Note that this doesn't work on vfat. Works fine on CD image and on separate ext2 partition. --- src/02_build_kernel.sh | 3 ++ src/08_generate_rootfs.sh | 68 ++++++++++++++++++++++++++++++++------- src/11_generate_iso.sh | 8 +++++ src/qemu32.sh | 2 +- src/qemu64.sh | 2 +- 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/02_build_kernel.sh b/src/02_build_kernel.sh index 65ebe9f75..c48a546b3 100755 --- a/src/02_build_kernel.sh +++ b/src/02_build_kernel.sh @@ -14,6 +14,9 @@ make defconfig # Changes the name of the system to 'minimal'. sed -i "s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=\"minimal\"/" .config +# Enable overlay support, e.g. merge ro and rw directories. +sed -i "s/.*CONFIG_OVERLAY_FS.*/CONFIG_OVERLAY_FS=y/" .config + # Compile the kernel with optimization for "parallel jobs" = "number of processors". # Good explanation of the different kernels: # http://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vmlinux diff --git a/src/08_generate_rootfs.sh b/src/08_generate_rootfs.sh index 1446f6bdc..54ca25ad6 100755 --- a/src/08_generate_rootfs.sh +++ b/src/08_generate_rootfs.sh @@ -54,8 +54,8 @@ EOF chmod +x prepare.sh # The script '/etc/switch.sh' is automatically executed as part of the '/init' -# process. We copy all files/folders to new mountpoint and then execute the -# command 'switch_root'. +# process. We overlay the '/minimal/rootfs' folder from the boot media, copy all +# files and folders to new mountpoint and then execute the command 'switch_root'. cat > switch.sh << EOF #!/bin/sh @@ -63,28 +63,74 @@ cat > switch.sh << EOF mount -t tmpfs none /mnt # Create folders for all crytical file systems. +echo "Create folders for all crytical file systems..." mkdir /mnt/dev mkdir /mnt/sys mkdir /mnt/proc mkdir /mnt/tmp +echo "...done." -# Move all crytical file systems in the new mountpoint. +# Copy root folders in the new mountpoint. +echo "Copying the root file system to /mnt..." +cp -a bin etc lib lib64 root sbin src usr /mnt +echo "...done." + +mkdir /tmp/mnt +echo "Searching available devices for /minimal/rootfs folder..." +for DEVICE in /dev/* ; do + DEV=\$(echo "\${DEVICE##*/}") + SYSDEV=\$(echo "/sys/class/block/\$DEV") + + case \$DEV in + *loop*) continue ;; + esac + + if [ ! -d "\$SYSDEV" ] ; then + continue + fi + + mount \$DEVICE /tmp/mnt 2>/dev/null + if [ -d /tmp/mnt/minimal/rootfs ] ; then + echo " Found /minimal/rootfs folder on device \$DEV." + + touch /tmp/mnt/minimal/rootfs/minimal.pid 2>/dev/null + if [ -f /tmp/mnt/minimal/rootfs/minimal.pid ] ; then + echo " Trying to overlay in read/write mode..." + rm -f /tmp/mnt/minimal/rootfs/minimal.pid + mount -t overlay -o lowerdir=/mnt,upperdir=/tmp/mnt/minimal/rootfs,workdir=/tmp/mnt/minimal/work none /mnt + OUT=\$? + if [ ! "\$OUT" = "0" ] ; then + echo " Mount failed (probably on vfat), moving on with other devices." + continue + fi + else + echo " Trying to overlay in read only mode..." + mkdir -p /tmp/minimal/work + mkdir -p /tmp/minimal/rootfs + mount -t overlay -o lowerdir=/mnt:/tmp/mnt/minimal/rootfs,upperdir=/tmp/minimal/rootfs,workdir=/tmp/minimal/work none /mnt + fi + + echo " ...done." + + break + else + umount /tmp/mnt 2>/dev/null + fi +done +echo "...done." + +# Move crytical file systems to the new mountpoint. echo "Remounting /dev, /sys, /tmp and /proc in /mnt..." mount --move /dev /mnt/dev mount --move /sys /mnt/sys -mount --move /tmp /mnt/tmp mount --move /proc /mnt/proc -echo "...done." - -# Copy all root folders in the new mountpoint. -echo "Moving the rest of the root file system to /mnt..." -cp -a bin etc lib lib64 root sbin src usr /mnt +mount --move /tmp /mnt/tmp echo "...done." # The new mountpoint becomes file system root. All original root folders are # deleted automatically as part of the command execution. The '/sbin/init' -# process is invoked and it becomes the new PID 1 parent process. -exec switch_root /mnt/ /sbin/init +# process is invoked and it becomes the new PID 1 parent process. +exec switch_root /mnt /sbin/init echo "You can never see this... unless there is a serious bug..." sleep 99999 diff --git a/src/11_generate_iso.sh b/src/11_generate_iso.sh index bebe36154..b6ede9699 100755 --- a/src/11_generate_iso.sh +++ b/src/11_generate_iso.sh @@ -41,6 +41,14 @@ chmod +rx src/*.sh chmod +r src/.config chmod +r src/*.txt +# Create the overlay directory '/minimal'. All files in this folder are merged +# in the root folder and can be manipulated thanks to overlayfs. +mkdir -p minimal/rootfs +cd minimal/rootfs +echo 'Sample file 1 from CD.' > file_from_cd_1.txt +echo 'Sample file 2 from CD.' > file_from_cd_2.txt +cd ../.. + # Create ISOLINUX configuration file. echo 'default kernel.bz initrd=rootfs.gz' > ./isolinux.cfg diff --git a/src/qemu32.sh b/src/qemu32.sh index 3d0ca2a92..aae299241 100755 --- a/src/qemu32.sh +++ b/src/qemu32.sh @@ -1,4 +1,4 @@ #!/bin/sh -qemu-system-i386 -m 64M -cdrom minimal_linux_live.iso +qemu-system-i386 -m 64M -hda hdd.img -cdrom minimal_linux_live.iso -boot d diff --git a/src/qemu64.sh b/src/qemu64.sh index 34392b5ce..2af546eff 100755 --- a/src/qemu64.sh +++ b/src/qemu64.sh @@ -1,4 +1,4 @@ #!/bin/sh -qemu-system-x86_64 -m 64M -cdrom minimal_linux_live.iso +qemu-system-x86_64 -m 64M -hda hdd.img -cdrom minimal_linux_live.iso -boot d