From e9affb4274a4d72d6aa1fde9d34862dfd93173cc Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Thu, 23 Oct 2014 04:59:06 +0200 Subject: [PATCH] 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 89d50b7460c..e453a629f74 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 5a9beeeafa1..7ecc95aa079 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 33c9a70eb1b..45229e871ab 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 aecda3e4101..ab5942b7945 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 22b52f77b14..9579eaa77d0 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 038afc585c2..8b269ffb525 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.