blob: 12c5a2f1bc81a1b6787122f9bbae32f368a9bf11 [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 Miller0b0d5fe2016-02-23 21:48:48 -060088# Set to y for yes, anything else for no.
89force_rwfst_jffs2=y
90flash_images_before_init=n
91
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060092rofst=squashfs
Andrew Jefferyacc2c852016-02-23 23:47:23 +103093rwfst=$(probe_fs_type $rwdev)
Milton Miller54d882e2016-02-05 12:07:53 -060094roopts=ro
95rwopts=rw
96
Milton Miller0b0d5fe2016-02-23 21:48:48 -060097image=/run/initramfs/image-
98trigger=${image}rwfs
99
Milton Miller54d882e2016-02-05 12:07:53 -0600100init=/sbin/init
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600101fsckbase=/sbin/fsck.
102fsck=$fsckbase$rwfst
Milton Millerb72114c2016-02-06 16:05:06 -0600103fsckopts=-a
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600104
105echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
106
Milton Miller06ccb1a2016-02-05 13:04:29 -0600107if grep -w debug-init-sh /proc/cmdline
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600108then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600109 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600110fi
111
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600112# If there are images in root move them to run/initramfs/ now.
113imagebasename=${image##*/}
114if test -n "${imagebasename}" -a "x$flash_images_before_init" = xy &&
115 ls /${imagebasename}* > /dev/null 2>&1
116then
117 echo "Pending flash updates found."
118 mv /${imagebasename}* ${image%$imagebasename}
119fi
120
121if grep -w clean-rwfs-filesystem /proc/cmdline
122then
123 echo "Cleaning of read-write overlay filesystem requested."
124 touch $trigger
125fi
126
127if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger
128then
129 echo "Converting read-write overlay filesystem to jffs2 forced."
130 touch $trigger
131fi
132
133if ls $image* > /dev/null 2>&1
134then
135 if ! test -x /update
136 then
137 debug_takeover "Flash update requested but /update missing!"
138 elif test -f $trigger -a ! -s $trigger
139 then
140 echo "Saving selected files from read-write overlay filesystem."
141 /update && rm -f $image*
142 echo "Clearing read-write overlay filesystem."
143 flash_eraseall /dev/$rwfs
144 echo "Restoring saved files to read-write overlay filesystem."
145 touch $trigger
146 /update
147 rm -rf /save $trigger
148 else
149 /update && rm -f $image*
150 fi
151
152 rwfst=$(probe_fs_type $rwdev)
153 fsck=$fsckbase$rwfst
154fi
155
Milton Millerb72114c2016-02-06 16:05:06 -0600156mount $rodev $rodir -t $rofst -o $roopts
157
158if test -x $rodir$fsck
159then
160 for fs in $fslist
161 do
162 mount --bind $fs $rodir/$fs
163 done
164 chroot $rodir $fsck $fsckopts $rwdev
165 rc=$?
166 for fs in $fslist
167 do
168 umount $rodir/$fs
169 done
170 if test $rc -gt 1
171 then
172 debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)"
173 fi
Milton Millerf81c1082016-02-23 21:00:06 -0600174elif test $fsck != /sbin/fsck.jffs2
175then
Milton Millerb72114c2016-02-06 16:05:06 -0600176 echo "No '$fsck' in read only fs, skipping fsck."
177fi
178
Milton Miller06ccb1a2016-02-05 13:04:29 -0600179if ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600180then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600181 msg="$(cat)" << HERE
182
183Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030184 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600185to to continue, or do change nothing to run from RAM for this boot.
186HERE
187 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600188fi
189
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600190rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600191mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600192
193mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
194
Milton Miller06ccb1a2016-02-05 13:04:29 -0600195while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
196do
197 msg="$(cat)" << HERE
198
199Unable to confirm /sbin/init is an executable non-empty file
200in merged file system mounted at /root.
201
202Change Root test failed! Invoking emergency shell.
203HERE
204 debug_takeover "$msg"
205done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600206
Milton Miller54d882e2016-02-05 12:07:53 -0600207for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600208do
209 mount --move $f root/$f
210done
211
Milton Miller54d882e2016-02-05 12:07:53 -0600212# switch_root /root $init
213exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600214