2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2012-12-04 19:57:59 +00:00
|
|
|
#
|
|
|
|
# todo:
|
|
|
|
# - crontab for scrubs, etc
|
|
|
|
# - zfs tunables
|
|
|
|
# - /etc/zfs/zpool.cache handling
|
|
|
|
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2012-12-04 18:17:54 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2012-12-06 23:12:10 +00:00
|
|
|
cfgSpl = config.boot.spl;
|
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;
|
|
|
|
|
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;
|
|
|
|
enableZfs = inInitrd || inSystem || enableAutoSnapshots;
|
|
|
|
|
2012-12-04 18:17:54 +00:00
|
|
|
kernel = config.boot.kernelPackages;
|
|
|
|
|
2014-03-22 17:27:23 +00:00
|
|
|
splPkg = if cfgZfs.useGit then kernel.spl_git else kernel.spl;
|
|
|
|
zfsPkg = if cfgZfs.useGit then kernel.zfs_git else kernel.zfs;
|
|
|
|
|
2014-01-22 00:11:51 +00:00
|
|
|
autosnapPkg = pkgs.zfstools.override {
|
2014-03-22 17:27:23 +00:00
|
|
|
zfs = zfsPkg;
|
2014-01-22 00:11:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
zfsAutoSnap = "${autosnapPkg}/bin/zfs-auto-snapshot";
|
|
|
|
|
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 = {
|
|
|
|
boot.spl.hostid = mkOption {
|
2012-12-04 19:57:59 +00:00
|
|
|
default = "";
|
|
|
|
example = "0xdeadbeef";
|
|
|
|
description = ''
|
|
|
|
ZFS uses a system's hostid to determine if a storage pool (zpool) is
|
|
|
|
native to this system, and should thus be imported automatically.
|
|
|
|
Unfortunately, this hostid can change under linux from boot to boot (by
|
2013-08-10 21:07:13 +00:00
|
|
|
changing network adapters, for instance). Specify a unique 32 bit hostid in
|
2012-12-04 19:57:59 +00:00
|
|
|
hex here for zfs to prevent getting a random hostid between boots and having to
|
|
|
|
manually import pools.
|
|
|
|
'';
|
|
|
|
};
|
2012-12-04 18:17:54 +00:00
|
|
|
|
2014-03-22 17:27:23 +00:00
|
|
|
boot.zfs.useGit = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = ''
|
|
|
|
Use the git version of the SPL and ZFS packages.
|
|
|
|
Note that these are unreleased versions, with less testing, and therefore
|
|
|
|
may be more unstable.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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
|
|
|
};
|
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 {
|
|
|
|
boot = {
|
|
|
|
kernelModules = [ "spl" "zfs" ] ;
|
2014-03-22 17:27:23 +00:00
|
|
|
extraModulePackages = [ splPkg zfsPkg ];
|
2014-01-22 00:11:51 +00:00
|
|
|
extraModprobeConfig = mkIf (cfgSpl.hostid != "") ''
|
|
|
|
options spl spl_hostid=${cfgSpl.hostid}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd = mkIf inInitrd {
|
2014-08-31 16:18:13 +00:00
|
|
|
kernelModules = [ "spl" "zfs" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
extraUtilsCommands =
|
|
|
|
''
|
2014-03-22 17:27:23 +00:00
|
|
|
cp -v ${zfsPkg}/sbin/zfs $out/bin
|
|
|
|
cp -v ${zfsPkg}/sbin/zdb $out/bin
|
|
|
|
cp -v ${zfsPkg}/sbin/zpool $out/bin
|
|
|
|
cp -pdv ${zfsPkg}/lib/lib*.so* $out/lib
|
2014-01-22 00:11:51 +00:00
|
|
|
cp -pdv ${pkgs.zlib}/lib/lib*.so* $out/lib
|
|
|
|
'';
|
|
|
|
postDeviceCommands =
|
|
|
|
''
|
|
|
|
zpool import -f -a
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-08-31 16:18:13 +00:00
|
|
|
boot.loader.grub = mkIf inInitrd {
|
|
|
|
zfsSupport = true;
|
|
|
|
};
|
|
|
|
|
2014-10-22 16:48:57 +00:00
|
|
|
environment.etc."zfs/zed.d".source = "${zfsPkg}/etc/zfs/zed.d/*";
|
2013-06-07 16:34:46 +00:00
|
|
|
|
2014-03-22 17:27:23 +00:00
|
|
|
system.fsPackages = [ zfsPkg ]; # XXX: needed? zfs doesn't have (need) a fsck
|
|
|
|
environment.systemPackages = [ zfsPkg ];
|
|
|
|
services.udev.packages = [ zfsPkg ]; # to hook zvol naming, etc.
|
2014-10-22 16:48:57 +00:00
|
|
|
systemd.packages = [ zfsPkg ];
|
|
|
|
|
|
|
|
systemd.targets."zfs".wantedBy = [ "multi-user.target" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf enableAutoSnapshots {
|
|
|
|
systemd.services."zfs-snapshot-frequent" = {
|
|
|
|
description = "ZFS auto-snapshotting every 15 mins";
|
2014-10-22 16:48:57 +00:00
|
|
|
after = [ "zfs-import-scan.service" "zfs-import-cache.service" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} frequent ${toString cfgSnapshots.frequent}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
startAt = "*:15,30,45";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services."zfs-snapshot-hourly" = {
|
|
|
|
description = "ZFS auto-snapshotting every hour";
|
2014-10-22 16:48:57 +00:00
|
|
|
after = [ "zfs-import-scan.service" "zfs-import-cache.service" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} hourly ${toString cfgSnapshots.hourly}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
startAt = "hourly";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services."zfs-snapshot-daily" = {
|
|
|
|
description = "ZFS auto-snapshotting every day";
|
2014-10-22 16:48:57 +00:00
|
|
|
after = [ "zfs-import-scan.service" "zfs-import-cache.service" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} daily ${toString cfgSnapshots.daily}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
startAt = "daily";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services."zfs-snapshot-weekly" = {
|
|
|
|
description = "ZFS auto-snapshotting every week";
|
2014-10-22 16:48:57 +00:00
|
|
|
after = [ "zfs-import-scan.service" "zfs-import-cache.service" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} weekly ${toString cfgSnapshots.weekly}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
startAt = "weekly";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services."zfs-snapshot-monthly" = {
|
|
|
|
description = "ZFS auto-snapshotting every month";
|
2014-10-22 16:48:57 +00:00
|
|
|
after = [ "zfs-import-scan.service" "zfs-import-cache.service" ];
|
2014-01-22 00:11:51 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${zfsAutoSnap} monthly ${toString cfgSnapshots.monthly}";
|
|
|
|
};
|
|
|
|
restartIfChanged = false;
|
|
|
|
startAt = "monthly";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2012-12-04 18:17:54 +00:00
|
|
|
}
|