1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
3 disk_ssd := /dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X
11 sudo zpool labelclear -f /dev/disk/by-partlabel/$(machine)_ssd_zpool || true
12 sudo $$(which sgdisk) --zap-all $(disk_ssd)
15 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
16 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(machine)_ssd_bios" $(disk_ssd)
17 sudo $$(which sgdisk) -n0:1M:+32M -t0:EF00 -c0:"$(machine)_ssd_efi" $(disk_ssd)
18 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(machine)_ssd_boot" $(disk_ssd)
19 sudo $$(which sgdisk) -n0:0:+4G -t0:8200 -c0:"$(machine)_ssd_swap" $(disk_ssd)
20 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(machine)_ssd_zpool" $(disk_ssd)
21 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
22 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
23 sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
24 sudo $$(which sgdisk) --backup=$(machine)_ssd.sgdisk $(disk_ssd)
26 format: umount format-efi format-boot format-zpool
28 sudo blkid /dev/disk/by-partlabel/$(machine)_ssd_efi -t TYPE=vfat || \
29 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(machine)_ssd_efi
31 sudo mkdir -p /mnt/$(machine)
32 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot; test $$? != 2 || \
33 sudo mkfs.ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot
35 sudo zpool list $(zpool) 2>/dev/null || \
36 sudo zpool create -o ashift=12 \
38 $(if $(cipher),-O encryption=$(cipher) \
39 -O keyformat=passphrase \
40 -O keylocation=prompt) \
41 -R /mnt/$(machine) $(zpool) /dev/disk/by-partlabel/$(machine)_ssd_zpool
43 autotrim=$(autotrim) \
55 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
56 sudo zfs list $(zpool)/reserved 2>/dev/null || \
57 sudo zfs create -o canmount=off -o mountpoint=none $(zpool)/reserved
58 sudo zfs set refreservation=$(reservation) $(zpool)/reserved
60 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
61 sudo zfs list $(zpool)/root 2>/dev/null || \
64 -o mountpoint=legacy \
71 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
74 -o mountpoint=legacy \
77 #sudo zfs set sync=disabled $(zpool)/var/tmp
78 #sudo zfs set copies=2 $(zpool)/home/documents
80 mount: mount-zpool mount-boot mount-efi
83 sudo zpool list $(zpool) || \
84 sudo zpool import -f $(zpool)
86 sudo zfs get -H encryption $(zpool) | \
87 grep -q '^$(zpool)\s*encryption\s*off' || \
88 sudo zfs get -H keystatus $(zpool) | \
89 grep -q '^$(zpool)\s*keystatus\s*available' || \
90 sudo zfs load-key $(zpool)
92 sudo mkdir -p /mnt/$(machine)
93 sudo mountpoint /mnt/$(machine) || \
94 sudo mount -v -t zfs $(zpool)/root /mnt/$(machine)
100 sudo mkdir -p /mnt/$(machine)/"$$p"; \
101 sudo mountpoint /mnt/$(machine)/"$$p" || \
102 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(machine)/"$$p" ; \
104 #sudo chmod 1777 /mnt/$(machine)/var/tmp
106 sudo mkdir -p /mnt/$(machine)/boot
107 sudo mountpoint /mnt/$(machine)/boot || \
108 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_boot /mnt/$(machine)/boot
109 #sudo mount -v -t zfs bpool/boot /mnt/$(machine)/boot
110 mount-efi: | mount-boot
111 sudo mkdir -p /mnt/$(machine)/boot/efi
112 sudo mountpoint /mnt/$(machine)/boot/efi || \
113 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_efi /mnt/$(machine)/boot/efi
116 # Workaround https://dev.gnupg.org/T3908
117 chmod o+rw $$GPG_TTY $$XAUTHORITY
119 sudo --preserve-env \
120 $$(which nixos-install) \
121 --root /mnt/$(machine) \
122 --flake '.#$(machine)' \
125 --option allow-import-from-derivation true \
128 # End workaround https://dev.gnupg.org/T3908
129 chmod o-rw $$GPG_TTY $$XAUTHORITY
139 ! sudo mountpoint /mnt/$(machine)/"$$p" || \
140 sudo umount -v /mnt/$(machine)/"$$p" ; \
142 ! sudo zpool list $(zpool) 2>/dev/null || \
143 zfs get -H encryption $(zpool) | \
144 grep -q '^$(zpool)\s*encryption\s*off' || \
145 zfs get -H keystatus $(zpool) | \
146 grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
147 sudo zfs unload-key $(zpool)
148 #! sudo zpool list bpool 2>/dev/null || \
149 #sudo zpool export bpool
150 ! sudo zpool list $(zpool) 2>/dev/null || \
151 sudo zpool export $(zpool)
154 pass machines/$(machine)/zfs/zpool | \
155 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
156 nixops ssh $(machine) -p 2222 'zfs load-key $(zpool) && pkill zfs'