blob: 9aa2131d280d85cae984f99b0e44b84f401c6e7a [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 II1650db52016-02-27 15:48:52 -0600104optfile=/run/initramfs/init-options
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600105update=/run/initramfs/update
Milton D. Miller II1650db52016-02-27 15:48:52 -0600106
107if test ! -f $optfile
108then
109 cat /proc/cmdline > $optfile
110fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600111
112echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
113
Milton D. Miller II1650db52016-02-27 15:48:52 -0600114if grep -w debug-init-sh $optfile
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600115then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600116 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600117fi
118
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600119# If there are images in root move them to /run/initramfs/ or /run/ now.
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600120imagebasename=${image##*/}
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600121if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600122then
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600123 if test "x$flash_images_before_init" = xy
124 then
125 echo "Flash images found, will update before starting init."
126 mv /${imagebasename}* ${image%$imagebasename}
127 else
128 echo "Flash images found, will use but deferring flash update."
129 mv /${imagebasename}* /run/
130 fi
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600131fi
132
Milton D. Miller II1650db52016-02-27 15:48:52 -0600133if grep -w clean-rwfs-filesystem $optfile
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600134then
135 echo "Cleaning of read-write overlay filesystem requested."
136 touch $trigger
137fi
138
139if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger
140then
141 echo "Converting read-write overlay filesystem to jffs2 forced."
142 touch $trigger
143fi
144
145if ls $image* > /dev/null 2>&1
146then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600147 if ! test -x $update
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600148 then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600149 debug_takeover "Flash update requested but $update missing!"
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600150 elif test -f $trigger -a ! -s $trigger
151 then
152 echo "Saving selected files from read-write overlay filesystem."
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600153 $update --no-restore-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600154 echo "Clearing read-write overlay filesystem."
155 flash_eraseall /dev/$rwfs
156 echo "Restoring saved files to read-write overlay filesystem."
157 touch $trigger
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600158 $update --no-save-files --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600159 else
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600160 $update --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600161 fi
162
163 rwfst=$(probe_fs_type $rwdev)
164 fsck=$fsckbase$rwfst
165fi
166
Milton D. Miller IIedfd12c2016-02-27 17:31:39 -0600167if grep -w overlay-filesystem-in-ram $optfile
168then
169 rwfst=none
170fi
171
Milton D. Miller II2728b732016-02-29 12:10:34 -0600172copyfiles=
173if grep -w copy-files-to-ram $optfile
174then
175 rwfst=none
176 copyfiles=y
177fi
178
179# It would be nice to do this after fsck but that mean rofs is mounted
180# which triggers the mtd is mounted check
181if test "$rwfst$copyfiles" = noney
182then
183 touch $trigger
184 $update --copy-files --clean-saved-files --no-restore-files
185fi
186
Milton D. Miller IIc201b042016-02-27 17:31:39 -0600187if grep -w copy-base-filesystem-to-ram $optfile &&
188 test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs
189then
190 # Remove any partial copy to avoid attempted usage later
191 if test -e /run/image-rofs
192 then
193 ls -l /run/image-rofs
194 rm -f /run/image-rofs
195 fi
196 debug_takeover "Copying $rodev to /run/image-rofs failed."
197fi
198
Milton D. Miller IIa36e99c2016-02-27 16:04:32 -0600199if test -s /run/image-rofs
200then
201 rodev=/run/image-rofs
202 roopts=$roopts,loop
203fi
204
Milton Millerb72114c2016-02-06 16:05:06 -0600205mount $rodev $rodir -t $rofst -o $roopts
206
207if test -x $rodir$fsck
208then
209 for fs in $fslist
210 do
211 mount --bind $fs $rodir/$fs
212 done
213 chroot $rodir $fsck $fsckopts $rwdev
214 rc=$?
215 for fs in $fslist
216 do
217 umount $rodir/$fs
218 done
219 if test $rc -gt 1
220 then
221 debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)"
222 fi
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600223elif test "$rwfst" != jffs2 -a "$rwfst" != none
Milton Millerf81c1082016-02-23 21:00:06 -0600224then
Milton Millerb72114c2016-02-06 16:05:06 -0600225 echo "No '$fsck' in read only fs, skipping fsck."
226fi
227
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600228if test "$rwfst" = none
229then
230 echo "Running with read-write overlay in RAM for this boot."
231 echo "No state will be preserved unless flash update performed."
232elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600233then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600234 msg="$(cat)" << HERE
235
236Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030237 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600238to to continue, or do change nothing to run from RAM for this boot.
239HERE
240 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600241fi
242
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600243rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600244mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600245
246mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
247
Milton Miller06ccb1a2016-02-05 13:04:29 -0600248while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
249do
250 msg="$(cat)" << HERE
251
252Unable to confirm /sbin/init is an executable non-empty file
253in merged file system mounted at /root.
254
255Change Root test failed! Invoking emergency shell.
256HERE
257 debug_takeover "$msg"
258done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600259
Milton Miller54d882e2016-02-05 12:07:53 -0600260for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600261do
262 mount --move $f root/$f
263done
264
Milton Miller54d882e2016-02-05 12:07:53 -0600265# switch_root /root $init
266exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600267