Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 3 | fslist="proc sys dev run" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 4 | rodir=run/initramfs/ro |
| 5 | rwdir=run/initramfs/rw |
| 6 | upper=$rwdir/cow |
| 7 | work=$rwdir/work |
| 8 | |
| 9 | cd / |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 10 | mkdir -p $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 11 | mount dev dev -tdevtmpfs |
| 12 | mount sys sys -tsysfs |
| 13 | mount proc proc -tproc |
| 14 | if ! grep run proc/mounts |
| 15 | then |
| 16 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 17 | fi |
| 18 | |
| 19 | mkdir -p $rodir $rwdir |
| 20 | |
| 21 | cp -rp init shutdown update whitelist bin sbin usr lib etc var run/initramfs |
| 22 | |
| 23 | # To start a interactive shell with job control at this point, run |
| 24 | # getty 38400 ttyS4 |
| 25 | |
| 26 | findmtd() { |
| 27 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 28 | m=${m%/name} |
| 29 | m=${m##*/} |
| 30 | echo $m |
| 31 | } |
| 32 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 33 | debug_takeover() { |
| 34 | echo "$@" |
| 35 | test -n "$@" && echo Enter password to try to manually fix. |
| 36 | cat << HERE |
| 37 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 38 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 39 | HERE |
| 40 | |
| 41 | while ! sulogin && ! test -f /takeover |
| 42 | do |
| 43 | echo getty failed, retrying |
| 44 | done |
| 45 | |
| 46 | # Touch /takeover in the above getty to become pid 1 |
| 47 | if test -e /takeover |
| 48 | then |
| 49 | cat << HERE |
| 50 | |
| 51 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 52 | When finished exec new init or cleanup and run reboot -f. |
| 53 | |
| 54 | Warning: No job control! Shell exit will panic the system! |
| 55 | HERE |
| 56 | export PS1=init#\ |
| 57 | exec /bin/sh |
| 58 | fi |
| 59 | } |
| 60 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 61 | env=$(findmtd u-boot-env) |
| 62 | if test -n $env |
| 63 | then |
| 64 | ln -s /dev/$env /run/mtd:u-boot-env |
| 65 | cp /run/mtd:u-boot-env /run/fw_env |
| 66 | fi |
| 67 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 68 | rofs=$(findmtd rofs) |
| 69 | rwfs=$(findmtd rwfs) |
| 70 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 71 | rodev=/dev/mtdblock${rofs#mtd} |
| 72 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 73 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 74 | rofst=squashfs |
| 75 | rwfst=ext4 |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 76 | roopts=ro |
| 77 | rwopts=rw |
| 78 | |
| 79 | init=/sbin/init |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 80 | fsck=/sbin/fsck.$rwfst |
| 81 | fsckopts=-a |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 82 | |
| 83 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 84 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 85 | if grep -w debug-init-sh /proc/cmdline |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 86 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 87 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 88 | fi |
| 89 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 90 | mount $rodev $rodir -t $rofst -o $roopts |
| 91 | |
| 92 | if test -x $rodir$fsck |
| 93 | then |
| 94 | for fs in $fslist |
| 95 | do |
| 96 | mount --bind $fs $rodir/$fs |
| 97 | done |
| 98 | chroot $rodir $fsck $fsckopts $rwdev |
| 99 | rc=$? |
| 100 | for fs in $fslist |
| 101 | do |
| 102 | umount $rodir/$fs |
| 103 | done |
| 104 | if test $rc -gt 1 |
| 105 | then |
| 106 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 107 | fi |
| 108 | else |
| 109 | echo "No '$fsck' in read only fs, skipping fsck." |
| 110 | fi |
| 111 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 112 | if ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 113 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 114 | msg="$(cat)" << HERE |
| 115 | |
| 116 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame^] | 117 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 118 | to to continue, or do change nothing to run from RAM for this boot. |
| 119 | HERE |
| 120 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 121 | fi |
| 122 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 123 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 124 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 125 | |
| 126 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 127 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 128 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 129 | do |
| 130 | msg="$(cat)" << HERE |
| 131 | |
| 132 | Unable to confirm /sbin/init is an executable non-empty file |
| 133 | in merged file system mounted at /root. |
| 134 | |
| 135 | Change Root test failed! Invoking emergency shell. |
| 136 | HERE |
| 137 | debug_takeover "$msg" |
| 138 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 139 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 140 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 141 | do |
| 142 | mount --move $f root/$f |
| 143 | done |
| 144 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 145 | # switch_root /root $init |
| 146 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 147 | |