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 |
| 105 | |
| 106 | if test ! -f $optfile |
| 107 | then |
| 108 | cat /proc/cmdline > $optfile |
| 109 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 110 | |
| 111 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 112 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 113 | if grep -w debug-init-sh $optfile |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 114 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 115 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 116 | fi |
| 117 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 118 | # If there are images in root move them to run/initramfs/ now. |
| 119 | imagebasename=${image##*/} |
| 120 | if test -n "${imagebasename}" -a "x$flash_images_before_init" = xy && |
| 121 | ls /${imagebasename}* > /dev/null 2>&1 |
| 122 | then |
| 123 | echo "Pending flash updates found." |
| 124 | mv /${imagebasename}* ${image%$imagebasename} |
| 125 | fi |
| 126 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 127 | if grep -w clean-rwfs-filesystem $optfile |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 128 | then |
| 129 | echo "Cleaning of read-write overlay filesystem requested." |
| 130 | touch $trigger |
| 131 | fi |
| 132 | |
| 133 | if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger |
| 134 | then |
| 135 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 136 | touch $trigger |
| 137 | fi |
| 138 | |
| 139 | if ls $image* > /dev/null 2>&1 |
| 140 | then |
| 141 | if ! test -x /update |
| 142 | then |
| 143 | debug_takeover "Flash update requested but /update missing!" |
| 144 | elif test -f $trigger -a ! -s $trigger |
| 145 | then |
| 146 | echo "Saving selected files from read-write overlay filesystem." |
| 147 | /update && rm -f $image* |
| 148 | echo "Clearing read-write overlay filesystem." |
| 149 | flash_eraseall /dev/$rwfs |
| 150 | echo "Restoring saved files to read-write overlay filesystem." |
| 151 | touch $trigger |
| 152 | /update |
| 153 | rm -rf /save $trigger |
| 154 | else |
| 155 | /update && rm -f $image* |
| 156 | fi |
| 157 | |
| 158 | rwfst=$(probe_fs_type $rwdev) |
| 159 | fsck=$fsckbase$rwfst |
| 160 | fi |
| 161 | |
Milton D. Miller II | a36e99c | 2016-02-27 16:04:32 -0600 | [diff] [blame] | 162 | if test -s /run/image-rofs |
| 163 | then |
| 164 | rodev=/run/image-rofs |
| 165 | roopts=$roopts,loop |
| 166 | fi |
| 167 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 168 | mount $rodev $rodir -t $rofst -o $roopts |
| 169 | |
| 170 | if test -x $rodir$fsck |
| 171 | then |
| 172 | for fs in $fslist |
| 173 | do |
| 174 | mount --bind $fs $rodir/$fs |
| 175 | done |
| 176 | chroot $rodir $fsck $fsckopts $rwdev |
| 177 | rc=$? |
| 178 | for fs in $fslist |
| 179 | do |
| 180 | umount $rodir/$fs |
| 181 | done |
| 182 | if test $rc -gt 1 |
| 183 | then |
| 184 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 185 | fi |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame^] | 186 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
Milton Miller | f81c108 | 2016-02-23 21:00:06 -0600 | [diff] [blame] | 187 | then |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 188 | echo "No '$fsck' in read only fs, skipping fsck." |
| 189 | fi |
| 190 | |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame^] | 191 | if test "$rwfst" = none |
| 192 | then |
| 193 | echo "Running with read-write overlay in RAM for this boot." |
| 194 | echo "No state will be preserved unless flash update performed." |
| 195 | elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 196 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 197 | msg="$(cat)" << HERE |
| 198 | |
| 199 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 200 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 201 | to to continue, or do change nothing to run from RAM for this boot. |
| 202 | HERE |
| 203 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 204 | fi |
| 205 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 206 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 207 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 208 | |
| 209 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 210 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 211 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 212 | do |
| 213 | msg="$(cat)" << HERE |
| 214 | |
| 215 | Unable to confirm /sbin/init is an executable non-empty file |
| 216 | in merged file system mounted at /root. |
| 217 | |
| 218 | Change Root test failed! Invoking emergency shell. |
| 219 | HERE |
| 220 | debug_takeover "$msg" |
| 221 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 222 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 223 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 224 | do |
| 225 | mount --move $f root/$f |
| 226 | done |
| 227 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 228 | # switch_root /root $init |
| 229 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 230 | |