|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | +busybox mount -n -t proc proc /proc |
| 4 | +busybox mount -n -t devtmpfs devtmpfs /dev |
| 5 | +busybox mount -n -t sysfs sysfs /sys |
| 6 | +busybox --install -s |
| 7 | +if [ ! -x "/dev/pts" ]; then mkdir /dev/pts; fi |
| 8 | +if [ ! -x "/dev/shm" ]; then mkdir /dev/shm; fi |
| 9 | +busybox mount -n -t devpts devpts /dev/pts -o gid=5,mode=620,ptmxmode=000 |
| 10 | + |
| 11 | +cmdline_arg() { |
| 12 | + local name="$1" |
| 13 | + local value="${2-}" |
| 14 | + for arg in $(cat /proc/cmdline); do |
| 15 | + if [[ "${arg%%=*}" == "${name}" ]]; then |
| 16 | + value="${arg#*=}" |
| 17 | + fi |
| 18 | + done |
| 19 | + echo "${value}" |
| 20 | +} |
| 21 | + |
| 22 | +# Custom debug breakpoint |
| 23 | +if [ "$(cmdline_arg rd.earlyshell)" != "" ]; then |
| 24 | + busybox sh |
| 25 | +fi |
| 26 | +if [ "$(cmdline_arg rd.earlytrace)" != "" ]; then |
| 27 | + set -x |
| 28 | +fi |
| 29 | + |
| 30 | +mdev -d |
| 31 | +mdev -s |
| 32 | +# Coldplugging but with using /sbin/modprobe (which is kmod) instead of busybox's modprobe |
| 33 | +# because busybox doesn't properly support the globs in modules.alias |
| 34 | +find /sys/ -name modalias -print0 | xargs -0 sort -u | tr '\n' '\0' | xargs -0 /sbin/modprobe -abq || true |
| 35 | +# Required to access disks, but not autoloaded: |
| 36 | +modprobe sd_mod |
| 37 | + |
| 38 | +if [ "$(cmdline_arg rd.earlyshell)" != "" ]; then |
| 39 | + busybox sh |
| 40 | +fi |
| 41 | + |
| 42 | +find_usr() { |
| 43 | + local UEVENTLINE="$1" |
| 44 | + local DRIVE= |
| 45 | + local WAITINGMSG= |
| 46 | + while [ "${DRIVE}" = "" ]; do |
| 47 | + DRIVE="$({ grep -s -l -m 1 -r "${UEVENTLINE}" /sys/class/block/*/uevent || true; } | cut -d / -f 5)" |
| 48 | + if [ "${DRIVE}" = "" ] && [ "${WAITINGMSG}" = "" ]; then |
| 49 | + echo "Waiting for drive..." >&2 |
| 50 | + WAITINGMSG=1 |
| 51 | + fi |
| 52 | + done |
| 53 | + DRIVE="/dev/${DRIVE}" |
| 54 | + echo "${DRIVE}" |
| 55 | +} |
| 56 | + |
| 57 | +# Ported code from the generators |
| 58 | +usr=$(cmdline_arg mount.usr $(cmdline_arg usr)) |
| 59 | +usrfstype=$(cmdline_arg mount.usrfstype $(cmdline_arg usrfstype auto)) |
| 60 | +usrflags=$(cmdline_arg mount.usrflags $(cmdline_arg usrflags ro)) |
| 61 | + |
| 62 | +case "${usr}" in |
| 63 | + LABEL=*) |
| 64 | + usr="LABEL=$(echo "$usr" | sed 's,/,\\x2f,g')" |
| 65 | + usr=$(find_usr "${usr}") |
| 66 | + ;; |
| 67 | + UUID=*) |
| 68 | + usr="${usr#UUID=}" |
| 69 | + usr="UUID=$(echo "$usr" | tr "[:upper:]" "[:lower:]")" |
| 70 | + usr=$(find_usr "${usr}") |
| 71 | + ;; |
| 72 | + PARTUUID=*) |
| 73 | + usr="${usr#PARTUUID=}" |
| 74 | + usr="PARTUUID=$(echo "$usr" | tr "[:upper:]" "[:lower:]")" |
| 75 | + usr=$(find_usr "${usr}") |
| 76 | + ;; |
| 77 | + PARTLABEL=*) |
| 78 | + usr=$(find_usr "${usr}") |
| 79 | + ;; |
| 80 | +esac |
| 81 | + |
| 82 | +if [ "${usr}" = "" ] && [ -f /usr.squashfs ]; then |
| 83 | + usr=/usr.squashfs |
| 84 | + usrfstype=squashfs |
| 85 | +elif [ "${usrfstype}" = btrfs ] || [ "${usrfstype}" = auto ]; then |
| 86 | + if [ "$(echo ",${usrflags}," | grep -v -F ',ro,')" != "" ]; then |
| 87 | + true # Don't set "norecovery" when mounting rw |
| 88 | + else |
| 89 | + usrflags="${usrflags},rescue=nologreplay" |
| 90 | + fi |
| 91 | +fi |
| 92 | +# Only proceed if the source is a path. |
| 93 | +if echo "${usr}" | grep -q "^/"; then |
| 94 | + echo "No mountable /usr partition given (usr='${usr}')" >&2 |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +usrhash=$(cmdline_arg verity.usrhash) |
| 99 | +if [ "${usrhash}" != "" ]; then |
| 100 | + # Hardcoded expected value from the image GPT layout |
| 101 | + veritysetup --panic-on-corruption --hash-offset=1065345024 open "${usr}" usr "${usr}" "${usrhash}" |
| 102 | + # If there's a hash mismatch during table initialization, |
| 103 | + # veritysetup reports it on stderr but still exits 0. |
| 104 | + # Manually check the target status and fail if invalid. |
| 105 | + read off len tgt status addl <<<$(dmsetup status usr) |
| 106 | + if [ "${status}" != V ]; then |
| 107 | + echo "Verity setup failed" >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +fi |
| 111 | + |
| 112 | +echo "Mounting /usr from ${usr}" |
| 113 | +mount -t "${usrfstype}" -o "${usrflags}" "${usr}" /sysusr/usr |
| 114 | + |
| 115 | +# Busybox doesn't load this for us |
| 116 | +modprobe loop |
| 117 | +losetup -r -f /sysusr/usr/lib/flatcar/bootengine.img |
| 118 | +mkdir /underlay /work |
| 119 | +mount -t tmpfs tmpfs /work |
| 120 | +mkdir /work/realinit /work/work |
| 121 | +mount -t squashfs /dev/loop0 /underlay |
| 122 | +mount -t overlay -o rw,lowerdir=/underlay,upperdir=/work/realinit,workdir=/work/work overlay /realinit |
| 123 | +mkdir -p /realinit/sysusr/usr |
| 124 | +mount -o move /sysusr/usr /realinit/sysusr/usr |
| 125 | +if [ "${usr}" = /usr.squashfs ]; then |
| 126 | + mkdir -p /oem |
| 127 | + mkdir -p /realinit/oem |
| 128 | + mount -o bind /oem /realinit/oem |
| 129 | + touch /realinit/usr.squashfs |
| 130 | + mount -o bind /usr.squashfs /realinit/usr.squashfs |
| 131 | +fi |
| 132 | +if [ "$(cmdline_arg rd.earlyshell)" != "" ]; then |
| 133 | + busybox sh |
| 134 | +fi |
| 135 | +killall mdev || true |
| 136 | +umount /proc |
| 137 | +umount /sys |
| 138 | +umount /dev/pts |
| 139 | +# Lazy unmount because /dev/console is held by the current process |
| 140 | +umount -l /dev |
| 141 | +exec switch_root /realinit /init |
0 commit comments