nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2012-12-04 19:57:59 +00:00
|
|
|
#
|
|
|
|
# todo:
|
|
|
|
# - crontab for scrubs, etc
|
|
|
|
# - zfs tunables
|
2012-12-04 18:17:54 +00:00
|
|
|
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
with utils;
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2012-12-04 18:17:54 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2014-03-22 17:27:23 +00:00
|
|
|
cfgZfs = config.boot.zfs;
|
2014-01-22 00:11:51 +00:00
|
|
|
cfgSnapshots = config.services.zfs.autoSnapshot;
|
2015-03-17 03:32:58 +00:00
|
|
|
cfgSnapFlags = cfgSnapshots.flags;
|
2017-03-02 16:13:54 +00:00
|
|
|
cfgScrub = config.services.zfs.autoScrub;
|
2014-01-22 00:11:51 +00:00
|
|
|
|
2012-12-04 18:17:54 +00:00
|
|
|
inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems;
|
2012-12-04 19:57:59 +00:00
|
|
|
inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems;
|
2014-01-22 00:11:51 +00:00
|
|
|
|
|
|
|
enableAutoSnapshots = cfgSnapshots.enable;
|
2017-03-02 16:13:54 +00:00
|
|
|
enableAutoScrub = cfgScrub.enable;
|
|
|
|
enableZfs = inInitrd || inSystem || enableAutoSnapshots || enableAutoScrub;
|
2014-01-22 00:11:51 +00:00
|
|
|
|
2012-12-04 18:17:54 +00:00
|
|
|
kernel = config.boot.kernelPackages;
|
|
|
|
|
2018-08-13 18:47:30 +01:00
|
|
|
packages = if config.boot.zfs.enableUnstable then {
|
2017-09-13 00:28:42 +01:00
|
|
|
zfs = kernel.zfsUnstable;
|
|
|
|
zfsUser = pkgs.zfsUnstable;
|
|
|
|
} else {
|
2017-01-02 06:46:46 +00:00
|
|
|
zfs = kernel.zfs;
|
|
|
|
zfsUser = pkgs.zfs;
|
|
|
|
};
|
2014-03-22 17:27:23 +00:00
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
autosnapPkg = pkgs.zfstools.override {
|
2017-01-02 06:46:46 +00:00
|
|
|
zfs = packages.zfsUser;
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
zfsAutoSnap = "${autosnapPkg}/bin/zfs-auto-snapshot";
|
|
|
|
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
datasetToPool = x: elemAt (splitString "/" x) 0;
|
|
|
|
|
|
|
|
fsToPool = fs: datasetToPool fs.device;
|
|
|
|
|
2015-11-25 19:09:09 +00:00
|
|
|
zfsFilesystems = filter (x: x.fsType == "zfs") config.system.build.fileSystems;
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
|
|
|
|
allPools = unique ((map fsToPool zfsFilesystems) ++ cfgZfs.extraPools);
|
|
|
|
|
2016-08-23 19:01:35 +01:00
|
|
|
rootPools = unique (map fsToPool (filter fsNeededForBoot zfsFilesystems));
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
|
|
|
|
dataPools = unique (filter (pool: !(elem pool rootPools)) allPools);
|
|
|
|
|
2015-03-20 02:49:25 +00:00
|
|
|
snapshotNames = [ "frequent" "hourly" "daily" "weekly" "monthly" ];
|
|
|
|
|
2018-06-16 02:39:07 +01:00
|
|
|
# When importing ZFS pools, there's one difficulty: These scripts may run
|
|
|
|
# before the backing devices (physical HDDs, etc.) of the pool have been
|
|
|
|
# scanned and initialized.
|
|
|
|
#
|
|
|
|
# An attempted import with all devices missing will just fail, and can be
|
|
|
|
# retried, but an import where e.g. two out of three disks in a three-way
|
|
|
|
# mirror are missing, will succeed. This is a problem: When the missing disks
|
|
|
|
# are later discovered, they won't be automatically set online, rendering the
|
|
|
|
# pool redundancy-less (and far slower) until such time as the system reboots.
|
|
|
|
#
|
|
|
|
# The solution is the below. poolReady checks the status of an un-imported
|
|
|
|
# pool, to see if *every* device is available -- in which case the pool will be
|
|
|
|
# in state ONLINE, as opposed to DEGRADED, FAULTED or MISSING.
|
|
|
|
#
|
|
|
|
# The import scripts then loop over this, waiting until the pool is ready or a
|
|
|
|
# sufficient amount of time has passed that we can assume it won't be. In the
|
|
|
|
# latter case it makes one last attempt at importing, allowing the system to
|
|
|
|
# (eventually) boot even with a degraded pool.
|
|
|
|
importLib = {zpoolCmd, awkCmd, cfgZfs}: ''
|
|
|
|
poolReady() {
|
|
|
|
pool="$1"
|
2018-10-16 10:45:25 +01:00
|
|
|
state="$("${zpoolCmd}" import 2>/dev/null | "${awkCmd}" "/pool: $pool/ { found = 1 }; /state:/ { if (found == 1) { print \$2; exit } }; END { if (found == 0) { print \"MISSING\" } }")"
|
2018-06-16 02:39:07 +01:00
|
|
|
if [[ "$state" = "ONLINE" ]]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo "Pool $pool in state $state, waiting"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
poolImported() {
|
|
|
|
pool="$1"
|
|
|
|
"${zpoolCmd}" list "$pool" >/dev/null 2>/dev/null
|
|
|
|
}
|
|
|
|
poolImport() {
|
|
|
|
pool="$1"
|
|
|
|
"${zpoolCmd}" import -d "${cfgZfs.devNodes}" -N $ZFS_FORCE "$pool"
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2012-12-04 18:17:54 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2012-12-04 19:57:59 +00:00
|
|
|
###### interface
|
2014-01-22 00:11:51 +00:00
|
|
|
|
|
|
|
options = {
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
boot.zfs = {
|
2017-09-13 00:28:42 +01:00
|
|
|
enableUnstable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Use the unstable zfs package. This might be an option, if the latest
|
|
|
|
kernel is not yet supported by a published release of ZFS. Enabling
|
|
|
|
this option will install a development version of ZFS on Linux. The
|
|
|
|
version will have already passed an extensive test suite, but it is
|
|
|
|
more likely to hit an undiscovered bug compared to running a released
|
|
|
|
version of ZFS on Linux.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
extraPools = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "tank" "data" ];
|
|
|
|
description = ''
|
|
|
|
Name or GUID of extra ZFS pools that you wish to import during boot.
|
|
|
|
|
|
|
|
Usually this is not necessary. Instead, you should set the mountpoint property
|
|
|
|
of ZFS filesystems to <literal>legacy</literal> and add the ZFS filesystems to
|
|
|
|
NixOS's <option>fileSystems</option> option, which makes NixOS automatically
|
|
|
|
import the associated pool.
|
|
|
|
|
|
|
|
However, in some cases (e.g. if you have many filesystems) it may be preferable
|
|
|
|
to exclusively use ZFS commands to manage filesystems. If so, since NixOS/systemd
|
|
|
|
will not be managing those filesystems, you will need to specify the ZFS pool here
|
|
|
|
so that NixOS automatically imports it on every boot.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-22 23:29:47 +00:00
|
|
|
devNodes = mkOption {
|
|
|
|
type = types.path;
|
2015-12-22 23:33:06 +00:00
|
|
|
default = "/dev/disk/by-id";
|
2015-12-22 23:29:47 +00:00
|
|
|
example = "/dev/disk/by-id";
|
|
|
|
description = ''
|
|
|
|
Name of directory from which to import ZFS devices.
|
|
|
|
|
2016-04-15 17:25:32 +01:00
|
|
|
This should be a path under /dev containing stable names for all devices needed, as
|
|
|
|
import may fail if device nodes are renamed concurrently with a device failing.
|
2015-12-22 23:29:47 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
forceImportRoot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Forcibly import the ZFS root pool(s) during early boot.
|
|
|
|
|
|
|
|
This is enabled by default for backwards compatibility purposes, but it is highly
|
|
|
|
recommended to disable this option, as it bypasses some of the safeguards ZFS uses
|
|
|
|
to protect your ZFS pools.
|
|
|
|
|
|
|
|
If you set this option to <literal>false</literal> and NixOS subsequently fails to
|
|
|
|
boot because it cannot import the root pool, you should boot with the
|
|
|
|
<literal>zfs_force=1</literal> option as a kernel parameter (e.g. by manually
|
|
|
|
editing the kernel params in grub during boot). You should only need to do this
|
|
|
|
once.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
forceImportAll = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Forcibly import all ZFS pool(s).
|
|
|
|
|
|
|
|
This is enabled by default for backwards compatibility purposes, but it is highly
|
|
|
|
recommended to disable this option, as it bypasses some of the safeguards ZFS uses
|
|
|
|
to protect your ZFS pools.
|
|
|
|
|
|
|
|
If you set this option to <literal>false</literal> and NixOS subsequently fails to
|
|
|
|
import your non-root ZFS pool(s), you should manually import each pool with
|
|
|
|
"zpool import -f <pool-name>", and then reboot. You should only need to do
|
|
|
|
this once.
|
|
|
|
'';
|
|
|
|
};
|
2017-09-15 16:18:09 +01:00
|
|
|
|
|
|
|
requestEncryptionCredentials = mkOption {
|
|
|
|
type = types.bool;
|
2019-07-04 22:11:52 +01:00
|
|
|
default = true;
|
2017-09-15 16:18:09 +01:00
|
|
|
description = ''
|
|
|
|
Request encryption keys or passwords for all encrypted datasets on import.
|
2018-06-28 11:04:30 +01:00
|
|
|
For root pools the encryption key can be supplied via both an
|
|
|
|
interactive prompt (keylocation=prompt) and from a file
|
|
|
|
(keylocation=file://). Note that for data pools the encryption key can
|
|
|
|
be only loaded from a file and not via interactive prompt since the
|
|
|
|
import is processed in a background systemd service.
|
2017-09-15 16:18:09 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-03-22 17:27:23 +00:00
|
|
|
};
|
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
services.zfs.autoSnapshot = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Enable the (OpenSolaris-compatible) ZFS auto-snapshotting service.
|
|
|
|
Note that you must set the <literal>com.sun:auto-snapshot</literal>
|
|
|
|
property to <literal>true</literal> on all datasets which you wish
|
|
|
|
to auto-snapshot.
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
You can override a child dataset to use, or not use auto-snapshotting
|
|
|
|
by setting its flag with the given interval:
|
|
|
|
<literal>zfs set com.sun:auto-snapshot:weekly=false DATASET</literal>
|
|
|
|
'';
|
|
|
|
};
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2015-03-17 03:32:58 +00:00
|
|
|
flags = mkOption {
|
|
|
|
default = "-k -p";
|
|
|
|
example = "-k -p --utc";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Flags to pass to the zfs-auto-snapshot command.
|
|
|
|
|
|
|
|
Run <literal>zfs-auto-snapshot</literal> (without any arguments) to
|
|
|
|
see available flags.
|
|
|
|
|
|
|
|
If it's not too inconvenient for snapshots to have timestamps in UTC,
|
|
|
|
it is suggested that you append <literal>--utc</literal> to the list
|
|
|
|
of default options (see example).
|
|
|
|
|
|
|
|
Otherwise, snapshot names can cause name conflicts or apparent time
|
|
|
|
reversals due to daylight savings, timezone or other date/time changes.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
frequent = mkOption {
|
|
|
|
default = 4;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Number of frequent (15-minute) auto-snapshots that you wish to keep.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hourly = mkOption {
|
|
|
|
default = 24;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Number of hourly auto-snapshots that you wish to keep.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
daily = mkOption {
|
|
|
|
default = 7;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Number of daily auto-snapshots that you wish to keep.
|
|
|
|
'';
|
|
|
|
};
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
weekly = mkOption {
|
|
|
|
default = 4;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Number of weekly auto-snapshots that you wish to keep.
|
2012-12-04 19:57:59 +00:00
|
|
|
'';
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
monthly = mkOption {
|
|
|
|
default = 12;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Number of monthly auto-snapshots that you wish to keep.
|
2012-12-04 19:57:59 +00:00
|
|
|
'';
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
2012-12-04 19:57:59 +00:00
|
|
|
};
|
2017-03-02 16:13:54 +00:00
|
|
|
|
|
|
|
services.zfs.autoScrub = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Enables periodic scrubbing of ZFS pools.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
interval = mkOption {
|
|
|
|
default = "Sun, 02:00";
|
|
|
|
type = types.str;
|
|
|
|
example = "daily";
|
|
|
|
description = ''
|
|
|
|
Systemd calendar expression when to scrub ZFS pools. See
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
2017-03-21 07:27:56 +00:00
|
|
|
<manvolnum>7</manvolnum></citerefentry>.
|
2017-03-02 16:13:54 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
pools = mkOption {
|
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
example = [ "tank" ];
|
|
|
|
description = ''
|
|
|
|
List of ZFS pools to periodically scrub. If empty, all pools
|
|
|
|
will be scrubbed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf enableZfs {
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
assertions = [
|
nixos: Add system-wide option to set the hostid
The old boot.spl.hostid option was not working correctly due to an
upstream bug.
Instead, now we will create the /etc/hostid file so that all applications
(including the ZFS kernel modules, ZFS user-space applications and other
unrelated programs) pick-up the same system-wide host id. Note that glibc
(and by extension, the `hostid` program) also respect the host id configured in
/etc/hostid, if it exists.
The hostid option is now mandatory when using ZFS because otherwise, ZFS will
require you to force-import your ZFS pools if you want to use them, which is
undesirable because it disables some of the checks that ZFS does to make sure it
is safe to import a ZFS pool.
The /etc/hostid file must also exist when booting the initrd, before the SPL
kernel module is loaded, so that ZFS picks up the hostid correctly.
The complexity in creating the /etc/hostid file is due to having to
write the host ID as a 32-bit binary value, taking into account the
endianness of the machine, while using only shell commands and/or simple
utilities (to avoid exploding the size of the initrd).
2014-10-23 03:59:06 +01:00
|
|
|
{
|
|
|
|
assertion = config.networking.hostId != null;
|
2017-12-20 22:56:03 +00:00
|
|
|
message = "ZFS requires networking.hostId to be set";
|
nixos: Add system-wide option to set the hostid
The old boot.spl.hostid option was not working correctly due to an
upstream bug.
Instead, now we will create the /etc/hostid file so that all applications
(including the ZFS kernel modules, ZFS user-space applications and other
unrelated programs) pick-up the same system-wide host id. Note that glibc
(and by extension, the `hostid` program) also respect the host id configured in
/etc/hostid, if it exists.
The hostid option is now mandatory when using ZFS because otherwise, ZFS will
require you to force-import your ZFS pools if you want to use them, which is
undesirable because it disables some of the checks that ZFS does to make sure it
is safe to import a ZFS pool.
The /etc/hostid file must also exist when booting the initrd, before the SPL
kernel module is loaded, so that ZFS picks up the hostid correctly.
The complexity in creating the /etc/hostid file is due to having to
write the host ID as a 32-bit binary value, taking into account the
endianness of the machine, while using only shell commands and/or simple
utilities (to avoid exploding the size of the initrd).
2014-10-23 03:59:06 +01:00
|
|
|
}
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
{
|
|
|
|
assertion = !cfgZfs.forceImportAll || cfgZfs.forceImportRoot;
|
|
|
|
message = "If you enable boot.zfs.forceImportAll, you must also enable boot.zfs.forceImportRoot";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
virtualisation.lxd.zfsSupport = true;
|
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
boot = {
|
2019-06-06 09:24:11 +01:00
|
|
|
kernelModules = [ "zfs" ];
|
|
|
|
extraModulePackages = with packages; [ zfs ];
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd = mkIf inInitrd {
|
2018-08-13 19:40:45 +01:00
|
|
|
kernelModules = [ "zfs" ] ++ optional (!cfgZfs.enableUnstable) "spl";
|
2014-01-22 00:11:51 +00:00
|
|
|
extraUtilsCommands =
|
|
|
|
''
|
2017-01-02 06:46:46 +00:00
|
|
|
copy_bin_and_libs ${packages.zfsUser}/sbin/zfs
|
|
|
|
copy_bin_and_libs ${packages.zfsUser}/sbin/zdb
|
|
|
|
copy_bin_and_libs ${packages.zfsUser}/sbin/zpool
|
2015-03-29 00:15:41 +00:00
|
|
|
'';
|
|
|
|
extraUtilsCommandsTest = mkIf inInitrd
|
|
|
|
''
|
|
|
|
$out/bin/zfs --help >/dev/null 2>&1
|
|
|
|
$out/bin/zpool --help >/dev/null 2>&1
|
2014-01-22 00:11:51 +00:00
|
|
|
'';
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
postDeviceCommands = concatStringsSep "\n" ([''
|
|
|
|
ZFS_FORCE="${optionalString cfgZfs.forceImportRoot "-f"}"
|
|
|
|
|
|
|
|
for o in $(cat /proc/cmdline); do
|
|
|
|
case $o in
|
|
|
|
zfs_force|zfs_force=1)
|
|
|
|
ZFS_FORCE="-f"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2018-06-16 02:39:07 +01:00
|
|
|
''] ++ [(importLib {
|
|
|
|
# See comments at importLib definition.
|
|
|
|
zpoolCmd = "zpool";
|
|
|
|
awkCmd = "awk";
|
|
|
|
inherit cfgZfs;
|
|
|
|
})] ++ (map (pool: ''
|
2016-07-13 01:18:48 +01:00
|
|
|
echo -n "importing root ZFS pool \"${pool}\"..."
|
2018-06-16 02:39:07 +01:00
|
|
|
# Loop across the import until it succeeds, because the devices needed may not be discovered yet.
|
|
|
|
if ! poolImported "${pool}"; then
|
|
|
|
for trial in `seq 1 60`; do
|
|
|
|
poolReady "${pool}" > /dev/null && msg="$(poolImport "${pool}" 2>&1)" && break
|
|
|
|
sleep 1
|
|
|
|
echo -n .
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
if [[ -n "$msg" ]]; then
|
|
|
|
echo "$msg";
|
2016-07-13 01:18:48 +01:00
|
|
|
fi
|
2018-06-16 02:39:07 +01:00
|
|
|
poolImported "${pool}" || poolImport "${pool}" # Try one last time, e.g. to import a degraded pool.
|
|
|
|
fi
|
2017-09-15 16:18:09 +01:00
|
|
|
${lib.optionalString cfgZfs.requestEncryptionCredentials ''
|
|
|
|
zfs load-key -a
|
|
|
|
''}
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
'') rootPools));
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
|
|
|
|
2014-08-31 17:18:13 +01:00
|
|
|
boot.loader.grub = mkIf inInitrd {
|
|
|
|
zfsSupport = true;
|
|
|
|
};
|
|
|
|
|
2017-03-02 16:13:54 +00:00
|
|
|
environment.etc."zfs/zed.d".source = "${packages.zfsUser}/etc/zfs/zed.d/";
|
2013-06-07 17:34:46 +01:00
|
|
|
|
2017-01-02 06:46:46 +00:00
|
|
|
system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck
|
|
|
|
environment.systemPackages = [ packages.zfsUser ]
|
|
|
|
++ optional enableAutoSnapshots autosnapPkg; # so the user can run the command to see flags
|
2015-03-17 03:32:58 +00:00
|
|
|
|
2017-01-02 06:46:46 +00:00
|
|
|
services.udev.packages = [ packages.zfsUser ]; # to hook zvol naming, etc.
|
|
|
|
systemd.packages = [ packages.zfsUser ];
|
2014-10-22 17:48:57 +01:00
|
|
|
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
systemd.services = let
|
|
|
|
getPoolFilesystems = pool:
|
2015-11-25 19:09:09 +00:00
|
|
|
filter (x: x.fsType == "zfs" && (fsToPool x) == pool) config.system.build.fileSystems;
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
|
|
|
|
getPoolMounts = pool:
|
|
|
|
let
|
|
|
|
mountPoint = fs: escapeSystemdPath fs.mountPoint;
|
|
|
|
in
|
|
|
|
map (x: "${mountPoint x}.mount") (getPoolFilesystems pool);
|
|
|
|
|
|
|
|
createImportService = pool:
|
|
|
|
nameValuePair "zfs-import-${pool}" {
|
|
|
|
description = "Import ZFS pool \"${pool}\"";
|
|
|
|
requires = [ "systemd-udev-settle.service" ];
|
|
|
|
after = [ "systemd-udev-settle.service" "systemd-modules-load.service" ];
|
|
|
|
wantedBy = (getPoolMounts pool) ++ [ "local-fs.target" ];
|
|
|
|
before = (getPoolMounts pool) ++ [ "local-fs.target" ];
|
|
|
|
unitConfig = {
|
|
|
|
DefaultDependencies = "no";
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
2018-06-16 02:39:07 +01:00
|
|
|
script = (importLib {
|
|
|
|
# See comments at importLib definition.
|
|
|
|
zpoolCmd="${packages.zfsUser}/sbin/zpool";
|
|
|
|
awkCmd="${pkgs.gawk}/bin/awk";
|
|
|
|
inherit cfgZfs;
|
|
|
|
}) + ''
|
|
|
|
poolImported "${pool}" && exit
|
|
|
|
echo -n "importing ZFS pool \"${pool}\"..."
|
|
|
|
# Loop across the import until it succeeds, because the devices needed may not be discovered yet.
|
|
|
|
for trial in `seq 1 60`; do
|
|
|
|
poolReady "${pool}" && poolImport "${pool}" && break
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
poolImported "${pool}" || poolImport "${pool}" # Try one last time, e.g. to import a degraded pool.
|
|
|
|
if poolImported "${pool}"; then
|
|
|
|
${optionalString cfgZfs.requestEncryptionCredentials "\"${packages.zfsUser}/sbin/zfs\" load-key -r \"${pool}\""}
|
|
|
|
echo "Successfully imported ${pool}"
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
'';
|
|
|
|
};
|
2016-07-19 08:57:13 +01:00
|
|
|
|
|
|
|
# This forces a sync of any ZFS pools prior to poweroff, even if they're set
|
|
|
|
# to sync=disabled.
|
|
|
|
createSyncService = pool:
|
|
|
|
nameValuePair "zfs-sync-${pool}" {
|
|
|
|
description = "Sync ZFS pool \"${pool}\"";
|
|
|
|
wantedBy = [ "shutdown.target" ];
|
2018-06-09 00:06:22 +01:00
|
|
|
unitConfig = {
|
|
|
|
DefaultDependencies = false;
|
|
|
|
};
|
2016-07-19 08:57:13 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
script = ''
|
2017-01-02 06:46:46 +00:00
|
|
|
${packages.zfsUser}/sbin/zfs set nixos:shutdown-time="$(date)" "${pool}"
|
2016-07-19 08:57:13 +01:00
|
|
|
'';
|
|
|
|
};
|
2018-06-13 11:38:15 +01:00
|
|
|
createZfsService = serv:
|
|
|
|
nameValuePair serv {
|
|
|
|
after = [ "systemd-modules-load.service" ];
|
|
|
|
wantedBy = [ "zfs.target" ];
|
|
|
|
};
|
2016-07-19 08:57:13 +01:00
|
|
|
|
2018-06-13 11:38:15 +01:00
|
|
|
in listToAttrs (map createImportService dataPools ++
|
|
|
|
map createSyncService allPools ++
|
|
|
|
map createZfsService [ "zfs-mount" "zfs-share" "zfs-zed" ]);
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
|
|
|
|
systemd.targets."zfs-import" =
|
|
|
|
let
|
|
|
|
services = map (pool: "zfs-import-${pool}.service") dataPools;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
requires = services;
|
|
|
|
after = services;
|
2018-06-15 19:54:49 +01:00
|
|
|
wantedBy = [ "zfs.target" ];
|
nixos/zfs: Improve the ZFS boot process
It turns out that the upstream systemd services that import ZFS pools contain
serious bugs. The first major problem is that importing pools fails if there
are no pools to import. The second major problem is that if a pool ends up in
/etc/zfs/zpool.cache but it disappears from the system (e.g. if you
reboot but during the reboot you unplug your ZFS-formatted USB pen drive),
then the import service will always fail and it will be impossible to get rid
of the pool from the cache (unless you manually delete the cache).
Also, the upstream service would always import all available ZFS pools every
boot, which may not be what is desired in some cases.
This commit will solve these problems in the following ways:
1. Ignore /etc/zfs/zpool.cache. This seems to be a major source of
issues, and also does not play well with NixOS's philosophy of
reproducible configurations. Instead, on every boot NixOS will try to import
the set of pools that are specified in its configuration. This is also the
direction that upstream is moving towards.
2. Instead of trying to import all ZFS pools, only import those that are
actually necessary. NixOS will automatically determine these from the
config.fileSystems.* option. Also, the user can import any additional
pools every boot by adding them to the config.boot.zfs.extraPools
option, but this is only necessary if their filesystems are not
specified in config.fileSystems.*.
3. Added options to configure if ZFS should force-import ZFS pools. This may
currently be necessary, especially if your pools have not been correctly
imported with a proper host id configuration (which is probably true for 99% of
current NixOS ZFS users). Once host id configuration becomes mandatory when
using ZFS in NixOS and we are sure that most users have updated their
configurations and rebooted at least once, we should disable force-import by
default. Probably, this shouldn't be done before the next stable release.
WARNING: This commit may change the order in which your non-ZFS vs ZFS
filesystems are mounted. To avoid this problem (now or in the future)
it is recommended that you set the 'mountpoint' property of your ZFS
filesystems to 'legacy', and that you manage them using
config.fileSystems, just like any other non-ZFS filesystem is usually
managed in NixOS.
2014-10-22 18:17:21 +01:00
|
|
|
};
|
|
|
|
|
2014-10-22 17:48:57 +01:00
|
|
|
systemd.targets."zfs".wantedBy = [ "multi-user.target" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf enableAutoSnapshots {
|
2015-03-20 02:49:25 +00:00
|
|
|
systemd.services = let
|
|
|
|
descr = name: if name == "frequent" then "15 mins"
|
|
|
|
else if name == "hourly" then "hour"
|
|
|
|
else if name == "daily" then "day"
|
|
|
|
else if name == "weekly" then "week"
|
|
|
|
else if name == "monthly" then "month"
|
|
|
|
else throw "unknown snapshot name";
|
|
|
|
numSnapshots = name: builtins.getAttr name cfgSnapshots;
|
|
|
|
in builtins.listToAttrs (map (snapName:
|
|
|
|
{
|
|
|
|
name = "zfs-snapshot-${snapName}";
|
|
|
|
value = {
|
|
|
|
description = "ZFS auto-snapshotting every ${descr snapName}";
|
|
|
|
after = [ "zfs-import.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} ${snapName} ${toString (numSnapshots snapName)}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
};
|
|
|
|
}) snapshotNames);
|
|
|
|
|
|
|
|
systemd.timers = let
|
2018-04-11 15:42:28 +01:00
|
|
|
timer = name: if name == "frequent" then "*:0,15,30,45" else name;
|
2015-03-20 02:49:25 +00:00
|
|
|
in builtins.listToAttrs (map (snapName:
|
|
|
|
{
|
|
|
|
name = "zfs-snapshot-${snapName}";
|
|
|
|
value = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = timer snapName;
|
|
|
|
Persistent = "yes";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}) snapshotNames);
|
2014-01-22 00:11:51 +00:00
|
|
|
})
|
2017-03-02 16:13:54 +00:00
|
|
|
|
|
|
|
(mkIf enableAutoScrub {
|
|
|
|
systemd.services.zfs-scrub = {
|
|
|
|
description = "ZFS pools scrubbing";
|
|
|
|
after = [ "zfs-import.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
script = ''
|
|
|
|
${packages.zfsUser}/bin/zpool scrub ${
|
|
|
|
if cfgScrub.pools != [] then
|
|
|
|
(concatStringsSep " " cfgScrub.pools)
|
|
|
|
else
|
|
|
|
"$(${packages.zfsUser}/bin/zpool list -H -o name)"
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.zfs-scrub = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
2019-01-14 19:55:37 +00:00
|
|
|
after = [ "multi-user.target" ]; # Apparently scrubbing before boot is complete hangs the system? #53583
|
2017-03-02 16:13:54 +00:00
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfgScrub.interval;
|
|
|
|
Persistent = "yes";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
2014-01-22 00:11:51 +00:00
|
|
|
];
|
2012-12-04 18:17:54 +00:00
|
|
|
}
|