]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/Makefile
nix: lower nix-gc CPU and IO priority
[julm/julm-nix.git] / hosts / oignon / Makefile
1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 hostName := oignon
3 disk_ssd := /dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X
4 zpool := $(hostName)
5 cipher := aes-128-gcm
6 autotrim := on
7 reservation := 1G
8 compression := zstd
9
10 wipe:
11 sudo modprobe zfs
12 sudo zpool labelclear -f /dev/disk/by-partlabel/$(hostName)_ssd_zpool || true
13 sudo $$(which sgdisk) --zap-all $(disk_ssd)
14
15 part: wipe
16 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
17 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(hostName)_ssd_bios" $(disk_ssd)
18 sudo $$(which sgdisk) -n0:1M:+32M -t0:EF00 -c0:"$(hostName)_ssd_efi" $(disk_ssd)
19 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(hostName)_ssd_boot" $(disk_ssd)
20 sudo $$(which sgdisk) -n0:0:+4G -t0:8200 -c0:"$(hostName)_ssd_swap" $(disk_ssd)
21 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(hostName)_ssd_zpool" $(disk_ssd)
22 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
23 printf '\200\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0' | sudo dd of=$(disk_ssd) bs=1 seek=462
24 sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
25 sudo $$(which sgdisk) --backup=$(hostName)_ssd.sgdisk $(disk_ssd)
26
27 format: umount format-efi format-boot format-zpool
28 format-efi:
29 sudo blkid /dev/disk/by-partlabel/$(hostName)_ssd_efi -t TYPE=vfat || \
30 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(hostName)_ssd_efi
31 format-boot:
32 sudo mkdir -p /mnt/$(hostName)
33 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot; test $$? != 2 || \
34 sudo mkfs.ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot
35 format-zpool:
36 sudo zpool list $(zpool) 2>/dev/null || \
37 sudo zpool create -o ashift=12 \
38 -O utf8only=yes \
39 $(if $(cipher),-O encryption=$(cipher) \
40 -O keyformat=passphrase \
41 -O keylocation=prompt) \
42 -R /mnt/$(hostName) $(zpool) /dev/disk/by-partlabel/$(hostName)_ssd_zpool
43 sudo zpool set \
44 autotrim=$(autotrim) \
45 $(zpool)
46 sudo zfs set \
47 acltype=off \
48 atime=off \
49 canmount=off \
50 compression=$(compression) \
51 dnodesize=auto \
52 relatime=on \
53 xattr=off \
54 mountpoint=/ \
55 $(zpool)
56 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
57 sudo zfs list $(zpool)/reserved 2>/dev/null || \
58 sudo zfs create -o canmount=off -o mountpoint=none $(zpool)/reserved
59 sudo zfs set refreservation=$(reservation) $(zpool)/reserved
60 # /
61 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
62 sudo zfs list $(zpool)/root 2>/dev/null || \
63 sudo zfs create \
64 -o canmount=on \
65 -o mountpoint=legacy \
66 $(zpool)/root
67 # /*
68 for p in \
69 nix \
70 var \
71 ; do \
72 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
73 sudo zfs create \
74 -o canmount=on \
75 -o mountpoint=legacy \
76 $(zpool)/"$$p" ; \
77 done
78 #sudo zfs set sync=disabled $(zpool)/var/tmp
79 #sudo zfs set copies=2 $(zpool)/home/files
80
81 mount: mount-zpool mount-boot mount-efi
82 mount-zpool:
83 # scan needed zpools
84 sudo zpool list $(zpool) || \
85 sudo zpool import -f $(zpool)
86 # load encryption key
87 sudo zfs get -H encryption $(zpool) | \
88 grep -q '^$(zpool)\s*encryption\s*off' || \
89 sudo zfs get -H keystatus $(zpool) | \
90 grep -q '^$(zpool)\s*keystatus\s*available' || \
91 sudo zfs load-key $(zpool)
92 # /
93 sudo mkdir -p /mnt/$(hostName)
94 sudo mountpoint /mnt/$(hostName) || \
95 sudo mount -v -t zfs $(zpool)/root /mnt/$(hostName)
96 # /*
97 for p in \
98 nix \
99 var \
100 ; do \
101 sudo mkdir -p /mnt/$(hostName)/"$$p"; \
102 sudo mountpoint /mnt/$(hostName)/"$$p" || \
103 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(hostName)/"$$p" ; \
104 done
105 #sudo chmod 1777 /mnt/$(hostName)/var/tmp
106 mount-boot:
107 sudo mkdir -p /mnt/$(hostName)/boot
108 sudo mountpoint /mnt/$(hostName)/boot || \
109 sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_boot /mnt/$(hostName)/boot
110 #sudo mount -v -t zfs bpool/boot /mnt/$(hostName)/boot
111 mount-efi: | mount-boot
112 sudo mkdir -p /mnt/$(hostName)/boot/efi
113 sudo mountpoint /mnt/$(hostName)/boot/efi || \
114 sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_efi /mnt/$(hostName)/boot/efi
115
116 bootstrap: mount
117 # Workaround https://dev.gnupg.org/T3908
118 chmod o+rw $$GPG_TTY $$XAUTHORITY
119
120 sudo --preserve-env \
121 $$(which nixos-install) \
122 --root /mnt/$(hostName) \
123 --flake '.#$(hostName)' \
124 --no-root-passwd \
125 --no-channel-copy \
126 --option allow-import-from-derivation true \
127 --show-trace
128
129 # End workaround https://dev.gnupg.org/T3908
130 chmod o-rw $$GPG_TTY $$XAUTHORITY
131
132 umount:
133 for p in \
134 boot/efi \
135 boot \
136 nix \
137 var \
138 "" \
139 ; do \
140 ! sudo mountpoint /mnt/$(hostName)/"$$p" || \
141 sudo umount -v /mnt/$(hostName)/"$$p" ; \
142 done
143 ! sudo zpool list $(zpool) 2>/dev/null || \
144 zfs get -H encryption $(zpool) | \
145 grep -q '^$(zpool)\s*encryption\s*off' || \
146 zfs get -H keystatus $(zpool) | \
147 grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
148 sudo zfs unload-key $(zpool)
149 #! sudo zpool list bpool 2>/dev/null || \
150 #sudo zpool export bpool
151 ! sudo zpool list $(zpool) 2>/dev/null || \
152 sudo zpool export $(zpool)
153
154 unlock:
155 pass hostNames/$(hostName)/zfs/zpool | \
156 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
157 nixops ssh $(hostName) -p 2222 'zfs load-key $(zpool) && pkill zfs'