puter/packages/disk/disk.bash

197 lines
3.9 KiB
Bash
Raw Normal View History

2024-12-01 16:43:53 +00:00
#!/usr/bin/env bash
2024-02-04 20:51:11 +00:00
set -o errexit
set -o nounset
set -o pipefail
2024-12-27 00:20:40 +00:00
progname="$0"
2024-02-04 20:51:11 +00:00
2025-01-05 15:58:54 +00:00
warn() {
local line
2024-12-27 00:20:40 +00:00
for line in "$@"; do
2025-01-05 15:58:54 +00:00
echo "$progname: $line" 1>&2
2024-12-27 00:20:40 +00:00
done
2025-01-05 15:58:54 +00:00
}
error() {
warn "$@"
2024-12-27 00:20:40 +00:00
exit 1
}
2025-03-01 21:21:00 +00:00
skip() {
if (($# < 1)); then
error 'name of value to be skipped is required'
fi
if (($# > 1)); then
error 'too many arguments'
fi
local skip=$1
for s in "${skips[@]}"; do
if [[ $s == "$skip" ]]; then
return 1
fi
done
return 0
}
args=$(
getopt \
--options r:b:l:c:m:B:M:v \
--longoptions root:,boot-label:,main-label:,cryptmain-label:,mapping:,boot-options:,main-options:,verbose \
--name "$progname" \
-- "$@"
)
2024-12-27 00:20:40 +00:00
eval set -- "$args"
2024-02-04 20:51:11 +00:00
root=/mnt
bootlbl=BOOT
mainlbl=main
2024-12-01 04:03:34 +00:00
cryptmainlbl=cryptmain
2025-03-01 21:21:00 +00:00
mapping=main
bootflags=
mainflags=
fatflags=()
ext4flags=()
skips=()
2024-02-04 20:51:11 +00:00
while true; do
2025-03-01 21:21:00 +00:00
case "$1" in
-r | --root)
root=$2
shift 2
;;
-b | --boot-label)
skips+=(bootlbl)
bootlbl=${2^^}
shift 2
;;
-l | --main-label)
skips+=(mainlbl)
mainlbl=$2
shift 2
;;
-c | --cryptmain-label)
skips+=(cryptmainlbl)
cryptmainlbl=$2
shift 2
;;
-m | --mapping)
skips+=(mapping)
mapping=$2
shift 2
;;
-B | --boot-options)
bootflags+=(--options "$2")
shift 2
;;
-M | --main-options)
mainflags+=(--options "$2")
shift 2
;;
-v | --verbose)
fatflags+=(-v)
ext4flags+=(-v)
shift
;;
--)
shift
break
;;
esac
2024-02-04 20:51:11 +00:00
done
2025-03-01 21:21:00 +00:00
if (($# < 1)); then
error 'an argument specifying the block device is required'
2024-12-27 00:20:40 +00:00
fi
2025-03-01 21:21:00 +00:00
if (($# > 1)); then
error 'too many arguments'
2024-02-04 20:51:11 +00:00
fi
blkdev=$1
sfdisk --label gpt --quiet -- "$blkdev" <<EOF
2024-04-20 19:49:50 +00:00
,512M,U;
,,L;
2024-02-04 20:51:11 +00:00
EOF
parts=()
json=$(sfdisk --json -- "$blkdev")
while IFS= read -r k; do
2025-03-01 21:21:00 +00:00
parts+=("$(jq --argjson k "$k" --raw-output '.partitiontable.partitions[$k].node' <<<"$json")")
2024-02-04 20:51:11 +00:00
done < <(jq '.partitiontable.partitions | keys[]' <<<"$json")
bootfs="${parts[0]}"
2024-12-01 04:03:34 +00:00
mainblkdev="${parts[1]}"
2024-02-04 20:51:11 +00:00
2025-03-01 21:21:00 +00:00
if ! skip bootlbl; then
read -rep "Which label should the boot file system have? [$bootlbl] " input
if [[ -n $input ]]; then
bootlbl=$input
fi
fi
mkfs.fat -F 32 -n "$bootlbl" "${fatflags[@]}" -- "$bootfs" >/dev/null
2024-02-04 20:51:11 +00:00
2024-12-01 04:03:34 +00:00
while true; do
2025-03-01 21:21:00 +00:00
read -rep 'Do you want your main partition to be encrypted? [y/N] ' input
case "$input" in
[Yy]*)
while true; do
read -rsp 'Enter password: ' password
warn ''
read -rsp 'Re-enter password: ' repassword
warn ''
if [[ $password == "$repassword" ]]; then
break
fi
done
if ! skip cryptmainlbl; then
read -rep "Which label should the main LUKS partition have? [$cryptmainlbl] " input
if [[ -n $input ]]; then
cryptmainlbl=$input
fi
fi
cryptsetup luksFormat --batch-mode --label "$cryptmainlbl" -- "$mainblkdev" <<<"$password"
if ! skip mapping; then
read -rep "Which name should the main LUKS mapping have? [$mapping] " input
if [[ -n $input ]]; then
mapping=$input
fi
fi
cryptsetup open -- "$mainblkdev" "$mapping" <<<"$password"
mainfs=/dev/mapper/$mapping
2024-12-01 04:03:34 +00:00
break
2025-03-01 21:21:00 +00:00
;;
'' | [Nn]*)
mainfs=$mainblkdev
break
;;
*) warn 'Please answer with yes or no' ;;
esac
2024-12-01 04:03:34 +00:00
done
2025-03-01 21:21:00 +00:00
if ! skip mainlbl; then
read -rep "Which label should the main file system have? [$mainlbl] " input
if [[ -n $input ]]; then
mainlbl=$input
fi
fi
mkfs.ext4 -qFL "$mainlbl" "${ext4flags[@]}" -- "$mainfs"
2024-02-04 20:51:11 +00:00
mkdir --parents -- "$root"
2025-03-01 21:21:00 +00:00
mount "${mainflags[@]}" -- "$mainfs" "$root"
2024-02-04 20:51:11 +00:00
mkdir -- "$root/boot"
2025-03-01 21:21:00 +00:00
mount "${bootflags[@]}" -- "$bootfs" "$root/boot"