ZFS: Difference between revisions
make zfs conflicting with fileSystems entries more clear |
→Remote unlock: Remove a note to a closed issue, assuming this has been resolved already. |
||
| (21 intermediate revisions by 11 users not shown) | |||
| Line 62: | Line 62: | ||
Disable the mount service with <code>systemd.services.zfs-mount.enable = false;</code> or remove the <code>fileSystems</code> entries in hardware-configuration.nix. Otherwise, use legacy mountpoints (created with e.g. <code>zfs create -o mountpoint=legacy</code>). Mountpoints must be specified with <code>fileSystems."/mount/point" = {};</code> or with <code>nixos-generate-config</code>. | Disable the mount service with <code>systemd.services.zfs-mount.enable = false;</code> or remove the <code>fileSystems</code> entries in hardware-configuration.nix. Otherwise, use legacy mountpoints (created with e.g. <code>zfs create -o mountpoint=legacy</code>). Mountpoints must be specified with <code>fileSystems."/mount/point" = {};</code> or with <code>nixos-generate-config</code>. | ||
==== Nix builds and ZFS properties like normalization or utf8only ==== | |||
These options are often suggested in guides to setting up ZFS. <code>normalization</code> makes filenames compare the same in cases where there exists more than one UTF8 bytestring that represents the same characters. <code>utf8only</code> prevents the creation of files with non-UTF8 filenames, e.g. filenames using a Latin1 character set. These are non-POSIX and will make the tests for certain packages fail, which may interfere with builds. After nix 2.30, builds no longer happen in /tmp by default, instead they happen in <code>/nix/var/nix/builds</code>. On any system where you plan to run nix builds, you should ensure that this filesystem is POSIX-compliant. Either mounting a tmpfs in that directory (if you have lots of RAM + swap) or creating a zfs dataset there which does not have these or other non-POSIX settings like <code>noatime</code>, <code>snapdir=visible</code>, <code>acltype=nfsv4</code>, or <code>caseinsensitivity=insensitive</code>. Many of these cannot be changed after dataset creation so if this is your root filesystem, you will need to restore from a backup in order to recreate them. | |||
== Guides == | == Guides == | ||
=== | === Root on ZFS with disko === | ||
disko[https://github.com/nix-community/disko/blob/master/example/zfs.nix] can partition disks declaratively and handle mount points at install time. | |||
Don't follow the Root on ZFS guide found in OpenZFS documentation. It was abandoned and has not been updated in years. See commit log for the openzfs-docs repo for details. | |||
=== Simple NixOS ZFS on root installation === | |||
Start from here in the NixOS manual: [https://nixos.org/manual/nixos/stable/#sec-installation-manual]. | Start from here in the NixOS manual: [https://nixos.org/manual/nixos/stable/#sec-installation-manual]. | ||
Under manual partitioning [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning] do this instead: | Under manual partitioning [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning] do this instead: | ||
==== Partition the disk ==== | |||
We need the following partitions: | We need the following partitions: | ||
| Line 145: | Line 139: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Let's use variables from now on for simplicity. | '''Let's use variables from now on for simplicity.''' Get the device ID in <code>/dev/disk/by-id/</code> (using {{ic|blkid}}), in our case here it is <code>nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O</code> | ||
Get the device ID in <code>/dev/disk/by-id/</code> (using {{ic|blkid}}), in our case here it is <code>nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O</code> | |||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
BOOT=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1 | BOOT=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1 | ||
| Line 156: | Line 149: | ||
{{note|It is often recommended to specify the drive using the device ID/UUID to prevent incorrect configuration, but it is also possible to use the device name (e.g. /dev/sda). See also: [[#Zpool created with bus-based disk names]], [https://wiki.archlinux.org/title/Persistent_block_device_naming Persistent block device naming - ArchWiki]}} | {{note|It is often recommended to specify the drive using the device ID/UUID to prevent incorrect configuration, but it is also possible to use the device name (e.g. /dev/sda). See also: [[#Zpool created with bus-based disk names]], [https://wiki.archlinux.org/title/Persistent_block_device_naming Persistent block device naming - ArchWiki]}} | ||
==== Make a ZFS pool with encryption and mount points ==== | |||
{{Note|zpool config can significantly affect performance (especially the ashift option) so you may want to do some research. The ZFS tuning cheatsheet or ArchWiki is a good place to start.}} | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 197: | Line 190: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Format boot partition and enable swap ==== | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mkfs.fat -F 32 -n boot $BOOT | mkfs.fat -F 32 -n boot $BOOT | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mkswap -L swap $SWAP | mkswap -L swap $SWAP | ||
| Line 208: | Line 200: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Installation ==== | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# Mount boot | # Mount boot | ||
| Line 335: | Line 326: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
You can confirm whether any specified configuration/tuning got applied via commands like <code> | You can confirm whether any specified configuration/tuning got applied via commands like <code>zarcsummary</code> and <code>zarcstat -a -s " "</code>. | ||
== Automatic scrubbing == | == Automatic scrubbing == | ||
| Line 348: | Line 339: | ||
=== Unlock encrypted ZFS via SSH on boot === | === Unlock encrypted ZFS via SSH on boot === | ||
{{ | {{merge|Remote_disk_unlocking}}In case you want unlock a machine remotely (after an update), having an ssh service in initrd for the password prompt is handy: | ||
In case you want unlock a machine remotely (after an update), having an ssh service in initrd for the password prompt is handy: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 380: | Line 369: | ||
* If your network card isn't started, you'll need to add the according Kernel module to the Kernel and initrd as well, e.g. <syntaxhighlight lang="nix"> | * If your network card isn't started, you'll need to add the according Kernel module to the Kernel and initrd as well, e.g. <syntaxhighlight lang="nix"> | ||
boot.kernelModules = [ "r8169" ]; | boot.kernelModules = [ "r8169" ]; | ||
boot.initrd.kernelModules = [ "r8169" ];</syntaxhighlight> | boot.initrd.kernelModules = [ "r8169" ];</syntaxhighlight>To know what kernel modules are needed, run <code>nix shell nixpkgs#pciutils --command lspci -v | grep -iA8 'network\|ethernet'</code> . | ||
After that you can unlock your datasets using the following ssh command: | After that you can unlock your datasets using the following ssh command: | ||
| Line 431: | Line 420: | ||
== Take snapshots automatically == | == Take snapshots automatically == | ||
See {{nixos:option|services.sanoid}} section in <code>man configuration.nix</code>. | See {{nixos:option|services.zfs.autoSnapshot}} or {{nixos:option|services.sanoid}} section in <code>man configuration.nix</code>. | ||
== NFS share == | == NFS share == | ||
| Line 446: | Line 435: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Only this line is needed. Configure firewall if necessary, as described in [[NFS]] article. | Only this line is needed. Configure firewall if necessary, as described in [[NFS]] article. | ||
{{warning|<code>zfs share</code> or <code>sharenfs</code> does not work if the <code>mountpoint</code> is set to <code>legacy</code> (or <code>none</code>, of course). I was unable to find a source for this behaviour, but I was stuck on the problem for days, until I realized the problem. ::Reply: sharenfs controlls what | |||
is written into <code>/etc/exports</code>. If ZFS does not know the mountpoint, as is the case in | |||
mountpoint legacy or none, the contents of <code>/etc/exports</code> would be wrong}} | |||
Then, set <code>sharenfs</code> property: | Then, set <code>sharenfs</code> property: | ||
| Line 458: | Line 451: | ||
ZFS Event Daemon (zed) monitors events generated by the ZFS Kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options] | ZFS Event Daemon (zed) monitors events generated by the ZFS Kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options] | ||
First, we need to configure a mail transfer agent, the program that sends email: | First, we need to configure a mail transfer agent, the program that sends email: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
age.secrets.msmtp = { | |||
file = "${inputs.self.outPath}/secrets/msmtp.age"; | |||
}; | |||
# for zed enableMail, enable sendmailSetuidWrapper | |||
services.mail.sendmailSetuidWrapper.enable = true; | |||
programs.msmtp = { | programs.msmtp = { | ||
enable = true; | enable = true; | ||
| Line 469: | Line 467: | ||
defaults = { | defaults = { | ||
aliases = "/etc/aliases"; | aliases = "/etc/aliases"; | ||
port = | port = 587; | ||
auth = "plain"; | |||
tls = "on"; | tls = "on"; | ||
tls_starttls = "on"; | |||
tls_starttls = " | |||
}; | }; | ||
accounts = { | accounts = { | ||
default = { | default = { | ||
host = "mail.example.com"; | host = "smtp.mail.example.com"; | ||
passwordeval = "cat | passwordeval = "cat ${config.age.secrets.msmtp.path}"; | ||
user = " | user = "myname@example.com"; | ||
from = " | from = "myname@example.com"; | ||
}; | }; | ||
}; | }; | ||
| Line 489: | Line 486: | ||
Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account. | Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account. | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nix"> | ||
{ | |||
root: | environment.etc.aliases.text = '' | ||
root: admin@example.com | |||
''; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Finally, | Finally, enable zed mail notification: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
services.zfs.zed | services.zfs.zed = { | ||
enableMail = true; | |||
ZED_EMAIL_ADDR = [ "root" ]; | settings = { | ||
ZED_EMAIL_ADDR = [ "root" ]; | |||
# send notification if scrub succeeds | |||
ZED_NOTIFY_VERBOSE = true; | |||
}; | |||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 520: | Line 513: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Guide]] | [[Category:Guide]] | ||