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 D. Miller II | 04d2aff | 2016-03-02 15:23:22 -0600 | [diff] [blame^] | 47 | # This fw_get_env_var is a simple but slightly broken version of fw_printenv: |
| 48 | # The u-boot environemnt starts with a crc32, followed by a flag byte |
| 49 | # when a redundannt environment is configured, followed by var=value\0 sets. |
| 50 | # The flag byte for nand is a 1 byte counter; for nor it is a 1 or 0 byte. |
| 51 | # The crc and/or nand flag byte can contain printable characters and be |
| 52 | # considered part of the first string and parsed as part of the variable |
| 53 | # name. In addition a variable could have a "\n" embedded in it, this code |
| 54 | # would split that variable. Ignore for now, the last set var is at the end. |
| 55 | |
| 56 | get_fw_env_var() { |
| 57 | strings /run/fw_env | sed -ne "s/^$1=//p" |
| 58 | } |
| 59 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 60 | debug_takeover() { |
| 61 | echo "$@" |
| 62 | test -n "$@" && echo Enter password to try to manually fix. |
| 63 | cat << HERE |
| 64 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 65 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 66 | HERE |
| 67 | |
| 68 | while ! sulogin && ! test -f /takeover |
| 69 | do |
| 70 | echo getty failed, retrying |
| 71 | done |
| 72 | |
| 73 | # Touch /takeover in the above getty to become pid 1 |
| 74 | if test -e /takeover |
| 75 | then |
| 76 | cat << HERE |
| 77 | |
| 78 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 79 | When finished exec new init or cleanup and run reboot -f. |
| 80 | |
| 81 | Warning: No job control! Shell exit will panic the system! |
| 82 | HERE |
| 83 | export PS1=init#\ |
| 84 | exec /bin/sh |
| 85 | fi |
| 86 | } |
| 87 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 88 | env=$(findmtd u-boot-env) |
| 89 | if test -n $env |
| 90 | then |
| 91 | ln -s /dev/$env /run/mtd:u-boot-env |
| 92 | cp /run/mtd:u-boot-env /run/fw_env |
| 93 | fi |
| 94 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 95 | rofs=$(findmtd rofs) |
| 96 | rwfs=$(findmtd rwfs) |
| 97 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 98 | rodev=/dev/mtdblock${rofs#mtd} |
| 99 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 100 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 101 | # Set to y for yes, anything else for no. |
| 102 | force_rwfst_jffs2=y |
| 103 | flash_images_before_init=n |
| 104 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 105 | rofst=squashfs |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 106 | rwfst=$(probe_fs_type $rwdev) |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 107 | roopts=ro |
| 108 | rwopts=rw |
| 109 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 110 | image=/run/initramfs/image- |
| 111 | trigger=${image}rwfs |
| 112 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 113 | init=/sbin/init |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 114 | fsckbase=/sbin/fsck. |
| 115 | fsck=$fsckbase$rwfst |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 116 | fsckopts=-a |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 117 | optfile=/run/initramfs/init-options |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 118 | update=/run/initramfs/update |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 119 | |
| 120 | if test ! -f $optfile |
| 121 | then |
| 122 | cat /proc/cmdline > $optfile |
Milton D. Miller II | 04d2aff | 2016-03-02 15:23:22 -0600 | [diff] [blame^] | 123 | get_fw_env_var openbmcinit >> $optfile |
| 124 | get_fw_env_var openbmconce >> $optfile |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 125 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 126 | |
| 127 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 128 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 129 | if grep -w debug-init-sh $optfile |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 130 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 131 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 132 | fi |
| 133 | |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 134 | # 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] | 135 | imagebasename=${image##*/} |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 136 | if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1 |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 137 | then |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 138 | if test "x$flash_images_before_init" = xy |
| 139 | then |
| 140 | echo "Flash images found, will update before starting init." |
| 141 | mv /${imagebasename}* ${image%$imagebasename} |
| 142 | else |
| 143 | echo "Flash images found, will use but deferring flash update." |
| 144 | mv /${imagebasename}* /run/ |
| 145 | fi |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 146 | fi |
| 147 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 148 | if grep -w clean-rwfs-filesystem $optfile |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 149 | then |
| 150 | echo "Cleaning of read-write overlay filesystem requested." |
| 151 | touch $trigger |
| 152 | fi |
| 153 | |
| 154 | if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger |
| 155 | then |
| 156 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 157 | touch $trigger |
| 158 | fi |
| 159 | |
| 160 | if ls $image* > /dev/null 2>&1 |
| 161 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 162 | if ! test -x $update |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 163 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 164 | debug_takeover "Flash update requested but $update missing!" |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 165 | elif test -f $trigger -a ! -s $trigger |
| 166 | then |
| 167 | echo "Saving selected files from read-write overlay filesystem." |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 168 | $update --no-restore-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 169 | echo "Clearing read-write overlay filesystem." |
| 170 | flash_eraseall /dev/$rwfs |
| 171 | echo "Restoring saved files to read-write overlay filesystem." |
| 172 | touch $trigger |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 173 | $update --no-save-files --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 174 | else |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 175 | $update --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 176 | fi |
| 177 | |
| 178 | rwfst=$(probe_fs_type $rwdev) |
| 179 | fsck=$fsckbase$rwfst |
| 180 | fi |
| 181 | |
Milton D. Miller II | edfd12c | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 182 | if grep -w overlay-filesystem-in-ram $optfile |
| 183 | then |
| 184 | rwfst=none |
| 185 | fi |
| 186 | |
Milton D. Miller II | 2728b73 | 2016-02-29 12:10:34 -0600 | [diff] [blame] | 187 | copyfiles= |
| 188 | if grep -w copy-files-to-ram $optfile |
| 189 | then |
| 190 | rwfst=none |
| 191 | copyfiles=y |
| 192 | fi |
| 193 | |
| 194 | # It would be nice to do this after fsck but that mean rofs is mounted |
| 195 | # which triggers the mtd is mounted check |
| 196 | if test "$rwfst$copyfiles" = noney |
| 197 | then |
| 198 | touch $trigger |
| 199 | $update --copy-files --clean-saved-files --no-restore-files |
| 200 | fi |
| 201 | |
Milton D. Miller II | c201b04 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 202 | if grep -w copy-base-filesystem-to-ram $optfile && |
| 203 | test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs |
| 204 | then |
| 205 | # Remove any partial copy to avoid attempted usage later |
| 206 | if test -e /run/image-rofs |
| 207 | then |
| 208 | ls -l /run/image-rofs |
| 209 | rm -f /run/image-rofs |
| 210 | fi |
| 211 | debug_takeover "Copying $rodev to /run/image-rofs failed." |
| 212 | fi |
| 213 | |
Milton D. Miller II | a36e99c | 2016-02-27 16:04:32 -0600 | [diff] [blame] | 214 | if test -s /run/image-rofs |
| 215 | then |
| 216 | rodev=/run/image-rofs |
| 217 | roopts=$roopts,loop |
| 218 | fi |
| 219 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 220 | mount $rodev $rodir -t $rofst -o $roopts |
| 221 | |
| 222 | if test -x $rodir$fsck |
| 223 | then |
| 224 | for fs in $fslist |
| 225 | do |
| 226 | mount --bind $fs $rodir/$fs |
| 227 | done |
| 228 | chroot $rodir $fsck $fsckopts $rwdev |
| 229 | rc=$? |
| 230 | for fs in $fslist |
| 231 | do |
| 232 | umount $rodir/$fs |
| 233 | done |
| 234 | if test $rc -gt 1 |
| 235 | then |
| 236 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 237 | fi |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 238 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
Milton Miller | f81c108 | 2016-02-23 21:00:06 -0600 | [diff] [blame] | 239 | then |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 240 | echo "No '$fsck' in read only fs, skipping fsck." |
| 241 | fi |
| 242 | |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 243 | if test "$rwfst" = none |
| 244 | then |
| 245 | echo "Running with read-write overlay in RAM for this boot." |
| 246 | echo "No state will be preserved unless flash update performed." |
| 247 | elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 248 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 249 | msg="$(cat)" << HERE |
| 250 | |
| 251 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 252 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 253 | to to continue, or do change nothing to run from RAM for this boot. |
| 254 | HERE |
| 255 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 256 | fi |
| 257 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 258 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 259 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 260 | |
| 261 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 262 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 263 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 264 | do |
| 265 | msg="$(cat)" << HERE |
| 266 | |
| 267 | Unable to confirm /sbin/init is an executable non-empty file |
| 268 | in merged file system mounted at /root. |
| 269 | |
| 270 | Change Root test failed! Invoking emergency shell. |
| 271 | HERE |
| 272 | debug_takeover "$msg" |
| 273 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 274 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 275 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 276 | do |
| 277 | mount --move $f root/$f |
| 278 | done |
| 279 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 280 | # switch_root /root $init |
| 281 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 282 | |