ZFS: Difference between revisions

imported>Vater
No edit summary
imported>Vater
m some people are prefering 'console' for syntax highlighting :-/ :-)
Line 25: Line 25:


To activate the configuration and load the ZFS kernel module, run:
To activate the configuration and load the ZFS kernel module, run:
<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
nixos-rebuild switch
nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>
Line 33: Line 33:
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="bash">
<syntaxhighlight lang=console>
zfs set mountpoint=legacy <pool>/<fs>
zfs set mountpoint=legacy <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
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="bash">
<syntaxhighlight lang=console>
nixos-generate-config
nixos-generate-config
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
nixos-rebuild switch
nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>
Line 93: Line 93:
To enable reservations pick any dataset of your and do:
To enable reservations pick any dataset of your and do:
: reserves enough disk space to have room for cleanups/deletion
: reserves enough disk space to have room for cleanups/deletion
<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
zfs set reservation=1G zroot
zfs set reservation=1G zroot
</syntaxhighlight>
</syntaxhighlight>
Line 103: Line 103:
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="bash">
<syntaxhighlight lang=console>
zfs set com.sun:auto-snapshot=true <pool>/<fs>
zfs set com.sun:auto-snapshot=true <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>
Line 129: Line 129:
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="bash">
<syntaxhighlight lang=console>
zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
</syntaxhighlight>
</syntaxhighlight>
Line 141: Line 141:
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="bash">
<syntaxhighlight lang=console>
# 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 215: Line 215:
(thanks to Danny Wilson for the instructions)
(thanks to Danny Wilson for the instructions)


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
# Add the zfs filesystem to the install environment (note this is no longer
# Add the zfs filesystem to the install environment (note this is no longer
# necessary since nixOS 18.09, as the install environment comes with
# necessary since nixOS 18.09, as the install environment comes with
Line 311: Line 311:
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="bash">
<syntaxhighlight lang=console>
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="bash">
<syntaxhighlight lang=console>
zpool create -o ashift=12 -o altroot="/mnt" -O encryption=aes-256-gcm -O keyformat=passphrase zroot /dev/sdxy
zpool create -o ashift=12 -o altroot="/mnt" -O encryption=aes-256-gcm -O keyformat=passphrase zroot /dev/sdxy
</syntaxHighlight>
</syntaxHighlight>
Line 325: Line 325:


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="bash">
<syntaxhighlight lang=console>
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 331: Line 331:
</syntaxHighlight>
</syntaxHighlight>


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
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="bash">
<syntaxhighlight lang=console>
mkfs.vfat /dev/sda1
mkfs.vfat /dev/sda1
mount /dev/sda1 /mnt/boot/
mount /dev/sda1 /mnt/boot/
</syntaxHighlight>
</syntaxHighlight>


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
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="bash">
<syntaxhighlight lang=console>
nixos-generate-config  --root /mnt
nixos-generate-config  --root /mnt
</syntaxHighlight>
</syntaxHighlight>