blob: 7fc65877c057fc11a730c56ae47251a36ebcf293 [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
Andrew Jefferyacc2c852016-02-23 23:47:23 +103033blkid_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
42probe_fs_type() {
43 fst=$(blkid_fs_type $1)
44 echo ${fst:=jffs2}
45}
46
Milton Miller06ccb1a2016-02-05 13:04:29 -060047debug_takeover() {
48 echo "$@"
49 test -n "$@" && echo Enter password to try to manually fix.
50 cat << HERE
51After fixing run exit to continue this script, or reboot -f to retry, or
52touch /takeover and exit to become PID 1 allowing editing of this script.
53HERE
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
65Takeover of init requested. Executing /bin/sh as PID 1.
66When finished exec new init or cleanup and run reboot -f.
67
68Warning: No job control! Shell exit will panic the system!
69HERE
70 export PS1=init#\
71 exec /bin/sh
72 fi
73}
74
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060075env=$(findmtd u-boot-env)
76if test -n $env
77then
78 ln -s /dev/$env /run/mtd:u-boot-env
79 cp /run/mtd:u-boot-env /run/fw_env
80fi
81
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060082rofs=$(findmtd rofs)
83rwfs=$(findmtd rwfs)
84
Milton Miller54d882e2016-02-05 12:07:53 -060085rodev=/dev/mtdblock${rofs#mtd}
86rwdev=/dev/mtdblock${rwfs#mtd}
87
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060088rofst=squashfs
Andrew Jefferyacc2c852016-02-23 23:47:23 +103089rwfst=$(probe_fs_type $rwdev)
Milton Miller54d882e2016-02-05 12:07:53 -060090roopts=ro
91rwopts=rw
92
93init=/sbin/init
Milton Millerb72114c2016-02-06 16:05:06 -060094fsck=/sbin/fsck.$rwfst
95fsckopts=-a
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060096
97echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
98
Milton Miller06ccb1a2016-02-05 13:04:29 -060099if grep -w debug-init-sh /proc/cmdline
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600100then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600101 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600102fi
103
Milton Millerb72114c2016-02-06 16:05:06 -0600104mount $rodev $rodir -t $rofst -o $roopts
105
106if test -x $rodir$fsck
107then
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
122else
123 echo "No '$fsck' in read only fs, skipping fsck."
124fi
125
Milton Miller06ccb1a2016-02-05 13:04:29 -0600126if ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600127then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600128 msg="$(cat)" << HERE
129
130Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030131 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600132to to continue, or do change nothing to run from RAM for this boot.
133HERE
134 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600135fi
136
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600137rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600138mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600139
140mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
141
Milton Miller06ccb1a2016-02-05 13:04:29 -0600142while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
143do
144 msg="$(cat)" << HERE
145
146Unable to confirm /sbin/init is an executable non-empty file
147in merged file system mounted at /root.
148
149Change Root test failed! Invoking emergency shell.
150HERE
151 debug_takeover "$msg"
152done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600153
Milton Miller54d882e2016-02-05 12:07:53 -0600154for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600155do
156 mount --move $f root/$f
157done
158
Milton Miller54d882e2016-02-05 12:07:53 -0600159# switch_root /root $init
160exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600161