mirror of
https://github.com/nix-community/disko.git
synced 2024-09-17 09:37:17 +03:00
reimplement disko using the nixos type system
This should make the code cleaner, more robust and errors should be clearer. we also changed the configuration format a bit.
This commit is contained in:
parent
adf901d581
commit
dcb0c42857
328
default.nix
328
default.nix
@ -1,312 +1,22 @@
|
|||||||
{ lib ? import <nixpkgs/lib> }:
|
{ lib ? import <nixpkgs/lib>
|
||||||
with lib;
|
, pkgs ? import <nixpkgs> {}
|
||||||
with builtins;
|
}:
|
||||||
|
|
||||||
let
|
let
|
||||||
|
types = import ./types.nix { inherit pkgs; };
|
||||||
helper.find-device = device:
|
eval = cfg: lib.evalModules {
|
||||||
let
|
modules = lib.singleton {
|
||||||
environment = helper.device-id device;
|
# _file = toString input;
|
||||||
in
|
imports = lib.singleton { topLevel.devices = cfg; };
|
||||||
# DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path
|
options = {
|
||||||
if hasPrefix "/dev/disk" device then
|
topLevel = lib.mkOption {
|
||||||
"${environment}='${device}'"
|
type = types.topLevel;
|
||||||
else ''
|
};
|
||||||
${environment}=$(for x in $(find /dev/disk/{by-path,by-id}/); do
|
|
||||||
dev=$x
|
|
||||||
if [ "$(readlink -f $x)" = "$(readlink -f '${device}')" ]; then
|
|
||||||
target=$dev
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if test -z ''${target+x}; then
|
|
||||||
echo 'unable to find path of disk: ${device}, bailing out' >&2
|
|
||||||
exit 1
|
|
||||||
else
|
|
||||||
echo $target
|
|
||||||
fi)
|
|
||||||
'';
|
|
||||||
|
|
||||||
helper.device-id = device: "DEVICE${builtins.substring 0 5 (builtins.hashString "sha1" device)}";
|
|
||||||
|
|
||||||
config-f = q: x: config.${x.type} q x;
|
|
||||||
|
|
||||||
config.filesystem = q: x: {
|
|
||||||
fileSystems.${x.mountpoint} = {
|
|
||||||
device = q.device;
|
|
||||||
fsType = x.format;
|
|
||||||
${if x ? options then "options" else null} = x.options;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config.zfs_filesystem = q: x: {
|
|
||||||
fileSystems.${x.mountpoint} = {
|
|
||||||
device = q.device;
|
|
||||||
fsType = "zfs";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config.devices = q: x:
|
|
||||||
foldl' recursiveUpdate { } (mapAttrsToList (name: config-f { device = "/dev/${name}"; }) x.content);
|
|
||||||
|
|
||||||
config.luks = q: x: {
|
|
||||||
boot.initrd.luks.devices.${x.name}.device = q.device;
|
|
||||||
} // config-f { device = "/dev/mapper/${x.name}"; } x.content;
|
|
||||||
|
|
||||||
config.lvm_lv = q: x:
|
|
||||||
config-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
|
|
||||||
|
|
||||||
config.lvm_vg = q: x:
|
|
||||||
foldl' recursiveUpdate { } (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs);
|
|
||||||
|
|
||||||
config.noop = q: x: { };
|
|
||||||
|
|
||||||
config.partition = q: x:
|
|
||||||
config-f { device = q.device + toString q.index; } x.content;
|
|
||||||
|
|
||||||
config.table = q: x:
|
|
||||||
foldl' recursiveUpdate { } (imap (index: config-f (q // { inherit index; })) x.partitions);
|
|
||||||
|
|
||||||
|
|
||||||
create-f = q: x: create.${x.type} q x;
|
|
||||||
|
|
||||||
create.btrfs = q: x: ''
|
|
||||||
mkfs.btrfs ${q.device}
|
|
||||||
${lib.optionalString (!isNull x.subvolumes or null) ''
|
|
||||||
MNTPOINT=$(mktemp -d)
|
|
||||||
(
|
|
||||||
mount ${q.device} "$MNTPOINT"
|
|
||||||
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
|
||||||
${concatMapStringsSep "\n" (subvolume: "btrfs subvolume create \"$MNTPOINT\"/${subvolume}") x.subvolumes}
|
|
||||||
)
|
|
||||||
''}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.filesystem = q: x: ''
|
|
||||||
mkfs.${x.format} \
|
|
||||||
${lib.optionalString (!isNull x.extraArgs or null) x.extraArgs} \
|
|
||||||
${q.device}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.devices = q: x:
|
|
||||||
let
|
|
||||||
raid-devices = lib.filterAttrs (_: dev: dev.type == "mdadm" || dev.type == "zpool" || dev.type == "lvm_vg") x.content;
|
|
||||||
other-devices = lib.filterAttrs (_: dev: dev.type != "mdadm" && dev.type != "zpool" && dev.type != "lvm_vg") x.content;
|
|
||||||
in
|
|
||||||
''
|
|
||||||
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) other-devices)}
|
|
||||||
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; name = name; }) raid-devices)}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.mdraid = q: x: ''
|
|
||||||
RAIDDEVICES_N_${x.name}=$((''${RAIDDEVICES_N_${x.name}:-0}+1))
|
|
||||||
RAIDDEVICES_${x.name}="''${RAIDDEVICES_${x.name}:-}${q.device} "
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.mdadm = q: x: ''
|
|
||||||
echo 'y' | mdadm --create /dev/md/${q.name} --level=${toString x.level or 1} --raid-devices=''${RAIDDEVICES_N_${q.name}} ''${RAIDDEVICES_${q.name}}
|
|
||||||
udevadm trigger --subsystem-match=block; udevadm settle
|
|
||||||
${create-f { device = "/dev/md/${q.name}"; } x.content}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.luks = q: x: ''
|
|
||||||
cryptsetup -q luksFormat ${q.device} ${if builtins.hasAttr "keyfile" x then x.keyfile else ""} ${toString (x.extraArgs or [])}
|
|
||||||
cryptsetup luksOpen ${q.device} ${x.name} ${if builtins.hasAttr "keyfile" x then "--key-file " + x.keyfile else ""}
|
|
||||||
${create-f { device = "/dev/mapper/${x.name}"; } x.content}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.lvm_pv = q: x: ''
|
|
||||||
pvcreate ${q.device}
|
|
||||||
LVMDEVICES_${x.vg}="''${LVMDEVICES_${x.vg}:-}${q.device} "
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.lvm_lv = q: x: ''
|
|
||||||
lvcreate \
|
|
||||||
${if hasInfix "%" x.size then "-l" else "-L"} ${x.size} \
|
|
||||||
-n ${q.name} \
|
|
||||||
${lib.optionalString (!isNull x.lvm_type or null) "--type=${x.lvm_type}"} \
|
|
||||||
${lib.optionalString (!isNull x.extraArgs or null) x.extraArgs} \
|
|
||||||
${q.vgname}
|
|
||||||
${create-f { device = "/dev/${q.vgname}/${q.name}"; } x.content}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.lvm_vg = q: x: ''
|
|
||||||
vgcreate ${q.name} $LVMDEVICES_${q.name}
|
|
||||||
${concatStrings (mapAttrsToList (name: create-f { inherit name; vgname = q.name; }) x.lvs)}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.noop = q: x: "";
|
|
||||||
|
|
||||||
create.partition = q: x:
|
|
||||||
let
|
|
||||||
env = helper.device-id q.device;
|
|
||||||
in
|
|
||||||
''
|
|
||||||
parted -s "''${${env}}" mkpart ${x.part-type} ${x.fs-type or ""} ${x.start} ${x.end}
|
|
||||||
# ensure /dev/disk/by-path/..-partN exists before continuing
|
|
||||||
udevadm trigger --subsystem-match=block; udevadm settle
|
|
||||||
${optionalString (x.bootable or false) ''
|
|
||||||
parted -s "''${${env}}" set ${toString q.index} boot on
|
|
||||||
''}
|
|
||||||
${concatMapStringsSep "" (flag: ''
|
|
||||||
parted -s "''${${env}}" set ${toString q.index} ${flag} on
|
|
||||||
'') (x.flags or [])}
|
|
||||||
${create-f { device = "\"\${${env}}\"-part" + toString q.index; } x.content}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.table = q: x: ''
|
|
||||||
${helper.find-device q.device}
|
|
||||||
parted -s "''${${helper.device-id q.device}}" mklabel ${x.format}
|
|
||||||
${concatStrings (imap (index: create-f (q // { inherit index; })) x.partitions)}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.zfs = q: x: ''
|
|
||||||
ZFSDEVICES_${x.pool}="''${ZFSDEVICES_${x.pool}:-}${q.device} "
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.zfs_filesystem = q: x: ''
|
|
||||||
zfs create ${q.pool}/${x.name} \
|
|
||||||
${lib.optionalString (isAttrs x.options or null) (concatStringsSep " " (mapAttrsToList (n: v: "-o ${n}=${v}") x.options))}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.zfs_volume = q: x: ''
|
|
||||||
zfs create ${q.pool}/${x.name} \
|
|
||||||
-V ${x.size} \
|
|
||||||
${lib.optionalString (isAttrs x.options or null) (concatStringsSep " " (mapAttrsToList (n: v: "-o ${n}=${v}") x.options))}
|
|
||||||
udevadm trigger --subsystem-match=block; udevadm settle
|
|
||||||
${create-f { device = "/dev/zvol/${q.pool}/${x.name}"; } x.content}
|
|
||||||
'';
|
|
||||||
|
|
||||||
create.zpool = q: x: ''
|
|
||||||
zpool create ${q.name} \
|
|
||||||
${lib.optionalString (!isNull (x.mode or null) && x.mode != "stripe") x.mode} \
|
|
||||||
${lib.optionalString (isAttrs x.options or null) (concatStringsSep " " (mapAttrsToList (n: v: "-o ${n}=${v}") x.options))} \
|
|
||||||
${lib.optionalString (isAttrs x.rootFsOptions or null) (concatStringsSep " " (mapAttrsToList (n: v: "-O ${n}=${v}") x.rootFsOptions))} \
|
|
||||||
''${ZFSDEVICES_${q.name}}
|
|
||||||
${concatMapStrings (create-f (q // { pool = q.name; })) x.datasets}
|
|
||||||
'';
|
|
||||||
|
|
||||||
|
|
||||||
mount-f = q: x: mount.${x.type} q x;
|
|
||||||
|
|
||||||
mount.filesystem = q: x: {
|
|
||||||
fs.${x.mountpoint} = ''
|
|
||||||
if ! findmnt ${q.device} "/mnt${x.mountpoint}" > /dev/null 2>&1; then
|
|
||||||
mount ${q.device} "/mnt${x.mountpoint}" \
|
|
||||||
-o X-mount.mkdir \
|
|
||||||
${lib.optionalString (isList x.mountOptions or null) (concatStringsSep " " x.mountOptions)}
|
|
||||||
fi
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
mount.zfs_filesystem = q: x:
|
|
||||||
optionalAttrs ((x.options.mountpoint or "") != "none")
|
|
||||||
(mount.filesystem (q // { device = q.dataset; }) (x // { mountOptions = [
|
|
||||||
(lib.optionalString ((x.options.mountpoint or "") != "legacy") "-o zfsutil")
|
|
||||||
"-t zfs"
|
|
||||||
]; }));
|
|
||||||
|
|
||||||
mount.btrfs = mount.filesystem;
|
|
||||||
|
|
||||||
mount.devices = q: x:
|
|
||||||
let
|
|
||||||
z = foldl' recursiveUpdate { } (mapAttrsToList (name: mount-f { device = "/dev/${name}"; inherit name; }) x.content);
|
|
||||||
# attrValues returns values sorted by name. This is important, because it
|
|
||||||
# ensures that "/" is processed before "/foo" etc.
|
|
||||||
in
|
|
||||||
''
|
|
||||||
${optionalString (hasAttr "table" z) (concatStringsSep "\n" (attrValues z.table))}
|
|
||||||
${optionalString (hasAttr "luks" z) (concatStringsSep "\n" (attrValues z.luks))}
|
|
||||||
${optionalString (hasAttr "lvm" z) (concatStringsSep "\n" (attrValues z.lvm))}
|
|
||||||
${optionalString (hasAttr "zpool" z) (concatStringsSep "\n" (attrValues z.zpool))}
|
|
||||||
${optionalString (hasAttr "zfs" z) (concatStringsSep "\n" (attrValues z.zfs))}
|
|
||||||
${optionalString (hasAttr "fs" z) (concatStringsSep "\n" (attrValues z.fs))}
|
|
||||||
'';
|
|
||||||
|
|
||||||
mount.luks = q: x: (
|
|
||||||
recursiveUpdate
|
|
||||||
(mount-f { device = "/dev/mapper/${x.name}"; } x.content)
|
|
||||||
{
|
|
||||||
luks.${q.device} = ''
|
|
||||||
cryptsetup status ${x.name} >/dev/null 2>/dev/null || cryptsetup luksOpen ${q.device} ${x.name} ${if builtins.hasAttr "keyfile" x then "--key-file " + x.keyfile else ""}
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
mount.lvm_lv = q: x:
|
|
||||||
mount-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
|
|
||||||
|
|
||||||
mount.lvm_vg = q: x: (
|
|
||||||
recursiveUpdate
|
|
||||||
(foldl' recursiveUpdate { } (mapAttrsToList (name: mount-f { inherit name; vgname = q.name; }) x.lvs))
|
|
||||||
{
|
|
||||||
lvm.${q.device} = ''
|
|
||||||
vgchange -a y
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
mount.lvm_pv = mount.noop;
|
|
||||||
|
|
||||||
mount.noop = q: x: { };
|
|
||||||
|
|
||||||
mount.mdadm = q: x:
|
|
||||||
mount-f { device = "/dev/md/${q.name}"; } x.content;
|
|
||||||
mount.mdraid = mount.noop;
|
|
||||||
|
|
||||||
mount.partition = q: x:
|
|
||||||
mount-f { device = "\"\${${q.device}}\"-part" + toString q.index; } x.content;
|
|
||||||
|
|
||||||
mount.table = q: x: (
|
|
||||||
recursiveUpdate
|
|
||||||
(foldl' recursiveUpdate { } (imap (index: mount-f (q // { inherit index; device = helper.device-id q.device; })) x.partitions))
|
|
||||||
{ table.${q.device} = helper.find-device q.device; }
|
|
||||||
);
|
|
||||||
|
|
||||||
mount.zfs = mount.noop;
|
|
||||||
|
|
||||||
mount.zpool = q: x:
|
|
||||||
let
|
|
||||||
datasets = [{
|
|
||||||
inherit (q) name;
|
|
||||||
type = "zfs_filesystem";
|
|
||||||
dataset = q.name;
|
|
||||||
mountpoint = x.mountpoint or "/${q.name}";
|
|
||||||
options = q.rootFsOptions or { };
|
|
||||||
}] ++ x.datasets;
|
|
||||||
in
|
|
||||||
recursiveUpdate
|
|
||||||
(foldl' recursiveUpdate { }
|
|
||||||
(
|
|
||||||
(map
|
|
||||||
(x: mount-f
|
|
||||||
({
|
|
||||||
dataset = x.dataset or "${q.name}/${x.name}";
|
|
||||||
mountpoint = x.mountpoint or "/${q.name}/${x.name}";
|
|
||||||
} // q)
|
|
||||||
x)
|
|
||||||
datasets)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
zpool.${q.device} = ''
|
|
||||||
zpool list '${q.name}' >/dev/null 2>/dev/null || zpool import '${q.name}'
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
mount.zfs_volume = q: x:
|
};
|
||||||
mount-f { device = "/dev/zvol/${q.dataset}"; } x.content;
|
in {
|
||||||
|
types = types;
|
||||||
in
|
create = cfg: (eval cfg).config.topLevel.create;
|
||||||
{
|
mount = cfg: (eval cfg).config.topLevel.mount;
|
||||||
config = config-f { };
|
config = cfg: (eval cfg).config.topLevel.config;
|
||||||
create = cfg: ''
|
|
||||||
set -efux
|
|
||||||
${create-f {} cfg}
|
|
||||||
'';
|
|
||||||
mount = cfg: ''
|
|
||||||
set -efux
|
|
||||||
${mount-f {} cfg}
|
|
||||||
'';
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,28 @@
|
|||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdb";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "primary";
|
partitions = [
|
||||||
start = "0%";
|
{
|
||||||
end = "100%";
|
name = "root";
|
||||||
content = {
|
type = "partition";
|
||||||
type = "btrfs";
|
start = "0%";
|
||||||
mountpoint = "/";
|
end = "100%";
|
||||||
subvolumes = [
|
content = {
|
||||||
"/home"
|
type = "btrfs";
|
||||||
"/test"
|
mountpoint = "/";
|
||||||
];
|
subvolumes = [
|
||||||
};
|
"/home"
|
||||||
}
|
"/test"
|
||||||
];
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
194
example/complex.nix
Normal file
194
example/complex.nix
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
{
|
||||||
|
disk = {
|
||||||
|
disk1 = {
|
||||||
|
type = "disk";
|
||||||
|
device = "/dev/vdb";
|
||||||
|
content = {
|
||||||
|
type = "table";
|
||||||
|
format = "gpt";
|
||||||
|
partitions = [
|
||||||
|
{
|
||||||
|
type = "partition";
|
||||||
|
start = "0";
|
||||||
|
end = "1M";
|
||||||
|
name = "grub";
|
||||||
|
flags = ["bios_grub"];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
type = "partition";
|
||||||
|
start = "1M";
|
||||||
|
end = "100%";
|
||||||
|
name = "luks";
|
||||||
|
bootable = true;
|
||||||
|
content = {
|
||||||
|
type = "luks";
|
||||||
|
name = "crypted1";
|
||||||
|
keyFile = "/tmp/secret.key";
|
||||||
|
extraArgs = [
|
||||||
|
"--hash sha512"
|
||||||
|
"--iter-time 5000"
|
||||||
|
];
|
||||||
|
content = {
|
||||||
|
type = "lvm_pv";
|
||||||
|
vg = "pool";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
disk2 = {
|
||||||
|
type = "disk";
|
||||||
|
device = "/dev/vdc";
|
||||||
|
content = {
|
||||||
|
type = "table";
|
||||||
|
format = "gpt";
|
||||||
|
partitions = [
|
||||||
|
{
|
||||||
|
type = "partition";
|
||||||
|
start = "0";
|
||||||
|
end = "1M";
|
||||||
|
name = "grub";
|
||||||
|
flags = ["bios_grub"];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
type = "partition";
|
||||||
|
start = "1M";
|
||||||
|
end = "100%";
|
||||||
|
name = "luks";
|
||||||
|
bootable = true;
|
||||||
|
content = {
|
||||||
|
type = "luks";
|
||||||
|
name = "crypted2";
|
||||||
|
keyFile = "/tmp/secret.key";
|
||||||
|
extraArgs = [
|
||||||
|
"--hash sha512"
|
||||||
|
"--iter-time 5000"
|
||||||
|
];
|
||||||
|
content = {
|
||||||
|
type = "lvm_pv";
|
||||||
|
vg = "pool";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
mdadm = {
|
||||||
|
raid1 = {
|
||||||
|
type = "mdadm";
|
||||||
|
level = 1;
|
||||||
|
content = {
|
||||||
|
type = "table";
|
||||||
|
format = "msdos";
|
||||||
|
partitions = [
|
||||||
|
{
|
||||||
|
type = "partition";
|
||||||
|
name = "xfs";
|
||||||
|
start = "1MiB";
|
||||||
|
end = "100%";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "xfs";
|
||||||
|
mountpoint = "/xfs_mdadm_lvm";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
lvm_vg = {
|
||||||
|
pool = {
|
||||||
|
type = "lvm_vg";
|
||||||
|
lvs = {
|
||||||
|
root = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "10M";
|
||||||
|
lvm_type = "mirror";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
|
mountpoint = "/ext4_on_lvm";
|
||||||
|
options = [
|
||||||
|
"defaults"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
raid1 = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "30M";
|
||||||
|
lvm_type = "raid0";
|
||||||
|
content = {
|
||||||
|
type = "mdraid";
|
||||||
|
name = "raid1";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
raid2 = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "30M";
|
||||||
|
lvm_type = "raid0";
|
||||||
|
content = {
|
||||||
|
type = "mdraid";
|
||||||
|
name = "raid1";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
zfs1 = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "128M";
|
||||||
|
lvm_type = "raid0";
|
||||||
|
content = {
|
||||||
|
type = "zfs";
|
||||||
|
pool = "zroot";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
zfs2 = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "128M";
|
||||||
|
lvm_type = "raid0";
|
||||||
|
content = {
|
||||||
|
type = "zfs";
|
||||||
|
pool = "zroot";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
zpool = {
|
||||||
|
zroot = {
|
||||||
|
type = "zpool";
|
||||||
|
mode = "mirror";
|
||||||
|
rootFsOptions = {
|
||||||
|
compression = "lz4";
|
||||||
|
"com.sun:auto-snapshot" = "false";
|
||||||
|
};
|
||||||
|
mountpoint = "/";
|
||||||
|
|
||||||
|
datasets = {
|
||||||
|
zfs_fs = {
|
||||||
|
zfs_type = "filesystem";
|
||||||
|
mountpoint = "/zfs_fs";
|
||||||
|
options."com.sun:auto-snapshot" = "true";
|
||||||
|
};
|
||||||
|
zfs_unmounted_fs = {
|
||||||
|
zfs_type = "filesystem";
|
||||||
|
options.mountpoint = "none";
|
||||||
|
};
|
||||||
|
zfs_legacy_fs = {
|
||||||
|
zfs_type = "filesystem";
|
||||||
|
options.mountpoint = "legacy";
|
||||||
|
mountpoint = "/zfs_legacy_fs";
|
||||||
|
};
|
||||||
|
zfs_testvolume = {
|
||||||
|
zfs_type = "volume";
|
||||||
|
size = "10M";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
|
mountpoint = "/ext4onzfs";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
@ -1,81 +1,81 @@
|
|||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdb";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "ESP";
|
partitions = [
|
||||||
start = "1MiB";
|
{
|
||||||
end = "100MiB";
|
type = "partition";
|
||||||
fs-type = "FAT32";
|
name = "ESP";
|
||||||
bootable = true;
|
# fs-type = "FAT32";
|
||||||
content = {
|
start = "1MiB";
|
||||||
type = "filesystem";
|
end = "100MiB";
|
||||||
format = "vfat";
|
bootable = true;
|
||||||
mountpoint = "/boot";
|
|
||||||
options = [
|
|
||||||
"defaults"
|
|
||||||
];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
type = "partition";
|
|
||||||
part-type = "primary";
|
|
||||||
start = "100MiB";
|
|
||||||
end = "100%";
|
|
||||||
content = {
|
|
||||||
type = "luks";
|
|
||||||
algo = "aes-xts...";
|
|
||||||
name = "crypted";
|
|
||||||
keyfile = "/tmp/secret.key";
|
|
||||||
extraArgs = [
|
|
||||||
"--hash sha512"
|
|
||||||
"--iter-time 5000"
|
|
||||||
];
|
|
||||||
content = {
|
|
||||||
type = "lvm_pv";
|
|
||||||
vg = "pool";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
pool = {
|
|
||||||
type = "lvm_vg";
|
|
||||||
lvs = {
|
|
||||||
root = {
|
|
||||||
type = "lvm_lv";
|
|
||||||
size = "100M";
|
|
||||||
mountpoint = "/";
|
|
||||||
content = {
|
content = {
|
||||||
type = "filesystem";
|
type = "filesystem";
|
||||||
format = "ext4";
|
format = "vfat";
|
||||||
mountpoint = "/";
|
mountpoint = "/boot";
|
||||||
options = [
|
options = [
|
||||||
"defaults"
|
"defaults"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
home = {
|
{
|
||||||
type = "lvm_lv";
|
type = "partition";
|
||||||
size = "10M";
|
name = "luks";
|
||||||
|
start = "100MiB";
|
||||||
|
end = "100%";
|
||||||
content = {
|
content = {
|
||||||
type = "filesystem";
|
type = "luks";
|
||||||
format = "ext4";
|
name = "crypted";
|
||||||
mountpoint = "/home";
|
keyFile = "/tmp/secret.key";
|
||||||
};
|
extraArgs = [
|
||||||
};
|
"--hash sha512"
|
||||||
raw = {
|
"--iter-time 5000"
|
||||||
type = "lvm_lv";
|
];
|
||||||
size = "10M";
|
content = {
|
||||||
content = {
|
type = "lvm_pv";
|
||||||
type = "noop";
|
vg = "pool";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
lvm_vg = {
|
||||||
|
pool = {
|
||||||
|
type = "lvm_vg";
|
||||||
|
lvs = {
|
||||||
|
root = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "100M";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
|
mountpoint = "/";
|
||||||
|
options = [
|
||||||
|
"defaults"
|
||||||
|
];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
home = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "10M";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
|
mountpoint = "/home";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
raw = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "10M";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,66 +1,74 @@
|
|||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdb";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "primary";
|
partitions = [
|
||||||
start = "0%";
|
{
|
||||||
end = "100%";
|
type = "partition";
|
||||||
content = {
|
name = "primary";
|
||||||
type = "lvm_pv";
|
start = "0%";
|
||||||
vg = "pool";
|
end = "100%";
|
||||||
};
|
content = {
|
||||||
}
|
type = "lvm_pv";
|
||||||
];
|
vg = "pool";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
vdc = {
|
vdc = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdc";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "primary";
|
partitions = [
|
||||||
start = "0%";
|
{
|
||||||
end = "100%";
|
type = "partition";
|
||||||
content = {
|
name = "primary";
|
||||||
type = "lvm_pv";
|
start = "0%";
|
||||||
vg = "pool";
|
end = "100%";
|
||||||
};
|
content = {
|
||||||
}
|
type = "lvm_pv";
|
||||||
];
|
vg = "pool";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
lvm_vg = {
|
||||||
pool = {
|
pool = {
|
||||||
type = "lvm_vg";
|
type = "lvm_vg";
|
||||||
lvs = {
|
lvs = {
|
||||||
root = {
|
root = {
|
||||||
type = "lvm_lv";
|
type = "lvm_lv";
|
||||||
size = "100M";
|
size = "100M";
|
||||||
|
lvm_type = "mirror";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
mountpoint = "/";
|
mountpoint = "/";
|
||||||
lvm_type = "mirror";
|
options = [
|
||||||
content = {
|
"defaults"
|
||||||
type = "filesystem";
|
];
|
||||||
format = "ext4";
|
|
||||||
mountpoint = "/";
|
|
||||||
options = [
|
|
||||||
"defaults"
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
home = {
|
|
||||||
type = "lvm_lv";
|
|
||||||
size = "10M";
|
|
||||||
lvm_type = "raid0";
|
|
||||||
content = {
|
|
||||||
type = "filesystem";
|
|
||||||
format = "ext4";
|
|
||||||
mountpoint = "/home";
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
home = {
|
||||||
|
type = "lvm_lv";
|
||||||
|
size = "10M";
|
||||||
|
lvm_type = "raid0";
|
||||||
|
content = {
|
||||||
|
type = "filesystem";
|
||||||
|
format = "ext4";
|
||||||
|
mountpoint = "/home";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,47 @@
|
|||||||
# usage: nix-instantiate --eval --json --strict example/config.nix | jq .
|
|
||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdb";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "primary";
|
partitions = [
|
||||||
start = "1MiB";
|
{
|
||||||
end = "100%";
|
type = "partition";
|
||||||
content = {
|
name = "mdadm";
|
||||||
type = "mdraid";
|
start = "1MiB";
|
||||||
name = "raid1";
|
end = "100%";
|
||||||
};
|
content = {
|
||||||
}
|
type = "mdraid";
|
||||||
];
|
name = "raid1";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
vdc = {
|
vdc = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdc";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
part-type = "primary";
|
partitions = [
|
||||||
start = "1MiB";
|
{
|
||||||
end = "100%";
|
type = "partition";
|
||||||
content = {
|
name = "mdadm";
|
||||||
type = "mdraid";
|
start = "1MiB";
|
||||||
name = "raid1";
|
end = "100%";
|
||||||
};
|
content = {
|
||||||
}
|
type = "mdraid";
|
||||||
];
|
name = "raid1";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
mdadm = {
|
||||||
raid1 = {
|
raid1 = {
|
||||||
type = "mdadm";
|
type = "mdadm";
|
||||||
level = 1;
|
level = 1;
|
||||||
@ -43,7 +51,7 @@
|
|||||||
partitions = [
|
partitions = [
|
||||||
{
|
{
|
||||||
type = "partition";
|
type = "partition";
|
||||||
part-type = "primary";
|
name = "primary";
|
||||||
start = "1MiB";
|
start = "1MiB";
|
||||||
end = "100%";
|
end = "100%";
|
||||||
content = {
|
content = {
|
||||||
|
@ -1,41 +1,46 @@
|
|||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "table";
|
type = "disk";
|
||||||
format = "gpt";
|
device = "/dev/vdb";
|
||||||
partitions = [
|
content = {
|
||||||
{
|
type = "table";
|
||||||
type = "partition";
|
format = "gpt";
|
||||||
# leave space for the grub aka BIOS boot
|
partitions = [
|
||||||
start = "0%";
|
{
|
||||||
end = "100%";
|
type = "partition";
|
||||||
part-type = "primary";
|
start = "0%";
|
||||||
bootable = true;
|
end = "100%";
|
||||||
content = {
|
name = "primary";
|
||||||
type = "filesystem";
|
bootable = true;
|
||||||
format = "ext4";
|
content = {
|
||||||
mountpoint = "/";
|
type = "filesystem";
|
||||||
};
|
format = "ext4";
|
||||||
}
|
mountpoint = "/";
|
||||||
];
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
vdc = {
|
vdc = {
|
||||||
type = "zfs";
|
type = "disk";
|
||||||
pool = "zroot";
|
device = "/dev/vdc";
|
||||||
|
content = {
|
||||||
|
type = "zfs";
|
||||||
|
pool = "zroot";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
zpool = {
|
||||||
zroot = {
|
zroot = {
|
||||||
type = "zpool";
|
type = "zpool";
|
||||||
mountpoint = "/";
|
datasets = {
|
||||||
|
zfs_fs = {
|
||||||
datasets = [
|
zfs_type = "filesystem";
|
||||||
{
|
|
||||||
type = "zfs_filesystem";
|
|
||||||
name = "zfs_fs";
|
|
||||||
mountpoint = "/zfs_fs";
|
mountpoint = "/zfs_fs";
|
||||||
options."com.sun:auto-snapshot" = "true";
|
options."com.sun:auto-snapshot" = "true";
|
||||||
}
|
};
|
||||||
];
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
{
|
{
|
||||||
type = "devices";
|
disk = {
|
||||||
content = {
|
|
||||||
vdb = {
|
vdb = {
|
||||||
type = "zfs";
|
type = "disk";
|
||||||
pool = "zroot";
|
device = "/dev/vdb";
|
||||||
|
content = {
|
||||||
|
type = "zfs";
|
||||||
|
pool = "zroot";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
vdc = {
|
vdc = {
|
||||||
type = "zfs";
|
type = "disk";
|
||||||
pool = "zroot";
|
device = "/dev/vdc";
|
||||||
|
content = {
|
||||||
|
type = "zfs";
|
||||||
|
pool = "zroot";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
zpool = {
|
||||||
zroot = {
|
zroot = {
|
||||||
type = "zpool";
|
type = "zpool";
|
||||||
mode = "mirror";
|
mode = "mirror";
|
||||||
@ -18,35 +27,31 @@
|
|||||||
};
|
};
|
||||||
mountpoint = "/";
|
mountpoint = "/";
|
||||||
|
|
||||||
datasets = [
|
datasets = {
|
||||||
{
|
zfs_fs = {
|
||||||
type = "zfs_filesystem";
|
zfs_type = "filesystem";
|
||||||
name = "zfs_fs";
|
|
||||||
mountpoint = "/zfs_fs";
|
mountpoint = "/zfs_fs";
|
||||||
options."com.sun:auto-snapshot" = "true";
|
options."com.sun:auto-snapshot" = "true";
|
||||||
}
|
};
|
||||||
{
|
zfs_unmounted_fs = {
|
||||||
type = "zfs_filesystem";
|
zfs_type = "filesystem";
|
||||||
name = "zfs_unmounted_fs";
|
|
||||||
options.mountpoint = "none";
|
options.mountpoint = "none";
|
||||||
}
|
};
|
||||||
{
|
zfs_legacy_fs = {
|
||||||
type = "zfs_filesystem";
|
zfs_type = "filesystem";
|
||||||
name = "zfs_legacy_fs";
|
|
||||||
options.mountpoint = "legacy";
|
options.mountpoint = "legacy";
|
||||||
mountpoint = "/zfs_legacy_fs";
|
mountpoint = "/zfs_legacy_fs";
|
||||||
}
|
};
|
||||||
{
|
zfs_testvolume = {
|
||||||
type = "zfs_volume";
|
zfs_type = "volume";
|
||||||
name = "zfs_testvolume";
|
|
||||||
size = "10M";
|
size = "10M";
|
||||||
content = {
|
content = {
|
||||||
type = "filesystem";
|
type = "filesystem";
|
||||||
format = "ext4";
|
format = "ext4";
|
||||||
mountpoint = "/ext4onzfs";
|
mountpoint = "/ext4onzfs";
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
];
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
20
tests/complex.nix
Normal file
20
tests/complex.nix
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{ pkgs ? (import <nixpkgs> { })
|
||||||
|
, makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
|
||||||
|
}:
|
||||||
|
makeDiskoTest {
|
||||||
|
disko-config = import ../example/complex.nix;
|
||||||
|
extraTestScript = ''
|
||||||
|
machine.succeed("test -b /dev/zroot/zfs_testvolume");
|
||||||
|
machine.succeed("test -b /dev/md/raid1p1");
|
||||||
|
|
||||||
|
|
||||||
|
machine.succeed("mountpoint /mnt");
|
||||||
|
machine.succeed("mountpoint /mnt/zfs_fs");
|
||||||
|
machine.succeed("mountpoint /mnt/zfs_legacy_fs");
|
||||||
|
machine.succeed("mountpoint /mnt/ext4onzfs");
|
||||||
|
machine.succeed("mountpoint /mnt/ext4_on_lvm");
|
||||||
|
'';
|
||||||
|
extraConfig = {
|
||||||
|
boot.kernelModules = [ "dm-raid" "dm-mirror" ];
|
||||||
|
};
|
||||||
|
}
|
@ -17,7 +17,7 @@
|
|||||||
};
|
};
|
||||||
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create disko-config);
|
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create disko-config);
|
||||||
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount disko-config);
|
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount disko-config);
|
||||||
num-disks = builtins.length (builtins.filter (x: builtins.match "vd." x == [ ]) (lib.attrNames disko-config.content));
|
num-disks = builtins.length (lib.attrNames disko-config.disk);
|
||||||
in
|
in
|
||||||
makeTest' {
|
makeTest' {
|
||||||
name = "disko";
|
name = "disko";
|
||||||
|
Loading…
Reference in New Issue
Block a user