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 | |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 33 | blkid_fs_type() { |
| 34 | # Emulate util-linux's `blkid -s TYPE -o value $1` |
| 35 | # Example busybox blkid output: |
| 36 | # # blkid /dev/mtdblock5 |
| 37 | # /dev/mtdblock5: TYPE="squashfs" |
| 38 | # Process output to extract TYPE value "squashfs". |
| 39 | blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//' |
| 40 | } |
| 41 | |
| 42 | probe_fs_type() { |
| 43 | fst=$(blkid_fs_type $1) |
| 44 | echo ${fst:=jffs2} |
| 45 | } |
| 46 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 47 | debug_takeover() { |
| 48 | echo "$@" |
| 49 | test -n "$@" && echo Enter password to try to manually fix. |
| 50 | cat << HERE |
| 51 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 52 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 53 | HERE |
| 54 | |
| 55 | while ! sulogin && ! test -f /takeover |
| 56 | do |
| 57 | echo getty failed, retrying |
| 58 | done |
| 59 | |
| 60 | # Touch /takeover in the above getty to become pid 1 |
| 61 | if test -e /takeover |
| 62 | then |
| 63 | cat << HERE |
| 64 | |
| 65 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 66 | When finished exec new init or cleanup and run reboot -f. |
| 67 | |
| 68 | Warning: No job control! Shell exit will panic the system! |
| 69 | HERE |
| 70 | export PS1=init#\ |
| 71 | exec /bin/sh |
| 72 | fi |
| 73 | } |
| 74 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 75 | env=$(findmtd u-boot-env) |
| 76 | if test -n $env |
| 77 | then |
| 78 | ln -s /dev/$env /run/mtd:u-boot-env |
| 79 | cp /run/mtd:u-boot-env /run/fw_env |
| 80 | fi |
| 81 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 82 | rofs=$(findmtd rofs) |
| 83 | rwfs=$(findmtd rwfs) |
| 84 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 85 | rodev=/dev/mtdblock${rofs#mtd} |
| 86 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 87 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 88 | rofst=squashfs |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 89 | rwfst=$(probe_fs_type $rwdev) |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 90 | roopts=ro |
| 91 | rwopts=rw |
| 92 | |
| 93 | init=/sbin/init |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 94 | fsck=/sbin/fsck.$rwfst |
| 95 | fsckopts=-a |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 96 | |
| 97 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 98 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 99 | if grep -w debug-init-sh /proc/cmdline |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 100 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 101 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 102 | fi |
| 103 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 104 | mount $rodev $rodir -t $rofst -o $roopts |
| 105 | |
| 106 | if test -x $rodir$fsck |
| 107 | then |
| 108 | for fs in $fslist |
| 109 | do |
| 110 | mount --bind $fs $rodir/$fs |
| 111 | done |
| 112 | chroot $rodir $fsck $fsckopts $rwdev |
| 113 | rc=$? |
| 114 | for fs in $fslist |
| 115 | do |
| 116 | umount $rodir/$fs |
| 117 | done |
| 118 | if test $rc -gt 1 |
| 119 | then |
| 120 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 121 | fi |
| 122 | else |
| 123 | echo "No '$fsck' in read only fs, skipping fsck." |
| 124 | fi |
| 125 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 126 | if ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 127 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 128 | msg="$(cat)" << HERE |
| 129 | |
| 130 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 131 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 132 | to to continue, or do change nothing to run from RAM for this boot. |
| 133 | HERE |
| 134 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 135 | fi |
| 136 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 137 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 138 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 139 | |
| 140 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 141 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 142 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 143 | do |
| 144 | msg="$(cat)" << HERE |
| 145 | |
| 146 | Unable to confirm /sbin/init is an executable non-empty file |
| 147 | in merged file system mounted at /root. |
| 148 | |
| 149 | Change Root test failed! Invoking emergency shell. |
| 150 | HERE |
| 151 | debug_takeover "$msg" |
| 152 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 153 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 154 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 155 | do |
| 156 | mount --move $f root/$f |
| 157 | done |
| 158 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 159 | # switch_root /root $init |
| 160 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 161 | |