#!/usr/bin/env bash # shellcheck shell=bash # # Read-only disk and partition discovery. # # This module never writes to a disk. It exists so the installer can present # accurate information and so destructive operations can refuse to touch the # live medium, mounted filesystems or optical drives. declare -ga DISK_LIST=() # disks_available_tool disks_tool_ok() { command -v lsblk >/dev/null 2>&1; } # disks_refresh - populate DISK_LIST with whole-disk device paths disks_refresh() { DISK_LIST=() disks_tool_ok || return 1 local name path while IFS= read -r name; do path="/dev/$name" [[ -b "$path" ]] && DISK_LIST+=("$path") done < <(lsblk -dn -o NAME 2>/dev/null | sort) ((${#DISK_LIST[@]})) } # _disk_attr PATH COLUMN - single value for a whole disk _disk_attr() { local path="$1" col="$2" lsblk -dn -o "$col" "$path" 2>/dev/null | head -n1 | sed 's/[[:space:]]*$//' } disk_size_bytes() { lsblk -bdn -o SIZE "$1" 2>/dev/null | head -n1 | tr -d ' ' } disk_size_human() { local b="${1:-0}" numfmt --to=iec --suffix=B "$b" 2>/dev/null || printf '%s bytes' "$b" } disk_model() { _disk_attr "$1" MODEL; } disk_transport() { _disk_attr "$1" TRAN; } disk_is_removable() { [[ "$(_disk_attr "$1" RM)" == "1" ]]; } disk_is_readonly() { [[ "$(_disk_attr "$1" RO)" == "1" ]]; } # disk_partitions PATH - print child device paths one per line disk_partitions() { local path="$1" name while IFS= read -r name; do [[ -n "$name" ]] && printf '/dev/%s\n' "$name" done < <(lsblk -rno NAME "$path" 2>/dev/null | tail -n +2) } disk_partition_count() { disk_partitions "$1" | grep -c . || true } # disk_is_mounted PATH - true when the disk or any child is mounted disk_is_mounted() { local path="$1" dev while IFS= read -r dev; do [[ -n "$dev" ]] || continue if grep -q "^${dev} " /proc/mounts 2>/dev/null; then return 0 fi done < <(printf '%s\n' "$path"; disk_partitions "$path") return 1 } # _parent_disk DEV - resolve a device (possibly a partition) to its whole disk _parent_disk() { local dev="$1" pk dev="$(readlink -f "$dev" 2>/dev/null || printf '%s' "$dev")" case "$dev" in /dev/loop*|/dev/dm-*|/dev/mapper/*|/dev/sr*) printf '%s' "$dev"; return ;; esac pk="$(lsblk -no PKNAME "$dev" 2>/dev/null | head -n1)" if [[ -n "$pk" ]]; then printf '/dev/%s' "$pk"; else printf '%s' "$dev"; fi } # _live_source_disks - whole disks that back the running live system _live_source_disks() { local src disk local -a sources=() # Sources directly referenced by the kernel command line. if [[ -r /proc/cmdline ]]; then while IFS= read -r tok; do case "$tok" in root=*|cdroot=*|boot=*) sources+=("${tok#*=}") ;; esac done < <(tr ' ' '\n' /dev/null)" [[ -n "$src" ]] && sources+=("$src") done fi local seen="" for src in "${sources[@]}"; do [[ -b "$src" ]] || continue disk="$(_parent_disk "$src")" case "$seen" in *" $disk "*) continue ;; esac seen+=" $disk " printf '%s\n' "$disk" done } # disk_protected_reason PATH - non-empty when the disk must not be a target disk_protected_reason() { local path="$1" live if disk_is_readonly "$path"; then printf 'read-only device' return fi case "$path" in /dev/sr*|/dev/loop*|/dev/dm-*|/dev/mapper/*) printf 'not a whole disk' return ;; esac if disk_is_mounted "$path"; then printf 'contains mounted filesystems' return fi while IFS= read -r live; do if [[ "$live" == "$path" ]]; then printf 'live installation medium' return fi done < <(_live_source_disks) } # disks_is_protected PATH disks_is_protected() { [[ -n "$(disk_protected_reason "$1")" ]]; } # disks_candidates - device paths that may be chosen as installation target disks_candidates() { local d for d in "${DISK_LIST[@]}"; do disks_is_protected "$d" || printf '%s\n' "$d" done } # disk_display_line PATH - one line description for menus disk_display_line() { local path="$1" size model tran flag="" reason size="$(disk_size_human "$(disk_size_bytes "$path")")" model="$(disk_model "$path")" tran="$(disk_transport "$path")" reason="$(disk_protected_reason "$path")" if [[ -n "$reason" ]]; then flag=" [PROTECTED: $reason]" fi printf '%-16s %8s %-8s %s%s' "$path" "$size" "${tran:-?}" "${model:-disk}" "$flag" } # disk_partition_table PATH - GPT/MBR/unknown via parted (read-only) disk_partition_table() { command -v parted >/dev/null 2>&1 || { printf 'unknown'; return; } local out out="$(parted -s "$1" print 2>/dev/null | awk -F': ' '/Partition Table/{print $2; exit}')" printf '%s' "${out:-unknown}" } # disk_free_space_bytes PATH - unpartitioned space, best effort disk_free_space_bytes() { command -v parted >/dev/null 2>&1 || return 1 parted -s -m "$1" unit B print free 2>/dev/null | \ awk -F: '$5=="free"{sum+=$4} END{printf "%d", sum}' } # disk_layout_text PATH - read-only partition listing disk_layout_text() { local path="$1" lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,MOUNTPOINTS "$path" 2>/dev/null }