mirror of
https://github.com/nix-community/disko
synced 2024-11-10 06:14:14 +00:00
tidy: move relevant variables and format script block
This commit is contained in:
parent
b2a106f8ed
commit
6bebcc728e
1 changed files with 85 additions and 80 deletions
|
@ -7,13 +7,6 @@ let
|
|||
"raidz2"
|
||||
"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
|
||||
{
|
||||
options = {
|
||||
|
@ -119,83 +112,95 @@ in
|
|||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
|
||||
# Try importing the pool without mounting anything if it exists.
|
||||
# This allows us to set mounpoints.
|
||||
if zpool import -N -f '${config.name}' || zpool list '${config.name}'; then
|
||||
echo "not creating zpool ${config.name} as a pool with that name already exists" >&2
|
||||
else
|
||||
continue=1
|
||||
for dev in "''${zfs_devices[@]}"; do
|
||||
if ! blkid "$dev" >/dev/null; then
|
||||
# blkid fails, so device seems empty
|
||||
:
|
||||
elif (blkid "$dev" -o export | grep '^PTUUID='); then
|
||||
echo "device $dev already has a partuuid, skipping creating zpool ${config.name}" >&2
|
||||
continue=0
|
||||
elif (blkid "$dev" -o export | grep '^TYPE=zfs_member'); then
|
||||
# zfs_member is a zfs partition, so we try to add the device to the pool
|
||||
:
|
||||
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 config.topology == null then
|
||||
''topology="${config.mode} \"''${zfs_devices}\""''
|
||||
default =
|
||||
let
|
||||
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);
|
||||
hasTopology = config.topology != null;
|
||||
in
|
||||
''
|
||||
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
|
||||
# Try importing the pool without mounting anything if it exists.
|
||||
# This allows us to set mounpoints.
|
||||
if zpool import -N -f '${config.name}' || zpool list '${config.name}'; then
|
||||
echo "not creating zpool ${config.name} as a pool with that name already exists" >&2
|
||||
else
|
||||
continue=1
|
||||
for dev in "''${zfs_devices[@]}"; do
|
||||
if ! blkid "$dev" >/dev/null; then
|
||||
# blkid fails, so device seems empty
|
||||
:
|
||||
elif (blkid "$dev" -o export | grep '^PTUUID='); then
|
||||
echo "device $dev already has a partuuid, skipping creating zpool ${config.name}" >&2
|
||||
continue=0
|
||||
elif (blkid "$dev" -o export | grep '^TYPE=zfs_member'); then
|
||||
# zfs_member is a zfs partition, so we try to add the device to the pool
|
||||
:
|
||||
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
|
||||
''
|
||||
echo "topology cannot be set when mode != 'prescribed', skipping creating zpool ${config.name}" >&2
|
||||
continue=0
|
||||
''
|
||||
}
|
||||
else
|
||||
entries=()
|
||||
${lib.concatMapStrings format_vdev config.topology.vdev}
|
||||
${lib.optionalString (config.topology.special != null)
|
||||
(format_output "special ${config.topology.special.mode}" config.topology.special.members)}
|
||||
${lib.optionalString (config.topology.cache != null)
|
||||
(format_output "cache" [config.topology.cache])}
|
||||
all_devices=()
|
||||
for line in "''${entries[@]}"; do
|
||||
# lineformat is mode=device1 device2 device3
|
||||
mode=''${line%%=*}
|
||||
devs=''${line#*=}
|
||||
IFS=' ' read -r -a devices <<< "$devs"
|
||||
all_devices+=("''${devices[@]}")
|
||||
# shellcheck disable=SC2089
|
||||
topology+=" ''${mode} ''${devices[*]}"
|
||||
done
|
||||
# all_devices sorted should equal zfs_devices sorted
|
||||
all_devices_list=$(echo "''${all_devices[*]}" | tr ' ' '\n' | sort)
|
||||
zfs_devices_list=$(echo "''${zfs_devices[*]}" | tr ' ' '\n' | sort)
|
||||
if [[ "$all_devices_list" != "$zfs_devices_list" ]]; then
|
||||
echo "not all disks accounted for, skipping creating zpool ${config.name}" >&2
|
||||
diff <(echo "$all_devices_list" ) <(echo "$zfs_devices_list") >&2
|
||||
continue=0
|
||||
entries=()
|
||||
${lib.optionalString (hasTopology && config.topology.vdev != null)
|
||||
(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)}
|
||||
${lib.optionalString (hasTopology && config.topology.cache != null)
|
||||
(format_output "cache" [config.topology.cache])}
|
||||
all_devices=()
|
||||
for line in "''${entries[@]}"; do
|
||||
# lineformat is mode=device1 device2 device3
|
||||
mode=''${line%%=*}
|
||||
devs=''${line#*=}
|
||||
IFS=' ' read -r -a devices <<< "$devs"
|
||||
all_devices+=("''${devices[@]}")
|
||||
# shellcheck disable=SC2089
|
||||
topology+=" ''${mode} ''${devices[*]}"
|
||||
done
|
||||
# all_devices sorted should equal zfs_devices sorted
|
||||
all_devices_list=$(echo "''${all_devices[*]}" | tr ' ' '\n' | sort)
|
||||
zfs_devices_list=$(echo "''${zfs_devices[*]}" | tr ' ' '\n' | sort)
|
||||
if [[ "$all_devices_list" != "$zfs_devices_list" ]]; then
|
||||
echo "not all disks accounted for, skipping creating zpool ${config.name}" >&2
|
||||
diff <(echo "$all_devices_list" ) <(echo "$zfs_devices_list") >&2
|
||||
continue=0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
if [ $continue -eq 1 ]; then
|
||||
# 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
|
||||
if [ $continue -eq 1 ]; then
|
||||
# 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)}
|
||||
'';
|
||||
${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
|
|
Loading…
Reference in a new issue