Commit 0f69b960 authored by Andrew Marshall's avatar Andrew Marshall
Browse files

nixos/zfs: teach to override devNodes per pool

This may be useful if some pools use LUKS or some other intermediate
layer, while others do not.
parent 838f2f70
Loading
Loading
Loading
Loading
+25 −6
Original line number Diff line number Diff line
{ config, lib, pkgs, utils, ... }:
{ config, lib, options, pkgs, utils, ... }:
#
# TODO: zfs tunables

@@ -56,7 +56,9 @@ let
  # 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}: ''
  importLib = {zpoolCmd, awkCmd, pool}: let
    devNodes = if pool != null && cfgZfs.pools ? ${pool} then cfgZfs.pools.${pool}.devNodes else cfgZfs.devNodes;
  in ''
    # shellcheck disable=SC2013
    for o in $(cat /proc/cmdline); do
      case $o in
@@ -67,7 +69,7 @@ let
    done
    poolReady() {
      pool="$1"
      state="$("${zpoolCmd}" import -d "${cfgZfs.devNodes}" 2>/dev/null | "${awkCmd}" "/pool: $pool/ { found = 1 }; /state:/ { if (found == 1) { print \$2; exit } }; END { if (found == 0) { print \"MISSING\" } }")"
      state="$("${zpoolCmd}" import -d "${devNodes}" 2>/dev/null | "${awkCmd}" "/pool: $pool/ { found = 1 }; /state:/ { if (found == 1) { print \$2; exit } }; END { if (found == 0) { print \"MISSING\" } }")"
      if [[ "$state" = "ONLINE" ]]; then
        return 0
      else
@@ -82,7 +84,7 @@ let
    poolImport() {
      pool="$1"
      # shellcheck disable=SC2086
      "${zpoolCmd}" import -d "${cfgZfs.devNodes}" -N $ZFS_FORCE "$pool"
      "${zpoolCmd}" import -d "${devNodes}" -N $ZFS_FORCE "$pool"
    }
  '';

@@ -143,7 +145,7 @@ let
        # See comments at importLib definition.
        zpoolCmd = "${cfgZfs.package}/sbin/zpool";
        awkCmd = "${pkgs.gawk}/bin/awk";
        inherit cfgZfs;
        inherit pool;
      }) + ''
        if ! poolImported "${pool}"; then
          echo -n "importing ZFS pool \"${pool}\"..."
@@ -329,6 +331,23 @@ in
        '';
      };

      pools = lib.mkOption {
        type = lib.types.attrsOf (lib.types.submodule {
          options = {
            devNodes = lib.mkOption {
              type = lib.types.path;
              default = cfgZfs.devNodes;
              defaultText = "config.boot.zfs.devNodes";
              description = options.boot.zfs.devNodes.description;
            };
          };
        });
        default = { };
        description = ''
          Configuration for individual pools to override global defaults.
        '';
      };

      removeLinuxDRM = lib.mkOption {
        type = lib.types.bool;
        default = false;
@@ -610,7 +629,7 @@ in
            # See comments at importLib definition.
            zpoolCmd = "zpool";
            awkCmd = "awk";
            inherit cfgZfs;
            pool = null;
          })] ++ (map (pool: ''
            echo -n "importing root ZFS pool \"${pool}\"..."
            # Loop across the import until it succeeds, because the devices needed may not be discovered yet.