From 3b0fa60a9865ca4887266fcfb7e467d8b8966d38 Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Wed, 22 Oct 2014 18:48:57 +0200 Subject: [PATCH 1/5] zfs: Enable systemd Also remove custom zfs services from NixOS. This makes NixOS more aligned with upstream. More importantly, it prepares the way for NixOS to use ZED (the ZFS event daemon). This service will automatically be enabled but it is not possible to configure it via configuration.nix yet. --- nixos/modules/tasks/filesystems/zfs.nix | 38 ++++++------------------- pkgs/os-specific/linux/zfs/default.nix | 26 +++++++++++------ pkgs/os-specific/linux/zfs/git.nix | 26 +++++++++++------ 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index eb72bfba33c0..43816db51fe1 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -152,40 +152,20 @@ in zfsSupport = true; }; - systemd.services."zpool-import" = { - description = "Import zpools"; - after = [ "systemd-udev-settle.service" ]; - wantedBy = [ "local-fs.target" ]; - serviceConfig = { - Type = "oneshot"; - RemainAfterExit = true; - ExecStart = "${zfsPkg}/sbin/zpool import -f -a"; - }; - restartIfChanged = false; - }; - - systemd.services."zfs-mount" = { - description = "Mount ZFS Volumes"; - after = [ "zpool-import.service" ]; - wantedBy = [ "local-fs.target" ]; - serviceConfig = { - Type = "oneshot"; - RemainAfterExit = true; - ExecStart = "${zfsPkg}/sbin/zfs mount -a"; - ExecStop = "${zfsPkg}/sbin/zfs umount -a"; - }; - restartIfChanged = false; - }; + environment.etc."zfs/zed.d".source = "${zfsPkg}/etc/zfs/zed.d/*"; system.fsPackages = [ zfsPkg ]; # XXX: needed? zfs doesn't have (need) a fsck environment.systemPackages = [ zfsPkg ]; services.udev.packages = [ zfsPkg ]; # to hook zvol naming, etc. + systemd.packages = [ zfsPkg ]; + + systemd.targets."zfs".wantedBy = [ "multi-user.target" ]; }) (mkIf enableAutoSnapshots { systemd.services."zfs-snapshot-frequent" = { description = "ZFS auto-snapshotting every 15 mins"; - after = [ "zpool-import.service" ]; + after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} frequent ${toString cfgSnapshots.frequent}"; @@ -196,7 +176,7 @@ in systemd.services."zfs-snapshot-hourly" = { description = "ZFS auto-snapshotting every hour"; - after = [ "zpool-import.service" ]; + after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} hourly ${toString cfgSnapshots.hourly}"; @@ -207,7 +187,7 @@ in systemd.services."zfs-snapshot-daily" = { description = "ZFS auto-snapshotting every day"; - after = [ "zpool-import.service" ]; + after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} daily ${toString cfgSnapshots.daily}"; @@ -218,7 +198,7 @@ in systemd.services."zfs-snapshot-weekly" = { description = "ZFS auto-snapshotting every week"; - after = [ "zpool-import.service" ]; + after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} weekly ${toString cfgSnapshots.weekly}"; @@ -229,7 +209,7 @@ in systemd.services."zfs-snapshot-monthly" = { description = "ZFS auto-snapshotting every month"; - after = [ "zpool-import.service" ]; + after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} monthly ${toString cfgSnapshots.monthly}"; diff --git a/pkgs/os-specific/linux/zfs/default.nix b/pkgs/os-specific/linux/zfs/default.nix index f0f260746d54..fc980286b9b7 100644 --- a/pkgs/os-specific/linux/zfs/default.nix +++ b/pkgs/os-specific/linux/zfs/default.nix @@ -20,24 +20,32 @@ stdenv.mkDerivation { NIX_CFLAGS_LINK = "-lgcc_s"; preConfigure = '' - ./autogen.sh + substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs" + substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs" + substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount" + substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount" + substituteInPlace ./udev/rules.d/* --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id" + substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest" + substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb" + substituteInPlace ./config/user-systemd.m4 --replace "/usr/lib/modules-load.d" "$out/etc/modules-load.d" + substituteInPlace ./config/zfs-build.m4 --replace "\$sysconfdir/init.d" "$out/etc/init.d" + substituteInPlace ./etc/zfs/Makefile.am --replace "\$(sysconfdir)" "$out/etc" + substituteInPlace ./cmd/zed/Makefile.am --replace "\$(sysconfdir)" "$out/etc" - substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs" - substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs" - substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount" - substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount" - substituteInPlace ./udev/rules.d/* --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id" - substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest" - substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb" + ./autogen.sh ''; configureFlags = [ - "--disable-systemd" + "--enable-systemd" "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source" "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build" "--with-spl=${spl}/libexec/spl" "--with-dracutdir=$(out)/lib/dracut" "--with-udevdir=$(out)/lib/udev" + "--with-systemdunitdir=$(out)/etc/systemd/system" + "--with-systemdpresetdir=$(out)/etc/systemd/system-preset" + "--sysconfdir=/etc" + "--localstatedir=/var" ]; enableParallelBuilding = true; diff --git a/pkgs/os-specific/linux/zfs/git.nix b/pkgs/os-specific/linux/zfs/git.nix index e88cb9d6130a..01e72d0af6db 100644 --- a/pkgs/os-specific/linux/zfs/git.nix +++ b/pkgs/os-specific/linux/zfs/git.nix @@ -17,24 +17,32 @@ stdenv.mkDerivation { NIX_CFLAGS_LINK = "-lgcc_s"; preConfigure = '' - ./autogen.sh + substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs" + substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs" + substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount" + substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount" + substituteInPlace ./udev/rules.d/* --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id" + substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest" + substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb" + substituteInPlace ./config/user-systemd.m4 --replace "/usr/lib/modules-load.d" "$out/etc/modules-load.d" + substituteInPlace ./config/zfs-build.m4 --replace "\$sysconfdir/init.d" "$out/etc/init.d" + substituteInPlace ./etc/zfs/Makefile.am --replace "\$(sysconfdir)" "$out/etc" + substituteInPlace ./cmd/zed/Makefile.am --replace "\$(sysconfdir)" "$out/etc" - substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs" - substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs" - substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount" - substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount" - substituteInPlace ./udev/rules.d/* --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id" - substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest" - substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb" + ./autogen.sh ''; configureFlags = [ - "--disable-systemd" + "--enable-systemd" "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source" "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build" "--with-spl=${spl_git}/libexec/spl" "--with-dracutdir=$(out)/lib/dracut" "--with-udevdir=$(out)/lib/udev" + "--with-systemdunitdir=$(out)/etc/systemd/system" + "--with-systemdpresetdir=$(out)/etc/systemd/system-preset" + "--sysconfdir=/etc" + "--localstatedir=/var" ]; enableParallelBuilding = true; From 1fea5866aea0aeed2c22dcee20e3fd3f088de3a5 Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Wed, 12 Nov 2014 21:36:26 +0100 Subject: [PATCH 2/5] Add `unique` list function It removes duplicate elements from a list. --- lib/lists.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/lists.nix b/lib/lists.nix index 566ee89c95bc..d57c4893daa8 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -223,4 +223,14 @@ rec { crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f]; + # Remove duplicate elements from the list + unique = list: + if list == [] then + [] + else + let + x = head list; + xs = unique (drop 1 list); + in [x] ++ remove x xs; + } From 12e77fdc3f6f223be1a4d5c88d6c79bff63ae70c Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Wed, 22 Oct 2014 19:17:21 +0200 Subject: [PATCH 3/5] 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. --- nixos/modules/tasks/filesystems/zfs.nix | 180 ++++++++++++++++++++---- pkgs/os-specific/linux/zfs/default.nix | 13 +- pkgs/os-specific/linux/zfs/git.nix | 11 ++ 3 files changed, 179 insertions(+), 25 deletions(-) diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index 43816db51fe1..aecda3e4101c 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -1,11 +1,10 @@ -{ config, lib, pkgs, ... }: +{ config, lib, pkgs, utils, ... }: # # todo: # - crontab for scrubs, etc # - zfs tunables -# - /etc/zfs/zpool.cache handling - +with utils; with lib; let @@ -31,6 +30,20 @@ let zfsAutoSnap = "${autosnapPkg}/bin/zfs-auto-snapshot"; + datasetToPool = x: elemAt (splitString "/" x) 0; + + fsToPool = fs: datasetToPool fs.device; + + zfsFilesystems = filter (x: x.fsType == "zfs") (attrValues config.fileSystems); + + isRoot = fs: fs.neededForBoot || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ]; + + allPools = unique ((map fsToPool zfsFilesystems) ++ cfgZfs.extraPools); + + rootPools = unique (map fsToPool (filter isRoot zfsFilesystems)); + + dataPools = unique (filter (pool: !(elem pool rootPools)) allPools); + in { @@ -42,24 +55,82 @@ in 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. + ZFS uses a system's hostid to determine if a storage pool (zpool) has been + imported on this system, and can thus be used again without reimporting. Unfortunately, this hostid can change under linux from boot to boot (by changing network adapters, for instance). Specify a unique 32 bit hostid in hex here for zfs to prevent getting a random hostid between boots and having to - manually import pools. + manually and forcibly reimport pools. ''; }; - 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. - ''; + 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. + ''; + }; + + 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 legacy and add the ZFS filesystems to + NixOS's 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. + ''; + }; + + forceImportRoot = mkOption { + type = types.bool; + default = true; + example = false; + 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 false and NixOS subsequently fails to + boot because it cannot import the root pool, you should boot with the + zfs_force=1 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; + example = false; + 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 false 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. + ''; + }; }; services.zfs.autoSnapshot = { @@ -124,6 +195,13 @@ in config = mkMerge [ (mkIf enableZfs { + assertions = [ + { + assertion = !cfgZfs.forceImportAll || cfgZfs.forceImportRoot; + message = "If you enable boot.zfs.forceImportAll, you must also enable boot.zfs.forceImportRoot"; + } + ]; + boot = { kernelModules = [ "spl" "zfs" ] ; extraModulePackages = [ splPkg zfsPkg ]; @@ -142,10 +220,20 @@ in cp -pdv ${zfsPkg}/lib/lib*.so* $out/lib cp -pdv ${pkgs.zlib}/lib/lib*.so* $out/lib ''; - postDeviceCommands = - '' - zpool import -f -a - ''; + 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 + ''] ++ (map (pool: '' + echo "importing root ZFS pool \"${pool}\"..." + zpool import -N $ZFS_FORCE "${pool}" + '') rootPools)); }; boot.loader.grub = mkIf inInitrd { @@ -159,13 +247,57 @@ in services.udev.packages = [ zfsPkg ]; # to hook zvol naming, etc. systemd.packages = [ zfsPkg ]; + systemd.services = let + getPoolFilesystems = pool: + filter (x: x.fsType == "zfs" && (fsToPool x) == pool) (attrValues config.fileSystems); + + 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; + }; + script = '' + zpool_cmd="${zfsPkg}/sbin/zpool" + ("$zpool_cmd" list "${pool}" >/dev/null) || "$zpool_cmd" import -N ${optionalString cfgZfs.forceImportAll "-f"} "${pool}" + ''; + }; + in listToAttrs (map createImportService dataPools) // { + "zfs-mount" = { after = [ "systemd-modules-load.service" ]; }; + "zfs-share" = { after = [ "systemd-modules-load.service" ]; }; + "zed" = { after = [ "systemd-modules-load.service" ]; }; + }; + + systemd.targets."zfs-import" = + let + services = map (pool: "zfs-import-${pool}.service") dataPools; + in + { + requires = services; + after = services; + }; + systemd.targets."zfs".wantedBy = [ "multi-user.target" ]; }) (mkIf enableAutoSnapshots { systemd.services."zfs-snapshot-frequent" = { description = "ZFS auto-snapshotting every 15 mins"; - after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; + after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} frequent ${toString cfgSnapshots.frequent}"; @@ -176,7 +308,7 @@ in systemd.services."zfs-snapshot-hourly" = { description = "ZFS auto-snapshotting every hour"; - after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; + after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} hourly ${toString cfgSnapshots.hourly}"; @@ -187,7 +319,7 @@ in systemd.services."zfs-snapshot-daily" = { description = "ZFS auto-snapshotting every day"; - after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; + after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} daily ${toString cfgSnapshots.daily}"; @@ -198,7 +330,7 @@ in systemd.services."zfs-snapshot-weekly" = { description = "ZFS auto-snapshotting every week"; - after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; + after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} weekly ${toString cfgSnapshots.weekly}"; @@ -209,7 +341,7 @@ in systemd.services."zfs-snapshot-monthly" = { description = "ZFS auto-snapshotting every month"; - after = [ "zfs-import-scan.service" "zfs-import-cache.service" ]; + after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${zfsAutoSnap} monthly ${toString cfgSnapshots.monthly}"; diff --git a/pkgs/os-specific/linux/zfs/default.nix b/pkgs/os-specific/linux/zfs/default.nix index fc980286b9b7..a5b8429c03a0 100644 --- a/pkgs/os-specific/linux/zfs/default.nix +++ b/pkgs/os-specific/linux/zfs/default.nix @@ -50,12 +50,23 @@ stdenv.mkDerivation { enableParallelBuilding = true; + # Remove provided services as they are buggy + postInstall = '' + rm $out/etc/systemd/system/zfs-import-*.service + + sed -i '/zfs-import-scan.service/d' $out/etc/systemd/system/* + + for i in $out/etc/systemd/system/*; do + substituteInPlace $i --replace "zfs-import-cache.service" "zfs-import.target" + done + ''; + meta = { description = "ZFS Filesystem Linux Kernel module"; longDescription = '' ZFS is a filesystem that combines a logical volume manager with a Copy-On-Write filesystem with data integrity detection and repair, - snapshotting, cloning, block devices, deduplication, and more. + snapshotting, cloning, block devices, deduplication, and more. ''; homepage = http://zfsonlinux.org/; license = stdenv.lib.licenses.cddl; diff --git a/pkgs/os-specific/linux/zfs/git.nix b/pkgs/os-specific/linux/zfs/git.nix index 01e72d0af6db..e5002bbbb3f3 100644 --- a/pkgs/os-specific/linux/zfs/git.nix +++ b/pkgs/os-specific/linux/zfs/git.nix @@ -47,6 +47,17 @@ stdenv.mkDerivation { enableParallelBuilding = true; + # Remove provided services as they are buggy + postInstall = '' + rm $out/etc/systemd/system/zfs-import-*.service + + sed -i '/zfs-import-scan.service/d' $out/etc/systemd/system/* + + for i in $out/etc/systemd/system/*; do + substituteInPlace $i --replace "zfs-import-cache.service" "zfs-import.target" + done + ''; + meta = { description = "ZFS Filesystem Linux Kernel module"; longDescription = '' From e9affb4274a4d72d6aa1fde9d34862dfd93173cc Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Thu, 23 Oct 2014 04:59:06 +0200 Subject: [PATCH 4/5] 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). --- .../installer/cd-dvd/installation-cd-base.nix | 3 ++ nixos/modules/system/boot/stage-1-init.sh | 3 ++ nixos/modules/system/boot/stage-1.nix | 9 ++++ nixos/modules/tasks/filesystems/zfs.nix | 20 ++------ nixos/modules/tasks/network-interfaces.nix | 50 ++++++++++++++++--- pkgs/stdenv/generic/default.nix | 1 + 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/nixos/modules/installer/cd-dvd/installation-cd-base.nix b/nixos/modules/installer/cd-dvd/installation-cd-base.nix index 89d50b7460c9..e453a629f74f 100644 --- a/nixos/modules/installer/cd-dvd/installation-cd-base.nix +++ b/nixos/modules/installer/cd-dvd/installation-cd-base.nix @@ -45,6 +45,9 @@ with lib; # Add support for cow filesystems and their utilities boot.supportedFilesystems = [ /* "zfs" */ "btrfs" ]; + # Configure host id for ZFS to work + networking.hostId = "8425e349"; + # Allow the user to log in as root without a password. users.extraUsers.root.initialHashedPassword = ""; } diff --git a/nixos/modules/system/boot/stage-1-init.sh b/nixos/modules/system/boot/stage-1-init.sh index 5a9beeeafa1d..7ecc95aa079c 100644 --- a/nixos/modules/system/boot/stage-1-init.sh +++ b/nixos/modules/system/boot/stage-1-init.sh @@ -122,6 +122,9 @@ for o in $(cat /proc/cmdline); do esac done +# Set hostid before modules are loaded. +# This is needed by the spl/zfs modules. +@setHostId@ # Load the required kernel modules. mkdir -p /lib diff --git a/nixos/modules/system/boot/stage-1.nix b/nixos/modules/system/boot/stage-1.nix index 33c9a70eb1b2..45229e871ab6 100644 --- a/nixos/modules/system/boot/stage-1.nix +++ b/nixos/modules/system/boot/stage-1.nix @@ -188,6 +188,15 @@ let fsInfo = let f = fs: [ fs.mountPoint (if fs.device != null then fs.device else "/dev/disk/by-label/${fs.label}") fs.fsType fs.options ]; in pkgs.writeText "initrd-fsinfo" (concatStringsSep "\n" (concatMap f fileSystems)); + + setHostId = optionalString (config.networking.hostId != null) '' + hi="${config.networking.hostId}" + ${if pkgs.stdenv.isBigEndian then '' + echo -ne "\x''${hi:0:2}\x''${hi:2:2}\x''${hi:4:2}\x''${hi:6:2}" > /etc/hostid + '' else '' + echo -ne "\x''${hi:6:2}\x''${hi:4:2}\x''${hi:2:2}\x''${hi:0:2}" > /etc/hostid + ''} + ''; }; diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index aecda3e4101c..ab5942b79453 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -51,19 +51,6 @@ in ###### interface options = { - boot.spl.hostid = mkOption { - default = ""; - example = "0xdeadbeef"; - description = '' - ZFS uses a system's hostid to determine if a storage pool (zpool) has been - imported on this system, and can thus be used again without reimporting. - Unfortunately, this hostid can change under linux from boot to boot (by - changing network adapters, for instance). Specify a unique 32 bit hostid in - hex here for zfs to prevent getting a random hostid between boots and having to - manually and forcibly reimport pools. - ''; - }; - boot.zfs = { useGit = mkOption { type = types.bool; @@ -196,6 +183,10 @@ in config = mkMerge [ (mkIf enableZfs { assertions = [ + { + assertion = config.networking.hostId != null; + message = "ZFS requires config.networking.hostId to be set"; + } { assertion = !cfgZfs.forceImportAll || cfgZfs.forceImportRoot; message = "If you enable boot.zfs.forceImportAll, you must also enable boot.zfs.forceImportRoot"; @@ -205,9 +196,6 @@ in boot = { kernelModules = [ "spl" "zfs" ] ; extraModulePackages = [ splPkg zfsPkg ]; - extraModprobeConfig = mkIf (cfgSpl.hostid != "") '' - options spl spl_hostid=${cfgSpl.hostid} - ''; }; boot.initrd = mkIf inInitrd { diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix index 22b52f77b145..9579eaa77d06 100644 --- a/nixos/modules/tasks/network-interfaces.nix +++ b/nixos/modules/tasks/network-interfaces.nix @@ -189,6 +189,10 @@ let }; + hexChars = stringToCharacters "0123456789abcdef"; + + isHexString = s: all (c: elem c hexChars) (stringToCharacters (toLower s)); + in { @@ -205,6 +209,20 @@ in ''; }; + networking.hostId = mkOption { + default = null; + example = "4e98920d"; + type = types.nullOr types.str; + description = '' + The 32-bit host ID of the machine, formatted as 8 hexadecimal characters. + + You should try to make this ID unique among your machines. You can + generate a random 32-bit ID using the following command: + + head -c4 /dev/urandom | od -A none -t x4 + ''; + }; + networking.enableIPv6 = mkOption { default = true; description = '' @@ -513,10 +531,15 @@ in config = { assertions = - flip map interfaces (i: { + (flip map interfaces (i: { assertion = i.subnetMask == null; message = "The networking.interfaces.${i.name}.subnetMask option is defunct. Use prefixLength instead."; - }); + })) ++ [ + { + assertion = cfg.hostId == null || (stringLength cfg.hostId == 8 && isHexString cfg.hostId); + message = "Invalid value given to the networking.hostId option."; + } + ]; boot.kernelModules = [ ] ++ optional cfg.enableIPv6 "ipv6" @@ -872,14 +895,29 @@ in # clear it if it's not configured in the NixOS configuration, # since it may have been set by dhcpcd in the meantime. system.activationScripts.hostname = - optionalString (config.networking.hostName != "") '' - hostname "${config.networking.hostName}" + optionalString (cfg.hostName != "") '' + hostname "${cfg.hostName}" ''; system.activationScripts.domain = - optionalString (config.networking.domain != "") '' - domainname "${config.networking.domain}" + optionalString (cfg.domain != "") '' + domainname "${cfg.domain}" ''; + environment.etc = mkIf (cfg.hostId != null) + [ + { + target = "hostid"; + source = pkgs.runCommand "gen-hostid" {} '' + hi="${cfg.hostId}" + ${if pkgs.stdenv.isBigEndian then '' + echo -ne "\x''${hi:0:2}\x''${hi:2:2}\x''${hi:4:2}\x''${hi:6:2}" > $out + '' else '' + echo -ne "\x''${hi:6:2}\x''${hi:4:2}\x''${hi:2:2}\x''${hi:0:2}" > $out + ''} + ''; + } + ]; + services.udev.extraRules = '' KERNEL=="tun", TAG+="systemd" diff --git a/pkgs/stdenv/generic/default.nix b/pkgs/stdenv/generic/default.nix index 038afc585c28..8b269ffb5252 100644 --- a/pkgs/stdenv/generic/default.nix +++ b/pkgs/stdenv/generic/default.nix @@ -187,6 +187,7 @@ let isArm = system == "armv5tel-linux" || system == "armv6l-linux" || system == "armv7l-linux"; + isBigEndian = system == "powerpc-linux"; # For convenience, bring in the library functions in lib/ so # packages don't have to do that themselves. From 7a7bf8368e25d13b1f2e915514c804728b7648ef Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Sat, 1 Nov 2014 18:27:54 +0100 Subject: [PATCH 5/5] nixos-generate-config: Configure networking.hostId by default The host id value gets generated by reading a 32-bit value from /dev/urandom. This makes programs that rely on a correct host id more reliable. It also makes using ZFS more seamless, as you don't need to configure the hostId manually; instead, it becomes part of your config from the moment you install NixOS. --- nixos/modules/installer/tools/nixos-generate-config.pl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nixos/modules/installer/tools/nixos-generate-config.pl b/nixos/modules/installer/tools/nixos-generate-config.pl index e8f100d64984..9abc1b4cc1b5 100644 --- a/nixos/modules/installer/tools/nixos-generate-config.pl +++ b/nixos/modules/installer/tools/nixos-generate-config.pl @@ -476,6 +476,14 @@ EOF EOF } + # Generate a random 32-bit value to use as the host id + open my $rnd, "<", "/dev/urandom" or die $!; + read $rnd, $hostIdBin, 4; + close $rnd; + + # Convert the 32-bit value to a hex string + my $hostIdHex = unpack("H*", $hostIdBin); + write_file($fn, <