Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP bcachefs #675

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions example/bcachefs-multidisk.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
disko.devices = {
disk = {
x = {
type = "disk";
device = "/dev/sdx";
content = {
type = "gpt";
partitions = {
ESP = {
size = "64M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
bcachefs = {
size = "100%";
content = {
type = "bcachefs";
pool = "broot";
};
};
};
};
};
y = {
type = "disk";
device = "/dev/sdy";
content = {
type = "gpt";
partitions = {
bcachefs = {
size = "100%";
content = {
type = "bcachefs";
pool = "broot";
};
};
};
};
};
};
bcachefspool = {
broot = {
type = "bcachefspool";
mountpoint = "/";
};
};
};
}

24 changes: 20 additions & 4 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, rootMountPoint ? "/mnt"
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
, toplevel-config ? {}
}:
with lib;
with builtins;
Expand Down Expand Up @@ -35,7 +36,7 @@ let
# option for valid contents of partitions (basically like devices, but without tables)
partitionType = extraArgs: lib.mkOption {
type = lib.types.nullOr (diskoLib.subType {
types = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; };
types = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap bcachefs; };
inherit extraArgs;
});
default = null;
Expand All @@ -45,7 +46,7 @@ let
# option for valid contents of devices
deviceType = extraArgs: lib.mkOption {
type = lib.types.nullOr (diskoLib.subType {
types = { inherit (diskoLib.types) table gpt btrfs filesystem zfs mdraid luks lvm_pv swap; };
types = { inherit (diskoLib.types) table gpt btrfs filesystem zfs mdraid luks lvm_pv swap bcachefs; };
inherit extraArgs;
});
default = null;
Expand Down Expand Up @@ -219,7 +220,7 @@ let
postMountHook = diskoLib.mkHook "shell commands to run after mount";
};
config._module.args = {
inherit diskoLib rootMountPoint;
inherit diskoLib rootMountPoint toplevel-config;
};
}
];
Expand Down Expand Up @@ -344,7 +345,7 @@ let
*/
toplevel = lib.types.submodule (cfg:
let
devices = { inherit (cfg.config) disk mdadm zpool lvm_vg nodev; };
devices = { inherit (cfg.config) disk mdadm zpool lvm_vg nodev bcachefspool; };
in
{
options = {
Expand All @@ -363,6 +364,11 @@ let
default = { };
description = "ZFS pool device";
};
bcachefspool = lib.mkOption {
type = lib.types.attrsOf diskoLib.types.bcachefspool;
default = { };
description = "BcacheFS pool device";
};
lvm_vg = lib.mkOption {
type = lib.types.attrsOf diskoLib.types.lvm_vg;
default = { };
Expand Down Expand Up @@ -523,6 +529,16 @@ let
in
lib.genAttrs configKeys (key: lib.mkMerge (lib.catAttrs key collectedConfigs));
};
_internal = {
bcachefspools = lib.mkOption {
internal = true;
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
description = ''
Disko Internal List of BcacheFS pool's
'';
default = {};
};
};
};
});

Expand Down
75 changes: 75 additions & 0 deletions lib/types/bcachefs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{ config, options, lib, diskoLib, parent, device, ... }:
{
options = {
type = lib.mkOption {
type = lib.types.enum [ "bcachefs" ];
internal = true;
description = "Type";
};
device = lib.mkOption {
type = lib.types.str;
description = "Device";
default = device;
};

pool = lib.mkOption {
type = lib.types.str;
description = "Pool";
};

label = lib.mkOption {
type = lib.types.str;
default = config._module.args.name;
description = "Label";
};

formatArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "Formating Arguments";
};

_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev: {
deviceDependencies.bcachefspool.${config.pool} = [ dev ];
};
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
echo ${config.device} >>"$disko_devices_dir"/bcachefs_${config.pool}/devices
echo ${config.label} >>"$disko_devices_dir"/bcachefs_${config.pool}/labels
echo ${lib.concatStringsSep " " config.formatArgs} >>"$disko_devices_dir"/bcachefs_${config.pool}/format_args
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default = { };
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = [
{
disko.devices._internal.bcachefspools.${config.pool} = [ (lib.traceVal config.device) ];
}
];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.bcachefs-tools ];
description = "Packages";
};
};
}
100 changes: 100 additions & 0 deletions lib/types/bcachefspool.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{ config, options, lib, rootMountPoint, diskoLib, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
type = lib.types.str;
default = config._module.args.name;
description = "Name";
};

type = lib.mkOption {
type = lib.types.enum [ "bcachefspool" ];
default = "bcachefspool";
internal = true;
description = "Type";
};

formatArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "Formating Arguments";
};

mountOptions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ "defaults" ];
description = "Mount options";
};

mountpoint = lib.mkOption {
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
default = null;
description = "A path to mount the bcachefs filesystem to.";
};

_meta = lib.mkOption {
internal = true;
readOnly = true;
type = diskoLib.jsonType;
default = { };
description = "Metadata";
};

_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
readarray -t bcachefs_devices < <(cat "$disko_devices_dir"/bcachefs_${config.name}/devices)
readarray -t bcachefs_labels < <(cat "$disko_devices_dir"/bcachefs_${config.name}/labels)
readarray -t bcachefs_format_options < <(cat "$disko_devices_dir"/bcachefs_${config.name}/format_args)

device_configs=()

for ((i=0; i<''${#bcachefs_devices[@]}; i++)); do
device=''${bcachefs_devices[$i]}
label=''${bcachefs_labels[$i]}
format_options=''${bcachefs_format_options[$i]}
device_configs+=("--label=$label $format_options $device")
done

bcachefs format --fs_label=${config.name} ${lib.concatStringsSep " " config.formatArgs} \
$(IFS=' \' ; echo "''${device_configs[*]}")
'';
};

_mount = diskoLib.mkMountOption {
inherit config options;
default = {
fs = lib.optionalAttrs (config.mountpoint != null) {
${config.mountpoint} = ''
readarray -t bcachefs_devices < <(cat "$disko_devices_dir"/bcachefs_${config.name}/devices)

mount -t bcachefs $(IFS=':' ; echo ''${bcachefs_devices[*]}) "${rootMountPoint}${config.mountpoint}" \
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
-o X-mount.mkdir
'';
};
};
};

_config = lib.mkOption {
internal = true;
readOnly = true;
default = [ {
fileSystems.${config.mountpoint} = {
device = "${lib.concatStringsSep ":" (lib.traceVal toplevel-config.disko.devices._internal.bcachefspools).${config.name}}";
fsType = "bcachefs";
options = config.mountOptions;
};
}
];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [];
description = "Packages";
};
};
}
4 changes: 2 additions & 2 deletions lib/types/disk.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
Expand All @@ -24,7 +24,7 @@
'';
default = "2G";
};
content = diskoLib.deviceType { parent = config; device = config.device; };
content = diskoLib.deviceType { parent = config; device = config.device; inherit toplevel-config; };
_meta = lib.mkOption {
internal = true;
readOnly = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/types/gpt.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, parent, device, ... }:
{ config, options, lib, diskoLib, parent, device, toplevel-config, ... }:
let
sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions);
sortedHybridPartitions = lib.filter (p: p.hybrid != null) sortedPartitions;
Expand Down Expand Up @@ -95,7 +95,7 @@ in
or - for relative sizes from the disks end
'';
};
content = diskoLib.partitionType { parent = config; device = partition.config.device; };
content = diskoLib.partitionType { parent = config; device = partition.config.device; inherit toplevel-config; };
hybrid = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule ({ ... } @ hp: {
options = {
Expand Down
4 changes: 2 additions & 2 deletions lib/types/luks.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, parent, device, ... }:
{ config, options, lib, diskoLib, parent, device, toplevel-config, ... }:
let
keyFile =
if config.settings ? "keyFile"
Expand Down Expand Up @@ -96,7 +96,7 @@ in
description = "Extra arguments to pass to `cryptsetup luksOpen` when opening";
example = [ "--timeout 10" ];
};
content = diskoLib.deviceType { parent = config; device = "/dev/mapper/${config.name}"; };
content = diskoLib.deviceType { parent = config; device = "/dev/mapper/${config.name}"; inherit toplevel-config; };
_parent = lib.mkOption {
internal = true;
default = parent;
Expand Down
4 changes: 2 additions & 2 deletions lib/types/lvm_vg.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
Expand Down Expand Up @@ -42,7 +42,7 @@
default = [ ];
description = "Extra arguments";
};
content = diskoLib.partitionType { parent = config; device = "/dev/${config.name}/${lv.config.name}"; };
content = diskoLib.partitionType { parent = config; device = "/dev/${config.name}/${lv.config.name}"; inherit toplevel-config; };
};
}));
default = { };
Expand Down
4 changes: 2 additions & 2 deletions lib/types/mdadm.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
Expand All @@ -22,7 +22,7 @@
default = "default";
description = "Metadata";
};
content = diskoLib.deviceType { parent = config; device = "/dev/md/${config.name}"; };
content = diskoLib.deviceType { parent = config; device = "/dev/md/${config.name}"; inherit toplevel-config; };
_meta = lib.mkOption {
internal = true;
readOnly = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/types/table.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, parent, device, ... }:
{ config, options, lib, diskoLib, parent, device, toplevel-config, ... }:
{
options = lib.warn ''
The legacy table is outdated and should not be used. We recommend using the gpt type instead.
Expand Down Expand Up @@ -61,7 +61,7 @@
default = false;
description = "Whether to make the partition bootable";
};
content = diskoLib.partitionType { parent = config; device = diskoLib.deviceNumbering config.device partition.config._index; };
content = diskoLib.partitionType { parent = config; device = diskoLib.deviceNumbering config.device partition.config._index; inherit toplevel-config; };
_index = lib.mkOption {
internal = true;
default = lib.toInt (lib.head (builtins.match ".*entry ([[:digit:]]+)]" name));
Expand Down
Loading