tidy: move relevant variables and format script block

This commit is contained in:
dylan madisetti committed 2024-08-01 15:59:33 -04:00
1 parent b2a106f8ed
commit 6bebcc728e
1 file changed
+85 -80
+85 -80
View File
@@ -7,13 +7,6 @@ let
"raidz2" "raidz2"
"raidz3" "raidz3"
]; ];
format_output = (mode: members: ''
entries+=("${mode}=${
lib.concatMapStringsSep " "
(d: "/dev/disk/by-partlabel/disk-${d}-zfs") members
}")
'');
format_vdev = (vdev: format_output vdev.mode vdev.members);
in in
{ {
options = { options = {
@@ -119,83 +112,95 @@ in
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default =
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name}) let
# Try importing the pool without mounting anything if it exists. format_output = (mode: members: ''
# This allows us to set mounpoints. entries+=("${mode}=${
if zpool import -N -f '${config.name}' || zpool list '${config.name}'; then lib.concatMapStringsSep " "
echo "not creating zpool ${config.name} as a pool with that name already exists" >&2 (d: "/dev/disk/by-partlabel/disk-${d}-zfs") members
else }")
continue=1 '');
for dev in "''${zfs_devices[@]}"; do format_vdev = (vdev: format_output vdev.mode vdev.members);
if ! blkid "$dev" >/dev/null; then hasTopology = config.topology != null;
# blkid fails, so device seems empty in
: ''
elif (blkid "$dev" -o export | grep '^PTUUID='); then readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
echo "device $dev already has a partuuid, skipping creating zpool ${config.name}" >&2 # Try importing the pool without mounting anything if it exists.
continue=0 # This allows us to set mounpoints.
elif (blkid "$dev" -o export | grep '^TYPE=zfs_member'); then if zpool import -N -f '${config.name}' || zpool list '${config.name}'; then
# zfs_member is a zfs partition, so we try to add the device to the pool echo "not creating zpool ${config.name} as a pool with that name already exists" >&2
: else
elif (blkid "$dev" -o export | grep '^TYPE='); then continue=1
echo "device $dev already has a partition, skipping creating zpool ${config.name}" >&2 for dev in "''${zfs_devices[@]}"; do
continue=0 if ! blkid "$dev" >/dev/null; then
fi # blkid fails, so device seems empty
done :
if [ $continue -eq 1 ]; then elif (blkid "$dev" -o export | grep '^PTUUID='); then
topology="" echo "device $dev already has a partuuid, skipping creating zpool ${config.name}" >&2
# For shell check continue=0
mode="${config.mode}" elif (blkid "$dev" -o export | grep '^TYPE=zfs_member'); then
if [ $mode != "prescribed" ]; then # zfs_member is a zfs partition, so we try to add the device to the pool
${if config.topology == null then :
''topology="${config.mode} \"''${zfs_devices}\""'' elif (blkid "$dev" -o export | grep '^TYPE='); then
echo "device $dev already has a partition, skipping creating zpool ${config.name}" >&2
continue=0
fi
done
if [ $continue -eq 1 ]; then
topology=""
# For shell check
mode="${config.mode}"
if [ $mode != "prescribed" ]; then
${if hasTopology then
''topology="${config.mode} \"''${zfs_devices}\""''
else
''
echo "topology cannot be set when mode != 'prescribed', skipping creating zpool ${config.name}" >&2
continue=0
''
}
else else
'' entries=()
echo "topology cannot be set when mode != 'prescribed', skipping creating zpool ${config.name}" >&2 ${lib.optionalString (hasTopology && config.topology.vdev != null)
continue=0 (lib.concatMapStrings format_vdev config.topology.vdev)}
'' ${lib.optionalString (hasTopology && config.topology.special != null)
} (format_output "special ${config.topology.special.mode}" config.topology.special.members)}
else ${lib.optionalString (hasTopology && config.topology.cache != null)
entries=() (format_output "cache" [config.topology.cache])}
${lib.concatMapStrings format_vdev config.topology.vdev} all_devices=()
${lib.optionalString (config.topology.special != null) for line in "''${entries[@]}"; do
(format_output "special ${config.topology.special.mode}" config.topology.special.members)} # lineformat is mode=device1 device2 device3
${lib.optionalString (config.topology.cache != null) mode=''${line%%=*}
(format_output "cache" [config.topology.cache])} devs=''${line#*=}
all_devices=() IFS=' ' read -r -a devices <<< "$devs"
for line in "''${entries[@]}"; do all_devices+=("''${devices[@]}")
# lineformat is mode=device1 device2 device3 # shellcheck disable=SC2089
mode=''${line%%=*} topology+=" ''${mode} ''${devices[*]}"
devs=''${line#*=} done
IFS=' ' read -r -a devices <<< "$devs" # all_devices sorted should equal zfs_devices sorted
all_devices+=("''${devices[@]}") all_devices_list=$(echo "''${all_devices[*]}" | tr ' ' '\n' | sort)
# shellcheck disable=SC2089 zfs_devices_list=$(echo "''${zfs_devices[*]}" | tr ' ' '\n' | sort)
topology+=" ''${mode} ''${devices[*]}" if [[ "$all_devices_list" != "$zfs_devices_list" ]]; then
done echo "not all disks accounted for, skipping creating zpool ${config.name}" >&2
# all_devices sorted should equal zfs_devices sorted diff <(echo "$all_devices_list" ) <(echo "$zfs_devices_list") >&2
all_devices_list=$(echo "''${all_devices[*]}" | tr ' ' '\n' | sort) continue=0
zfs_devices_list=$(echo "''${zfs_devices[*]}" | tr ' ' '\n' | sort) fi
if [[ "$all_devices_list" != "$zfs_devices_list" ]]; then fi
echo "not all disks accounted for, skipping creating zpool ${config.name}" >&2 fi
diff <(echo "$all_devices_list" ) <(echo "$zfs_devices_list") >&2 if [ $continue -eq 1 ]; then
continue=0 # shellcheck disable=SC2090
zpool create -f ${config.name} \
-R ${rootMountPoint} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
''${topology:+ $topology}
if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
zfs unmount ${config.name}
fi fi
fi fi
fi fi
if [ $continue -eq 1 ]; then ${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
# shellcheck disable=SC2090 '';
zpool create -f ${config.name} \
-R ${rootMountPoint} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
''${topology:+ $topology}
if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
zfs unmount ${config.name}
fi
fi
fi
${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
'';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;