ZFS: Difference between revisions

From NixOS Wiki
imported>Makefu
No edit summary
imported>Fadenb
(Syntax highlighting)
Line 20: Line 20:
Just add the following to your <code>configuration.nix</code> file:
Just add the following to your <code>configuration.nix</code> file:


<pre>
<syntaxhighlight lang="nix">
   boot.supportedFilesystems = [ "zfs" ];
   boot.supportedFilesystems = [ "zfs" ];
</pre>
</syntaxhighlight>


To activate the configuration and load the ZFS kernel module, run:
To activate the configuration and load the ZFS kernel module, run:


<pre>
<syntaxhighlight lang="bash">
   nixos-rebuild switch
   nixos-rebuild switch
   modprobe zfs
   modprobe zfs
</pre>
</syntaxhighlight>


(Note that manually loading the ZFS kernel module is only necessary in the install environment).<ref>Todo: Verify if this is still the case with NixOS >= 17.03 </ref>
(Note that manually loading the ZFS kernel module is only necessary in the install environment).<ref>Todo: Verify if this is still the case with NixOS >= 17.03 </ref>
Line 37: Line 37:
If you want NixOS to auto-mount your ZFS filesystems during boot, you should set their <code>mountpoint</code> property to <code>legacy</code> and treat it like if it were any other filesystem, i.e.: mount the filesystem manually and regenerate your list of filesystems, as such:
If you want NixOS to auto-mount your ZFS filesystems during boot, you should set their <code>mountpoint</code> property to <code>legacy</code> and treat it like if it were any other filesystem, i.e.: mount the filesystem manually and regenerate your list of filesystems, as such:


<pre>
<syntaxhighlight lang="bash">
   zfs set mountpoint=legacy <pool>/<fs>
   zfs set mountpoint=legacy <pool>/<fs>
   mount -t zfs <pool>/<fs> <mountpoint>
   mount -t zfs <pool>/<fs> <mountpoint>
Line 45: Line 45:


   nixos-rebuild switch
   nixos-rebuild switch
</pre>
</syntaxhighlight>


NixOS will now make sure that your filesystem is always mounted during boot.
NixOS will now make sure that your filesystem is always mounted during boot.
The <code>nixos-generate-config</code> command regenerates your <code>/etc/nixos/hardware-configuration.nix</code> file, which includes the list of filesystems for NixOS to mount during boot, e.g.:
The <code>nixos-generate-config</code> command regenerates your <code>/etc/nixos/hardware-configuration.nix</code> file, which includes the list of filesystems for NixOS to mount during boot, e.g.:
<pre>
<syntaxhighlight lang="nix">
(...)
(...)
   fileSystems."/home" =
   fileSystems."/home" =
Line 61: Line 61:
     };
     };
(...)
(...)
</pre>
</syntaxhighlight>


== How to use the auto-snapshotting service ==
== How to use the auto-snapshotting service ==
Line 67: Line 67:
To auto-snapshot a ZFS filesystem or a ZVol, set its <code>com.sun:auto-snapshot</code> property to <code>true</code>, like this:
To auto-snapshot a ZFS filesystem or a ZVol, set its <code>com.sun:auto-snapshot</code> property to <code>true</code>, like this:


<pre>
<syntaxhighlight lang="bash">
$ zfs set com.sun:auto-snapshot=true <pool>/<fs>
$ zfs set com.sun:auto-snapshot=true <pool>/<fs>
</pre>
</syntaxhighlight>


(Note that by default this property will be inherited by all descendent datasets, but you can set their properties to false if you prefer.)
(Note that by default this property will be inherited by all descendent datasets, but you can set their properties to false if you prefer.)
Line 75: Line 75:
Then, to enable the auto-snapshot service, add this to your <code>configuration.nix</code>:
Then, to enable the auto-snapshot service, add this to your <code>configuration.nix</code>:


<pre>
<syntaxhighlight lang="nix">
services.zfs.autoSnapshot.enable = true;
services.zfs.autoSnapshot.enable = true;
</pre>
</syntaxhighlight>


And finally, run <code>nixos-rebuild switch</code> to activate the new configuration!
And finally, run <code>nixos-rebuild switch</code> to activate the new configuration!
Line 84: Line 84:
You can globally override this configuration by setting the desired number of snapshots in your <code>configuration.nix</code>, like this:
You can globally override this configuration by setting the desired number of snapshots in your <code>configuration.nix</code>, like this:


<pre>
<syntaxhighlight lang="nix">
services.zfs.autoSnapshot = {
services.zfs.autoSnapshot = {
   enable = true;
   enable = true;
Line 90: Line 90:
   monthly = 1;  # keep only one monthly snapshot (instead of twelve)
   monthly = 1;  # keep only one monthly snapshot (instead of twelve)
};
};
</pre>
</syntaxhighlight>


You can also disable a given type of snapshots on a per-dataset basis by setting a ZFS property, like this:
You can also disable a given type of snapshots on a per-dataset basis by setting a ZFS property, like this:


<pre>
<syntaxhighlight lang="bash">
$ zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
$ zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
</pre>
</syntaxhighlight>


This would disable only weekly snapshots on the given filesystem.
This would disable only weekly snapshots on the given filesystem.
Line 105: Line 105:
(thanks to Danny Wilson for the instructions)
(thanks to Danny Wilson for the instructions)


<pre>
<syntaxhighlight lang="bash">
# Add the zfs filesystem to the install environment:
# Add the zfs filesystem to the install environment:
nano /etc/nixos/configuration.nix
nano /etc/nixos/configuration.nix
Line 186: Line 186:
# Ready to go!
# Ready to go!
nixos-install
nixos-install
</pre>
</syntaxhighlight>


== Need more info? ==
== Need more info? ==

Revision as of 19:52, 22 August 2017

NixOS has native support for ZFS. It uses the code from the ZFS on Linux project, including kernel modules and userspace utilities.

What works

All functionality supported by ZFS on Linux, including:

  • Using ZFS as the root filesystem (using either MS-DOS or GPT partitions)
  • Encrypted ZFS pools (using Linux's dm-crypt)
  • All the other ZFS goodies (cheap snapshotting, checksumming, compression, RAID-Z, ...)
  • Auto-snapshotting service

Known issues

  • As of 2014-03-04, you shouldn't use a ZVol as a swap device, as it can deadlock under memory pressure
  • As of 2014-03-04, you should set the mountpoint property of your ZFS filesystems to be legacy and let NixOS mount them like any other filesystem (such as ext4 or btrfs), otherwise some filesystems may fail to mount due to ordering issues
  • As of 2014-03-04, all ZFS pools available to the system will be forcibly imported during boot, regardless if you had imported them before or not. You should be careful not to have any other system accessing them at the same time, otherwise it will corrupt your pools. Normally (for the common desktop user) this should not be a problem, as a hard disk is usually only directly connected to one machine.

How to use it

Just add the following to your configuration.nix file:

  boot.supportedFilesystems = [ "zfs" ];

To activate the configuration and load the ZFS kernel module, run:

  nixos-rebuild switch
  modprobe zfs

(Note that manually loading the ZFS kernel module is only necessary in the install environment).[1]

All ZFS functionality should now be available.

If you want NixOS to auto-mount your ZFS filesystems during boot, you should set their mountpoint property to legacy and treat it like if it were any other filesystem, i.e.: mount the filesystem manually and regenerate your list of filesystems, as such:

  zfs set mountpoint=legacy <pool>/<fs>
  mount -t zfs <pool>/<fs> <mountpoint>

  # This will regenerate your /etc/nixos/hardware-configuration.nix file:
  nixos-generate-config

  nixos-rebuild switch

NixOS will now make sure that your filesystem is always mounted during boot. The nixos-generate-config command regenerates your /etc/nixos/hardware-configuration.nix file, which includes the list of filesystems for NixOS to mount during boot, e.g.:

(...)
  fileSystems."/home" =
    { device = "rpool/home";
      fsType = "zfs";
    };

  fileSystems."/backup" =
    { device = "rpool/backup";
      fsType = "zfs";
    };
(...)

How to use the auto-snapshotting service

To auto-snapshot a ZFS filesystem or a ZVol, set its com.sun:auto-snapshot property to true, like this:

$ zfs set com.sun:auto-snapshot=true <pool>/<fs>

(Note that by default this property will be inherited by all descendent datasets, but you can set their properties to false if you prefer.)

Then, to enable the auto-snapshot service, add this to your configuration.nix:

services.zfs.autoSnapshot.enable = true;

And finally, run nixos-rebuild switch to activate the new configuration!

By default, the auto-snapshot service will keep the latest four 15-minute, 24 hourly, 7 daily, 4 weekly and 12 monthly snapshots. You can globally override this configuration by setting the desired number of snapshots in your configuration.nix, like this:

services.zfs.autoSnapshot = {
  enable = true;
  frequent = 8; # keep the latest eight 15-minute snapshots (instead of four)
  monthly = 1;  # keep only one monthly snapshot (instead of twelve)
};

You can also disable a given type of snapshots on a per-dataset basis by setting a ZFS property, like this:

$ zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>

This would disable only weekly snapshots on the given filesystem.

How to install NixOS on a ZFS root filesystem

Here's an example of how to create a ZFS root pool using 4 disks in RAID-10 mode (striping+mirroring), create a ZFS root+home filesystems and install NixOS on them: (thanks to Danny Wilson for the instructions)

# Add the zfs filesystem to the install environment:
nano /etc/nixos/configuration.nix

## ---8<-------------------------8<---
  boot.supportedFilesystems = [ "zfs" ];
## ---8<-------------------------8<---

nixos-rebuild switch

# Load the just installed ZFS kernel module
modprobe zfs

# Create boot partition and (zfs) data partition
# See: https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-install-Ubuntu-to-a-Native-ZFS-Root-Filesystem#step-2-disk-partitioning
fdisk /dev/sda

# Copy the partition table to the other disks
sfdisk --dump /dev/sda | sfdisk /dev/sdb
sfdisk --dump /dev/sda | sfdisk /dev/sdc
sfdisk --dump /dev/sda | sfdisk /dev/sdd

# Create a RAID-10 ZFS pool. Use "-o ashift=12" to create your ZFS pool with 4K sectors
zpool create -o ashift=12 -o altroot=/mnt rpool mirror /dev/sda2 /dev/sdb2 mirror /dev/sdc2 /dev/sdd2

# Create the filesystems
zfs create -o mountpoint=none rpool/root
zfs create -o mountpoint=legacy rpool/root/nixos
zfs create -o mountpoint=legacy rpool/home
zfs set compression=lz4 rpool/home    # compress the home directories automatically

# Mount the filesystems manually
mount -t zfs rpool/root/nixos /mnt

mkdir /mnt/home
mount -t zfs rpool/home /mnt/home

# Create a raid mirror of the first partitions for /boot (GRUB)
mdadm --build /dev/md127 --metadata=0.90 --level=1 --raid-devices=4 /dev/sd[a,b,c,d]1
mkfs.ext4 -m 0 -L boot -j /dev/md127

mkdir /mnt/boot
mount /dev/md127 /mnt/boot

# Generate the NixOS configuration, as per the NixOS manual
nixos-generate-config --root /mnt

# Now edit the generated hardware config:
nano /mnt/etc/nixos/hardware-configuration.nix

## ---8<-------------------------8<---
# This is what you want:

  fileSystems."/" =
    { device = "rpool/root/nixos";
      fsType = "zfs";
    };

  fileSystems."/home" =
    { device = "rpool/home";
      fsType = "zfs";
    };

  fileSystems."/boot" =
    { device = "/dev/md127";
      fsType = "ext4";
    };
## ---8<-------------------------8<---

# configuration.nix needs an adjustment:
nano /mnt/etc/nixos/configuration.nix

## ---8<-------------------------8<---
# This is some more of what you want:

  boot.loader.grub.devices = [ "/dev/sda" "/dev/sdb" "/dev/sdc" "/dev/sdd" ];
  boot.supportedFilesystems = [ "zfs" ];
## ---8<-------------------------8<---

# Ready to go!
nixos-install

Need more info?

Feel free to ask your questions on the NixOS mailing list or the IRC channel: http://nixos.org/development/

  1. Todo: Verify if this is still the case with NixOS >= 17.03