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_ssd := /dev/disk/by-id/ata-CT250MX500SSD1_2004E2849DD1
13 sudo zpool labelclear -f /dev/disk/by-partlabel/$(machine)_ssd_zpool || true
14 sudo $$(which sgdisk) --zap-all $(disk_ssd)
17 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
18 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(machine)_ssd_bios" $(disk_ssd)
19 sudo $$(which sgdisk) -n0:1M:+32M -t0:EF00 -c0:"$(machine)_ssd_efi" $(disk_ssd)
20 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(machine)_ssd_boot" $(disk_ssd)
21 sudo $$(which sgdisk) -n0:0:+4G -t0:8200 -c0:"$(machine)_ssd_swap" $(disk_ssd)
22 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(machine)_ssd_zpool" $(disk_ssd)
23 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
24 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
25 sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
26 sudo $$(which sgdisk) --backup=$(machine)_ssd.sgdisk $(disk_ssd)
28 format: umount format-efi format-boot format-zpool
30 sudo blkid /dev/disk/by-partlabel/$(machine)_ssd_efi -t TYPE=vfat || \
31 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(machine)_ssd_efi
33 sudo mkdir -p /mnt/$(machine)
34 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot; test $$? != 2 || \
35 sudo mkfs.ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot
37 sudo zpool list $(zpool) 2>/dev/null || \
38 sudo zpool create -o ashift=12 \
39 $(if $(cipher),-O encryption=$(cipher) \
40 -O keyformat=passphrase \
41 -O keylocation=prompt) \
42 -R /mnt/$(machine) $(zpool) /dev/disk/by-partlabel/$(machine)_ssd_zpool
44 autotrim=$(autotrim) \
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
61 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
62 sudo zfs list $(zpool)/root 2>/dev/null || \
65 -o mountpoint=legacy \
77 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
80 -o mountpoint=legacy \
83 sudo zfs set sync=disabled $(zpool)/var/tmp
84 sudo zfs set copies=2 $(zpool)/home/documents
86 mount: mount-zpool mount-boot mount-efi
89 sudo zpool list $(zpool) || \
90 sudo zpool import -f $(zpool)
92 sudo zfs get -H encryption $(zpool) | \
93 grep -q '^$(zpool)\s*encryption\s*off' || \
94 sudo zfs get -H keystatus $(zpool) | \
95 grep -q '^$(zpool)\s*keystatus\s*available' || \
96 sudo zfs load-key $(zpool)
98 sudo mkdir -p /mnt/$(machine)
99 sudo mountpoint /mnt/$(machine) || \
100 sudo mount -v -t zfs $(zpool)/root /mnt/$(machine)
110 sudo mkdir -p /mnt/$(machine)/"$$p"; \
111 sudo mountpoint /mnt/$(machine)/"$$p" || \
112 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(machine)/"$$p" ; \
114 sudo chmod 1777 /mnt/$(machine)/var/tmp
116 sudo mkdir -p /mnt/$(machine)/boot
117 sudo mountpoint /mnt/$(machine)/boot || \
118 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_boot /mnt/$(machine)/boot
119 #sudo mount -v -t zfs bpool/boot /mnt/$(machine)/boot
120 mount-efi: | mount-boot
121 sudo mkdir -p /mnt/$(machine)/boot/efi
122 sudo mountpoint /mnt/$(machine)/boot/efi || \
123 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_efi /mnt/$(machine)/boot/efi
126 # Workaround https://dev.gnupg.org/T3908
127 chmod o+rw $$GPG_TTY $$XAUTHORITY
129 sudo --preserve-env \
130 $$(which nixos-install) \
131 --root /mnt/$(machine) \
132 --flake '.#$(machine)' \
137 # End workaround https://dev.gnupg.org/T3908
138 chmod o-rw $$GPG_TTY $$XAUTHORITY
153 ! sudo mountpoint /mnt/$(machine)/"$$p" || \
154 sudo umount -v /mnt/$(machine)/"$$p" ; \
156 ! sudo zpool list $(zpool) 2>/dev/null || \
157 zfs get -H encryption $(zpool) | \
158 grep -q '^$(zpool)\s*encryption\s*off' || \
159 zfs get -H keystatus $(zpool) | \
160 grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
161 sudo zfs unload-key $(zpool)
162 #! sudo zpool list bpool 2>/dev/null || \
163 #sudo zpool export bpool
164 ! sudo zpool list $(zpool) 2>/dev/null || \
165 sudo zpool export $(zpool)
168 pass machines/$(machine)/zfs/zpool | \
169 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
170 nixops ssh $(machine) -p 2222 'zfs load-key $(zpool) && pkill zfs'