puter/packages/disk/disk.bash

120 lines
2.1 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
}
args=$(getopt --options r:m:b:l:c: --longoptions=root:,mapping:,boot-label:,main-label:,cryptmain-label: --name "$progname" -- "$@")
eval set -- "$args"
2024-02-04 20:51:11 +00:00
root=/mnt
2024-12-01 04:03:34 +00:00
mapping=main
2024-02-04 20:51:11 +00:00
bootlbl=BOOT
mainlbl=main
2024-12-01 04:03:34 +00:00
cryptmainlbl=cryptmain
2024-02-04 20:51:11 +00:00
while true; do
case "$1" in
2024-12-27 00:20:40 +00:00
(-r | --root)
2024-02-04 20:51:11 +00:00
root=$2
shift 2
;;
2024-12-27 00:20:40 +00:00
(-m | --mapping)
2024-12-01 04:03:34 +00:00
mapping=$2
shift 2
;;
2024-12-27 00:20:40 +00:00
(-b | --boot-label)
2024-02-04 20:51:11 +00:00
bootlbl=${2^^}
shift 2
;;
2024-12-27 00:20:40 +00:00
(-l | --main-label)
2024-02-04 20:51:11 +00:00
mainlbl=$2
shift 2
;;
2024-12-27 00:20:40 +00:00
(-c | --cryptmain-label)
2024-12-01 04:03:34 +00:00
cryptmainlbl=$2
shift 2
;;
2024-12-27 00:20:40 +00:00
(--)
2024-02-04 20:51:11 +00:00
shift
break
;;
esac
done
2024-12-27 00:20:40 +00:00
if (( $# < 1 )); then
error 'an argument specifying the block device is required'
fi
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
parts+=("$(jq --argjson k "$k" --raw-output '.partitiontable.partitions[$k].node' <<<"$json")")
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
mkfs.vfat -F 32 -n "$bootlbl" -- "$bootfs" >/dev/null
2024-12-01 04:03:34 +00:00
while true; do
read -r -p 'Do you want your main partition to be encrypted [y/N]? ' luks
case "$luks" in
2024-12-14 10:59:14 +00:00
([Yy]*)
2024-12-01 04:03:34 +00:00
while true; do
read -r -s -p 'Enter password: ' password
2025-01-05 15:58:54 +00:00
warn ''
2024-12-01 04:03:34 +00:00
read -r -s -p 'Re-enter password: ' repassword
2025-01-05 15:58:54 +00:00
warn ''
2024-12-01 16:43:53 +00:00
if [[ $password == "$repassword" ]]; then
2024-12-01 04:03:34 +00:00
break
fi
done
cryptsetup luksFormat --batch-mode --label "$cryptmainlbl" "$mainblkdev" <<<"$password"
cryptsetup open "$mainblkdev" "$mapping" <<<"$password"
mainfs=/dev/mapper/$mapping
break
;;
2024-12-14 10:59:14 +00:00
('' | [Nn]*)
2024-12-01 04:03:34 +00:00
mainfs=$mainblkdev
break
;;
2025-01-05 15:58:54 +00:00
(*) warn 'Please answer with yes or no' ;;
2024-12-01 04:03:34 +00:00
esac
done
2024-07-01 22:06:05 +00:00
mkfs.ext4 -q -F -L "$mainlbl" -- "$mainfs"
2024-02-04 20:51:11 +00:00
mkdir --parents -- "$root"
2024-07-01 22:06:05 +00:00
mount --options noatime -- "$mainfs" "$root"
2024-02-04 20:51:11 +00:00
mkdir -- "$root/boot"
mount -- "$bootfs" "$root/boot"