ZFS: Difference between revisions

imported>Mic92
Remove dead link
imported>Hmenke
m lang="console" is only useful if the line starts with $
Line 31: Line 31:


To activate the configuration and load the ZFS kernel module, run:
To activate the configuration and load the ZFS kernel module, run:
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
nixos-rebuild switch
nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>
Line 39: Line 39:
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:


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs set mountpoint=legacy <pool>/<fs>
zfs set mountpoint=legacy <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
mount -t zfs <pool>/<fs> <mountpoint>
mount -t zfs <pool>/<fs> <mountpoint>
</syntaxhighlight>
</syntaxhighlight>


This will regenerate your /etc/nixos/hardware-configuration.nix file:
This will regenerate your /etc/nixos/hardware-configuration.nix file:
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
nixos-generate-config
nixos-generate-config
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
nixos-rebuild switch
nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>
Line 99: Line 99:
To reserve space create a new unused dataset that gets a guaranteed disk space of 1GB.
To reserve space create a new unused dataset that gets a guaranteed disk space of 1GB.


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs create -o refreservation=1G -o mountpoint=none zroot/reserved
zfs create -o refreservation=1G -o mountpoint=none zroot/reserved
</syntaxhighlight>
</syntaxhighlight>
Line 106: Line 106:
The dataset itself should not be used. In case you would run out of space you can shrink the reservation to reclaim enough disk space to cleanup the other data from the pool:
The dataset itself should not be used. In case you would run out of space you can shrink the reservation to reclaim enough disk space to cleanup the other data from the pool:


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs set refreservation=none zroot/reserved
zfs set refreservation=none zroot/reserved
</syntaxhighlight>
</syntaxhighlight>
Line 114: Line 114:
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:


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs set com.sun:auto-snapshot=true <pool>/<fs>
zfs set com.sun:auto-snapshot=true <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>
Line 140: Line 140:
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:


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>
Line 153: Line 153:
different sections of the dataset hierarchy, like this:
different sections of the dataset hierarchy, like this:


<syntaxhighlight lang="console">
<syntaxhighlight lang="none">
rpool/
rpool/
       local/
       local/
Line 207: Line 207:
These instructions will get you started with a single-disk ZFS setup. If you're interested in setting up RAID, see below.
These instructions will get you started with a single-disk ZFS setup. If you're interested in setting up RAID, see below.


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
# Always use the by-id aliases for devices, otherwise ZFS can choke on imports.
# Always use the by-id aliases for devices, otherwise ZFS can choke on imports.
DISK=/dev/disk/by-id/...
DISK=/dev/disk/by-id/...
Line 279: Line 279:
(thanks to Danny Wilson for the instructions)
(thanks to Danny Wilson for the instructions)


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
# Verify that the installer environment has loaded the ZFS kernel module (default since 18.09)
# Verify that the installer environment has loaded the ZFS kernel module (default since 18.09)
lsmod | grep zfs
lsmod | grep zfs
Line 359: Line 359:
Encrypted datasets can be added on top as follow:
Encrypted datasets can be added on top as follow:
: posixacl are needed for journald
: posixacl are needed for journald
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs create -o  acltype=posixacl -o xattr=sa -o encryption=aes-256-gcm -o keyformat=passphrase -o mountpoint=none zroot/root
zfs create -o  acltype=posixacl -o xattr=sa -o encryption=aes-256-gcm -o keyformat=passphrase -o mountpoint=none zroot/root
</syntaxHighlight>
</syntaxHighlight>


Instead of encrypting just a dataset (and all its child datasets) you can also directly encrypt the whole pool upon creation:
Instead of encrypting just a dataset (and all its child datasets) you can also directly encrypt the whole pool upon creation:
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zpool create -o ashift=12 -o altroot="/mnt" -O mountpoint=none -O encryption=aes-256-gcm -O keyformat=passphrase zroot /dev/sdxy
zpool create -o ashift=12 -o altroot="/mnt" -O mountpoint=none -O encryption=aes-256-gcm -O keyformat=passphrase zroot /dev/sdxy
</syntaxHighlight>
</syntaxHighlight>
Line 373: Line 373:


A full encrypted nixos installation on an UEFI system could look like this:
A full encrypted nixos installation on an UEFI system could look like this:
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
zfs create -o mountpoint=legacy -o sync=disabled zroot/root/tmp
zfs create -o mountpoint=legacy -o sync=disabled zroot/root/tmp
zfs create -o mountpoint=legacy -o com.sun:auto-snapshot=true zroot/root/home
zfs create -o mountpoint=legacy -o com.sun:auto-snapshot=true zroot/root/home
Line 379: Line 379:
</syntaxHighlight>
</syntaxHighlight>


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
mount -t zfs zroot/root/nixos /mnt
mount -t zfs zroot/root/nixos /mnt
mkdir /mnt/{home,tmp,boot}
mkdir /mnt/{home,tmp,boot}
</syntaxHighlight>
</syntaxHighlight>
: assuming that /dev/sda1 is the boot partition
: assuming that /dev/sda1 is the boot partition
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
mkfs.vfat /dev/sda1
mkfs.vfat /dev/sda1
mount /dev/sda1 /mnt/boot/
mount /dev/sda1 /mnt/boot/
</syntaxHighlight>
</syntaxHighlight>


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
mount -t zfs zroot/root/home /mnt/home/
mount -t zfs zroot/root/home /mnt/home/
mount -t zfs zroot/root/tmp /mnt/tmp/
mount -t zfs zroot/root/tmp /mnt/tmp/
</syntaxHighlight>
</syntaxHighlight>


<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
nixos-generate-config  --root /mnt
nixos-generate-config  --root /mnt
</syntaxHighlight>
</syntaxHighlight>