From 9b56c1afdb56337b79769edcefbd32218c0181ba Mon Sep 17 00:00:00 2001 From: Michon van Dooren Date: Sun, 17 Nov 2024 15:17:59 +0100 Subject: [PATCH] Add support for ZFS spare, log & dedup vdevs --- example/zfs-with-vdevs.nix | 73 ++++++++++++++++++++++++++++++++++---- lib/tests.nix | 2 +- lib/types/zpool.nix | 49 +++++++++++++++++++++---- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/example/zfs-with-vdevs.nix b/example/zfs-with-vdevs.nix index 2161f5c..5ee4016 100644 --- a/example/zfs-with-vdevs.nix +++ b/example/zfs-with-vdevs.nix @@ -43,9 +43,57 @@ }; }; }; - z = { + spare = { type = "disk"; - device = "/dev/sdz"; + device = "/dev/vdc"; + content = { + type = "gpt"; + partitions = { + zfs = { + size = "100%"; + content = { + type = "zfs"; + pool = "zroot"; + }; + }; + }; + }; + }; + log = { + type = "disk"; + device = "/dev/vdd"; + content = { + type = "gpt"; + partitions = { + zfs = { + size = "100%"; + content = { + type = "zfs"; + pool = "zroot"; + }; + }; + }; + }; + }; + dedup = { + type = "disk"; + device = "/dev/vde"; + content = { + type = "gpt"; + partitions = { + zfs = { + size = "100%"; + content = { + type = "zfs"; + pool = "zroot"; + }; + }; + }; + }; + }; + special = { + type = "disk"; + device = "/dev/vdf"; content = { type = "gpt"; partitions = { @@ -61,7 +109,7 @@ }; cache = { type = "disk"; - device = "/dev/vdc"; + device = "/dev/vdg"; content = { type = "gpt"; partitions = { @@ -91,9 +139,22 @@ ]; } ]; - special = { - members = [ "z" ]; - }; + spare = [ "spare" ]; + log = [ + { + members = [ "log" ]; + } + ]; + dedup = [ + { + members = [ "dedup" ]; + } + ]; + special = [ + { + members = [ "special" ]; + } + ]; cache = [ "cache" ]; }; }; diff --git a/lib/tests.nix b/lib/tests.nix index 25b6a59..f8a1751 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -38,7 +38,7 @@ let }; # list of devices generated inside qemu - devices = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf" ]; + devices = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf" "/dev/vdg" "/dev/vdh" ]; # This is the test generator for a disko test makeDiskoTest = diff --git a/lib/types/zpool.nix b/lib/types/zpool.nix index 46bab77..453bff1 100644 --- a/lib/types/zpool.nix +++ b/lib/types/zpool.nix @@ -68,11 +68,38 @@ in members = [ "x" "y" "/dev/sda1" ]; }]; }; - special = lib.mkOption { - type = lib.types.nullOr vdev; - default = null; + spare = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; description = '' - A vdev definition for a special device. See + A list of devices to use as hot spares. See + https://openzfs.github.io/openzfs-docs/man/master/7/zpoolconcepts.7.html#Hot_Spares + for details. + ''; + }; + log = lib.mkOption { + type = lib.types.listOf vdev; + default = [ ]; + description = '' + A list of vdevs used for the zfs intent log (ZIL). See + https://openzfs.github.io/openzfs-docs/man/master/7/zpoolconcepts.7.html#Intent_Log + for details. + ''; + }; + dedup = lib.mkOption { + type = lib.types.listOf vdev; + default = [ ]; + description = '' + A list of vdevs used for the deduplication table. See + https://openzfs.github.io/openzfs-docs/man/master/7/zpoolconcepts.7.html#dedup + for details. + ''; + }; + special = lib.mkOption { + type = lib.types.either (lib.types.listOf vdev) (lib.types.nullOr vdev); + default = [ ]; + description = '' + A list of vdevs used as special devices. See https://openzfs.github.io/openzfs-docs/man/master/7/zpoolconcepts.7.html#special for details. ''; @@ -86,7 +113,6 @@ in for details. ''; }; - # TODO: Consider supporting log, spare, and dedup options. }; }); }; @@ -182,8 +208,17 @@ in entries=() ${lib.optionalString (hasTopology && topology.vdev != null) (lib.concatMapStrings formatVdev topology.vdev)} - ${lib.optionalString (hasTopology && topology.special != null) - (formatOutput "special ${topology.special.mode}" topology.special.members)} + ${lib.optionalString (hasTopology && topology.spare != []) + (formatOutput "spare" topology.spare)} + ${lib.optionalString (hasTopology && topology.log != []) + (lib.concatMapStrings (vdev: formatOutput "log ${vdev.mode}" vdev.members) topology.log)} + ${lib.optionalString (hasTopology && topology.dedup != []) + (lib.concatMapStrings (vdev: formatOutput "dedup ${vdev.mode}" vdev.members) topology.dedup)} + ${lib.optionalString (hasTopology && topology.special != null && topology.special != []) + (lib.concatMapStrings + (vdev: formatOutput "special ${vdev.mode}" vdev.members) + (lib.lists.toList topology.special) + )} ${lib.optionalString (hasTopology && topology.cache != []) (formatOutput "cache" topology.cache)} all_devices=()