#!/bin/sh # System initialization sequence: # # /init # | # +--(1) /etc/01_prepare.sh # | # +--(2) /etc/02_overlay.sh (this file) # | # +-- /etc/03_init.sh # | # +-- /sbin/init # | # +--(1) /etc/04_bootscript.sh # | | # | +-- udhcpc # | | # | +-- /etc/05_rc.udhcp # | # +--(2) /bin/sh (Alt + F1, main console) # | # +--(2) /bin/sh (Alt + F2) # | # +--(2) /bin/sh (Alt + F3) # | # +--(2) /bin/sh (Alt + F4) # Create the new mountpoint in RAM. mount -t tmpfs none /mnt # Create folders for all critical file systems. mkdir /mnt/dev mkdir /mnt/sys mkdir /mnt/proc mkdir /mnt/tmp mkdir /mnt/var echo "Created folders for all critical file systems." # 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 var /mnt !2 >/dev/null global DEFAULT_OVERLAY_DIR := '"/tmp/minimal/overlay'" global DEFAULT_UPPER_DIR := '"/tmp/minimal/rootfs'" global DEFAULT_WORK_DIR := '"/tmp/minimal/work'" echo "Searching available devices for overlay content..." for DEVICE in [/dev/*] { global DEV := $[echo $(DEVICE##*/)] global SYSDEV := $[echo "/sys/class/block/$DEV] matchstr $DEV { *loop* { continue } } if test ! -d $SYSDEV { continue } mkdir -p /tmp/mnt/device global DEVICE_MNT := '/tmp/mnt/device' global OVERLAY_DIR := ''"" global OVERLAY_MNT := ''"" global UPPER_DIR := ''"" global WORK_DIR := ''"" mount $DEVICE $DEVICE_MNT !2 >/dev/null if test -d $DEVICE_MNT/minimal/rootfs -a -d $DEVICE_MNT/minimal/work { # folder echo " Found '/minimal' folder on device '$DEVICE'." touch $DEVICE_MNT/minimal/rootfs/minimal.pid !2 >/dev/null if test -f $DEVICE_MNT/minimal/rootfs/minimal.pid { # read/write mode echo " Device '$DEVICE' is mounted in read/write mode." rm -f $DEVICE_MNT/minimal/rootfs/minimal.pid global OVERLAY_DIR := $DEFAULT_OVERLAY_DIR global OVERLAY_MNT := $DEVICE_MNT global UPPER_DIR := "$DEVICE_MNT/minimal/rootfs" global WORK_DIR := "$DEVICE_MNT/minimal/work" } else { # read only mode echo " Device '$DEVICE' is mounted in read only mode." global OVERLAY_DIR := "$DEVICE_MNT/minimal/rootfs" global OVERLAY_MNT := $DEVICE_MNT global UPPER_DIR := $DEFAULT_UPPER_DIR global WORK_DIR := $DEFAULT_WORK_DIR } } elif test -f $DEVICE_MNT/minimal.img { #image echo " Found '/minimal.img' image on device '$DEVICE'." mkdir -p /tmp/mnt/image global IMAGE_MNT := '/tmp/mnt/image' global LOOP_DEVICE := $[losetup -f] losetup $LOOP_DEVICE $DEVICE_MNT/minimal.img mount $LOOP_DEVICE $IMAGE_MNT if test -d $IMAGE_MNT/rootfs -a -d $IMAGE_MNT/work { touch $IMAGE_MNT/rootfs/minimal.pid !2 >/dev/null if test -f $IMAGE_MNT/rootfs/minimal.pid { # read/write mode echo " Image '$DEVICE/minimal.img' is mounted in read/write mode." rm -f $IMAGE_MNT/rootfs/minimal.pid global OVERLAY_DIR := $DEFAULT_OVERLAY_DIR global OVERLAY_MNT := $IMAGE_MNT global UPPER_DIR := "$IMAGE_MNT/rootfs" global WORK_DIR := "$IMAGE_MNT/work" } else { # read only mode echo " Image '$DEVICE/minimal.img' is mounted in read only mode." global OVERLAY_DIR := "$IMAGE_MNT/rootfs" global OVERLAY_MNT := $IMAGE_MNT global UPPER_DIR := $DEFAULT_UPPER_DIR global WORK_DIR := $DEFAULT_WORK_DIR } } else { umount $IMAGE_MNT rm -rf $IMAGE_MNT } } if test $OVERLAY_DIR != "" -a $UPPER_DIR != "" -a $WORK_DIR != "" { mkdir -p $OVERLAY_DIR mkdir -p $UPPER_DIR mkdir -p $WORK_DIR mount -t overlay -o lowerdir=$OVERLAY_DIR:/mnt,upperdir=$UPPER_DIR,workdir=$WORK_DIR none /mnt !2 >/dev/null global OUT := $Status if test ! $OUT = "0" { echo " Mount failed (probably on vfat)." umount $OVERLAY_MNT !2 >/dev/null rmdir $OVERLAY_MNT !2 >/dev/null rmdir $DEFAULT_OVERLAY_DIR !2 >/dev/null rmdir $DEFAULT_UPPER_DIR !2 >/dev/null rmdir $DEFAULT_WORK_DIR !2 >/dev/null } else { # All done, time to go. echo " Overlay data from device '$DEVICE' has been merged." break } } else { echo " Device '$DEVICE' has no proper overlay structure." } umount $DEVICE_MNT !2 >/dev/null rm -rf $DEVICE_MNT !2 >/dev/null } # Move critical file systems to the new mountpoint. mount --move /dev /mnt/dev mount --move /sys /mnt/sys mount --move /proc /mnt/proc mount --move /tmp /mnt/tmp echo "Mount locations /dev, /sys, /tmp and /proc have been moved to /mnt." # 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. echo "Switching from initramfs root area to overlayfs root area." exec switch_root /mnt /etc/03_init.sh echo "(/etc/02_overlay.sh) - there is a serious bug..." # Wait until any key has been pressed. read -n1 -s (CommandList children: [ (C {(mount)} {(-t)} {(tmpfs)} {(none)} {(/mnt)}) (C {(mkdir)} {(/mnt/dev)}) (C {(mkdir)} {(/mnt/sys)}) (C {(mkdir)} {(/mnt/proc)}) (C {(mkdir)} {(/mnt/tmp)}) (C {(mkdir)} {(/mnt/var)}) (C {(echo)} {(DQ ("Created folders for all critical file systems."))}) (C {(echo)} {(DQ ("Copying the root file system to /mnt..."))}) (SimpleCommand words: [{(cp)} {(-a)} {(bin)} {(etc)} {(lib)} {(lib64)} {(root)} {(sbin)} {(src)} {(usr)} {(var)} {(/mnt)}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[157])] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DEFAULT_OVERLAY_DIR) op: Equal rhs: {(DQ (/tmp/minimal/overlay))} spids: [161] ) ] spids: [161] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DEFAULT_UPPER_DIR) op: Equal rhs: {(DQ (/tmp/minimal/rootfs))} spids: [166] ) ] spids: [166] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DEFAULT_WORK_DIR) op: Equal rhs: {(DQ (/tmp/minimal/work))} spids: [171] ) ] spids: [171] ) (C {(echo)} {(DQ ("Searching available devices for overlay content..."))}) (ForEach iter_name: DEVICE iter_words: [{(/dev/) (Lit_Other "*")}] do_arg_iter: False body: (DoGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DEV) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(echo)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VOp1_DPound arg_word: {("*") (Lit_Slash /)} ) spids: [202 207] ) ) } ) ] ) left_token: spids: [198 209] ) } spids: [197] ) ] spids: [197] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SYSDEV) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(echo)} {(DQ (/sys/class/block/) ($ VSub_Name "$DEV"))})] ) left_token: spids: [213 220] ) } spids: [212] ) ] spids: [212] ) (Case to_match: {($ VSub_Name "$DEV")} arms: [ (case_arm pat_list: [{(Lit_Other "*") (loop) (Lit_Other "*")}] action: [(ControlFlow token:)] spids: [231 234 238 -1] ) ] spids: [224 228 241] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(KW_Bang "!")} {(-d)} {(DQ ($ VSub_Name "$SYSDEV"))} {(Lit_Other "]")} ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 261] ) ] spids: [-1 267] ) (C {(mkdir)} {(-p)} {(/tmp/mnt/device)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DEVICE_MNT) op: Equal rhs: {(/tmp/mnt/device)} spids: [278] ) ] spids: [278] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:OVERLAY_DIR) op:Equal rhs:{(DQ )} spids:[283])] spids: [283] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:OVERLAY_MNT) op:Equal rhs:{(DQ )} spids:[288])] spids: [288] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:UPPER_DIR) op:Equal rhs:{(DQ )} spids:[293])] spids: [293] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:WORK_DIR) op:Equal rhs:{(DQ )} spids:[298])] spids: [298] ) (SimpleCommand words: [{(mount)} {($ VSub_Name "$DEVICE")} {($ VSub_Name "$DEVICE_MNT")}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[310])] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-d)} {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs)} {(-a)} {(-d)} {($ VSub_Name "$DEVICE_MNT") (/minimal/work)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} {(DQ (" Found '/minimal' folder on device '") ($ VSub_Name "$DEVICE") ("'."))} ) (SimpleCommand words: [{(touch)} {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs/minimal.pid)}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[355])] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-f)} {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs/minimal.pid)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} { (DQ (" Device '") ($ VSub_Name "$DEVICE") ("' is mounted in read/write mode.") ) } ) (C {(rm)} {(-f)} {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs/minimal.pid)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_OVERLAY_DIR")} spids: [398] ) ] spids: [398] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_MNT) op: Equal rhs: {($ VSub_Name "$DEVICE_MNT")} spids: [402] ) ] spids: [402] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UPPER_DIR) op: Equal rhs: {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs)} spids: [406] ) ] spids: [406] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:WORK_DIR) op: Equal rhs: {($ VSub_Name "$DEVICE_MNT") (/minimal/work)} spids: [411] ) ] spids: [411] ) ] spids: [-1 372] ) ] else_action: [ (C {(echo)} { (DQ (" Device '") ($ VSub_Name "$DEVICE") ("' is mounted in read only mode.") ) } ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_DIR) op: Equal rhs: {($ VSub_Name "$DEVICE_MNT") (/minimal/rootfs)} spids: [433] ) ] spids: [433] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_MNT) op: Equal rhs: {($ VSub_Name "$DEVICE_MNT")} spids: [438] ) ] spids: [438] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UPPER_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_UPPER_DIR")} spids: [442] ) ] spids: [442] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:WORK_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_WORK_DIR")} spids: [446] ) ] spids: [446] ) ] spids: [416 450] ) ] spids: [-1 334] ) (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-f)} {($ VSub_Name "$DEVICE_MNT") (/minimal.img)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} { (DQ (" Found '/minimal.img' image on device '") ($ VSub_Name "$DEVICE") ("'.")) } ) (C {(mkdir)} {(-p)} {(/tmp/mnt/image)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:IMAGE_MNT) op: Equal rhs: {(/tmp/mnt/image)} spids: [490] ) ] spids: [490] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:LOOP_DEVICE) op: Equal rhs: { (CommandSubPart command_list: (CommandList children:[(C {(losetup)} {(-f)})]) left_token: spids: [496 500] ) } spids: [495] ) ] spids: [495] ) (C {(losetup)} {($ VSub_Name "$LOOP_DEVICE")} {($ VSub_Name "$DEVICE_MNT") (/minimal.img)} ) (C {(mount)} {($ VSub_Name "$LOOP_DEVICE")} {($ VSub_Name "$IMAGE_MNT")}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-d)} {($ VSub_Name "$IMAGE_MNT") (/rootfs)} {(-a)} {(-d)} {($ VSub_Name "$IMAGE_MNT") (/work)} {(Lit_Other "]")} ) terminator: ) ] action: [ (SimpleCommand words: [{(touch)} {($ VSub_Name "$IMAGE_MNT") (/rootfs/minimal.pid)}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [547] ) ] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-f)} {($ VSub_Name "$IMAGE_MNT") (/rootfs/minimal.pid)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} { (DQ (" Image '") ($ VSub_Name "$DEVICE") ("/minimal.img' is mounted in read/write mode.") ) } ) (C {(rm)} {(-f)} {($ VSub_Name "$IMAGE_MNT") (/rootfs/minimal.pid)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_OVERLAY_DIR")} spids: [590] ) ] spids: [590] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_MNT) op: Equal rhs: {($ VSub_Name "$IMAGE_MNT")} spids: [594] ) ] spids: [594] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UPPER_DIR) op: Equal rhs: {($ VSub_Name "$IMAGE_MNT") (/rootfs)} spids: [598] ) ] spids: [598] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:WORK_DIR) op: Equal rhs: {($ VSub_Name "$IMAGE_MNT") (/work)} spids: [603] ) ] spids: [603] ) ] spids: [-1 564] ) ] else_action: [ (C {(echo)} { (DQ (" Image '") ($ VSub_Name "$DEVICE") ("/minimal.img' is mounted in read only mode.") ) } ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_DIR) op: Equal rhs: {($ VSub_Name "$IMAGE_MNT") (/rootfs)} spids: [625] ) ] spids: [625] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OVERLAY_MNT) op: Equal rhs: {($ VSub_Name "$IMAGE_MNT")} spids: [630] ) ] spids: [630] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UPPER_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_UPPER_DIR")} spids: [634] ) ] spids: [634] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:WORK_DIR) op: Equal rhs: {($ VSub_Name "$DEFAULT_WORK_DIR")} spids: [638] ) ] spids: [638] ) ] spids: [608 642] ) ] spids: [-1 539] ) ] else_action: [ (C {(umount)} {($ VSub_Name "$IMAGE_MNT")}) (C {(rm)} {(-rf)} {($ VSub_Name "$IMAGE_MNT")}) ] spids: [645 660] ) ] spids: [453 466] ) ] spids: [-1 663] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$OVERLAY_DIR"))} {(KW_Bang "!") (Lit_Other "=")} {(DQ )} {(-a)} {(DQ ($ VSub_Name "$UPPER_DIR"))} {(KW_Bang "!") (Lit_Other "=")} {(DQ )} {(-a)} {(DQ ($ VSub_Name "$WORK_DIR"))} {(KW_Bang "!") (Lit_Other "=")} {(DQ )} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(mkdir)} {(-p)} {($ VSub_Name "$OVERLAY_DIR")}) (C {(mkdir)} {(-p)} {($ VSub_Name "$UPPER_DIR")}) (C {(mkdir)} {(-p)} {($ VSub_Name "$WORK_DIR")}) (SimpleCommand words: [ {(mount)} {(-t)} {(overlay)} {(-o)} {(Lit_VarLike "lowerdir=") ($ VSub_Name "$OVERLAY_DIR") (Lit_Other ":") (/mnt) (Lit_Comma ",") (Lit_VarLike "upperdir=") ($ VSub_Name "$UPPER_DIR") (Lit_Comma ",") (Lit_VarLike "workdir=") ($ VSub_Name "$WORK_DIR") } {(none)} {(/mnt)} ] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[757])] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:OUT) op: Equal rhs: {($ VSub_QMark "$?")} spids: [762] ) ] spids: [762] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(KW_Bang "!")} {(DQ ($ VSub_Name "$OUT"))} {(Lit_Other "=")} {(DQ (0))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} {(DQ (" Mount failed (probably on vfat)."))}) (SimpleCommand words: [{(umount)} {($ VSub_Name "$OVERLAY_MNT")}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [802] ) ] ) (SimpleCommand words: [{(rmdir)} {($ VSub_Name "$OVERLAY_MNT")}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [810] ) ] ) (SimpleCommand words: [{(rmdir)} {($ VSub_Name "$DEFAULT_OVERLAY_DIR")}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [820] ) ] ) (SimpleCommand words: [{(rmdir)} {($ VSub_Name "$DEFAULT_UPPER_DIR")}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [828] ) ] ) (SimpleCommand words: [{(rmdir)} {($ VSub_Name "$DEFAULT_WORK_DIR")}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [836] ) ] ) ] spids: [-1 786] ) ] else_action: [ (C {(echo)} { (DQ (" Overlay data from device '") ($ VSub_Name "$DEVICE") ("' has been merged.") ) } ) (ControlFlow token:) ] spids: [840 859] ) ] spids: [-1 709] ) ] else_action: [ (C {(echo)} { (DQ (" Device '") ($ VSub_Name "$DEVICE") ("' has no proper overlay structure.")) } ) ] spids: [862 874] ) (SimpleCommand words: [{(umount)} {($ VSub_Name "$DEVICE_MNT")}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[882])] ) (SimpleCommand words: [{(rm)} {(-rf)} {($ VSub_Name "$DEVICE_MNT")}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[892])] ) ] spids: [194 895] ) spids: [188 192] ) (C {(mount)} {(--move)} {(/dev)} {(/mnt/dev)}) (C {(mount)} {(--move)} {(/sys)} {(/mnt/sys)}) (C {(mount)} {(--move)} {(/proc)} {(/mnt/proc)}) (C {(mount)} {(--move)} {(/tmp)} {(/mnt/tmp)}) (C {(echo)} {(DQ ("Mount locations /dev, /sys, /tmp and /proc have been moved to /mnt."))}) (C {(echo)} {(DQ ("Switching from initramfs root area to overlayfs root area."))}) (C {(exec)} {(switch_root)} {(/mnt)} {(/etc/03_init.sh)}) (C {(echo)} {(DQ ("(/etc/02_overlay.sh) - there is a serious bug..."))}) (C {(read)} {(-n1)} {(-s)}) ] )