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 $(if $(unicode_normalization),-O normalization=$(unicode_normalization) \
46 -R /mnt/$(server) $(rpool) /dev/disk/by-partlabel/$(server)_nvme_root
48 autotrim=$(autotrim) \
60 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
61 sudo zfs list $(rpool)/reserved 2>/dev/null || \
62 sudo zfs create -o canmount=off -o mountpoint=none $(rpool)/reserved
63 sudo zfs set refreservation=$(reservation) $(rpool)/reserved
65 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
66 sudo zfs list $(rpool)/root 2>/dev/null || \
69 -o mountpoint=legacy \
72 #sudo zfs list bpool/boot 2>/dev/null || \
75 # -o mountpoint=legacy \
86 sudo zfs list $(rpool)/"$$p" 2>/dev/null || \
89 -o mountpoint=legacy \
93 com.sun:auto-snapshot=false \
96 com.sun:auto-snapshot=false \
99 com.sun:auto-snapshot=false \
103 mount: mount-rpool mount-boot mount-efi
106 sudo zpool list $(rpool) || \
107 sudo zpool import -f $(rpool)
108 # load encryption key
109 sudo zfs get -H encryption $(rpool) | \
110 grep -q '^$(rpool)\s*encryption\s*off' || \
111 sudo zfs get -H keystatus $(rpool) | \
112 grep -q '^$(rpool)\s*keystatus\s*available' || \
113 sudo zfs load-key $(rpool)
115 sudo mkdir -p /mnt/$(server)
116 sudo mountpoint /mnt/$(server) || \
117 sudo mount -v -t zfs $(rpool)/root /mnt/$(server)
127 sudo mkdir -p /mnt/$(server)/"$$p"; \
128 sudo mountpoint /mnt/$(server)/"$$p" || \
129 sudo mount -v -t zfs $(rpool)/"$$p" /mnt/$(server)/"$$p" ; \
131 sudo chmod 1777 /mnt/$(server)/var/tmp
133 sudo mkdir -p /mnt/$(server)/boot
134 sudo mountpoint /mnt/$(server)/boot || \
135 sudo mount -v /dev/disk/by-partlabel/$(server)_sd_boot /mnt/$(server)/boot
136 #sudo mount -v -t zfs bpool/boot /mnt/$(server)/boot
137 mount-efi: | mount-boot
138 sudo mkdir -p /mnt/$(server)/boot/efi
139 sudo mountpoint /mnt/$(server)/boot/efi || \
140 sudo mount -v /dev/disk/by-partlabel/$(server)_sd_efi /mnt/$(server)/boot/efi
143 # Workaround https://dev.gnupg.org/T3908
144 chmod o+rw $$GPG_TTY $$XAUTHORITY
146 sudo --preserve-env \
147 NIXOS_CONFIG="$$PWD/install.nix" \
148 $$(which nixos-install) \
149 --root /mnt/$(server) \
154 # End workaround https://dev.gnupg.org/T3908
155 chmod o-rw $$GPG_TTY $$XAUTHORITY
157 sudo sourcephile-shred-tmp
171 ! sudo mountpoint /mnt/$(server)/"$$p" || \
172 sudo umount -v /mnt/$(server)/"$$p" ; \
174 ! sudo zpool list $(rpool) 2>/dev/null || \
175 zfs get -H encryption $(rpool) | \
176 grep -q '^$(rpool)\s*encryption\s*off' || \
177 zfs get -H keystatus $(rpool) | \
178 grep -q '^$(rpool)\s*keystatus\s*unavailable' || \
179 sudo zfs unload-key $(rpool)
180 #! sudo zpool list bpool 2>/dev/null || \
181 #sudo zpool export bpool
182 ! sudo zpool list $(rpool) 2>/dev/null || \
183 sudo zpool export $(rpool)
186 pass machines/$(server)/zfs/rpool | \
187 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
188 nixops ssh $(server) -p 2222 'zfs load-key $(rpool) && pkill zfs'