init (9914B)
1 #!/bin/busybox sh 2 # chamesle initramfs. no root= means live: find the stick, overlay a tmpfs on the squashfs 3 # options: root= rootfstype= rootflags= init= chamesle.iso=PATH chamesle.toram=1 chamesle.label=NAME 4 # chamesle.wait=SECS chamesle.debug=1 (shell before switch_root) chamesle.shell=1 (shell first) 5 export PATH=/bin:/sbin:/usr/bin:/usr/sbin 6 7 /bin/busybox mkdir -p /proc /sys /dev /mnt /newroot /overlay 8 /bin/busybox mount -t proc proc /proc 9 /bin/busybox mount -t sysfs sysfs /sys 10 /bin/busybox mount -t devtmpfs devtmpfs /dev 11 /bin/busybox --install -s /bin 2>/dev/null 12 exec </dev/console >/dev/console 2>&1 13 14 log() { echo "[live] $*"; } 15 SPLASH=0 16 emergency() { 17 # something's wrong: the blue screen says what, any key goes on to the shell. not for the debug shells 18 if [ "$SPLASH" = 1 ] && [ "$DEBUG" = 0 ] && [ "$SHELL_FIRST" = 0 ]; then 19 plymouth display-message --text=chamesle-error 20 plymouth display-message --text="$*" 21 plymouth watch-keystroke >/dev/null 2>&1 22 fi 23 [ "$SPLASH" = 1 ] && { plymouth quit; SPLASH=0; } 24 echo "[live] $*" 25 echo "[live] starting a shell; leave it with 'exit' to try again" 26 setsid cttyhack sh 27 } 28 # LUKS1 and LUKS2 both start with "LUKS\xba\xbe" 29 is_luks() { 30 [ "$(dd if="$1" bs=6 count=1 2>/dev/null | od -An -tx1 | tr -d ' \t\n\r')" = "4c554b53babe" ] 31 } 32 33 LABEL=CHAMESLE ISO_HINT= DEBUG=0 SHELL_FIRST=0 TORAM=0 WAIT=60 INIT=/usr/bin/dinit 34 ROOT= ROOTFSTYPE= ROOTFLAGS= QUIET=0 35 for a in $(cat /proc/cmdline); do 36 case "$a" in 37 chamesle.iso=*) ISO_HINT=${a#chamesle.iso=} ;; 38 chamesle.debug=1) DEBUG=1 ;; 39 chamesle.shell=1) SHELL_FIRST=1 ;; 40 chamesle.toram=1) TORAM=1 ;; 41 chamesle.label=*) LABEL=${a#chamesle.label=} ;; 42 chamesle.wait=*) WAIT=${a#chamesle.wait=} ;; 43 init=/*) INIT=${a#init=} ;; 44 root=*) ROOT=${a#root=} ;; 45 rootfstype=*) ROOTFSTYPE=${a#rootfstype=} ;; 46 rootflags=*) ROOTFLAGS=${a#rootflags=} ;; 47 quiet) QUIET=1 ;; 48 esac 49 done 50 51 # storage and filesystem drivers 52 if [ -d "/lib/modules/$(uname -r)" ]; then 53 for m in usb_storage uas xhci_pci ehci_pci ohci_pci uhci_hcd ahci nvme virtio_pci virtio_blk virtio_scsi \ 54 libata ata_piix ata_generic pata_acpi scsi_mod mmc_block sd_mod sr_mod isofs udf vfat exfat ext4 xfs btrfs f2fs \ 55 squashfs loop overlay dm_mod dm_crypt; do 56 modprobe "$m" 2>/dev/null || FAILED_MODS="${FAILED_MODS:-} $m" 57 done 58 [ -n "${FAILED_MODS:-}" ] && log "modules not loaded (built in or not shipped):$FAILED_MODS" 59 mdev -s 2>/dev/null 60 else 61 log "no kernel modules in the initramfs; relying on built-in drivers" 62 fi 63 64 # drivers for whatever hardware is there. again while waiting, new controllers bring new devices 65 load_hw_modules() { 66 [ -d "/lib/modules/$(uname -r)" ] || return 0 67 for f in $(find /sys/devices -name modalias 2>/dev/null); do 68 read -r alias < "$f" 2>/dev/null || continue 69 [ -n "$alias" ] && modprobe -q "$alias" 2>/dev/null 70 done 71 mdev -s 2>/dev/null 72 return 0 73 } 74 load_hw_modules 75 76 # boot splash 77 if grep -qw splash /proc/cmdline && [ -x /usr/bin/plymouthd ]; then 78 mkdir -p /run/plymouth /run/udev /dev/pts 79 mount -t devpts devpts /dev/pts 2>/dev/null 80 # plymouth only finds the framebuffer through udev 81 if [ -x /usr/bin/udevd ]; then 82 udevd --daemon --resolve-names=never 2>/dev/null 83 udevadm trigger --action=add --subsystem-match=graphics --subsystem-match=drm 84 udevadm settle --timeout=5 85 fi 86 plymouthd --mode=boot --attach-to-session --pid-file=/run/plymouth/pid && plymouth show-splash && SPLASH=1 87 fi 88 89 [ "$SHELL_FIRST" = 1 ] && emergency "chamesle.shell=1" 90 91 # with several chamesle isos on one ventoy stick, take the one matching our build id (not after 15s) 92 STRICT=1 93 [ -s /build-id ] || STRICT=0 94 has_root() { 95 [ -f "$1/live/rootfs.squashfs" ] || return 1 96 [ "$STRICT" = 0 ] || [ "$(cat "$1/live/build-id" 2>/dev/null)" = "$(cat /build-id)" ] 97 } 98 99 # every block device, and iso files on them (ventoy). sets MEDIUM 100 scan_once() { 101 n=0 102 ldev=$(findfs "LABEL=$LABEL" 2>/dev/null) && [ -b "$ldev" ] && mkdir -p /mnt/label && 103 mount -o ro "$ldev" /mnt/label 2>/dev/null && { 104 if has_root /mnt/label; then MEDIUM=/mnt/label; return 0; fi 105 umount /mnt/label 2>/dev/null 106 } 107 for dev in /dev/sr[0-9]* /dev/sd[a-z] /dev/sd[a-z][0-9]* /dev/vd[a-z] /dev/vd[a-z][0-9]* \ 108 /dev/nvme[0-9]n[0-9] /dev/nvme[0-9]n[0-9]p[0-9]* /dev/mmcblk[0-9] /dev/mmcblk[0-9]p[0-9]* \ 109 /dev/mapper/*; do 110 [ -b "$dev" ] || continue 111 n=$((n + 1)); mp=/mnt/d$n 112 mkdir -p "$mp" 113 mount -o ro "$dev" "$mp" 2>/dev/null || continue 114 if has_root "$mp"; then MEDIUM=$mp; return 0; fi 115 k=0 116 check_isos() { 117 for iso in "$@"; do 118 [ -f "$iso" ] || continue 119 k=$((k + 1)); imp=/mnt/i$n-$k 120 mkdir -p "$imp" 121 if mount -o loop,ro "$iso" "$imp" 2>/dev/null; then 122 if has_root "$imp"; then MEDIUM=$imp; return 0; fi 123 umount "$imp" 2>/dev/null 124 fi 125 done 126 return 1 127 } 128 set -- 129 [ -n "${ISO_HINT:-}" ] && [ -f "$mp$ISO_HINT" ] && set -- "$@" "$mp$ISO_HINT" 130 for f in "$mp"/[Cc]hamesle*.iso "$mp"/*/[Cc]hamesle*.iso "$mp"/*.iso "$mp"/*/*.iso; do 131 [ -f "$f" ] && set -- "$@" "$f" 132 done 133 check_isos "$@" 134 [ -n "${MEDIUM:-}" ] && return 0 135 umount "$mp" 2>/dev/null 136 done 137 return 1 138 } 139 140 # findfs knows UUID= and LABEL=; a /dev path is used as it is 141 find_dev() { 142 case "$1" in 143 /dev/*) [ -b "$1" ] && echo "$1" ;; 144 *) findfs "$1" 2>/dev/null ;; 145 esac 146 } 147 148 if [ -n "$ROOT" ]; then 149 i=0 150 until DEV=$(find_dev "$ROOT") && [ -n "$DEV" ]; do 151 i=$((i + 1)) 152 if [ "$i" -ge "$WAIT" ]; then 153 emergency "could not find the root partition ($ROOT)" 154 i=0 155 fi 156 [ "$i" = 1 ] && log "waiting for the root partition ($ROOT)..." 157 sleep 1 158 load_hw_modules 159 done 160 if is_luks "$DEV"; then 161 log "encrypted root partition: enter the passphrase to unlock it" 162 until [ -e /dev/mapper/root ]; do 163 if [ "$SPLASH" = 1 ]; then 164 plymouth ask-for-password --prompt="Passphrase" --command="cryptsetup open --key-file=- $DEV root" || 165 plymouth display-message --text="Wrong passphrase, try again" 166 else 167 cryptsetup open "$DEV" root || log "wrong passphrase, try again" 168 fi 169 done 170 DEV=/dev/mapper/root 171 fi 172 until mount ${ROOTFSTYPE:+-t "$ROOTFSTYPE"} -o "rw${ROOTFLAGS:+,$ROOTFLAGS}" "$DEV" /newroot; do 173 emergency "could not mount $DEV on / (rootfstype=${ROOTFSTYPE:-auto} rootflags=${ROOTFLAGS:-none})" 174 done 175 else 176 while :; do 177 i=0 178 until scan_once; do 179 i=$((i + 1)) 180 [ "$i" -ge 15 ] && STRICT=0 181 if [ "$i" -ge "$WAIT" ]; then break; fi 182 [ "$i" = 1 ] && log "waiting for the live medium..." 183 sleep 1 184 load_hw_modules 185 done 186 [ -n "${MEDIUM:-}" ] && break 187 log "filesystems: $(tr -d '\t' </proc/filesystems | grep -E 'iso9660|squashfs|overlay|vfat|ext4' | tr '\n' ' ')" 188 log "kernel modules loaded: $(cut -d' ' -f1 /proc/modules 2>/dev/null | tr '\n' ' ')" 189 log "block devices: $(cat /proc/partitions 2>/dev/null | awk 'NR>2 {print $4}' | tr '\n' ' ')" 190 emergency "could not find the live medium (a device with /live/rootfs.squashfs)" 191 done 192 log "medium found: $MEDIUM" 193 194 SQ=$MEDIUM/live/rootfs.squashfs 195 if [ "$TORAM" = 1 ]; then 196 log "copying the system into RAM (this takes a moment)..." 197 avail=$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo) 198 need=$(du -k "$SQ" | awk '{print $1}') 199 if [ "${avail:-0}" -lt "$((need * 2 + 50000))" ]; then 200 emergency "not enough RAM for toram (need ${need}KB plus the running system, have ${avail:-0}KB)" 201 fi 202 cp "$SQ" /rootfs.squashfs && SQ=/rootfs.squashfs 203 fi 204 205 mkdir -p /mnt/squash 206 until mount -t squashfs -o loop,ro "$SQ" /mnt/squash; do 207 emergency "could not mount $SQ" 208 done 209 mount -t tmpfs -o size=75% tmpfs /overlay 210 mkdir -p /overlay/upper /overlay/work 211 until mount -t overlay overlay -o lowerdir=/mnt/squash,upperdir=/overlay/upper,workdir=/overlay/work /newroot; do 212 emergency "could not create the overlay root" 213 done 214 mkdir -p /newroot/live-medium 215 mount --move "$MEDIUM" /newroot/live-medium 216 fi 217 218 mkdir -p /newroot/proc /newroot/sys /newroot/dev /newroot/run 219 mount --move /proc /newroot/proc 220 mount --move /sys /newroot/sys 221 mount --move /dev /newroot/dev 222 # dinit wants /run for its socket before any service runs 223 mount -t tmpfs -o mode=0755,nosuid,nodev tmpfs /newroot/run 224 if [ -n "${MEDIUM:-}" ]; then 225 : > /newroot/run/chamesle-live 226 # what the installer copies 227 mkdir -p /newroot/run/chamesle/rootfs 228 mount --bind /mnt/squash /newroot/run/chamesle/rootfs 229 fi 230 [ "$DEBUG" = 1 ] && emergency "chamesle.debug=1: about to switch root" 231 [ -x "/newroot$INIT" ] || emergency "$INIT not found in the system image" 232 INIT_ARGS= 233 # dinit starts the blue screen (its recovery service) on its own if the system can't finish starting. its log is 234 # how the blue screen knows what failed 235 [ "$INIT" = /usr/bin/dinit ] && [ -x /newroot/usr/lib/chamesle/blue-screen ] && 236 INIT_ARGS="--auto-recovery --log-file /run/dinit.log" 237 [ "$QUIET" = 1 ] && [ "$INIT" = /usr/bin/dinit ] && [ "$DEBUG" = 0 ] && INIT_ARGS="$INIT_ARGS --quiet" 238 [ "$SPLASH" = 1 ] && plymouth update-root-fs --new-root-dir=/newroot 239 udevadm control --exit 2>/dev/null 240 exec switch_root /newroot "$INIT" $INIT_ARGS