1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 #disk := /dev/disk/by-id/usb-Generic-_Multi-Card_20071114173400000-0:0
3 #disk := /dev/disk/by-id/nvme-Samsung_SSD_970_EVO_Plus_250GB_S4EUNJ0N211426T
5 disk_sd := $(shell sourcephile-nix-get machines.$(server).config.boot.loader.grub.devices.0)
6 disk_nvme := /dev/disk/by-id/nvme-Samsung_SSD_970_EVO_Plus_250GB_S4EUNJ0N211426T
7 rpool := $(server)_nvme
11 #unicode_normalization := formD
15 sudo zpool labelclear -f /dev/disk/by-partlabel/$(server)_nvme_rpool || true
16 sudo $$(which sgdisk) --zap-all $(disk_sd)
18 part: wipe-sd wipe-nvme
20 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(server)_sd_bios" $(disk_sd)
21 sudo $$(which sgdisk) -n0:1M:+100M -t0:EF00 -c0:"$(server)_sd_efi" $(disk_sd)
22 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(server)_sd_boot" $(disk_sd)
23 sudo $$(which sgdisk) --randomize-guids $(disk_sd)
24 sudo $$(which sgdisk) --backup=$(server)_sd.sgdisk $(disk_sd)
26 sudo $$(which sgdisk) -n0:0:+8G -t0:8200 -c0:"$(server)_nvme_swap" $(disk_nvme)
27 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(server)_nvme_rpool" $(disk_nvme)
28 sudo $$(which sgdisk) --randomize-guids $(disk_nvme)
29 sudo $$(which sgdisk) --backup=$(server)_nvme.sgdisk $(disk_nvme)
31 format: umount format-efi format-boot format-rpool
33 sudo blkid /dev/disk/by-partlabel/$(server)_sd_efi -t TYPE=vfat || \
34 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(server)_sd_efi
36 sudo mkdir -p /mnt/$(server)
37 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(server)_sd_boot; test $$? != 2 || \
38 sudo mkfs.ext2 /dev/disk/by-partlabel/$(server)_sd_boot
40 sudo zpool list $(rpool) 2>/dev/null || \
41 sudo zpool create -o ashift=12 \
42 $(if $(cipher),-O encryption=$(cipher) \
43 -O keyformat=passphrase \
44 -O keylocation=prompt) \
45 -O normalization=formD \
46 $(if $(unicode_normalization),-O normalization=$(unicode_normalization) \
47 -R /mnt/$(server) $(rpool) /dev/disk/by-partlabel/$(server)_nvme_root
49 autotrim=$(autotrim) \
61 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
62 sudo zfs list $(rpool)/reserved 2>/dev/null || \
63 sudo zfs create -o canmount=off -o mountpoint=none $(rpool)/reserved
64 sudo zfs set refreservation=$(reservation) $(rpool)/reserved
66 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
67 sudo zfs list $(rpool)/root 2>/dev/null || \
70 -o mountpoint=legacy \
73 #sudo zfs list bpool/boot 2>/dev/null || \
76 # -o mountpoint=legacy \
87 sudo zfs list $(rpool)/"$$p" 2>/dev/null || \
90 -o mountpoint=legacy \
94 com.sun:auto-snapshot=false \
97 com.sun:auto-snapshot=false \
100 com.sun:auto-snapshot=false \
104 mount: mount-rpool mount-boot mount-efi
107 sudo zpool list $(rpool) || \
108 sudo zpool import -f $(rpool)
109 # load encryption key
110 sudo zfs get -H encryption $(rpool) | \
111 grep -q '^$(rpool)\s*encryption\s*off' || \
112 sudo zfs get -H keystatus $(rpool) | \
113 grep -q '^$(rpool)\s*keystatus\s*available' || \
114 sudo zfs load-key $(rpool)
116 sudo mkdir -p /mnt/$(server)
117 sudo mountpoint /mnt/$(server) || \
118 sudo mount -v -t zfs $(rpool)/root /mnt/$(server)
128 sudo mkdir -p /mnt/$(server)/"$$p"; \
129 sudo mountpoint /mnt/$(server)/"$$p" || \
130 sudo mount -v -t zfs $(rpool)/"$$p" /mnt/$(server)/"$$p" ; \
132 sudo chmod 1777 /mnt/$(server)/var/tmp
134 sudo mkdir -p /mnt/$(server)/boot
135 sudo mountpoint /mnt/$(server)/boot || \
136 sudo mount -v /dev/disk/by-partlabel/$(server)_sd_boot /mnt/$(server)/boot
137 #sudo mount -v -t zfs bpool/boot /mnt/$(server)/boot
138 mount-efi: | mount-boot
139 sudo mkdir -p /mnt/$(server)/boot/efi
140 sudo mountpoint /mnt/$(server)/boot/efi || \
141 sudo mount -v /dev/disk/by-partlabel/$(server)_sd_efi /mnt/$(server)/boot/efi
144 # Workaround https://dev.gnupg.org/T3908
145 chmod o+rw $$GPG_TTY $$XAUTHORITY
147 sudo --preserve-env \
148 NIXOS_CONFIG="$$PWD/install.nix" \
149 $$(which nixos-install) \
150 --root /mnt/$(server) \
155 # End workaround https://dev.gnupg.org/T3908
156 chmod o-rw $$GPG_TTY $$XAUTHORITY
158 sudo sourcephile-shred-tmp
172 ! sudo mountpoint /mnt/$(server)/"$$p" || \
173 sudo umount -v /mnt/$(server)/"$$p" ; \
175 ! sudo zpool list $(rpool) 2>/dev/null || \
176 zfs get -H encryption $(rpool) | \
177 grep -q '^$(rpool)\s*encryption\s*off' || \
178 zfs get -H keystatus $(rpool) | \
179 grep -q '^$(rpool)\s*keystatus\s*unavailable' || \
180 sudo zfs unload-key $(rpool)
181 #! sudo zpool list bpool 2>/dev/null || \
182 #sudo zpool export bpool
183 ! sudo zpool list $(rpool) 2>/dev/null || \
184 sudo zpool export $(rpool)
187 pass machines/$(server)/zfs/rpool | \
188 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
189 nixops ssh $(server) -p 2222 'zfs load-key $(rpool) && pkill zfs'