Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 3 | fslist="proc sys dev run" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 4 | rodir=run/initramfs/ro |
| 5 | rwdir=run/initramfs/rw |
| 6 | upper=$rwdir/cow |
| 7 | work=$rwdir/work |
| 8 | |
| 9 | cd / |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 10 | mkdir -p $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 11 | mount dev dev -tdevtmpfs |
| 12 | mount sys sys -tsysfs |
| 13 | mount proc proc -tproc |
| 14 | if ! grep run proc/mounts |
| 15 | then |
| 16 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 17 | fi |
| 18 | |
| 19 | mkdir -p $rodir $rwdir |
| 20 | |
| 21 | cp -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 | |
| 26 | findmtd() { |
| 27 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 28 | m=${m%/name} |
| 29 | m=${m##*/} |
| 30 | echo $m |
| 31 | } |
| 32 | |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 33 | blkid_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 | |
| 42 | probe_fs_type() { |
| 43 | fst=$(blkid_fs_type $1) |
| 44 | echo ${fst:=jffs2} |
| 45 | } |
| 46 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 47 | debug_takeover() { |
| 48 | echo "$@" |
| 49 | test -n "$@" && echo Enter password to try to manually fix. |
| 50 | cat << HERE |
| 51 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 52 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 53 | HERE |
| 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 | |
| 65 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 66 | When finished exec new init or cleanup and run reboot -f. |
| 67 | |
| 68 | Warning: No job control! Shell exit will panic the system! |
| 69 | HERE |
| 70 | export PS1=init#\ |
| 71 | exec /bin/sh |
| 72 | fi |
| 73 | } |
| 74 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 75 | env=$(findmtd u-boot-env) |
| 76 | if test -n $env |
| 77 | then |
| 78 | ln -s /dev/$env /run/mtd:u-boot-env |
| 79 | cp /run/mtd:u-boot-env /run/fw_env |
| 80 | fi |
| 81 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 82 | rofs=$(findmtd rofs) |
| 83 | rwfs=$(findmtd rwfs) |
| 84 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 85 | rodev=/dev/mtdblock${rofs#mtd} |
| 86 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 87 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 88 | # Set to y for yes, anything else for no. |
| 89 | force_rwfst_jffs2=y |
| 90 | flash_images_before_init=n |
| 91 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 92 | rofst=squashfs |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 93 | rwfst=$(probe_fs_type $rwdev) |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 94 | roopts=ro |
| 95 | rwopts=rw |
| 96 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 97 | image=/run/initramfs/image- |
| 98 | trigger=${image}rwfs |
| 99 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 100 | init=/sbin/init |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 101 | fsckbase=/sbin/fsck. |
| 102 | fsck=$fsckbase$rwfst |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 103 | fsckopts=-a |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 104 | optfile=/run/initramfs/init-options |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 105 | update=/run/initramfs/update |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 106 | |
| 107 | if test ! -f $optfile |
| 108 | then |
| 109 | cat /proc/cmdline > $optfile |
| 110 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 111 | |
| 112 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 113 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 114 | if grep -w debug-init-sh $optfile |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 115 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 116 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 117 | fi |
| 118 | |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 119 | # If there are images in root move them to /run/initramfs/ or /run/ now. |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 120 | imagebasename=${image##*/} |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 121 | if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1 |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 122 | then |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 123 | 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 Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 131 | fi |
| 132 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 133 | if grep -w clean-rwfs-filesystem $optfile |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 134 | then |
| 135 | echo "Cleaning of read-write overlay filesystem requested." |
| 136 | touch $trigger |
| 137 | fi |
| 138 | |
| 139 | if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger |
| 140 | then |
| 141 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 142 | touch $trigger |
| 143 | fi |
| 144 | |
| 145 | if ls $image* > /dev/null 2>&1 |
| 146 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 147 | if ! test -x $update |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 148 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 149 | debug_takeover "Flash update requested but $update missing!" |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 150 | elif test -f $trigger -a ! -s $trigger |
| 151 | then |
| 152 | echo "Saving selected files from read-write overlay filesystem." |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 153 | $update --no-restore-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 154 | 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 II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 158 | $update --no-save-files --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 159 | else |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame^] | 160 | $update --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 161 | fi |
| 162 | |
| 163 | rwfst=$(probe_fs_type $rwdev) |
| 164 | fsck=$fsckbase$rwfst |
| 165 | fi |
| 166 | |
Milton D. Miller II | edfd12c | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 167 | if grep -w overlay-filesystem-in-ram $optfile |
| 168 | then |
| 169 | rwfst=none |
| 170 | fi |
| 171 | |
Milton D. Miller II | c201b04 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 172 | if grep -w copy-base-filesystem-to-ram $optfile && |
| 173 | test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs |
| 174 | then |
| 175 | # Remove any partial copy to avoid attempted usage later |
| 176 | if test -e /run/image-rofs |
| 177 | then |
| 178 | ls -l /run/image-rofs |
| 179 | rm -f /run/image-rofs |
| 180 | fi |
| 181 | debug_takeover "Copying $rodev to /run/image-rofs failed." |
| 182 | fi |
| 183 | |
Milton D. Miller II | a36e99c | 2016-02-27 16:04:32 -0600 | [diff] [blame] | 184 | if test -s /run/image-rofs |
| 185 | then |
| 186 | rodev=/run/image-rofs |
| 187 | roopts=$roopts,loop |
| 188 | fi |
| 189 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 190 | mount $rodev $rodir -t $rofst -o $roopts |
| 191 | |
| 192 | if test -x $rodir$fsck |
| 193 | then |
| 194 | for fs in $fslist |
| 195 | do |
| 196 | mount --bind $fs $rodir/$fs |
| 197 | done |
| 198 | chroot $rodir $fsck $fsckopts $rwdev |
| 199 | rc=$? |
| 200 | for fs in $fslist |
| 201 | do |
| 202 | umount $rodir/$fs |
| 203 | done |
| 204 | if test $rc -gt 1 |
| 205 | then |
| 206 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 207 | fi |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 208 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
Milton Miller | f81c108 | 2016-02-23 21:00:06 -0600 | [diff] [blame] | 209 | then |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 210 | echo "No '$fsck' in read only fs, skipping fsck." |
| 211 | fi |
| 212 | |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 213 | if test "$rwfst" = none |
| 214 | then |
| 215 | echo "Running with read-write overlay in RAM for this boot." |
| 216 | echo "No state will be preserved unless flash update performed." |
| 217 | elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 218 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 219 | msg="$(cat)" << HERE |
| 220 | |
| 221 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 222 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 223 | to to continue, or do change nothing to run from RAM for this boot. |
| 224 | HERE |
| 225 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 226 | fi |
| 227 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 228 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 229 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 230 | |
| 231 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 232 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 233 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 234 | do |
| 235 | msg="$(cat)" << HERE |
| 236 | |
| 237 | Unable to confirm /sbin/init is an executable non-empty file |
| 238 | in merged file system mounted at /root. |
| 239 | |
| 240 | Change Root test failed! Invoking emergency shell. |
| 241 | HERE |
| 242 | debug_takeover "$msg" |
| 243 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 244 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 245 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 246 | do |
| 247 | mount --move $f root/$f |
| 248 | done |
| 249 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 250 | # switch_root /root $init |
| 251 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 252 | |