patina-install (4029B)
1 #!/usr/bin/bash 2 # Installs Sector F Linux onto a whole drive, from the payload on the live disc (ADR-0014, ADR-0011): 3 # 4 # patina-install /dev/vda 5 # 6 # Run as root, through pkexec (the polkit rule lets the installer's user do that on the live disc, 7 # and only there). The installer app calls it after the person has chosen the drive and confirmed. 8 # 9 # It refuses anything but a whole, unmounted, writable disk that is not the one the live system 10 # booted from, and wipes it: the picker in the app is the first line of defence, this is the second, 11 # since a bug in an app must not be able to erase the wrong drive. 12 # 13 # Progress goes to stdout as lines "SIZE <bytes>" (the system's size) and "STEP <n>/<total> <what>", 14 # then bootc's own output. 15 set -euo pipefail 16 17 die() { echo "patina-install: $*" >&2; exit 1; } 18 19 drive=${1:-} 20 [[ -n "$drive" ]] || die "usage: patina-install /dev/<drive>" 21 [[ $EUID -eq 0 ]] || die "must run as root (through pkexec)" 22 [[ -b "$drive" ]] || die "$drive is not a block device" 23 drive=$(readlink -f "$drive") 24 25 [[ "$(lsblk -dno TYPE "$drive")" == disk ]] || die "$drive is not a whole disk" 26 [[ "$(lsblk -dno RO "$drive")" == 0 ]] || die "$drive is read-only" 27 size=$(lsblk -bdno SIZE "$drive") 28 (( size >= 12 * 1024**3 )) || die "$drive is smaller than 12 GB" 29 30 # Not the drive the live system booted from (the ISO's drive, or the USB stick it was written to). 31 live=$(findmnt -no SOURCE /run/initramfs/live 2>/dev/null || true) 32 if [[ -n "$live" ]]; then 33 live_disk=$(lsblk -no PKNAME "$live" 2>/dev/null | head -1) 34 [[ -z "$live_disk" ]] && live_disk=$(basename "$live") 35 [[ "$(basename "$drive")" != "$live_disk" ]] || die "$drive is the drive this system booted from" 36 fi 37 38 # Nothing on it may be in use. 39 if lsblk -nro MOUNTPOINTS "$drive" | grep -q .; then die "$drive has mounted partitions"; fi 40 41 payload=$(cat /usr/lib/patina-install/payload) 42 target=$(cat /usr/lib/patina-install/target) 43 [[ -n "$payload" && -n "$target" ]] || die "this disc has no payload" 44 podman image exists "$payload" || die "the payload $payload is not on this disc" 45 46 # bootc unpacks the image into scratch space, and the live system's disk is RAM. It reads the payload 47 # through a helper that runs in the live system's own mount namespace, so the scratch that fills is 48 # the live system's /var/tmp (a bind mount into the container does not reach it): a tmpfs is 49 # mounted there, of 70% of the RAM (it takes memory only as it is used). It must hold the largest 50 # layer with room to spare; better to say so now than to fail half way through, with the drive 51 # already wiped. (podman prints sizes as 797kB, 1.53GB, ...: converted to bytes.) 52 largest=$(podman history --format '{{.Size}}' "$payload" | 53 awk '{ v = $1 + 0; u = $1; sub(/^[0-9.]+/, "", u); 54 m = (u == "GB") ? 1e9 : (u == "MB") ? 1e6 : (u == "kB") ? 1e3 : 1; printf "%d\n", v * m }' | 55 sort -n | tail -1) 56 [[ -n "$largest" ]] || die "cannot read the payload's layer sizes" 57 scratch_mb=$(( $(awk '/^MemTotal:/ { print $2 }' /proc/meminfo) * 7 / 10 / 1024 )) 58 need=$(( largest * 5 / 4 )) 59 if (( scratch_mb * 1024 * 1024 < need )); then 60 die "not enough memory to install: needs $(( need / 1024**2 )) MB of scratch space, can have $scratch_mb MB (this computer needs more RAM)" 61 fi 62 mount -t tmpfs -o size="${scratch_mb}m" tmpfs /var/tmp 63 trap 'umount /var/tmp' EXIT 64 65 # The size of the system being written, for the installer's progress bar: it compares it with the 66 # bytes the kernel has written to the drive. 67 echo "SIZE $(podman image inspect --format '{{.Size}}' "$payload")" 68 echo "STEP 1/2 Writing the system to the drive" 69 # bootc runs from inside the image it installs, as its docs have it: the host's /dev, and the 70 # payload from the live system's container storage. 71 podman run --rm --privileged --pid=host --network none \ 72 -v /dev:/dev -v /var/lib/containers:/var/lib/containers \ 73 "$payload" \ 74 bootc install to-disk --wipe --filesystem btrfs \ 75 --source-imgref "containers-storage:$payload" --target-imgref "$target" \ 76 "$drive" 77 78 echo "STEP 2/2 Done"