]> Git — Sourcephile - julm/julm-nix.git/blob - Makefile
zfs: remove home dataset setup
[julm/julm-nix.git] / Makefile
1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 machine := oignon
3 disk_ssd := /dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X
4 zpool := $(machine)
5 cipher := aes-128-gcm
6 autotrim := on
7 reservation := 1G
8
9 wipe:
10 sudo modprobe zfs
11 sudo zpool labelclear -f /dev/disk/by-partlabel/$(machine)_ssd_zpool || true
12 sudo $$(which sgdisk) --zap-all $(disk_ssd)
13
14 part: wipe
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)
25
26 format: umount format-efi format-boot format-zpool
27 format-efi:
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
30 format-boot:
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
34 format-zpool:
35 sudo zpool list $(zpool) 2>/dev/null || \
36 sudo zpool create -o ashift=12 \
37 -O utf8only=yes \
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
42 sudo zpool set \
43 autotrim=$(autotrim) \
44 $(zpool)
45 sudo zfs set \
46 acltype=off \
47 atime=off \
48 canmount=off \
49 compression=lz4 \
50 dnodesize=auto \
51 relatime=on \
52 xattr=off \
53 mountpoint=/ \
54 $(zpool)
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
59 # /
60 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
61 sudo zfs list $(zpool)/root 2>/dev/null || \
62 sudo zfs create \
63 -o canmount=on \
64 -o mountpoint=legacy \
65 $(zpool)/root
66 # /*
67 for p in \
68 nix \
69 var \
70 ; do \
71 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
72 sudo zfs create \
73 -o canmount=on \
74 -o mountpoint=legacy \
75 $(zpool)/"$$p" ; \
76 done
77 #sudo zfs set sync=disabled $(zpool)/var/tmp
78 #sudo zfs set copies=2 $(zpool)/home/documents
79
80 mount: mount-zpool mount-boot mount-efi
81 mount-zpool:
82 # scan needed zpools
83 sudo zpool list $(zpool) || \
84 sudo zpool import -f $(zpool)
85 # load encryption key
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)
91 # /
92 sudo mkdir -p /mnt/$(machine)
93 sudo mountpoint /mnt/$(machine) || \
94 sudo mount -v -t zfs $(zpool)/root /mnt/$(machine)
95 # /*
96 for p in \
97 nix \
98 var \
99 ; do \
100 sudo mkdir -p /mnt/$(machine)/"$$p"; \
101 sudo mountpoint /mnt/$(machine)/"$$p" || \
102 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(machine)/"$$p" ; \
103 done
104 #sudo chmod 1777 /mnt/$(machine)/var/tmp
105 mount-boot:
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
114
115 bootstrap: mount
116 # Workaround https://dev.gnupg.org/T3908
117 chmod o+rw $$GPG_TTY $$XAUTHORITY
118
119 sudo --preserve-env \
120 $$(which nixos-install) \
121 --root /mnt/$(machine) \
122 --flake '.#$(machine)' \
123 --no-root-passwd \
124 --no-channel-copy \
125 --option allow-import-from-derivation true \
126 --show-trace
127
128 # End workaround https://dev.gnupg.org/T3908
129 chmod o-rw $$GPG_TTY $$XAUTHORITY
130
131 umount:
132 for p in \
133 boot/efi \
134 boot \
135 nix \
136 var \
137 "" \
138 ; do \
139 ! sudo mountpoint /mnt/$(machine)/"$$p" || \
140 sudo umount -v /mnt/$(machine)/"$$p" ; \
141 done
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)
152
153 unlock:
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'