-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-install.sh
executable file
·99 lines (78 loc) · 2.75 KB
/
pre-install.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# More safety, by turning some bugs into errors.
# Without `errexit` you don’t need ! and can replace
# ${PIPESTATUS[0]} with a simple $?, but I prefer safety.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
LONGOPTS=boot-partition:,nixos-partition:
OPTIONS=b:n:
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
mainDrive="/dev/disk/by-label/nixos" bootDrive="/dev/disk/by-label/boot"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-b|--boot-partition)
bootDrive="$2"
shift 2
;;
-n|--nixos-partition)
mainDrive="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
echo "Creating btrfs subvolumes..."
mount -t btrfs $mainDrive /mnt
# We first create the subvolumes outlined above:
btrfs subvolume create /mnt/root
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/nix
btrfs subvolume create /mnt/persist
btrfs subvolume create /mnt/log
btrfs subvolume create /mnt/swap
# We then take an empty *readonly* snapshot of the root subvolume,
# which we'll eventually rollback to on every boot.
btrfs subvolume snapshot -r /mnt/root /mnt/root-blank
umount /mnt
echo "Mounting subvolumes..."
mount -o subvol=root,compress=zstd,noatime $mainDrive /mnt
mkdir /mnt/home
mount -o subvol=home,compress=zstd,noatime $mainDrive /mnt/home
mkdir /mnt/nix
mount -o subvol=nix,compress=zstd,noatime $mainDrive /mnt/nix
mkdir /mnt/persist
mount -o subvol=persist,compress=zstd,noatime $mainDrive /mnt/persist
mkdir -p /mnt/var/log
mount -o subvol=log,compress=zstd,noatime $mainDrive /mnt/var/log
mkdir -p /mnt/swap
mount -o subvol=swap,noatime $mainDrive /mnt/swap
echo "Mounting boot partition..."
mkdir /mnt/boot
mount $bootDrive /mnt/boot
echo "Creating swap file..."
btrfs filesystem mkswapfile --size 16g --uuid clear /mnt/swap/swapfile