-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.sh
61 lines (50 loc) · 1.12 KB
/
disk.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
set -o errexit
set -o nounset
set -o pipefail
opts=$(getopt --options r:m:b:l:c: --longoptions=root:,boot-label:,main-label: --name "$0" -- "$@")
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
,512M,U;
,,L;
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]}"
mainblkdev="${parts[1]}"
mkfs.vfat -F 32 -n "$bootlbl" -- "$bootfs" >/dev/null
mkfs.ext4 -q -F -L "$mainlbl" -- "$mainblkdev"
mkdir --parents -- "$root"
mount --options noatime -- "$mainblkdev" "$root"
mkdir -- "$root/boot"
mount -- "$bootfs" "$root/boot"