Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | rodir=run/initramfs/ro |
| 4 | rwdir=run/initramfs/rw |
| 5 | upper=$rwdir/cow |
| 6 | work=$rwdir/work |
| 7 | |
| 8 | cd / |
| 9 | mkdir -p sys proc dev run |
| 10 | mount dev dev -tdevtmpfs |
| 11 | mount sys sys -tsysfs |
| 12 | mount proc proc -tproc |
| 13 | if ! grep run proc/mounts |
| 14 | then |
| 15 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 16 | fi |
| 17 | |
| 18 | mkdir -p $rodir $rwdir |
| 19 | |
| 20 | cp -rp init shutdown update whitelist bin sbin usr lib etc var run/initramfs |
| 21 | |
| 22 | # To start a interactive shell with job control at this point, run |
| 23 | # getty 38400 ttyS4 |
| 24 | |
| 25 | findmtd() { |
| 26 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 27 | m=${m%/name} |
| 28 | m=${m##*/} |
| 29 | echo $m |
| 30 | } |
| 31 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 32 | env=$(findmtd u-boot-env) |
| 33 | if test -n $env |
| 34 | then |
| 35 | ln -s /dev/$env /run/mtd:u-boot-env |
| 36 | cp /run/mtd:u-boot-env /run/fw_env |
| 37 | fi |
| 38 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 39 | rofs=$(findmtd rofs) |
| 40 | rwfs=$(findmtd rwfs) |
| 41 | |
| 42 | rofst=squashfs |
| 43 | rwfst=ext4 |
| 44 | |
| 45 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 46 | |
| 47 | if grep -w debug-init-sh /proc/cmdline || |
| 48 | ! mount -o rw /dev/mtdblock${rwfs#mtd} $rwdir -t $rwfst |
| 49 | then |
| 50 | echo Please mount the rw file system on $rwdir from this shell |
| 51 | while ! sulogin && ! test -f /takeover |
| 52 | do |
| 53 | echo getty failed, retrying |
| 54 | done |
| 55 | fi |
| 56 | |
| 57 | # Touch /takeover in the above getty to become pid 1 |
| 58 | if test -e /takeover |
| 59 | then |
| 60 | export PS1=init#\ |
| 61 | exec /bin/sh |
| 62 | fi |
| 63 | |
| 64 | mount -o ro /dev/mtdblock${rofs#mtd} $rodir -t $rofst |
| 65 | |
| 66 | rm -rf $work |
| 67 | mkdir -p $upper |
| 68 | mkdir -p $work |
| 69 | |
| 70 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 71 | |
Milton Miller | 92fdfc1 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 72 | if ! chroot /root /bin/sh -c "test -x /sbin/init -a -s /sbin/init" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 73 | then |
Milton Miller | 92fdfc1 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 74 | echo "Unable to confirm /sbin/init is an executable non-empty file." |
| 75 | echo "Change Root test failed! Invoking emergency shell." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 76 | PS1=rescue#\ sulogin |
| 77 | fi |
| 78 | |
| 79 | for f in sys dev proc run |
| 80 | do |
| 81 | mount --move $f root/$f |
| 82 | done |
| 83 | |
| 84 | # switch_root /root /sbin/init |
| 85 | exec chroot /root /sbin/init |
| 86 | |