puter/disk.sh

62 lines
1.1 KiB
Bash
Raw Normal View History

2024-02-04 20:51:11 +00:00
set -o errexit
set -o nounset
set -o pipefail
2024-08-30 23:17:38 +00:00
opts=$(getopt --options r:m:b:l:c: --longoptions=root:,boot-label:,main-label: --name "$0" -- "$@")
2024-02-04 20:51:11 +00:00
eval set -- "$opts"
root=/mnt
bootlbl=BOOT
mainlbl=main
while true; do
case "$1" in
-r | --root)
root=$2
shift 2
;;
-b | --boot-label)
bootlbl=${2^^}
shift 2
;;
-l | --main-label)
mainlbl=$2
shift 2
;;
--)
shift
break
;;
esac
done
if [[ $# != 1 ]]; then
printf '%s\n' "$0: an argument specifying the block device is required" 1>&2
exit 1
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-08-30 23:17:38 +00:00
mainfs="${parts[1]}"
2024-02-04 20:51:11 +00:00
mkfs.vfat -F 32 -n "$bootlbl" -- "$bootfs" >/dev/null
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"