ZFS: Difference between revisions
imported>ElvishJerricco m minor clarification about importing at boot |
improve module that selects the oldest kernel. |
||
(58 intermediate revisions by 25 users not shown) | |||
Line 1: | Line 1: | ||
[https://zfsonlinux.org/ {{PAGENAME}}] ([[wikipedia:en:{{PAGENAME}}]]) - also known as [https://openzfs.org/ OpenZFS] ([[wikipedia:en:OpenZFS]]) - is a modern filesystem[[category:filesystem]] which is well supported on [[NixOS]]. | |||
Besides the ''zfs'' package (''ZFS Filesystem Linux Kernel module'') <ref>https://search.nixos.org/packages?channel=unstable&show=zfs&query=zfs</ref> itself there are many packages in the [[{{PAGENAME}}]] ecosystem available. | |||
[[{{PAGENAME}}]] integrates into NixOS via the ''boot.zfs''<ref>https://search.nixos.org/options?channel=unstable&query=boot.zfs</ref> and ''service.zfs''<ref>https://search.nixos.org/options?channel=unstable&query=services.zfs</ref> options. | |||
== Limitations == | |||
== | ==== Latest kernel compatible with ZFS ==== | ||
Newer kernels might not be supported by ZFS yet. If you are running a kernel which is not officially supported by zfs, the module will refuse to evaluate and show an error. | |||
== | You can pin to a newer kernel version explicitly, but note that this version may be dropped by upstream and in nixpkgs prior to zfs supporting the next version. See [[Linux kernel]] for more information.<syntaxhighlight lang="nix"> | ||
{ | |||
boot.kernelPackages = pkgs.linuxPackages_latest; | |||
# OR | |||
boot.kernelPackages = pkgs.linuxPackages_6_6 | |||
} | |||
</syntaxhighlight> | |||
This snippet will configure the latest compatible kernel. | |||
Note that this can over time jump back to old kernel versions because non-lts kernel version | |||
get removed over time and their newer replacements might be not supported by zfs yet. | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
## | { | ||
boot.zfs. | lib, | ||
pkgs, | |||
config, | |||
... | |||
}: | |||
let | |||
isUnstable = config.boot.zfs.package == pkgs.zfsUnstable; | |||
zfsCompatibleKernelPackages = lib.filterAttrs ( | |||
name: kernelPackages: | |||
(builtins.match "linux_[0-9]+_[0-9]+" name) != null | |||
&& (builtins.tryEval kernelPackages).success | |||
&& ( | |||
(!isUnstable && !kernelPackages.zfs.meta.broken) | |||
|| (isUnstable && !kernelPackages.zfs_unstable.meta.broken) | |||
) | |||
) pkgs.linuxKernel.packages; | |||
latestKernelPackage = lib.last ( | |||
lib.sort (a: b: (lib.versionOlder a.kernel.version b.kernel.version)) ( | |||
builtins.attrValues zfsCompatibleKernelPackages | |||
) | |||
); | |||
in | |||
{ | |||
# Note this might jump back and worth as kernel get added or removed. | |||
boot.kernelPackages = latestKernelPackage; | |||
} | |||
</syntaxhighlight> | |||
==== Partial support for SWAP on ZFS ==== | |||
ZFS does not support swapfiles. SWAP devices can be used instead. Additionally, hibernation is disabled by default due to a [https://github.com/NixOS/nixpkgs/pull/208037 high risk] of data corruption. Note that even if that pull request is merged, it does not fully mitigate the risk. If you wish to enable hibernation regardless and made sure that not swapfiles on ZFS are used, set <code>boot.zfs.allowHibernation = true</code>. | |||
==== Zpool not found ==== | |||
If NixOS fails to import the zpool on reboot, you may need to add <syntaxhighlight lang="nix" inline>boot.zfs.devNodes = "/dev/disk/by-path";</syntaxhighlight> or <syntaxhighlight lang="nix" inline>boot.zfs.devNodes = "/dev/disk/by-partuuid";</syntaxhighlight> to your configuration.nix file. | |||
The differences can be tested by running <code>zpool import -d /dev/disk/by-id</code> when none of the pools are discovered, eg. a live iso. | |||
==== declarative mounting of ZFS datasets ==== | |||
When using legacy mountpoints (created with eg<code>zfs create -o mountpoint=legacy</code>) mountpoints must be specified with <code>fileSystems."/mount/point" = {};</code>. ZFS native mountpoints are not managed as part of the system configuration but better support hibernation with a separate swap partition. This can lead to conflicts if ZFS mount service is also enabled for the same datasets. Disable it with <code>systemd.services.zfs-mount.enable = false;</code>. | |||
== Guides == | |||
==== '''OpenZFS Documentation for installing''' ==== | |||
{{warning|This guide is not endorsed by NixOS and some features like immutable root do not have upstream support and could break on updates. If an issue arises while following this guide, please consult the guides support channels.}} | |||
One guide for a NixOS installation with ZFS is maintained at [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/ OpenZFS Documentation (''Getting Started'' for ''NixOS'')] | |||
It is about: | |||
* [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/index.html#installation enabling ZFS on an existing NixOS installation] and | |||
* [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/#root-on-zfs (installing NixOS with) Root on ZFS]. | |||
It is not about: | |||
* Give understandable, easy to follow and close to the standard installation guide instructions | |||
* integrating ZFS into your existing config | |||
==== '''Simple NixOS ZFS in root installation''' ==== | |||
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: | |||
'''Partition your disk with your favorite partition tool.''' | |||
We need the following partitions: | |||
* 1G for boot partition with "boot" as the partition label (also called name in some tools) and ef00 as partition code | |||
* 4G for a swap partition with "swap" as the partition label and 8200 as partition code. We will encrypt this with a random secret on each boot. | |||
* The rest of disk space for zfs with "root" as the partition label and 8300 as partition code (default code) | |||
Reason for swap partition: ZFS does use a caching mechanism that is different from the normal Linux cache infrastructure. | |||
In low-memory situations, ZFS therefore might need a bit longer to free up memory from its cache. The swap partition will help with that. | |||
Example with gdisk: | |||
<syntaxhighlight lang="bash"> | |||
sudo gdisk /dev/nvme0n1 | |||
GPT fdisk (gdisk) version 1.0.10 | |||
... | |||
# boot partition | |||
Command (? for help): n | |||
Partition number (1-128, default 1): | |||
First sector (2048-1000215182, default = 2048) or {+-}size{KMGTP}: | |||
Last sector (2048-1000215182, default = 1000215175) or {+-}size{KMGTP}: +1G | |||
Current type is 8300 (Linux filesystem) | |||
Hex code or GUID (L to show codes, Enter = 8300): ef00 | |||
Changed type of partition to 'EFI system partition' | |||
# Swap partition | |||
Command (? for help): n | |||
Partition number (2-128, default 2): | |||
First sector (2099200-1000215182, default = 2099200) or {+-}size{KMGTP}: | |||
Last sector (2099200-1000215182, default = 1000215175) or {+-}size{KMGTP}: +4G | |||
Current type is 8300 (Linux filesystem) | |||
Hex code or GUID (L to show codes, Enter = 8300): 8200 | |||
Changed type of partition to 'Linux swap' | |||
# root partition | |||
Command (? for help): n | |||
Partition number (3-128, default 3): | |||
First sector (10487808-1000215182, default = 10487808) or {+-}size{KMGTP}: | |||
Last sector (10487808-1000215182, default = 1000215175) or {+-}size{KMGTP}: | |||
Current type is 8300 (Linux filesystem) | |||
Hex code or GUID (L to show codes, Enter = 8300): | |||
Changed type of partition to 'Linux filesystem' | |||
# write changes | |||
Command (? for help): w | |||
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING | |||
PARTITIONS!! | |||
Do you want to proceed? (Y/N): y | |||
OK; writing new GUID partition table (GPT) to /dev/nvme0n1. | |||
The operation has completed successfully. | |||
</syntaxhighlight> | |||
Final partition table | |||
<syntaxhighlight lang=bash> | |||
Number Start (sector) End (sector) Size Code Name | |||
1 2048 2099199 1024.0 MiB EF00 EFI system partition | |||
2 2099200 10487807 4.0 GiB 8200 Linux swap | |||
3 10487808 1000215175 471.9 GiB 8300 Linux filesystem | |||
</syntaxhighlight> | |||
'''Let's use variables from now on for simplicity. | |||
Get the device ID in <code>/dev/disk/by-id/</code>, in our case here it is <code>nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O</code> | |||
''' | |||
<syntaxhighlight lang=bash> | |||
BOOT=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1 | |||
SWAP=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2 | |||
DISK=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part3 | |||
'''Make 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 [https://jrs-s.net/2018/08/17/zfs-tuning-cheat-sheet/ ZFS tuning cheatsheet] or [https://wiki.archlinux.org/title/ZFS#Storage_pools ArchWiki] is a good place to start. | |||
<syntaxhighlight lang="bash"> | |||
zpool create -O encryption=on -O keyformat=passphrase -O keylocation=prompt -O compression=zstd -O mountpoint=none -O xattr=sa -O acltype=posixacl -o ashift=12 zpool $DISK | |||
# enter the password to decrypt the pool at boot | |||
Enter new passphrase: | |||
Re-enter new passphrase: | |||
# Create datasets | |||
zfs create zpool/root | |||
zfs create zpool/nix | |||
zfs create zpool/var | |||
zfs create zpool/home | |||
mkdir -p /mnt | |||
mount -t zfs zpool/root /mnt -o zfsutil | |||
mkdir /mnt/nix /mnt/var /mnt/home | |||
mount -t zfs zpool/nix /mnt/nix -o zfsutil | |||
mount -t zfs zpool/var /mnt/var -o zfsutil | |||
mount -t zfs zpool/home /mnt/home -o zfsutil | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Output from <syntaxhighlight lang="bash" inline>zpool status</syntaxhighlight>: | |||
<syntaxhighlight > | |||
zpool status | |||
pool: zpool | |||
state: ONLINE | |||
... | |||
config: | |||
NAME STATE READ WRITE CKSUM | |||
zpool ONLINE 0 0 0 | |||
nvme-eui.0025384b21406566-part2 ONLINE 0 0 0 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= | '''Format boot partition with fat as filesystem''' | ||
<syntaxhighlight lang="bash"> | |||
mkfs.fat -F 32 -n boot $BOOT | |||
</syntaxhighlight> | |||
'''Enable swap''' | |||
<syntaxhighlight lang="bash"> | |||
mkswap -L swap $SWAP | |||
swapon $SWAP | |||
</syntaxhighlight> | |||
= | '''Installation:''' | ||
# Mount boot | |||
<syntaxhighlight lang="bash"> | |||
mkdir -p /mnt/boot | |||
mount $BOOT /mnt/boot | |||
# Generate the nixos config | |||
nixos-generate-config --root /mnt | |||
... | |||
writing /mnt/etc/nixos/hardware-configuration.nix... | |||
writing /mnt/etc/nixos/configuration.nix... | |||
For more hardware-specific settings, see https://github.com/NixOS/nixos-hardware. | |||
</syntaxhighlight> | |||
Now edit the configuration.nix that was just created in <code>/mnt/etc/nixos/configuration.nix</code> and make sure to have at least the following content in it. | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# | { | ||
... | |||
# Boot loader config for configuration.nix: | |||
boot.loader.systemd-boot.enable = true; | |||
# for local disks that are not shared over the network, we don't need this to be random | |||
networking.hostId = "8425e349"; | |||
... | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Now check the hardware-configuration.nix in <code>/mnt/etc/nixos/hardware-configuration.nix</code> and add whats missing e.g. <code>options = [ "zfsutil" ]</code> for all filesystems except boot and <code>randomEncryption = true;</code> for the swap partition. Also change the generated swap device to the partition we created e.g. <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2</code> in this case and <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1</code> for boot. | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
... | |||
fileSystems."/" = { | |||
device = "zpool/root"; | |||
fsType = "zfs"; | |||
# the zfsutil option is needed when mounting zfs datasets without "legacy" mountpoints | |||
options = [ "zfsutil" ]; | |||
}; | |||
fileSystems."/nix" = { | |||
device = "zpool/nix"; | |||
fsType = "zfs"; | |||
options = [ "zfsutil" ]; | |||
}; | |||
fileSystems."/var" = { | |||
fileSystems."/" = | device = "zpool/var"; | ||
fsType = "zfs"; | |||
options = [ "zfsutil" ]; | |||
}; | |||
fileSystems."/home" = { | |||
device = "zpool/home"; | |||
fileSystems."/ | fsType = "zfs"; | ||
options = [ "zfsutil" ]; | |||
}; | |||
fileSystems."/boot" = { | |||
device = "/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1"; | |||
fileSystems."/ | fsType = "vfat"; | ||
}; | }; | ||
## /etc/nixos/ | swapDevices = [{ | ||
device = "/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2"; | |||
randomEncryption = true; | |||
}]; | |||
} | |||
</syntaxhighlight> | |||
Now you may install nixos with <code>nixos-install</code> | |||
== Importing on boot == | |||
If you create a zpool, it will not be imported on the next boot unless you either add the zpool name to <syntaxhighlight lang="nix" inline>boot.zfs.extraPools</syntaxhighlight>: | |||
<syntaxhighlight lang="nix"> | |||
## In /etc/nixos/configuration.nix: | |||
boot.zfs.extraPools = [ "zpool_name" ]; | |||
</syntaxhighlight> | |||
or if you are using legacy mountpoints, add a <syntaxhighlight lang="nix" inline>fileSystems</syntaxhighlight> entry and NixOS will automatically detect that the pool needs to be imported: | |||
<syntaxhighlight lang="nix"> | |||
## In /etc/nixos/configuration.nix: | |||
fileSystems."/mount/point" = { | |||
device = "zpool_name"; | |||
fsType = "zfs"; | |||
}; | |||
</syntaxhighlight> | |||
=== Zpool created with bus-based disk names === | |||
If you used bus-based disk names in the <syntaxhighlight inline>zpool create</syntaxhighlight> command, e.g., <syntaxhighlight inline>/dev/sda</syntaxhighlight>, NixOS may run into issues importing the pool if the names change. Even if the pool is able to be mounted (with <syntaxhighlight lang="nix" inline>boot.zfs.devNodes = "/dev/disk/by-partuuid";</syntaxhighlight> set), this may manifest as a <syntaxhighlight inline>FAULTED</syntaxhighlight> disk and a <syntaxhighlight inline>DEGRADED</syntaxhighlight> pool reported by <syntaxhighlight inline>zpool status</syntaxhighlight>. The fix is to re-import the pool using disk IDs: | |||
<syntaxhighlight> | |||
# zpool export zpool_name | |||
# zpool import -d /dev/disk/by-id zpool_name | |||
</syntaxhighlight> | </syntaxhighlight> | ||
The import setting is reflected in <syntaxhighlight inline="" lang="bash">/etc/zfs/zpool.cache</syntaxhighlight>, so it should persist through subsequent boots. | |||
=== Zpool created with disk IDs === | |||
If you used disk IDs to refer to disks in the <code>zpool create</code> command, e.g., <code>/dev/disk/by-id</code>, then NixOS may consistently fail to import the pool unless <code>boot.zfs.devNodes = "/dev/disk/by-id"</code> is also set. | |||
== Mount datasets at boot == | == Mount datasets at boot == | ||
Line 121: | Line 345: | ||
You can tweak the interval (defaults to once a week) and which pools should be scrubbed (defaults to all). | You can tweak the interval (defaults to once a week) and which pools should be scrubbed (defaults to all). | ||
== Remote unlock == | == Remote unlock == | ||
Line 170: | Line 375: | ||
authorizedKeys = [ "ssh-rsa AAAA..." ]; | authorizedKeys = [ "ssh-rsa AAAA..." ]; | ||
}; | }; | ||
}; | }; | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* In order to use DHCP in the initrd, network manager must not be enabled and <syntaxhighlight lang="nix" inline>networking.useDHCP = true;</syntaxhighlight> must be set. | * In order to use DHCP in the initrd, network manager must not be enabled and <syntaxhighlight lang="nix" inline>networking.useDHCP = true;</syntaxhighlight> must be set. | ||
* If your network card isn't started, you'll need to add the according kernel module to the 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.initrd.kernelModules = [ "r8169" ];</syntaxhighlight> | |||
After that you can unlock your datasets using the following ssh command: | |||
<syntaxhighlight> | |||
ssh -p 2222 root@host "zpool import -a; zfs load-key -a && killall zfs" | |||
</syntaxhighlight> | |||
Alternatively you could also add the commands as postCommands to your configuration.nix, then you just have to ssh into the initrd: | |||
<syntaxhighlight | <syntaxhighlight> | ||
boot = { | boot = { | ||
initrd.network = { | initrd.network = { | ||
postCommands = '' | postCommands = '' | ||
# Import all pools | |||
zpool import -a | |||
# Or import selected pools | |||
zpool import pool2 | |||
zpool import pool3 | |||
zpool import pool4 | |||
# Add the load-key command to the .profile | |||
echo "zfs load-key -a; killall zfs" >> /root/.profile | |||
''; | ''; | ||
}; | }; | ||
Line 214: | Line 408: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
After that you can unlock your datasets using the following ssh command: | |||
<syntaxhighlight> | |||
ssh -p 2222 root@host | |||
</syntaxhighlight> | |||
== Reservations == | |||
On ZFS, the performance will deteriorate significantly when more than 80% of the available space is used. To avoid this, reserve disk space beforehand. | |||
To reserve space create a new unused dataset that gets a guaranteed disk space of 10GB. | |||
<syntaxhighlight lang="console"> | |||
# zfs create -o refreservation=10G -o mountpoint=none zroot/reserved | |||
</syntaxhighlight> | |||
== Auto ZFS trimming == | |||
<syntaxhighlight lang="nix" inline>services.zfs.trim.enable = true;</syntaxhighlight>. | |||
This will periodically run <code>zpool trim</code>. Note that this is different from the <code>autotrim</code> pool property. For further information, see the <code>zpool-trim</code> and <code>zpoolprops</code> man pages. | |||
== Take snapshots automatically == | |||
See <code>services.sanoid</code> section in <code>man configuration.nix</code>. | |||
== NFS share == | == NFS share == | ||
With <code>sharenfs</code> property, ZFS has build-in support for generating <code>/etc/exports.d/zfs.exports</code> file, which in turn is processed by NFS service automatically. | With <code>sharenfs</code> property, ZFS has build-in support for generating <code>/etc/exports.d/zfs.exports</code> file, which in turn is processed by NFS service automatically. | ||
Line 235: | Line 454: | ||
For more options, see <code>man 5 exports</code>. | For more options, see <code>man 5 exports</code>. | ||
Todo: | Todo: sharesmb property for Samba. | ||
== Mail notification for ZFS Event Daemon == | == Mail notification for ZFS Event Daemon == |