mirror of
https://github.com/nix-community/disko
synced 2024-11-10 06:14:14 +00:00
Merge pull request #568 from nix-community/idempotent-create
types *: make create idempotent
This commit is contained in:
commit
59e50d4ecb
15 changed files with 215 additions and 141 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env bash
|
||||
set -efux -o pipefail
|
||||
# dependencies: bash jq util-linux lvm2 mdadm zfs
|
||||
# dependencies: bash jq util-linux lvm2 mdadm zfs gnugrep
|
||||
disk=$(realpath "$1")
|
||||
|
||||
lsblk -a -f >&2
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
"com.sun:auto-snapshot" = "false";
|
||||
};
|
||||
mountpoint = "/";
|
||||
postCreateHook = "zfs snapshot zroot@blank";
|
||||
postCreateHook = "zfs list -t snapshot -H -o name | grep -E '^zroot@blank$' || zfs snapshot zroot@blank";
|
||||
|
||||
datasets = {
|
||||
zfs_fs = {
|
||||
|
|
|
@ -243,15 +243,23 @@ let
|
|||
# running direct mode
|
||||
machine.succeed("${tsp-format}")
|
||||
machine.succeed("${tsp-mount}")
|
||||
machine.succeed("${tsp-mount}") # verify that the command is idempotent
|
||||
machine.succeed("${tsp-mount}") # verify that mount is idempotent
|
||||
machine.succeed("${tsp-disko}") # verify that we can destroy and recreate
|
||||
machine.succeed("mkdir -p /mnt/home")
|
||||
machine.succeed("touch /mnt/home/testfile")
|
||||
machine.succeed("${tsp-format}") # verify that format is idempotent
|
||||
machine.succeed("test -e /mnt/home/testfile")
|
||||
''}
|
||||
${lib.optionalString (testMode == "module") ''
|
||||
# running module mode
|
||||
machine.succeed("${nodes.machine.system.build.formatScript}")
|
||||
machine.succeed("${nodes.machine.system.build.mountScript}")
|
||||
machine.succeed("${nodes.machine.system.build.mountScript}") # verify that the command is idempotent
|
||||
machine.succeed("${nodes.machine.system.build.mountScript}") # verify that mount is idempotent
|
||||
machine.succeed("${nodes.machine.system.build.diskoScript}") # verify that we can destroy and recreate again
|
||||
machine.succeed("mkdir -p /mnt/home")
|
||||
machine.succeed("touch /mnt/home/testfile")
|
||||
machine.succeed("${nodes.machine.system.build.formatScript}") # verify that format is idempotent
|
||||
machine.succeed("test -e /mnt/home/testfile")
|
||||
''}
|
||||
|
||||
${postDisko}
|
||||
|
|
|
@ -31,7 +31,11 @@ let
|
|||
swapCreate = mountpoint: swap:
|
||||
lib.concatMapStringsSep
|
||||
"\n"
|
||||
(file: ''btrfs filesystem mkswapfile --size ${file.size} "${mountpoint}/${file.path}"'')
|
||||
(file: ''
|
||||
if ! test -e "${mountpoint}/${file.path}"; then
|
||||
btrfs filesystem mkswapfile --size ${file.size} "${mountpoint}/${file.path}"
|
||||
fi
|
||||
'')
|
||||
(lib.attrValues swap);
|
||||
|
||||
in
|
||||
|
@ -112,7 +116,11 @@ in
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
mkfs.btrfs ${config.device} ${toString config.extraArgs}
|
||||
# create the filesystem only if the device seems empty
|
||||
if ! (blkid '${config.device}' -o export | grep -q '^TYPE='); then
|
||||
mkfs.btrfs "${config.device}" ${toString config.extraArgs}
|
||||
fi
|
||||
if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then
|
||||
${lib.optionalString (config.swap != {}) ''
|
||||
(
|
||||
MNTPOINT=$(mktemp -d)
|
||||
|
@ -128,10 +136,13 @@ in
|
|||
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
||||
SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}"
|
||||
mkdir -p "$(dirname "$SUBVOL_ABS_PATH")"
|
||||
if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then
|
||||
btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs}
|
||||
fi
|
||||
${swapCreate "$SUBVOL_ABS_PATH" subvol.swap}
|
||||
)
|
||||
'') (lib.attrValues config.subvolumes)}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
@ -206,7 +217,7 @@ in
|
|||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs:
|
||||
[ pkgs.btrfs-progs pkgs.coreutils ];
|
||||
[ pkgs.btrfs-progs pkgs.coreutils pkgs.gnugrep ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
|
|
|
@ -44,9 +44,11 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! (blkid '${config.device}' | grep -q 'TYPE='); then
|
||||
mkfs.${config.format} \
|
||||
${toString config.extraArgs} \
|
||||
${config.device}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
@ -79,7 +81,7 @@
|
|||
readOnly = true;
|
||||
# type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs:
|
||||
[ pkgs.util-linux ] ++ (
|
||||
[ pkgs.util-linux pkgs.gnugrep ] ++ (
|
||||
# TODO add many more
|
||||
if (config.format == "xfs") then [ pkgs.xfsprogs ]
|
||||
else if (config.format == "btrfs") then [ pkgs.btrfs-progs ]
|
||||
|
|
|
@ -153,7 +153,12 @@ in
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! blkid "${config.device}" >/dev/null; then
|
||||
${lib.concatStrings (map (partition: ''
|
||||
if sgdisk \
|
||||
--info=${toString partition._index} \
|
||||
${config.device} > /dev/null 2>&1
|
||||
then
|
||||
sgdisk \
|
||||
--align-end \
|
||||
--new=${toString partition._index}:${partition.start}:${partition.end} \
|
||||
|
@ -164,7 +169,7 @@ in
|
|||
partprobe ${config.device}
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
${lib.optionalString (partition.content != null) partition.content._create}
|
||||
fi
|
||||
'') sortedPartitions)}
|
||||
|
||||
${
|
||||
|
@ -179,8 +184,13 @@ in
|
|||
${lib.concatMapStrings (p:
|
||||
p.hybrid._create
|
||||
)
|
||||
sortedHybridPartitions}
|
||||
sortedHybridPartitions
|
||||
}
|
||||
fi
|
||||
|
||||
${lib.concatStrings (map (partition: ''
|
||||
${lib.optionalString (partition.content != null) partition.content._create}
|
||||
'') sortedPartitions)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
|
|
@ -112,6 +112,7 @@ in
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! blkid "${config.device}" >/dev/null || ! (blkid "${config.device}" -o export | grep -q '^TYPE='); then
|
||||
${lib.optionalString config.askPassword ''
|
||||
set +x
|
||||
askPassword() {
|
||||
|
@ -132,6 +133,7 @@ in
|
|||
${toString (lib.forEach config.additionalKeyFiles (keyFile: ''
|
||||
cryptsetup luksAddKey ${config.device} ${keyFile} ${keyFileArgs}
|
||||
''))}
|
||||
fi
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
};
|
||||
|
@ -176,7 +178,7 @@ in
|
|||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
|
||||
default = pkgs: [ pkgs.gnugrep pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
|
|
|
@ -31,7 +31,9 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! (blkid '${config.device}' | grep -q 'TYPE='); then
|
||||
pvcreate ${config.device}
|
||||
fi
|
||||
echo "${config.device}" >>"$disko_devices_dir"/lvm_${config.vg}
|
||||
'';
|
||||
};
|
||||
|
@ -49,7 +51,7 @@
|
|||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.lvm2 ];
|
||||
default = pkgs: [ pkgs.gnugrep pkgs.lvm2 ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
|
|
|
@ -68,9 +68,12 @@
|
|||
in
|
||||
''
|
||||
readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name})
|
||||
if ! vgdisplay "${config.name}" >/dev/null; then
|
||||
vgcreate ${config.name} \
|
||||
"''${lvm_devices[@]}"
|
||||
fi
|
||||
${lib.concatMapStrings (lv: ''
|
||||
if ! lvdisplay '${config.name}/${lv.name}'; then
|
||||
lvcreate \
|
||||
--yes \
|
||||
${if lib.hasInfix "%" lv.size then "-l" else "-L"} ${lv.size} \
|
||||
|
@ -78,6 +81,10 @@
|
|||
${lib.optionalString (lv.lvm_type != null) "--type=${lv.lvm_type}"} \
|
||||
${toString lv.extraArgs} \
|
||||
${config.name}
|
||||
fi
|
||||
'') sortedLvs}
|
||||
|
||||
${lib.concatMapStrings (lv: ''
|
||||
${lib.optionalString (lv.content != null) lv.content._create}
|
||||
'') sortedLvs}
|
||||
'';
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! test -e /dev/md/${config.name}; then
|
||||
readarray -t disk_devices < <(cat "$disko_devices_dir"/raid_${config.name})
|
||||
echo 'y' | mdadm --create /dev/md/${config.name} \
|
||||
--level=${toString config.level} \
|
||||
|
@ -47,6 +48,7 @@
|
|||
udevadm settle
|
||||
# for some reason mdadm devices spawn with an existing partition table, so we need to wipe it
|
||||
sgdisk --zap-all /dev/md/${config.name}
|
||||
fi
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
};
|
||||
|
|
|
@ -40,9 +40,11 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! blkid "${config.device}" -o export | grep -q '^TYPE='; then
|
||||
mkswap \
|
||||
${toString config.extraArgs} \
|
||||
${config.device}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
|
|
@ -88,6 +88,7 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! blkid "${config.device}" >/dev/null; then
|
||||
parted -s ${config.device} -- mklabel ${config.format}
|
||||
${lib.concatStrings (map (partition: ''
|
||||
${lib.optionalString (config.format == "gpt") ''
|
||||
|
@ -112,6 +113,10 @@
|
|||
udevadm settle
|
||||
${lib.optionalString (partition.content != null) partition.content._create}
|
||||
'') config.partitions)}
|
||||
fi
|
||||
${lib.concatStrings (map (partition: ''
|
||||
${lib.optionalString (partition.content != null) partition.content._create}
|
||||
'') config.partitions)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
|
|
@ -59,8 +59,10 @@
|
|||
# since (create order != mount order)
|
||||
# -p creates parents automatically
|
||||
default = ''
|
||||
if ! zfs get type ${config._name} >/dev/null 2>&1; then
|
||||
zfs create -up ${config._name} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)}
|
||||
fi
|
||||
'';
|
||||
} // { readOnly = false; };
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! zfs get type ${config._parent.name}/${config.name} >/dev/null 2>&1; then
|
||||
zfs create ${config._parent.name}/${config.name} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
||||
-V ${config.size}
|
||||
|
@ -54,6 +55,7 @@
|
|||
partprobe /dev/zvol/${config._parent.name}/${config.name}
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
fi
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
};
|
||||
|
|
|
@ -62,6 +62,23 @@
|
|||
inherit config options;
|
||||
default = ''
|
||||
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
|
||||
if 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='); then
|
||||
echo "device $dev already has a partition, skipping creating zpool ${config.name}" >&2
|
||||
continue=0
|
||||
fi
|
||||
done
|
||||
if [ $continue -eq 1 ]; then
|
||||
zpool create -f ${config.name} \
|
||||
-R ${rootMountPoint} ${config.mode} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
||||
|
@ -70,6 +87,8 @@
|
|||
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)}
|
||||
'';
|
||||
};
|
||||
|
@ -98,7 +117,7 @@
|
|||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.util-linux ] ++ lib.flatten (map (dataset: dataset._pkgs pkgs) (lib.attrValues config.datasets));
|
||||
default = pkgs: [ pkgs.gnugrep pkgs.util-linux ] ++ lib.flatten (map (dataset: dataset._pkgs pkgs) (lib.attrValues config.datasets));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue