blob: bc7138cf12f18c646025e1f9f98c5b2f9c231909 [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
Milton Millerba65b7b2016-02-05 12:07:53 -06003fslist="proc sys dev run"
Milton D. Miller II0e775142016-01-20 14:57:54 -06004rodir=run/initramfs/ro
5rwdir=run/initramfs/rw
6upper=$rwdir/cow
7work=$rwdir/work
8
9cd /
Milton Millerba65b7b2016-02-05 12:07:53 -060010mkdir -p $fslist
Milton D. Miller II0e775142016-01-20 14:57:54 -060011mount dev dev -tdevtmpfs
12mount sys sys -tsysfs
13mount proc proc -tproc
14if ! grep run proc/mounts
15then
16 mount tmpfs run -t tmpfs -o mode=755,nodev
17fi
18
19mkdir -p $rodir $rwdir
20
21cp -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
26findmtd() {
27 m=$(grep -xl "$1" /sys/class/mtd/*/name)
28 m=${m%/name}
29 m=${m##*/}
30 echo $m
31}
32
Milton D. Miller II8084d742016-01-27 20:18:16 -060033env=$(findmtd u-boot-env)
34if test -n $env
35then
36 ln -s /dev/$env /run/mtd:u-boot-env
37 cp /run/mtd:u-boot-env /run/fw_env
38fi
39
Milton D. Miller II0e775142016-01-20 14:57:54 -060040rofs=$(findmtd rofs)
41rwfs=$(findmtd rwfs)
42
Milton Millerba65b7b2016-02-05 12:07:53 -060043rodev=/dev/mtdblock${rofs#mtd}
44rwdev=/dev/mtdblock${rwfs#mtd}
45
Milton D. Miller II0e775142016-01-20 14:57:54 -060046rofst=squashfs
47rwfst=ext4
Milton Millerba65b7b2016-02-05 12:07:53 -060048roopts=ro
49rwopts=rw
50
51init=/sbin/init
Milton D. Miller II0e775142016-01-20 14:57:54 -060052
53echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
54
55if grep -w debug-init-sh /proc/cmdline ||
Milton Millerba65b7b2016-02-05 12:07:53 -060056 ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller II0e775142016-01-20 14:57:54 -060057then
58 echo Please mount the rw file system on $rwdir from this shell
59 while ! sulogin && ! test -f /takeover
60 do
61 echo getty failed, retrying
62 done
63fi
64
65# Touch /takeover in the above getty to become pid 1
66if test -e /takeover
67then
68 export PS1=init#\
69 exec /bin/sh
70fi
71
Milton Millerba65b7b2016-02-05 12:07:53 -060072mount $rodev $rodir -t $rofst -o $roopts
Milton D. Miller II0e775142016-01-20 14:57:54 -060073
74rm -rf $work
75mkdir -p $upper
76mkdir -p $work
77
78mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
79
Milton Millerba65b7b2016-02-05 12:07:53 -060080if ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
Milton D. Miller II0e775142016-01-20 14:57:54 -060081then
Milton Miller016e0df2016-02-05 11:06:39 -060082 echo "Unable to confirm /sbin/init is an executable non-empty file."
83 echo "Change Root test failed! Invoking emergency shell."
Milton D. Miller II0e775142016-01-20 14:57:54 -060084 PS1=rescue#\ sulogin
85fi
86
Milton Millerba65b7b2016-02-05 12:07:53 -060087for f in $fslist
Milton D. Miller II0e775142016-01-20 14:57:54 -060088do
89 mount --move $f root/$f
90done
91
Milton Millerba65b7b2016-02-05 12:07:53 -060092# switch_root /root $init
93exec chroot /root $init
Milton D. Miller II0e775142016-01-20 14:57:54 -060094