blob: 6750de38a445792ba4f29b654c778ba139b4dc4e [file] [log] [blame]
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06001#!/bin/sh
2
Milton Miller54d882e2016-02-05 12:07:53 -06003fslist="proc sys dev run"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06004rodir=run/initramfs/ro
5rwdir=run/initramfs/rw
6upper=$rwdir/cow
7work=$rwdir/work
8
9cd /
Milton Miller54d882e2016-02-05 12:07:53 -060010mkdir -p $fslist
Milton D. Miller IId89d5e02016-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 Miller06ccb1a2016-02-05 13:04:29 -060033debug_takeover() {
34 echo "$@"
35 test -n "$@" && echo Enter password to try to manually fix.
36 cat << HERE
37After fixing run exit to continue this script, or reboot -f to retry, or
38touch /takeover and exit to become PID 1 allowing editing of this script.
39HERE
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
51Takeover of init requested. Executing /bin/sh as PID 1.
52When finished exec new init or cleanup and run reboot -f.
53
54Warning: No job control! Shell exit will panic the system!
55HERE
56 export PS1=init#\
57 exec /bin/sh
58 fi
59}
60
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060061env=$(findmtd u-boot-env)
62if test -n $env
63then
64 ln -s /dev/$env /run/mtd:u-boot-env
65 cp /run/mtd:u-boot-env /run/fw_env
66fi
67
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060068rofs=$(findmtd rofs)
69rwfs=$(findmtd rwfs)
70
Milton Miller54d882e2016-02-05 12:07:53 -060071rodev=/dev/mtdblock${rofs#mtd}
72rwdev=/dev/mtdblock${rwfs#mtd}
73
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060074rofst=squashfs
75rwfst=ext4
Milton Miller54d882e2016-02-05 12:07:53 -060076roopts=ro
77rwopts=rw
78
79init=/sbin/init
Milton Millerb72114c2016-02-06 16:05:06 -060080fsck=/sbin/fsck.$rwfst
81fsckopts=-a
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060082
83echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
84
Milton Miller06ccb1a2016-02-05 13:04:29 -060085if grep -w debug-init-sh /proc/cmdline
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060086then
Milton Miller06ccb1a2016-02-05 13:04:29 -060087 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060088fi
89
Milton Millerb72114c2016-02-06 16:05:06 -060090mount $rodev $rodir -t $rofst -o $roopts
91
92if test -x $rodir$fsck
93then
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
108else
109 echo "No '$fsck' in read only fs, skipping fsck."
110fi
111
Milton Miller06ccb1a2016-02-05 13:04:29 -0600112if ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600113then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600114 msg="$(cat)" << HERE
115
116Mounting read-write $rwdev filesystem failed. Please fix and run
117 mount $rwdev $rwdir -t $rwfs -o $rwopts
118to to continue, or do change nothing to run from RAM for this boot.
119HERE
120 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600121fi
122
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600123rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600124mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600125
126mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
127
Milton Miller06ccb1a2016-02-05 13:04:29 -0600128while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
129do
130 msg="$(cat)" << HERE
131
132Unable to confirm /sbin/init is an executable non-empty file
133in merged file system mounted at /root.
134
135Change Root test failed! Invoking emergency shell.
136HERE
137 debug_takeover "$msg"
138done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600139
Milton Miller54d882e2016-02-05 12:07:53 -0600140for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600141do
142 mount --move $f root/$f
143done
144
Milton Miller54d882e2016-02-05 12:07:53 -0600145# switch_root /root $init
146exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600147