1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
3 disk_ssd := /dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X
12 sudo zpool labelclear -f /dev/disk/by-partlabel/$(hostName)_ssd_zpool || true
13 sudo $$(which sgdisk) --zap-all $(disk_ssd)
16 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
17 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(hostName)_ssd_bios" $(disk_ssd)
18 sudo $$(which sgdisk) -n0:1M:+32M -t0:EF00 -c0:"$(hostName)_ssd_efi" $(disk_ssd)
19 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(hostName)_ssd_boot" $(disk_ssd)
20 sudo $$(which sgdisk) -n0:0:+4G -t0:8200 -c0:"$(hostName)_ssd_swap" $(disk_ssd)
21 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(hostName)_ssd_zpool" $(disk_ssd)
22 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
23 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
24 sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
25 sudo $$(which sgdisk) --backup=$(hostName)_ssd.sgdisk $(disk_ssd)
27 format: umount format-efi format-boot format-zpool
29 sudo blkid /dev/disk/by-partlabel/$(hostName)_ssd_efi -t TYPE=vfat || \
30 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(hostName)_ssd_efi
32 sudo mkdir -p /mnt/$(hostName)
33 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot; test $$? != 2 || \
34 sudo mkfs.ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot
36 sudo zpool list $(zpool) 2>/dev/null || \
37 sudo zpool create -o ashift=12 \
39 $(if $(cipher),-O encryption=$(cipher) \
40 -O keyformat=passphrase \
41 -O keylocation=prompt) \
42 -R /mnt/$(hostName) $(zpool) /dev/disk/by-partlabel/$(hostName)_ssd_zpool
44 autotrim=$(autotrim) \
50 compression=$(compression) \
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 \
72 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
75 -o mountpoint=legacy \
78 #sudo zfs set sync=disabled $(zpool)/var/tmp
79 #sudo zfs set copies=2 $(zpool)/home/files
81 mount: mount-zpool mount-boot mount-efi
84 sudo zpool list $(zpool) || \
85 sudo zpool import -f $(zpool)
87 sudo zfs get -H encryption $(zpool) | \
88 grep -q '^$(zpool)\s*encryption\s*off' || \
89 sudo zfs get -H keystatus $(zpool) | \
90 grep -q '^$(zpool)\s*keystatus\s*available' || \
91 sudo zfs load-key $(zpool)
93 sudo mkdir -p /mnt/$(hostName)
94 sudo mountpoint /mnt/$(hostName) || \
95 sudo mount -v -t zfs $(zpool)/root /mnt/$(hostName)
101 sudo mkdir -p /mnt/$(hostName)/"$$p"; \
102 sudo mountpoint /mnt/$(hostName)/"$$p" || \
103 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(hostName)/"$$p" ; \
105 #sudo chmod 1777 /mnt/$(hostName)/var/tmp
107 sudo mkdir -p /mnt/$(hostName)/boot
108 sudo mountpoint /mnt/$(hostName)/boot || \
109 sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_boot /mnt/$(hostName)/boot
110 #sudo mount -v -t zfs bpool/boot /mnt/$(hostName)/boot
111 mount-efi: | mount-boot
112 sudo mkdir -p /mnt/$(hostName)/boot/efi
113 sudo mountpoint /mnt/$(hostName)/boot/efi || \
114 sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_efi /mnt/$(hostName)/boot/efi
117 # Workaround https://dev.gnupg.org/T3908
118 chmod o+rw $$GPG_TTY $$XAUTHORITY
120 sudo --preserve-env \
121 $$(which nixos-install) \
122 --root /mnt/$(hostName) \
123 --flake '.#$(hostName)' \
126 --option allow-import-from-derivation true \
129 # End workaround https://dev.gnupg.org/T3908
130 chmod o-rw $$GPG_TTY $$XAUTHORITY
140 ! sudo mountpoint /mnt/$(hostName)/"$$p" || \
141 sudo umount -v /mnt/$(hostName)/"$$p" ; \
143 ! sudo zpool list $(zpool) 2>/dev/null || \
144 zfs get -H encryption $(zpool) | \
145 grep -q '^$(zpool)\s*encryption\s*off' || \
146 zfs get -H keystatus $(zpool) | \
147 grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
148 sudo zfs unload-key $(zpool)
149 #! sudo zpool list bpool 2>/dev/null || \
150 #sudo zpool export bpool
151 ! sudo zpool list $(zpool) 2>/dev/null || \
152 sudo zpool export $(zpool)
155 pass hostNames/$(hostName)/zfs/zpool | \
156 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
157 nixops ssh $(hostName) -p 2222 'zfs load-key $(zpool) && pkill zfs'