]> Git — Sourcephile - sourcephile-nix.git/blob - servers/mermet/Makefile
syncoid: sanoid: study
[sourcephile-nix.git] / servers / mermet / Makefile
1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 mermet_deployment := maintenance
3 mermet_disk := /dev/disk/by-id/ata-Samsung_SSD_840_EVO_250GB_S1DBNSAF340110R
4 #mermet_cipher :=
5 mermet_cipher := aes-128-gcm
6 mermet_autotrim :=
7 mermet_reservation := 1G
8 #mermet_channel := $$(nix-env -p /nix/var/nix/profiles/per-user/$$USER/channels -q nixpkgs --no-name --out-path)
9
10 echo:
11 echo $(MAKEFILES)
12
13 wipeout: umount
14 #sudo zpool labelclear -f $(mermet_disk)-part3 || true
15 sudo zpool labelclear -f $(mermet_disk)-part5 || true
16 sudo $$(which sgdisk) --zap-all $(mermet_disk)
17
18 partition:
19 sudo modprobe zfs
20 set -x; if test -e sfdisk; then \
21 sudo $$(which sfdisk) $(losurdo_disk) <sfdisk.txt; \
22 else \
23 sudo $$(which sgdisk) --zap-all $(losurdo_disk) && \
24 sudo partprobe && \
25 sudo $$(which sgdisk) -a1 -n1:34:2047 -t1:EF02 $(losurdo_disk) && \
26 sudo $$(which sgdisk) -n2:1M:+512M -t2:EF00 $(losurdo_disk) && \
27 sudo $$(which sgdisk) -n3:0:+512M -t3:8300 $(losurdo_disk) && \
28 sudo $$(which sgdisk) -n4:0:+4G -t4:8200 $(losurdo_disk) && \
29 sudo $$(which sgdisk) -n5:0:0 -t5:BF01 $(losurdo_disk) && \
30 sudo $$(which sgdisk) --randomize-guids $(losurdo_disk) && \
31 sudo $$(which sfdisk) -d $(losurdo_disk) | \
32 sed -e 's&/dev/sd.&$(losurdo_disk)&' >sfdisk.txt; \
33 fi
34
35 format:
36 # DOC: https://github.com/zfsonlinux/zfs/wiki/Debian-Buster-Root-on-ZFS
37 sudo mkdir -p /mnt/mermet
38 blkid -t TYPE=ext2 $(mermet_disk)-part3; test $$? != 2 || \
39 mkfs.ext2 $(mermet_disk)-part3
40 # bpool
41 ## NOTE: enable only ZFS features supported by GRUB
42 #sudo zpool list bpool 2>/dev/null || \
43 #sudo zpool create -o ashift=12 -d \
44 # -o feature@allocation_classes=enabled \
45 # -o feature@async_destroy=enabled \
46 # -o feature@bookmarks=enabled \
47 # -o feature@embedded_data=enabled \
48 # -o feature@empty_bpobj=enabled \
49 # -o feature@enabled_txg=enabled \
50 # -o feature@extensible_dataset=enabled \
51 # -o feature@filesystem_limits=enabled \
52 # -o feature@hole_birth=enabled \
53 # -o feature@large_blocks=enabled \
54 # -o feature@lz4_compress=enabled \
55 # -o feature@project_quota=enabled \
56 # -o feature@resilver_defer=enabled \
57 # -o feature@spacemap_histogram=enabled \
58 # -o feature@spacemap_v2=enabled \
59 # -o feature@userobj_accounting=enabled \
60 # -o feature@zpool_checkpoint=enabled \
61 # -o feature@multi_vdev_crash_dump=disabled \
62 # -o feature@large_dnode=disabled \
63 # -o feature@sha512=disabled \
64 # -o feature@skein=disabled \
65 # -o feature@edonr=disabled \
66 # -O normalization=formD \
67 # -R /mnt/mermet bpool $(mermet_disk)-part3
68 #sudo zfs set \
69 # acltype=posixacl \
70 # canmount=off \
71 # compression=lz4 \
72 # devices=off \
73 # relatime=on \
74 # xattr=sa \
75 # mountpoint=/ \
76 # bpool
77
78 # swap
79 # Note: configured with a volatile key in configuration.nix
80 #blkid -t TYPE=crypto_LUKS $(mermet_disk)-part4; test $$? != 2 || \
81 #sudo cryptsetup luksFormat --cipher aes-xts-plain64 --key-size 256 --hash sha256 $(mermet_disk)-part4
82 #sudo cryptsetup luksOpen $(mermet_disk)-part4 swap
83 #blkid -t TYPE=swap /dev/mapper/-swap; test $$? != 2 || \
84 #sudo mkswap --check --label swap
85 #sudo cryptsetup luksClose $(mermet_disk)-part4 swap
86 # rpool
87 sudo zpool list rpool 2>/dev/null || \
88 sudo zpool create -o ashift=12 \
89 $(if $(mermet_cipher),-O encryption=$(mermet_cipher) \
90 -O keyformat=passphrase \
91 -O keylocation=prompt) \
92 -O normalization=formD \
93 -R /mnt/mermet rpool $(mermet_disk)-part5
94 sudo zfs set \
95 acltype=posixacl \
96 atime=off \
97 $(if $(mermet_autotrim),autotrim=on) \
98 canmount=off \
99 compression=lz4 \
100 dnodesize=auto \
101 relatime=on \
102 xattr=sa \
103 mountpoint=/ \
104 rpool
105 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
106 sudo zfs list rpool/reserved 2>/dev/null || \
107 sudo zfs create -o canmount=off -o mountpoint=none rpool/reserved
108 sudo zfs set refreservation=$(mermet_reservation) rpool/reserved
109 # /
110 # NOTE: mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
111 sudo zfs list rpool/root 2>/dev/null || \
112 sudo zfs create \
113 -o canmount=on \
114 -o mountpoint=legacy \
115 rpool/root
116 # /boot
117 #sudo zfs list bpool/boot 2>/dev/null || \
118 #sudo zfs create \
119 # -o canmount=on \
120 # -o mountpoint=legacy \
121 # bpool/boot
122 # /boot/efi
123 sudo blkid $(mermet_disk)-part2 -t TYPE=vfat || \
124 sudo mkfs.vfat -F 32 -s 1 -n EFI $(mermet_disk)-part2
125 # /*
126 for p in \
127 home \
128 nix \
129 var \
130 var/cache \
131 var/log \
132 var/mail \
133 var/redis \
134 var/tmp \
135 var/www \
136 ; do \
137 sudo zfs list rpool/"$$p" 2>/dev/null || \
138 sudo zfs create \
139 -o canmount=on \
140 -o mountpoint=legacy \
141 rpool/"$$p" ; \
142 done
143 sudo zfs set \
144 com.sun:auto-snapshot=false \
145 rpool/nix
146 sudo zfs set \
147 com.sun:auto-snapshot=false \
148 rpool/var/cache
149 sudo zfs set \
150 com.sun:auto-snapshot=false \
151 sync=disabled \
152 rpool/var/tmp
153
154 mount:
155 # scan needed zpools
156 #sudo zpool list bpool || \
157 #sudo zpool import -f bpool
158 sudo zpool list rpool || \
159 sudo zpool import -f rpool
160 # load encryption key
161 zfs get -H encryption rpool | \
162 grep -q '^rpool\s*encryption\s*off' || \
163 zfs get -H keystatus rpool | \
164 grep -q '^rpool\s*keystatus\s*available' || \
165 sudo zfs load-key rpool
166 # /
167 sudo mkdir -p /mnt/mermet
168 sudo mountpoint /mnt/mermet || \
169 sudo mount -v -t zfs rpool/root /mnt/mermet
170 # /boot
171 sudo mkdir -p /mnt/mermet/boot
172 sudo mountpoint /mnt/mermet/boot || \
173 sudo mount -v $(mermet_disk)-part3 /mnt/mermet/boot
174 #sudo mount -v -t zfs bpool/boot /mnt/mermet/boot
175 # /boot/efi
176 sudo mkdir -p /mnt/mermet/boot/efi
177 sudo mountpoint /mnt/mermet/boot/efi || \
178 sudo mount -v $(mermet_disk)-part2 /mnt/mermet/boot/efi
179 # /*
180 for p in \
181 home \
182 nix \
183 var \
184 var/cache \
185 var/log \
186 var/mail \
187 var/redis \
188 var/tmp \
189 var/www \
190 ; do \
191 sudo mkdir -p /mnt/mermet/"$$p"; \
192 sudo mountpoint /mnt/mermet/"$$p" || \
193 sudo mount -v -t zfs rpool/"$$p" /mnt/mermet/"$$p" ; \
194 done
195 sudo chmod 1777 /mnt/mermet/var/tmp
196
197 bootstrap: mount
198 #test "$$(sudo grub-probe /mnt/mermet/boot)" = zfs
199 # NOTE: nixos-install will install GRUB following configuration.nix
200 # BIOS
201 #sudo grub-install $(mermet_disk)
202 # UEFI
203 #sudo grub-install \
204 # --target=x86_64-efi \
205 # --efi-directory=/mnt/mermet/boot/efi \
206 # --bootloader-id=nixos \
207 # --recheck \
208 # --no-floppy
209
210 pass servers/mermet/dropbear/host.key | \
211 sudo install -D -o root -g root -m 400 /dev/stdin \
212 /mnt/mermet/etc/dropbear/host.key && \
213 test -s /mnt/mermet/etc/dropbear/host.key
214
215 #trap "test ! -e SHRED-ME || sudo find SHRED-ME -type f -exec shred -u {} + && sudo rm -rf SHRED-ME" EXIT ;
216 sudo \
217 GNUPGHOME="$$GNUPGHOME" \
218 GPG_TTY="$$GPG_TTY" \
219 DBUS_SESSION_BUS_ADDRESS="$$DBUS_SESSION_BUS_ADDRESS" \
220 LANG="$$LANG" \
221 LC_CTYPE="$$LC_CTYPE" \
222 MERMET_DEPLOYMENT="$$MERMET_DEPLOYMENT" \
223 NIXOS_CONFIG="$$(readlink -e ../configuration.nix)" \
224 NIX_CONF_DIR="$$NIX_CONF_DIR" \
225 NIX_PATH="$$NIX_PATH" \
226 PASSWORD_STORE_DIR="$$PASSWORD_STORE_DIR" \
227 PATH="$$PATH" \
228 SSL_CERT_FILE="$$SSL_CERT_FILE" \
229 $$(which nixos-install) \
230 --root /mnt/mermet \
231 $(if $(mermet_channel),--channel "$(mermet_channel)") \
232 --option -Inixops=$$(nix-instantiate --eval -E '(import <nixpkgs> {}).nixops + ""') \
233 --no-root-passwd \
234 --show-trace
235
236 umount:
237 for p in \
238 boot/efi \
239 boot \
240 home \
241 nix \
242 var/cache \
243 var/log \
244 var/mail \
245 var/redis \
246 var/tmp \
247 var/www \
248 var \
249 "" \
250 ; do \
251 ! sudo mountpoint /mnt/mermet/"$$p" || \
252 sudo umount -v /mnt/mermet/"$$p" ; \
253 done
254 ! sudo zpool list rpool 2>/dev/null || \
255 zfs get -H encryption rpool | \
256 grep -q '^rpool\s*encryption\s*off' || \
257 zfs get -H keystatus rpool | \
258 grep -q '^rpool\s*keystatus\s*unavailable' || \
259 sudo zfs unload-key rpool
260 #! sudo zpool list bpool 2>/dev/null || \
261 #sudo zpool export bpool
262 ! sudo zpool list rpool 2>/dev/null || \
263 sudo zpool export rpool
264
265 unlock:
266 pass servers/mermet/zfs/rpool | \
267 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(MERMET_DEPLOYMENT)}" \
268 nixops ssh mermet -p 2222 'zfs load-key rpool && pkill zfs'