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 D. Miller II | 9be688c | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 60 | setup_resolv() { |
| 61 | runresolv=/run/systemd/resolve/resolv.conf |
| 62 | etcresolv=/etc/resolv.conf |
| 63 | |
| 64 | if test ! -e $etcresolv -a ! -L $etcresolv |
| 65 | then |
| 66 | mkdir -p ${runresolv%/*} |
| 67 | ln -s $runresolv $etcresolv |
| 68 | fi |
| 69 | if test ! -f $runresolv |
| 70 | then |
| 71 | cat /proc/net/pnp > $runresolv |
| 72 | fi |
| 73 | |
| 74 | return 0 |
| 75 | } |
| 76 | |
| 77 | try_tftp() { |
| 78 | # split into tftp:// host:port/ path/on/remote |
| 79 | # then spilt off / and then :port from the end of host:port/ |
| 80 | # and : from the beginning of port |
| 81 | |
| 82 | rest="${1#tftp://}" |
| 83 | path=${rest#*/} |
| 84 | host=${rest%$path} |
| 85 | host="${host%/}" |
| 86 | port="${host#${host%:*}}" |
| 87 | host="${host%$port}" |
| 88 | port="${port#:}" |
| 89 | |
| 90 | setup_resolv |
| 91 | |
| 92 | if test -z "$host" -o -z "$path" |
| 93 | then |
| 94 | debug_takeover "Invalid tftp download url '$url'." |
| 95 | elif echo "Downloading '$url' from $host ..." && |
| 96 | ! tftp -g -r "$path" -l /run/image-rofs "$host" ${port+"$port"} |
| 97 | then |
| 98 | debug_takeover "Download of '$url' failed." |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | try_wget() { |
| 103 | setup_resolv |
| 104 | |
| 105 | echo "Downloading '$1' ..." |
| 106 | if ! wget -O /run/image-rofs "$1" |
| 107 | then |
| 108 | debug_takeover "Download of '$url' failed." |
| 109 | fi |
| 110 | } |
| 111 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 112 | debug_takeover() { |
| 113 | echo "$@" |
| 114 | test -n "$@" && echo Enter password to try to manually fix. |
| 115 | cat << HERE |
| 116 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 117 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 118 | HERE |
| 119 | |
| 120 | while ! sulogin && ! test -f /takeover |
| 121 | do |
| 122 | echo getty failed, retrying |
| 123 | done |
| 124 | |
| 125 | # Touch /takeover in the above getty to become pid 1 |
| 126 | if test -e /takeover |
| 127 | then |
| 128 | cat << HERE |
| 129 | |
| 130 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 131 | When finished exec new init or cleanup and run reboot -f. |
| 132 | |
| 133 | Warning: No job control! Shell exit will panic the system! |
| 134 | HERE |
| 135 | export PS1=init#\ |
| 136 | exec /bin/sh |
| 137 | fi |
| 138 | } |
| 139 | |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 140 | env=$(findmtd u-boot-env) |
| 141 | if test -n $env |
| 142 | then |
| 143 | ln -s /dev/$env /run/mtd:u-boot-env |
| 144 | cp /run/mtd:u-boot-env /run/fw_env |
| 145 | fi |
| 146 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 147 | rofs=$(findmtd rofs) |
| 148 | rwfs=$(findmtd rwfs) |
| 149 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 150 | rodev=/dev/mtdblock${rofs#mtd} |
| 151 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 152 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 153 | # Set to y for yes, anything else for no. |
| 154 | force_rwfst_jffs2=y |
| 155 | flash_images_before_init=n |
Milton D. Miller II | 9be688c | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 156 | consider_download_files=y |
| 157 | consider_download_tftp=y |
| 158 | consider_download_http=y |
| 159 | consider_download_ftp=y |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 160 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 161 | rofst=squashfs |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 162 | rwfst=$(probe_fs_type $rwdev) |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 163 | roopts=ro |
| 164 | rwopts=rw |
| 165 | |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 166 | image=/run/initramfs/image- |
| 167 | trigger=${image}rwfs |
| 168 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 169 | init=/sbin/init |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 170 | fsckbase=/sbin/fsck. |
| 171 | fsck=$fsckbase$rwfst |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 172 | fsckopts=-a |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 173 | optfile=/run/initramfs/init-options |
Milton D. Miller II | 9be688c | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 174 | urlfile=/run/initramfs/init-download-url |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 175 | update=/run/initramfs/update |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 176 | |
Milton D. Miller II | d297720 | 2016-03-07 07:59:09 -0600 | [diff] [blame] | 177 | if test -e /${optfile##*/} |
| 178 | then |
| 179 | cp /${optfile##*/} $optfile |
| 180 | fi |
| 181 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 182 | if test ! -f $optfile |
| 183 | then |
| 184 | cat /proc/cmdline > $optfile |
Milton D. Miller II | 04d2aff | 2016-03-02 15:23:22 -0600 | [diff] [blame] | 185 | get_fw_env_var openbmcinit >> $optfile |
| 186 | get_fw_env_var openbmconce >> $optfile |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 187 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 188 | |
| 189 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 190 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 191 | if grep -w debug-init-sh $optfile |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 192 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 193 | debug_takeover "Debug initial shell requested by command line." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 194 | fi |
| 195 | |
Milton D. Miller II | 9be688c | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 196 | if test "x$consider_download_files" = xy && |
| 197 | grep -w openbmc-init-download-files $optfile |
| 198 | then |
| 199 | if test -f ${urlfile##*/} |
| 200 | then |
| 201 | cp ${urlfile##*/} $urlfile |
| 202 | fi |
| 203 | if test ! -f $urlfile |
| 204 | then |
| 205 | get_fw_env_var openbmcinitdownloadurl > $urlfile |
| 206 | fi |
| 207 | url="$(cat $urlfile)" |
| 208 | rest="${url#*://}" |
| 209 | proto="${url%$rest}" |
| 210 | |
| 211 | if test -z "$url" |
| 212 | then |
| 213 | echo "Download url empty. Ignoring download request." |
| 214 | elif test -z "$proto" |
| 215 | then |
| 216 | echo "Download failed." |
| 217 | elif test "$proto" = tftp:// |
| 218 | then |
| 219 | if test "x$consider_download_tftp" = xy |
| 220 | then |
| 221 | try_tftp "$url" |
| 222 | else |
| 223 | echo "Download failed." |
| 224 | fi |
| 225 | elif test "$proto" = http:// |
| 226 | then |
| 227 | if test "x$consider_download_http" = xy |
| 228 | then |
| 229 | try_wget "$url" |
| 230 | else |
| 231 | echo "Download failed." |
| 232 | fi |
| 233 | elif test "$proto" = ftp:// |
| 234 | then |
| 235 | if test "x$consider_download_ftp" = xy |
| 236 | then |
| 237 | try_wget "$url" |
| 238 | else |
| 239 | echo "Download failed." |
| 240 | fi |
| 241 | else |
| 242 | echo "Download failed." |
| 243 | fi |
| 244 | fi |
| 245 | |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 246 | # 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] | 247 | imagebasename=${image##*/} |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 248 | if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1 |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 249 | then |
Milton D. Miller II | 1aaffe8 | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 250 | if test "x$flash_images_before_init" = xy |
| 251 | then |
| 252 | echo "Flash images found, will update before starting init." |
| 253 | mv /${imagebasename}* ${image%$imagebasename} |
| 254 | else |
| 255 | echo "Flash images found, will use but deferring flash update." |
| 256 | mv /${imagebasename}* /run/ |
| 257 | fi |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 258 | fi |
| 259 | |
Milton D. Miller II | 1650db5 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 260 | if grep -w clean-rwfs-filesystem $optfile |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 261 | then |
| 262 | echo "Cleaning of read-write overlay filesystem requested." |
| 263 | touch $trigger |
| 264 | fi |
| 265 | |
| 266 | if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger |
| 267 | then |
| 268 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 269 | touch $trigger |
| 270 | fi |
| 271 | |
| 272 | if ls $image* > /dev/null 2>&1 |
| 273 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 274 | if ! test -x $update |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 275 | then |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 276 | debug_takeover "Flash update requested but $update missing!" |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 277 | elif test -f $trigger -a ! -s $trigger |
| 278 | then |
| 279 | echo "Saving selected files from read-write overlay filesystem." |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 280 | $update --no-restore-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 281 | echo "Clearing read-write overlay filesystem." |
| 282 | flash_eraseall /dev/$rwfs |
| 283 | echo "Restoring saved files to read-write overlay filesystem." |
| 284 | touch $trigger |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 285 | $update --no-save-files --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 286 | else |
Milton D. Miller II | dbc1137 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 287 | $update --clean-saved-files |
Milton Miller | 0b0d5fe | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 288 | fi |
| 289 | |
| 290 | rwfst=$(probe_fs_type $rwdev) |
| 291 | fsck=$fsckbase$rwfst |
| 292 | fi |
| 293 | |
Milton D. Miller II | edfd12c | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 294 | if grep -w overlay-filesystem-in-ram $optfile |
| 295 | then |
| 296 | rwfst=none |
| 297 | fi |
| 298 | |
Milton D. Miller II | 2728b73 | 2016-02-29 12:10:34 -0600 | [diff] [blame] | 299 | copyfiles= |
| 300 | if grep -w copy-files-to-ram $optfile |
| 301 | then |
| 302 | rwfst=none |
| 303 | copyfiles=y |
| 304 | fi |
| 305 | |
| 306 | # It would be nice to do this after fsck but that mean rofs is mounted |
| 307 | # which triggers the mtd is mounted check |
| 308 | if test "$rwfst$copyfiles" = noney |
| 309 | then |
| 310 | touch $trigger |
| 311 | $update --copy-files --clean-saved-files --no-restore-files |
| 312 | fi |
| 313 | |
Milton D. Miller II | c201b04 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 314 | if grep -w copy-base-filesystem-to-ram $optfile && |
| 315 | test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs |
| 316 | then |
| 317 | # Remove any partial copy to avoid attempted usage later |
| 318 | if test -e /run/image-rofs |
| 319 | then |
| 320 | ls -l /run/image-rofs |
| 321 | rm -f /run/image-rofs |
| 322 | fi |
| 323 | debug_takeover "Copying $rodev to /run/image-rofs failed." |
| 324 | fi |
| 325 | |
Milton D. Miller II | a36e99c | 2016-02-27 16:04:32 -0600 | [diff] [blame] | 326 | if test -s /run/image-rofs |
| 327 | then |
| 328 | rodev=/run/image-rofs |
| 329 | roopts=$roopts,loop |
| 330 | fi |
| 331 | |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 332 | mount $rodev $rodir -t $rofst -o $roopts |
| 333 | |
| 334 | if test -x $rodir$fsck |
| 335 | then |
| 336 | for fs in $fslist |
| 337 | do |
| 338 | mount --bind $fs $rodir/$fs |
| 339 | done |
| 340 | chroot $rodir $fsck $fsckopts $rwdev |
| 341 | rc=$? |
| 342 | for fs in $fslist |
| 343 | do |
| 344 | umount $rodir/$fs |
| 345 | done |
| 346 | if test $rc -gt 1 |
| 347 | then |
| 348 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 349 | fi |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 350 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
Milton Miller | f81c108 | 2016-02-23 21:00:06 -0600 | [diff] [blame] | 351 | then |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 352 | echo "No '$fsck' in read only fs, skipping fsck." |
| 353 | fi |
| 354 | |
Milton D. Miller II | 6dba31f | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 355 | if test "$rwfst" = none |
| 356 | then |
| 357 | echo "Running with read-write overlay in RAM for this boot." |
| 358 | echo "No state will be preserved unless flash update performed." |
| 359 | elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 360 | then |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 361 | msg="$(cat)" << HERE |
| 362 | |
| 363 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | ed5fb27 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 364 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 365 | to to continue, or do change nothing to run from RAM for this boot. |
| 366 | HERE |
| 367 | debug_takeover "$msg" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 368 | fi |
| 369 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 370 | rm -rf $work |
Milton Miller | b72114c | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 371 | mkdir -p $upper $work |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 372 | |
| 373 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 374 | |
Milton Miller | 06ccb1a | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 375 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 376 | do |
| 377 | msg="$(cat)" << HERE |
| 378 | |
| 379 | Unable to confirm /sbin/init is an executable non-empty file |
| 380 | in merged file system mounted at /root. |
| 381 | |
| 382 | Change Root test failed! Invoking emergency shell. |
| 383 | HERE |
| 384 | debug_takeover "$msg" |
| 385 | done |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 386 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 387 | for f in $fslist |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 388 | do |
| 389 | mount --move $f root/$f |
| 390 | done |
| 391 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 392 | # switch_root /root $init |
| 393 | exec chroot /root $init |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 394 | |