Skip to content

Commit

Permalink
WIP bcachefs
Browse files Browse the repository at this point in the history
  • Loading branch information
jopejoe1 committed Jun 12, 2024
1 parent 1f79052 commit e46a467
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 40 deletions.
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
25 changes: 15 additions & 10 deletions lib/types/bcachefs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
default = device;
};

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

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

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

_parent = lib.mkOption {
Expand All @@ -37,16 +38,16 @@
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev: {
deviceDependencies.bcachefspool.${config.name} = [ dev ];
deviceDependencies.bcachefspool.${config.pool} = [ dev ];
};
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
echo ${config.device} >>"$disko_devices_dir"/bcachefs_${config.name}/devices
echo ${config.label} >>"$disko_devices_dir"/bcachefs_${config.name}/labels
echo ${lib.concatStringsSep " " config.formatOptions} >>"$disko_devices_dir"/bcachefs_${config.name}/format_options
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 {
Expand All @@ -56,7 +57,11 @@
_config = lib.mkOption {
internal = true;
readOnly = true;
default = [ ];
default = [
{
disko.devices._internal.bcachefspools.${config.pool} = [ (lib.traceVal config.device) ];
}
];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
Expand Down
34 changes: 22 additions & 12 deletions lib/types/bcachefspool.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, rootMountPoint, diskoLib, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
Expand All @@ -14,10 +14,10 @@
description = "Type";
};

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

mountOptions = lib.mkOption {
Expand All @@ -36,8 +36,7 @@
internal = true;
readOnly = true;
type = diskoLib.jsonType;
default =
lib.optionalAttrs (config.content != null) (config.content._meta [ "bcachefspool" config.name ]);
default = { };
description = "Metadata";
};

Expand All @@ -46,7 +45,7 @@
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_options)
readarray -t bcachefs_format_options < <(cat "$disko_devices_dir"/bcachefs_${config.name}/format_args)
device_configs=()
Expand All @@ -57,26 +56,37 @@
device_configs+=("--label=$label $format_options $device")
done
bcachefs format --fs_label=${config.name} ${lib.concatStringsSep " " config.formatOptions} \
bcachefs format --fs_label=${config.name} ${lib.concatStringsSep " " config.formatArgs} \
$(IFS=' \' ; echo "''${device_configs[*]}")
'';
};

_mount = diskoLib.mkMountOption {
inherit config options;
default = "TODO";
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 = lib.optional (config.options.mountpoint or "" != "none") {
default = [ {
fileSystems.${config.mountpoint} = {
device = "${lib.concatStringsSep ":" config.formatOptions}";
device = "${lib.concatStringsSep ":" (lib.traceVal toplevel-config.disko.devices._internal.bcachefspools).${config.name}}";
fsType = "bcachefs";
options = config.mountOptions;
};
};
}
];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
Expand Down
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
4 changes: 2 additions & 2 deletions lib/types/zfs_volume.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, parent, ... }:
{ config, options, lib, diskoLib, parent, toplevel-config, ... }:
{
options = {
name = lib.mkOption {
Expand Down Expand Up @@ -30,7 +30,7 @@
description = "Size of the dataset";
};

content = diskoLib.partitionType { parent = config; device = "/dev/zvol/${config._parent.name}/${config.name}"; };
content = diskoLib.partitionType { parent = config; device = "/dev/zvol/${config._parent.name}/${config.name}"; inherit toplevel-config; };

_parent = lib.mkOption {
internal = true;
Expand Down
1 change: 1 addition & 0 deletions module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let
rootMountPoint = config.disko.rootMountPoint;
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
toplevel-config = config;
};
cfg = config.disko;
in
Expand Down
Loading

0 comments on commit e46a467

Please sign in to comment.