mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 13:03:28 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff442f5d14 | ||
|
|
6bb82b77ce | ||
|
|
06fb9283ca | ||
|
|
69265ccde5 | ||
|
|
cbc1f418d0 | ||
|
|
aa5d53aee8 | ||
|
|
c7e0b00007 | ||
|
|
aba0ae38df | ||
|
|
b5cfd59e9a | ||
|
|
c4fe2d108b | ||
|
|
78d6a1365c | ||
|
|
8d78963819 | ||
|
|
b5d1320ebc | ||
|
|
7b63642358 | ||
|
|
d0c543d740 | ||
|
|
ca27b88c88 | ||
|
|
c5140c6079 | ||
|
|
1770bf1ae5 | ||
|
|
51d33bbb7f | ||
|
|
76c0a6dba3 | ||
|
|
a8e75da08f | ||
|
|
8f806681d7 | ||
|
|
329d3d7e8b | ||
|
|
f6dbc8952d | ||
|
|
ddbe63d43e | ||
|
|
b36e87600d | ||
|
|
212ff71553 | ||
|
|
b2c95fb68f | ||
|
|
0d8c6ad4a4 | ||
|
|
0c8f1b9eab | ||
|
|
46b9f51eb6 | ||
|
|
eea057603d | ||
|
|
9a7ab516cf | ||
|
|
87d86e499c | ||
|
|
23f112ad4e |
No files matched your search
@@ -4,3 +4,6 @@ result-*
|
||||
|
||||
# Created by the NixOS interactive test driver
|
||||
.nixos-test-history
|
||||
|
||||
# Created by `:bl` in `nix repl`
|
||||
repl-result-out
|
||||
@@ -108,7 +108,7 @@ let
|
||||
else if hasDiskoModuleFlake then
|
||||
(builtins.getFlake flake).nixosConfigurations.${flakeAttr}.config.system.build.${diskoAttr}
|
||||
or (pkgs.writeShellScriptBin "disko-compat-error" ''
|
||||
echo 'Error: Attribute `nixosConfigurations.${flakeAttr}.config.system.build.${diskoAttr}` >&2
|
||||
echo 'Error: Attribute `nixosConfigurations.${flakeAttr}.config.system.build.${diskoAttr}`' >&2
|
||||
echo ' not found in flake `${flake}`!' >&2
|
||||
echo ' This is probably caused by the locked version of disko in the flake' >&2
|
||||
echo ' being different from the version of disko you executed.' >&2
|
||||
|
||||
@@ -4,5 +4,7 @@ set -efux -o pipefail
|
||||
disk=$(realpath "$1")
|
||||
|
||||
lsblk -a -f >&2
|
||||
# since we currently cannot recursively deactivate swaps on zfs volumes, we need to deactivate all of them.
|
||||
lsblk --output-all --json | jq -r -f "$(dirname "$0")/zfs-swap-deactivate.jq" | bash -x
|
||||
lsblk --output-all --json | jq -r --arg disk_to_clear "$disk" -f "$(dirname "$0")/disk-deactivate.jq" | bash -x
|
||||
lsblk -a -f >&2
|
||||
@@ -1,20 +1,25 @@
|
||||
def shellquote:
|
||||
gsub("'"; "''\\''") |
|
||||
"'" + . + "'"
|
||||
;
|
||||
|
||||
# since lsblk lacks zfs support, we have to do it this way
|
||||
def remove:
|
||||
if .fstype == "zfs_member" then
|
||||
"if type zpool >/dev/null; then zpool destroy -f \(.label); zpool labelclear -f \(.label); fi"
|
||||
elif .fstype == "LVM2_member" then
|
||||
[
|
||||
"vg=$(pvs \(.path) --noheadings --options vg_name | grep -o '[a-zA-Z0-9-]*')",
|
||||
"vg=$(pvs \(.path | shellquote) --noheadings --options vg_name | grep -o '[a-zA-Z0-9-]*')",
|
||||
"vgchange -a n \"$vg\"",
|
||||
"vgremove -f \"$vg\""
|
||||
]
|
||||
elif .fstype == "swap" then
|
||||
"swapoff \(.path)"
|
||||
"swapoff \(.path | shellquote)"
|
||||
elif .fstype == null then
|
||||
# maybe its zfs
|
||||
[
|
||||
# the next line has some horrible escaping
|
||||
"zpool=$(if type zdb >/dev/null; then zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p'; fi)",
|
||||
"zpool=$(if type zdb >/dev/null; then zdb -l \(.path | shellquote) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p'; fi)",
|
||||
"if [[ -n \"${zpool}\" ]]; then zpool destroy -f \"$zpool\"; zpool labelclear -f \"$zpool\"; fi",
|
||||
"unset zpool"
|
||||
]
|
||||
@@ -27,25 +32,25 @@ def deactivate:
|
||||
if .type == "disk" or .type == "loop" then
|
||||
[
|
||||
# If this disk is a member of raid, stop that raid
|
||||
"md_dev=$(lsblk \(.path) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')",
|
||||
"md_dev=$(lsblk \(.path | shellquote) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')",
|
||||
"if [[ -n \"${md_dev}\" ]]; then umount \"$md_dev\"; mdadm --stop \"$md_dev\"; fi",
|
||||
# Remove all file-systems and other magic strings
|
||||
"wipefs --all -f \(.path)",
|
||||
"wipefs --all -f \(.path | shellquote)",
|
||||
# Remove the MBR bootstrap code
|
||||
"dd if=/dev/zero of=\(.path) bs=440 count=1"
|
||||
"dd if=/dev/zero of=\(.path | shellquote) bs=440 count=1"
|
||||
]
|
||||
elif .type == "part" then
|
||||
[
|
||||
"wipefs --all -f \(.path)"
|
||||
"wipefs --all -f \(.path | shellquote)"
|
||||
]
|
||||
elif .type == "crypt" then
|
||||
[
|
||||
"cryptsetup luksClose \(.path)",
|
||||
"wipefs --all -f \(.path)"
|
||||
"cryptsetup luksClose \(.path | shellquote)",
|
||||
"wipefs --all -f \(.path | shellquote)"
|
||||
]
|
||||
elif .type == "swap" then
|
||||
[
|
||||
"swapoff \(.path)",
|
||||
"swapoff \(.path | shellquote)"
|
||||
]
|
||||
elif .type == "lvm" then
|
||||
(.name | split("-")[0]) as $vgname |
|
||||
@@ -55,7 +60,7 @@ def deactivate:
|
||||
]
|
||||
elif (.type | contains("raid")) then
|
||||
[
|
||||
"mdadm --stop \(.name)"
|
||||
"mdadm --stop \(.name | shellquote)"
|
||||
]
|
||||
else
|
||||
["echo Warning: unknown type '\(.type)'. Consider handling this in https://github.com/nix-community/disko/blob/master/disk-deactivate/disk-deactivate.jq"]
|
||||
@@ -64,7 +69,7 @@ def deactivate:
|
||||
|
||||
def walk:
|
||||
[
|
||||
(.mountpoints[] | select(. != null) | "umount -R \(.)"),
|
||||
(.mountpoints[] | select(. != null) | "umount -R \(. | shellquote)"),
|
||||
((.children // []) | map(walk)),
|
||||
remove,
|
||||
deactivate
|
||||
@@ -87,4 +92,3 @@ def init:
|
||||
;
|
||||
|
||||
.blockdevices | map(init) | flatten | join("\n")
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
def turnOffSwaps:
|
||||
if (.name | test("^zd[0-9]+$")) and .type == "disk" then
|
||||
"swapoff /dev/" + .name
|
||||
else
|
||||
[]
|
||||
end
|
||||
;
|
||||
|
||||
.blockdevices | map(turnOffSwaps) | flatten | join("\n")
|
||||
|
||||
+74
-8
@@ -1,11 +1,9 @@
|
||||
# Generating Disk Images with Secrets Included using Disko
|
||||
|
||||
Using Disko on NixOS allows you to efficiently create `.raw` VM images from a
|
||||
system configuration. The generated image can be used as a VM or directly
|
||||
written to a physical drive to create a bootable disk. Follow the steps below to
|
||||
generate disk images:
|
||||
Using Disko on NixOS allows you to efficiently create `.raw` disk images from a system configuration.
|
||||
Follow the steps below to generate disk images:
|
||||
|
||||
## Generating the `.raw` VM Image
|
||||
## Generating the `.raw` Image
|
||||
|
||||
1. **Create a NixOS configuration that includes the disko and the disk
|
||||
configuration of your choice**
|
||||
@@ -24,16 +22,29 @@ In the this example we create a flake containing a nixos configuration for
|
||||
disko.inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
outputs = { self, disko, nixpkgs }: {
|
||||
outputs = { self, disko, nixpkgs, ... }: {
|
||||
nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
modules = [
|
||||
# You can get this file from here: https://github.com/nix-community/disko/blob/master/example/simple-efi.nix
|
||||
./simple-efi.nix
|
||||
disko.nixosModules.disko
|
||||
({ config, ... }: {
|
||||
({ config, modulesPath, ... }: {
|
||||
imports = [
|
||||
# include this line to boot to boot the image inside a VM
|
||||
# "${modulesPath}/profiles/qemu-guest.nix"
|
||||
# On other hardware, you may need a hardware-configuration.nix as generated by nixos-generate-config or use nixos-facter (https://github.com/numtide/nixos-facter)
|
||||
# ./hardware-configuration.nix
|
||||
];
|
||||
|
||||
# Optional. Useful for testing
|
||||
# users.users.root.initialPassword = "root";
|
||||
|
||||
boot.loader.grub.efiSupport = lib.mkDefault true;
|
||||
boot.loader.grub.efiInstallAsRemovable = lib.mkDefault true;
|
||||
|
||||
# shut up state version warning
|
||||
system.stateVersion = config.system.nixos.version;
|
||||
system.stateVersion = config.system.nixos.release;
|
||||
# Adjust this to your liking.
|
||||
# WARNING: if you set a too low value the image might be not big enough to contain the nixos installation
|
||||
disko.devices.disk.main.imageSize = "10G";
|
||||
@@ -141,3 +152,58 @@ In the this example we create a flake containing a nixos configuration for
|
||||
|
||||
By following these instructions and understanding the process, you can smoothly
|
||||
generate disk images with Disko for your NixOS system configurations.
|
||||
|
||||
## Test the image inside a VM
|
||||
|
||||
Make sure you uncommented the `"${modulesPath}/profiles/qemu-guest.nix"` import in the example above!
|
||||
|
||||
Write the following script to `qemu.nix`. Note that it expects an UEFI compatible image!
|
||||
|
||||
```
|
||||
with import <nixpkgs> {};
|
||||
|
||||
writeShellApplication {
|
||||
name = "test-image";
|
||||
runtimeInputs = [ qemu ];
|
||||
text = ''
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 <path-to-boot-image>"
|
||||
exit 1
|
||||
fi
|
||||
tmpFile=$(mktemp /tmp/test-image.XXXXXX)
|
||||
trap 'rm -f $tmpFile' EXIT
|
||||
cp "$1" "$tmpFile"
|
||||
qemu-system-x86_64 \
|
||||
-enable-kvm \
|
||||
-m 2G \
|
||||
-cpu max \
|
||||
-smp 2 \
|
||||
-netdev user,id=net0,hostfwd=tcp::2222-:22 \
|
||||
-device virtio-net-pci,netdev=net0 \
|
||||
-drive if=pflash,format=raw,readonly=on,file=${OVMF.firmware} \
|
||||
-drive if=pflash,format=raw,readonly=on,file=${OVMF.variables} \
|
||||
-drive "if=virtio,format=raw,file=$tmpFile"
|
||||
'';
|
||||
}
|
||||
```
|
||||
|
||||
This command will run a qemu vm with your image:
|
||||
|
||||
```
|
||||
$(nix-build ./qemu.nix)/bin/test-image ./main.raw
|
||||
```
|
||||
|
||||
Replace `main.raw` with the image that you build!
|
||||
|
||||
Tip: You can customize memory (`-m` flag) or cpu (`-smp` flag) available to the VM to your liking.
|
||||
|
||||
## Build the image inside the nix sandbox
|
||||
|
||||
Instead of the diskoImagesScript, we can also build the image inside the nix store.
|
||||
This approach is slower because it requires copying the image after the build and
|
||||
we also don't have a secure way to embed secrets this way.
|
||||
|
||||
```
|
||||
nix build .#nixosConfigurations.myhost.config.system.build.diskoImages
|
||||
ls -a ./result/main.raw
|
||||
```
|
||||
@@ -1,10 +1,10 @@
|
||||
# Running Interactive VMs with disko
|
||||
# Running Interactive VMs with `disko`
|
||||
|
||||
disko now exports its own flavor of interactive VMs (similiar to
|
||||
config.system.build.vm). Simply import the disko module and build the vm runner
|
||||
with:
|
||||
|
||||
```
|
||||
```bash
|
||||
nix run -L '.#nixosConfigurations.mymachine.config.system.build.vmWithDisko'
|
||||
```
|
||||
|
||||
@@ -21,6 +21,7 @@ option:
|
||||
}
|
||||
```
|
||||
|
||||
extraConfig that is set in disko.tests.extraConfig is also applied to the
|
||||
interactive VMs. imageSize of the VMs will be determined by the imageSize in the
|
||||
disk type in your disko config. memorySize is set by disko.memSize
|
||||
Extra configuration that is set in `disko.tests.extraConfig` is also applied to the
|
||||
interactive VMs. The image size of the VMs will be determined by the
|
||||
`disko.devices.disk.<name>.imageSize`
|
||||
specifications. The memory size is set by `disko.memSize`.
|
||||
+17
-2
@@ -55,15 +55,30 @@ Instead, you can build and then run the VM for a test in interactive mode. This
|
||||
will create the VM and all virtual disks as required by the test's config, but
|
||||
allow you to interact with the machine on a terminal afterwards.
|
||||
|
||||
First, build the interactive test driver and run it:
|
||||
First, build the interactive test driver:
|
||||
|
||||
```
|
||||
nix build .#checks.x86_64-linux.simple-efi.driverInteractive
|
||||
```
|
||||
|
||||
The build outputs will be linked in the "result" directory. Of interest will be
|
||||
the generated Python "test-script" it contains, which will contain the commands
|
||||
that the test driver will run.
|
||||
|
||||
You can run the test driver with:
|
||||
```
|
||||
result/bin/nixos-test-driver --keep-vm-state
|
||||
```
|
||||
|
||||
This will open an IPython prompt in which you can use the same objects and
|
||||
functions as in `extraTestScript`. In there, you can run
|
||||
functions as in `extraTestScript`.
|
||||
|
||||
In there, you can debug the test-script, by copy-pasting and running (parts of)
|
||||
its contents into the prompt. This will, for example, allow you to inspect the
|
||||
VM state after running each of the generated "disko-format", "disko-mount",
|
||||
"disko-destroy-format-mount", etc. scripts.
|
||||
|
||||
From the prompt, you can also run:
|
||||
|
||||
```
|
||||
machine.shell_interact()
|
||||
|
||||
+125
-10
@@ -1,15 +1,15 @@
|
||||
{
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
device = "/dev/disk/by-path/pci-0000:02:00.0-nvme-1";
|
||||
vdb = {
|
||||
device = "/dev/vdb";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
end = "500M";
|
||||
vdb1 = {
|
||||
type = "EF00";
|
||||
size = "100M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
@@ -17,18 +17,133 @@
|
||||
mountOptions = [ "umask=0077" ];
|
||||
};
|
||||
};
|
||||
root = {
|
||||
name = "root";
|
||||
end = "-0";
|
||||
|
||||
vdb2 = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "bcachefs";
|
||||
mountpoint = "/";
|
||||
type = "bcachefs";
|
||||
# This refers to a filesystem in the `bcachefs_filesystems` attrset below.
|
||||
filesystem = "mounted_subvolumes_in_multi";
|
||||
label = "group_a.vdb2";
|
||||
extraFormatArgs = [
|
||||
"--discard"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
vdc = {
|
||||
device = "/dev/vdc";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
vdc1 = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "bcachefs";
|
||||
filesystem = "mounted_subvolumes_in_multi";
|
||||
label = "group_a.vdc1";
|
||||
extraFormatArgs = [
|
||||
"--discard"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
vdd = {
|
||||
device = "/dev/vdd";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
vdd1 = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "bcachefs";
|
||||
filesystem = "mounted_subvolumes_in_multi";
|
||||
label = "group_b.vdd1";
|
||||
extraFormatArgs = [
|
||||
"--force"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
vde = {
|
||||
device = "/dev/vde";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
vdd1 = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "bcachefs";
|
||||
filesystem = "relies_on_external_subvolume";
|
||||
label = "group_a.vde1";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
bcachefs_filesystems = {
|
||||
# Example showing mounted subvolumes in a multi-disk configuration.
|
||||
mounted_subvolumes_in_multi = {
|
||||
type = "bcachefs_filesystem";
|
||||
passwordFile = "/tmp/secret.key";
|
||||
extraFormatArgs = [
|
||||
"--compression=lz4"
|
||||
"--background_compression=lz4"
|
||||
];
|
||||
subvolumes = {
|
||||
# Subvolume name is different from mountpoint.
|
||||
"subvolumes/root" = {
|
||||
mountpoint = "/";
|
||||
mountOptions = [
|
||||
"verbose"
|
||||
];
|
||||
};
|
||||
# Subvolume name is the same as the mountpoint.
|
||||
"subvolumes/home" = {
|
||||
mountpoint = "/home";
|
||||
};
|
||||
# Nested subvolume doesn't need a mountpoint as its parent is mounted.
|
||||
"subvolumes/home/user" = {
|
||||
};
|
||||
# Parent is not mounted so the mountpoint must be set.
|
||||
"subvolumes/nix" = {
|
||||
mountpoint = "/nix";
|
||||
};
|
||||
# This subvolume will be created but not mounted.
|
||||
"subvolumes/test" = {
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# Example showing a bcachefs filesystem without subvolumes,
|
||||
# which relies on a subvolume in another filesystem being mounted
|
||||
# and uses a hard-coded UUID.
|
||||
relies_on_external_subvolume = {
|
||||
type = "bcachefs_filesystem";
|
||||
mountpoint = "/home/Documents";
|
||||
extraFormatArgs = [
|
||||
"--compression=lz4"
|
||||
"--background_compression=lz4"
|
||||
];
|
||||
mountOptions = [
|
||||
"verbose"
|
||||
];
|
||||
uuid = "64e50034-ebe2-eaf8-1f93-cf56266a8d86";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
+3
-3
@@ -107,13 +107,13 @@
|
||||
lvm_type = "mirror";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "exfat";
|
||||
mountpoint = "/exfat_on_lvm";
|
||||
format = "ext4";
|
||||
mountpoint = "/ext4_on_lvm";
|
||||
mountOptions = [
|
||||
"defaults"
|
||||
];
|
||||
postMountHook = ''
|
||||
touch /mnt/exfat_on_lvm/file-from-postMountHook
|
||||
touch /mnt/ext4_on_lvm/file-from-postMountHook
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
Generated
+3
-3
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1738136902,
|
||||
"narHash": "sha256-pUvLijVGARw4u793APze3j6mU1Zwdtz7hGkGGkD87qw=",
|
||||
"lastModified": 1746576598,
|
||||
"narHash": "sha256-FshoQvr6Aor5SnORVvh/ZdJ1Sa2U4ZrIMwKBX5k2wu0=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "9a5db3142ce450045840cc8d832b13b8a2018e0c",
|
||||
"rev": "b3582c75c7f21ce0b429898980eddbbf05c68e55",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"i686-linux"
|
||||
"aarch64-linux"
|
||||
"riscv64-linux"
|
||||
"aarch64-darwin"
|
||||
];
|
||||
forAllSystems = lib.genAttrs supportedSystems;
|
||||
|
||||
@@ -68,6 +69,12 @@
|
||||
diskoVersion = version;
|
||||
};
|
||||
|
||||
checkJqSyntax = pkgs.runCommand "check-jq-syntax" { nativeBuildInputs = [ pkgs.jq ]; } ''
|
||||
echo '{ "blockdevices" : [] }' | jq -r -f ${./disk-deactivate/disk-deactivate.jq} --arg disk_to_clear foo
|
||||
echo '{ "blockdevices" : [] }' | jq -r -f ${./disk-deactivate/zfs-swap-deactivate.jq}
|
||||
touch $out
|
||||
'';
|
||||
|
||||
jsonTypes = pkgs.writeTextFile {
|
||||
name = "jsonTypes";
|
||||
text = (builtins.toJSON diskoLib.jsonTypes);
|
||||
@@ -83,7 +90,7 @@
|
||||
//
|
||||
pkgs.lib.optionalAttrs (!pkgs.stdenv.buildPlatform.isRiscV64 && !pkgs.stdenv.hostPlatform.isx86_32)
|
||||
{
|
||||
inherit jsonTypes treefmt;
|
||||
inherit jsonTypes treefmt checkJqSyntax;
|
||||
inherit (self.packages.${system}) disko-doc;
|
||||
}
|
||||
);
|
||||
|
||||
+19
-3
@@ -44,6 +44,7 @@ let
|
||||
# option for valid contents of partitions (basically like devices, but without tables)
|
||||
_partitionTypes = {
|
||||
inherit (diskoLib.types)
|
||||
bcachefs
|
||||
btrfs
|
||||
filesystem
|
||||
zfs
|
||||
@@ -69,6 +70,7 @@ let
|
||||
# option for valid contents of devices
|
||||
_deviceTypes = {
|
||||
inherit (diskoLib.types)
|
||||
bcachefs
|
||||
table
|
||||
gpt
|
||||
btrfs
|
||||
@@ -611,6 +613,7 @@ let
|
||||
let
|
||||
devices = {
|
||||
inherit (cfg.config)
|
||||
bcachefs_filesystems
|
||||
disk
|
||||
mdadm
|
||||
zpool
|
||||
@@ -621,6 +624,11 @@ let
|
||||
in
|
||||
{
|
||||
options = {
|
||||
bcachefs_filesystems = lib.mkOption {
|
||||
type = lib.types.attrsOf diskoLib.types.bcachefs_filesystem;
|
||||
default = { };
|
||||
description = "bcachefs filesystem";
|
||||
};
|
||||
disk = lib.mkOption {
|
||||
type = lib.types.attrsOf diskoLib.types.disk;
|
||||
default = { };
|
||||
@@ -687,6 +695,7 @@ let
|
||||
throw "No disks defined, did you forget to import your disko config?"
|
||||
else
|
||||
v;
|
||||
# @todo Do we need to add bcachefs-tools or not?
|
||||
destroyDependencies = with pkgs; [
|
||||
util-linux
|
||||
e2fsprogs
|
||||
@@ -866,9 +875,15 @@ let
|
||||
umount -Rv "${rootMountPoint}" || :
|
||||
|
||||
# shellcheck disable=SC2043,2041
|
||||
for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do
|
||||
for dev in ${
|
||||
toString (
|
||||
lib.catAttrs "device" (lib.attrValues (lib.filterAttrs (name: disk: disk.destroy) devices.disk))
|
||||
)
|
||||
};
|
||||
do
|
||||
$BASH ${../disk-deactivate}/disk-deactivate "$dev"
|
||||
done
|
||||
|
||||
'';
|
||||
};
|
||||
_destroy = lib.mkOption {
|
||||
@@ -879,7 +894,8 @@ let
|
||||
'';
|
||||
default =
|
||||
let
|
||||
selectedDisks = lib.escapeShellArgs (lib.catAttrs "device" (lib.attrValues devices.disk));
|
||||
disksToWipe = lib.attrValues (lib.filterAttrs (name: disk: disk.destroy) devices.disk);
|
||||
selectedDisks = lib.escapeShellArgs (lib.catAttrs "device" disksToWipe);
|
||||
in
|
||||
''
|
||||
if [ "$1" != "--yes-wipe-all-disks" ]; then
|
||||
@@ -904,7 +920,7 @@ let
|
||||
umount -Rv "${rootMountPoint}" || :
|
||||
|
||||
# shellcheck disable=SC2043,2041
|
||||
for dev in ${selectedDisks}; do
|
||||
for dev in ${toString (lib.catAttrs "device" disksToWipe)}; do
|
||||
$BASH ${../disk-deactivate}/disk-deactivate "$dev"
|
||||
done
|
||||
'';
|
||||
|
||||
@@ -28,7 +28,7 @@ let
|
||||
disks = lib.attrValues cfg_.disko.devices.disk;
|
||||
rootDisk = {
|
||||
name = "root";
|
||||
file = ''"$tmp"/${lib.escapeShellArg (builtins.head disks).name}.qcow2'';
|
||||
file = ''"$tmp"/${lib.escapeShellArg (builtins.head disks).imageName}.qcow2'';
|
||||
driveExtraOpts.cache = "writeback";
|
||||
driveExtraOpts.werror = "report";
|
||||
deviceExtraOpts.bootindex = "1";
|
||||
@@ -36,7 +36,7 @@ let
|
||||
};
|
||||
otherDisks = map (disk: {
|
||||
name = disk.name;
|
||||
file = ''"$tmp"/${lib.escapeShellArg disk.name}.qcow2'';
|
||||
file = ''"$tmp"/${lib.escapeShellArg disk.imageName}.qcow2'';
|
||||
driveExtraOpts.werror = "report";
|
||||
}) (builtins.tail disks);
|
||||
|
||||
@@ -78,8 +78,8 @@ in
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
${lib.concatMapStringsSep "\n" (disk: ''
|
||||
${hostPkgs.qemu}/bin/qemu-img create -f qcow2 \
|
||||
-b ${config.system.build.diskoImages}/${lib.escapeShellArg disk.name}.qcow2 \
|
||||
-F qcow2 "$tmp"/${lib.escapeShellArg disk.name}.qcow2
|
||||
-b ${config.system.build.diskoImages}/${lib.escapeShellArg disk.imageName}.qcow2 \
|
||||
-F qcow2 "$tmp"/${lib.escapeShellArg disk.imageName}.qcow2
|
||||
'') disks}
|
||||
set +f
|
||||
${config.system.build.vm}/bin/run-*-vm
|
||||
|
||||
@@ -74,6 +74,7 @@ let
|
||||
disko.devices = lib.mkForce cleanedConfig.disko.devices;
|
||||
boot.loader.grub.devices = lib.mkForce cleanedConfig.boot.loader.grub.devices;
|
||||
nixpkgs.hostPlatform = lib.mkForce pkgs.stdenv.hostPlatform;
|
||||
nixpkgs.buildPlatform = lib.mkForce pkgs.stdenv.hostPlatform;
|
||||
}
|
||||
];
|
||||
}
|
||||
@@ -125,7 +126,7 @@ let
|
||||
${pkgs.systemdMinimal}/lib/systemd/systemd-udevd --daemon
|
||||
partprobe
|
||||
udevadm trigger --action=add
|
||||
udevadm settle
|
||||
udevadm settle --timeout=120
|
||||
|
||||
${lib.optionalString diskoCfg.testMode ''
|
||||
export IN_DISKO_TEST=1
|
||||
@@ -135,6 +136,7 @@ let
|
||||
|
||||
installer = lib.optionalString cfg.copyNixStore ''
|
||||
${binfmtSetup}
|
||||
unset NIX_REMOTE
|
||||
# populate nix db, so nixos-install doesn't complain
|
||||
export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix
|
||||
nix-store --load-db < "${closureInfo}/registration"
|
||||
@@ -144,7 +146,7 @@ let
|
||||
time xargs cp --recursive --target ${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths
|
||||
|
||||
${systemToInstall.config.system.build.nixos-install}/bin/nixos-install --root ${systemToInstall.config.disko.rootMountPoint} --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
|
||||
umount -Rv ${systemToInstall.config.disko.rootMountPoint}
|
||||
umount -Rv ${lib.escapeShellArg systemToInstall.config.disko.rootMountPoint}
|
||||
'';
|
||||
|
||||
QEMU_OPTS = lib.concatStringsSep " " (
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
{
|
||||
config,
|
||||
device,
|
||||
diskoLib,
|
||||
lib,
|
||||
options,
|
||||
parent,
|
||||
...
|
||||
}:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "bcachefs" ];
|
||||
internal = true;
|
||||
description = "Type.";
|
||||
};
|
||||
device = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = device;
|
||||
description = "Device to use.";
|
||||
example = "/dev/sda";
|
||||
};
|
||||
filesystem = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Name of the bcachefs filesystem this partition belongs to.";
|
||||
example = "main_bcachefs_filesystem";
|
||||
};
|
||||
# These are passed as arguments to the device corresponding to this one in the invocation of the `bcachefs format` command
|
||||
# in the bcachefs_filesystem type defined in bcachefs_filesystem.nix used to format the bcachefs filesystem that this device is a part of.
|
||||
extraFormatArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments passed to the bcachefs format command.";
|
||||
example = [ "--discard" ];
|
||||
};
|
||||
# This value is passed to the `--label` option for the device corresponding to this one in the invocation of the `bcachefs format` command
|
||||
# in the bcachefs_filesystem type defined in bcachefs_filesystem.nix used to format the bcachefs filesystem that this device is a part of.
|
||||
label = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
description = ''
|
||||
Label to use for this device.
|
||||
This value is passed as the `--label` argument to the `bcachefs format` command when formatting the device.
|
||||
'';
|
||||
example = "group_a.sda2";
|
||||
};
|
||||
_parent = lib.mkOption {
|
||||
internal = true;
|
||||
default = parent;
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
# Ensures that this file's `_create` will be ran for all member devices that are part of the filesystem being created,
|
||||
# before the `_create` in bcachefs_filesystem.nix is ran.
|
||||
default = dev: {
|
||||
deviceDependencies.bcachefs_filesystems.${config.filesystem} = [ dev ];
|
||||
};
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
# The bcachefs_filesystem type defined in bcachefs_filesystem.nix will include this device when formatting and mounting the filesystem.
|
||||
# The current file should not run the `bcachefs format` command. Instead, the`bcachefs format` command will be ran
|
||||
# in the `_create` attribute in bcachefs_filesystem.nix, once it has collected and generated the arguments specifying the devices that should be part of the filesystem.
|
||||
default = ''
|
||||
# Write device arguments to temporary directory for bcachefs_filesystem.
|
||||
{
|
||||
printf '%s\n' '--label="${config.label}"';
|
||||
${lib.concatMapStrings (args: ''printf '%s\n' '${args}';'') config.extraFormatArgs}
|
||||
printf '%s\n' '${config.device}';
|
||||
} >> "$disko_devices_dir/bcachefs-${lib.escapeShellArg config.filesystem}";
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
# Empty, since mounting will be handled by the bcachefs_filesystem type defined in bcachefs_filesystem.nix.
|
||||
default = { };
|
||||
};
|
||||
_unmount = diskoLib.mkUnmountOption {
|
||||
inherit config options;
|
||||
# Empty, since unmounting will be handled by the bcachefs_filesystem type defined in bcachefs_filesystem.nix.
|
||||
default = { };
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
# Empty, since NixOS configuration will be handled by the bcachefs_filesystem type defined in bcachefs_filesystem.nix.
|
||||
default = { };
|
||||
description = "NixOS configuration.";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ ];
|
||||
description = "Packages.";
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,339 @@
|
||||
{
|
||||
config,
|
||||
diskoLib,
|
||||
lib,
|
||||
options,
|
||||
parent,
|
||||
rootMountPoint,
|
||||
...
|
||||
}:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the bcachefs filesystem.";
|
||||
example = "main_bcachefs_filesystem";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "bcachefs_filesystem" ];
|
||||
internal = true;
|
||||
description = "Type.";
|
||||
};
|
||||
extraFormatArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments passed to the `bcachefs format` command.";
|
||||
example = [
|
||||
"--compression=lz4"
|
||||
"--background_compression=lz4"
|
||||
];
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "X-mount.mkdir" ];
|
||||
description = ''
|
||||
Options to pass to mount.
|
||||
The "X-mount.mkdir" option is always automatically added.
|
||||
'';
|
||||
example = [
|
||||
"noatime"
|
||||
"verbose"
|
||||
];
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Path to mount the bcachefs filesystem to.";
|
||||
example = "/";
|
||||
};
|
||||
uuid = lib.mkOption {
|
||||
type = lib.types.strMatching "[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}";
|
||||
default =
|
||||
let
|
||||
# Generate a deterministic but random-looking UUID based on the filesystem name
|
||||
# This avoids the need for impure access to nixpkgs at evaluation time
|
||||
hash = builtins.hashString "sha256" "${config.name}";
|
||||
hexChars = builtins.substring 0 32 hash;
|
||||
p1 = builtins.substring 0 8 hexChars;
|
||||
p2 = builtins.substring 8 4 hexChars;
|
||||
p3 = builtins.substring 12 4 hexChars;
|
||||
p4 = builtins.substring 16 4 hexChars;
|
||||
p5 = builtins.substring 20 12 hexChars;
|
||||
in
|
||||
"${p1}-${p2}-${p3}-${p4}-${p5}";
|
||||
defaultText = "generated deterministically based on filesystem name";
|
||||
example = "809b3a2b-828a-4730-95e1-75b6343e415a";
|
||||
description = ''
|
||||
The UUID of the bcachefs filesystem.
|
||||
If not provided, a deterministic UUID will be generated based on the filesystem name.
|
||||
'';
|
||||
};
|
||||
passwordFile = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = ''
|
||||
Path to the file containing the password for encryption.
|
||||
Setting this option will automatically cause the `--encrypted` option to be passed to `bcachefs format` and cause the filesystem to have encryption enabled.
|
||||
'';
|
||||
example = "/tmp/disk.key";
|
||||
};
|
||||
subvolumes = lib.mkOption {
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submodule (
|
||||
{ config, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = ''
|
||||
Path of the subvolume within the filesystem.
|
||||
Leading forward slashes are automatically removed.
|
||||
'';
|
||||
example = "subvolumes/home";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "bcachefs_subvolume" ];
|
||||
default = "bcachefs_subvolume";
|
||||
internal = true;
|
||||
description = "Type.";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = lib.naturalSort [
|
||||
"X-mount.mkdir"
|
||||
"X-mount.subdir=${lib.removePrefix "/" config.name}"
|
||||
];
|
||||
description = ''
|
||||
Options to pass to mount.
|
||||
The "X-mount.mkdir" and "X-mount.subdir" options are always automatically added.
|
||||
'';
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Path to mount the subvolume to.";
|
||||
example = "/";
|
||||
};
|
||||
};
|
||||
}
|
||||
)
|
||||
);
|
||||
default = { };
|
||||
description = "List of subvolumes to define.";
|
||||
example = {
|
||||
"subvolumes/root" = {
|
||||
mountpoint = "/";
|
||||
extraFormatArgs = [
|
||||
"--compression=lz4"
|
||||
"--background_compression=lz4"
|
||||
];
|
||||
mountOptions = [
|
||||
"verbose"
|
||||
];
|
||||
};
|
||||
"subvolumes/home" = {
|
||||
mountpoint = "/home";
|
||||
};
|
||||
};
|
||||
};
|
||||
_parent = lib.mkOption {
|
||||
internal = true;
|
||||
default = parent;
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
# This sets a string variable containing arguments to be passed to the `bcachefs format` command.
|
||||
# This string will consist of `--label` and other arguments that correspond to the values of the `label` and `extraFormatArgs` attributes, respectively,
|
||||
# from each of the bcachefs devices in this filesystem specified in the configuration.
|
||||
# Then, it sets the `default` attribute to a string containing shell commands that calls the `bcachefs format` command, passing in the arguments generated, as well as a `--uuid` value.
|
||||
default = ''
|
||||
if ! test -s "$disko_devices_dir/bcachefs-${config.name}"; then
|
||||
printf "\033[31mERROR:\033[0m No devices found for bcachefs filesystem \"${config.name}\"!\nDid you forget to add some or misspell the filesystem name?\n" >&2;
|
||||
exit 1;
|
||||
fi;
|
||||
|
||||
# Create the filesystem.
|
||||
(
|
||||
# Empty out $@.
|
||||
set --;
|
||||
# Collect devices and arguments to $@.
|
||||
while IFS= read -r line; do
|
||||
# Append current line as a new positional parameter
|
||||
set -- "$@" "$line";
|
||||
done < "$disko_devices_dir/bcachefs-${config.name}";
|
||||
|
||||
# Format the filesystem with all devices and arguments.
|
||||
if ! blkid -o export "$(blkid -lU ${config.uuid})" | grep -q 'TYPE=bcachefs' >&2 2>&1; then
|
||||
bcachefs format \
|
||||
"$@" \
|
||||
--uuid="${config.uuid}" \
|
||||
${lib.concatStringsSep " \\\n" config.extraFormatArgs} \
|
||||
${
|
||||
lib.optionalString (config.passwordFile != null) ''--encrypted < "${config.passwordFile}"''
|
||||
};
|
||||
fi;
|
||||
);
|
||||
|
||||
# Mount the bcachefs filesystem onto a temporary directory,
|
||||
# then, create the subvolumes from inside of that directory.
|
||||
${lib.optionalString (config.subvolumes != { }) ''
|
||||
if blkid -o export "$(blkid -lU ${config.uuid})" | grep -q 'TYPE=bcachefs' >&2 2>&1; then
|
||||
${lib.concatMapStrings (subvolume: ''
|
||||
(
|
||||
TEMPDIR="$(mktemp -d)";
|
||||
MNTPOINT="$(mktemp -d)";
|
||||
${lib.optionalString (
|
||||
config.passwordFile != null
|
||||
) ''bcachefs unlock -k session "/dev/disk/by-uuid/${config.uuid}" < "${config.passwordFile}";''}
|
||||
bcachefs mount \
|
||||
-o "${lib.concatStringsSep "," (lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions))}" \
|
||||
UUID="${config.uuid}" \
|
||||
"$MNTPOINT";
|
||||
trap 'umount "$MNTPOINT"; rm -rf "$MNTPOINT"; rm -rf "$TEMPDIR";' EXIT;
|
||||
SUBVOL_ABS_PATH="$MNTPOINT/${subvolume.name}";
|
||||
# Check if it's already a subvolume (using snapshot).
|
||||
if ! bcachefs subvolume snapshot "$SUBVOL_ABS_PATH" "$TEMPDIR/" >&2 2>&1; then
|
||||
# It's not a subvolume, now check if it's a directory.
|
||||
if ! test -d "$SUBVOL_ABS_PATH"; then
|
||||
# It's not a subvolume AND not a directory, so create it.
|
||||
mkdir -p -- "$(dirname -- "$SUBVOL_ABS_PATH")";
|
||||
bcachefs subvolume create "$SUBVOL_ABS_PATH";
|
||||
fi
|
||||
fi;
|
||||
)
|
||||
'') (lib.attrValues config.subvolumes)}
|
||||
fi;
|
||||
''}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default =
|
||||
let
|
||||
subvolumeMounts = diskoLib.deepMergeMap (
|
||||
subvolume:
|
||||
lib.optionalAttrs (subvolume.mountpoint != null) {
|
||||
${subvolume.mountpoint} = ''
|
||||
if ! findmnt "${rootMountPoint}${subvolume.mountpoint}" >&2 2>&1; then
|
||||
# @todo Figure out why the "X-mount.mkdir" option here doesn't seem to work,
|
||||
# necessitating running `mkdir` here.
|
||||
mkdir -p "${rootMountPoint}${subvolume.mountpoint}";
|
||||
${lib.optionalString (
|
||||
config.passwordFile != null
|
||||
) ''bcachefs unlock -k session "/dev/disk/by-uuid/${config.uuid}" < "${config.passwordFile}";''}
|
||||
bcachefs mount \
|
||||
-o "${
|
||||
lib.concatStringsSep "," (
|
||||
lib.unique (
|
||||
[
|
||||
"X-mount.mkdir"
|
||||
"X-mount.subdir=${lib.removePrefix "/" subvolume.name}"
|
||||
]
|
||||
++ subvolume.mountOptions
|
||||
)
|
||||
)
|
||||
}" \
|
||||
UUID="${config.uuid}" \
|
||||
"${rootMountPoint}${subvolume.mountpoint}";
|
||||
fi;
|
||||
'';
|
||||
}
|
||||
) (lib.attrValues config.subvolumes);
|
||||
in
|
||||
{
|
||||
fs =
|
||||
subvolumeMounts
|
||||
// lib.optionalAttrs (config.mountpoint != null) {
|
||||
${config.mountpoint} = ''
|
||||
if ! findmnt "${rootMountPoint}${config.mountpoint}" >&2 2>&1; then
|
||||
# @todo Figure out why the "X-mount.mkdir" option here doesn't seem to work,
|
||||
# necessitating running `mkdir` here.
|
||||
mkdir -p "${rootMountPoint}${config.mountpoint}";
|
||||
${lib.optionalString (
|
||||
config.passwordFile != null
|
||||
) ''bcachefs unlock -k session "/dev/disk/by-uuid/${config.uuid}" < "${config.passwordFile}";''}
|
||||
bcachefs mount \
|
||||
-o "${lib.concatStringsSep "," (lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions))}" \
|
||||
UUID="${config.uuid}" \
|
||||
"${rootMountPoint}${config.mountpoint}";
|
||||
fi;
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
_unmount = diskoLib.mkUnmountOption {
|
||||
inherit config options;
|
||||
default =
|
||||
let
|
||||
subvolumeMounts = lib.concatMapAttrs (
|
||||
_: subvolume:
|
||||
lib.optionalAttrs (subvolume.mountpoint != null) {
|
||||
${subvolume.mountpoint} = ''
|
||||
if findmnt "UUID=${config.uuid}" "${rootMountPoint}${subvolume.mountpoint}" >&2 2>&1; then
|
||||
umount "${rootMountPoint}${subvolume.mountpoint}";
|
||||
fi;
|
||||
'';
|
||||
}
|
||||
) config.subvolumes;
|
||||
in
|
||||
{
|
||||
fs =
|
||||
subvolumeMounts
|
||||
// lib.optionalAttrs (config.mountpoint != null) {
|
||||
${config.mountpoint} = ''
|
||||
if findmnt "UUID=${config.uuid}" "${rootMountPoint}${config.mountpoint}" >&2 2>&1; then
|
||||
umount "${rootMountPoint}${config.mountpoint}";
|
||||
fi;
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
(lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = "/dev/disk/by-uuid/${config.uuid}";
|
||||
fsType = "bcachefs";
|
||||
options = lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions);
|
||||
neededForBoot = true;
|
||||
};
|
||||
})
|
||||
++ (map (subvolume: {
|
||||
fileSystems.${subvolume.mountpoint} = {
|
||||
device = "/dev/disk/by-uuid/${config.uuid}";
|
||||
fsType = "bcachefs";
|
||||
options = lib.unique (
|
||||
[
|
||||
"X-mount.mkdir"
|
||||
"X-mount.subdir=${lib.removePrefix "/" subvolume.name}"
|
||||
]
|
||||
++ subvolume.mountOptions
|
||||
);
|
||||
neededForBoot = true;
|
||||
};
|
||||
}) (lib.filter (subvolume: subvolume.mountpoint != null) (lib.attrValues config.subvolumes)));
|
||||
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
|
||||
pkgs.util-linux
|
||||
];
|
||||
description = "Packages.";
|
||||
};
|
||||
};
|
||||
}
|
||||
+2
-2
@@ -160,7 +160,7 @@ in
|
||||
(
|
||||
MNTPOINT=$(mktemp -d)
|
||||
mount ${device} "$MNTPOINT" -o subvol=/
|
||||
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
||||
trap 'umount "$MNTPOINT"; rm -rf "$MNTPOINT"' EXIT
|
||||
${swapCreate "$MNTPOINT" config.swap}
|
||||
)
|
||||
''}
|
||||
@@ -168,7 +168,7 @@ in
|
||||
(
|
||||
MNTPOINT=$(mktemp -d)
|
||||
mount "${config.device}" "$MNTPOINT" -o subvol=/
|
||||
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
||||
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
|
||||
|
||||
@@ -22,6 +22,11 @@
|
||||
type = diskoLib.optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile
|
||||
description = "Device path";
|
||||
};
|
||||
destroy = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "If false, disko will not wipe or destroy this disk's contents during the destroy stage";
|
||||
};
|
||||
imageName = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config.name;
|
||||
|
||||
@@ -106,6 +106,7 @@
|
||||
pkgs.gnugrep
|
||||
]
|
||||
++ (
|
||||
# TODO add many more
|
||||
if (config.format == "xfs") then
|
||||
[ pkgs.xfsprogs ]
|
||||
else if (config.format == "btrfs") then
|
||||
@@ -122,8 +123,6 @@
|
||||
[ pkgs.bcachefs-tools ]
|
||||
else if (config.format == "f2fs") then
|
||||
[ pkgs.f2fs-tools ]
|
||||
else if (config.format == "exfat") then
|
||||
[ pkgs.exfatprogs ]
|
||||
else
|
||||
[ ]
|
||||
);
|
||||
|
||||
+2
-2
@@ -204,7 +204,7 @@ in
|
||||
${lib.optionalString (hp.config.mbrPartitionType != null) ''
|
||||
sfdisk --label-nested dos --part-type "${parent.device}" ${(toString partition.config._index)} ${hp.config.mbrPartitionType}
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout 120
|
||||
''}
|
||||
${lib.optionalString hp.config.mbrBootableFlag ''
|
||||
sfdisk --label-nested dos --activate "${parent.device}" ${(toString partition.config._index)}
|
||||
@@ -288,7 +288,7 @@ in
|
||||
# ensure /dev/disk/by-path/..-partN exists before continuing
|
||||
partprobe "${config.device}" || : # sometimes partprobe fails, but the partitions are still up2date
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout 120
|
||||
''
|
||||
) sortedPartitions}
|
||||
|
||||
|
||||
+22
-11
@@ -130,15 +130,22 @@ in
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
if ! blkid "${config.device}" >/dev/null || ! (blkid "${config.device}" -o export | grep -q '^TYPE='); then
|
||||
if ! blkid "${config.device}" >/dev/null || ! cryptsetup isLuks "${config.device}"; then
|
||||
${lib.optionalString config.askPassword ''
|
||||
promptSecret() {
|
||||
prompt=$1
|
||||
var=$2
|
||||
|
||||
echo -n "$prompt"
|
||||
IFS= read -r -s "$var"
|
||||
echo
|
||||
}
|
||||
|
||||
askPassword() {
|
||||
if [ -z ''${IN_DISKO_TEST+x} ]; then
|
||||
set +x
|
||||
echo "Enter password for ${config.device}: "
|
||||
IFS= read -r -s password
|
||||
echo "Enter password for ${config.device} again to be safe: "
|
||||
IFS= read -r -s password_check
|
||||
promptSecret "Enter password for ${config.device}: " password
|
||||
promptSecret "Enter password for ${config.device} again to be safe: " password_check
|
||||
export password
|
||||
[ "$password" = "$password_check" ]
|
||||
set -x
|
||||
@@ -151,13 +158,17 @@ in
|
||||
done
|
||||
''}
|
||||
cryptsetup -q luksFormat "${config.device}" ${toString config.extraFormatArgs} ${keyFileArgs}
|
||||
${cryptsetupOpen} --persistent
|
||||
${toString (
|
||||
lib.forEach config.additionalKeyFiles (keyFile: ''
|
||||
cryptsetup luksAddKey "${config.device}" ${keyFile} ${keyFileArgs}
|
||||
'')
|
||||
)}
|
||||
fi
|
||||
|
||||
if ! cryptsetup status "${config.name}" >/dev/null; then
|
||||
${cryptsetupOpen} --persistent
|
||||
fi
|
||||
${toString (
|
||||
lib.forEach config.additionalKeyFiles (keyFile: ''
|
||||
cryptsetup luksAddKey "${config.device}" ${keyFile} ${keyFileArgs}
|
||||
'')
|
||||
)}
|
||||
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
};
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@
|
||||
"''${disk_devices[@]}"
|
||||
partprobe "/dev/md/${config.name}"
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout=120
|
||||
# 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
|
||||
|
||||
+4
-2
@@ -55,6 +55,8 @@
|
||||
fs-type = lib.mkOption {
|
||||
type = lib.types.nullOr (
|
||||
lib.types.enum [
|
||||
# @todo Check if this is needed
|
||||
"bcachefs"
|
||||
"btrfs"
|
||||
"ext2"
|
||||
"ext3"
|
||||
@@ -147,7 +149,7 @@
|
||||
# ensure /dev/disk/by-path/..-partN exists before continuing
|
||||
partprobe "${config.device}"
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout 120
|
||||
${lib.optionalString partition.bootable ''
|
||||
parted -s "${config.device}" -- set ${toString partition._index} boot on
|
||||
''}
|
||||
@@ -157,7 +159,7 @@
|
||||
# ensure further operations can detect new partitions
|
||||
partprobe "${config.device}"
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout 120
|
||||
'') config.partitions
|
||||
)}
|
||||
fi
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
zvol_wait
|
||||
partprobe "/dev/zvol/${config._parent.name}/${config.name}"
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
udevadm settle --timeout=120
|
||||
fi
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
|
||||
+18
-9
@@ -8,6 +8,7 @@
|
||||
nixos-install-tools,
|
||||
binlore,
|
||||
diskoVersion,
|
||||
stdenv,
|
||||
}:
|
||||
|
||||
let
|
||||
@@ -20,20 +21,28 @@ let
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin $out/share/disko
|
||||
cp -r install-cli.nix cli.nix default.nix disk-deactivate lib $out/share/disko
|
||||
set -x
|
||||
|
||||
for i in disko disko-install; do
|
||||
scripts=(disko)
|
||||
${lib.optionalString (!stdenv.isDarwin) ''
|
||||
scripts+=(disko-install)
|
||||
''}
|
||||
|
||||
for i in "''${scripts[@]}"; do
|
||||
sed -e "s|libexec_dir=\".*\"|libexec_dir=\"$out/share/disko\"|" "$i" > "$out/bin/$i"
|
||||
chmod 755 "$out/bin/$i"
|
||||
wrapProgram "$out/bin/$i" \
|
||||
--set DISKO_VERSION "${diskoVersion}" \
|
||||
--prefix NIX_PATH : "nixpkgs=${path}" \
|
||||
--prefix PATH : ${
|
||||
lib.makeBinPath [
|
||||
nix
|
||||
coreutils
|
||||
nixos-install-tools
|
||||
]
|
||||
} \
|
||||
--prefix NIX_PATH : "nixpkgs=${path}"
|
||||
lib.makeBinPath (
|
||||
[
|
||||
nix
|
||||
coreutils
|
||||
]
|
||||
++ lib.optional (!stdenv.isDarwin) nixos-install-tools
|
||||
)
|
||||
}
|
||||
done
|
||||
'';
|
||||
# Otherwise resholve thinks that disko and disko-install might be able to execute their arguments
|
||||
@@ -46,7 +55,7 @@ let
|
||||
homepage = "https://github.com/nix-community/disko";
|
||||
license = licenses.mit;
|
||||
maintainers = with maintainers; [ lassulus ];
|
||||
platforms = platforms.linux;
|
||||
platforms = platforms.unix;
|
||||
mainProgram = finalAttrs.name;
|
||||
};
|
||||
});
|
||||
|
||||
+115
-6
@@ -6,12 +6,121 @@ diskoLib.testLib.makeDiskoTest {
|
||||
inherit pkgs;
|
||||
name = "bcachefs";
|
||||
disko-config = ../example/bcachefs.nix;
|
||||
extraTestScript = ''
|
||||
machine.succeed("mountpoint /");
|
||||
machine.succeed("lsblk >&2");
|
||||
enableOCR = true;
|
||||
bootCommands = ''
|
||||
machine.wait_for_text("enter passphrase for /");
|
||||
machine.send_chars("secretsecret\n");
|
||||
machine.wait_for_text("enter passphrase for /home");
|
||||
machine.send_chars("secretsecret\n");
|
||||
machine.wait_for_text("enter passphrase for /nix");
|
||||
machine.send_chars("secretsecret\n");
|
||||
'';
|
||||
# so that the installer boots with a bcachefs enabled kernel
|
||||
extraInstallerConfig = {
|
||||
boot.supportedFilesystems = [ "bcachefs" ];
|
||||
extraSystemConfig = {
|
||||
environment.systemPackages = [
|
||||
pkgs.jq
|
||||
];
|
||||
};
|
||||
extraTestScript = ''
|
||||
# Print debug information.
|
||||
machine.succeed("ls -la /subvolumes >&2");
|
||||
machine.succeed("lsblk >&2");
|
||||
machine.succeed("lsblk -f >&2");
|
||||
machine.succeed("mount >&2");
|
||||
machine.succeed("bcachefs show-super /dev/vda2 >&2");
|
||||
machine.succeed("bcachefs show-super /dev/vdd1 >&2");
|
||||
machine.succeed("findmnt --json >&2");
|
||||
|
||||
# Verify subvolume structure.
|
||||
machine.succeed("test -d /subvolumes/root");
|
||||
machine.succeed("test -d /subvolumes/home");
|
||||
machine.succeed("test -d /subvolumes/home/user");
|
||||
machine.succeed("test -d /subvolumes/nix");
|
||||
machine.succeed("test -d /subvolumes/test");
|
||||
machine.fail("test -d /subvolumes/non-existent");
|
||||
|
||||
# Verify existence of mountpoints.
|
||||
machine.succeed("mountpoint /");
|
||||
machine.succeed("mountpoint /home");
|
||||
machine.succeed("mountpoint /nix");
|
||||
machine.succeed("mountpoint /home/Documents");
|
||||
machine.fail("mountpoint /non-existent");
|
||||
|
||||
# Verify device membership and labels.
|
||||
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Devices:' | grep -q '3'");
|
||||
machine.succeed("bcachefs show-super /dev/vdd1 | grep 'Devices:' | grep -q '1'");
|
||||
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdb2'");
|
||||
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdc1'");
|
||||
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdd1'");
|
||||
machine.succeed("bcachefs show-super /dev/vdd1 | grep 'Label:' | grep -q 'vde1'");
|
||||
machine.fail("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'non-existent'");
|
||||
|
||||
# @todo Verify format arguments.
|
||||
|
||||
# Verify mount options from configuration.
|
||||
machine.succeed("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| select(.target == "/") \
|
||||
| .options \
|
||||
| split(",") \
|
||||
| contains(["verbose", "compression=lz4", "background_compression=lz4"]) \
|
||||
'
|
||||
""");
|
||||
|
||||
machine.succeed("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| .. \
|
||||
| select(.target? == "/home/Documents") \
|
||||
| .options \
|
||||
| split(",") \
|
||||
| contains(["verbose"]) \
|
||||
'
|
||||
""");
|
||||
|
||||
machine.fail("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| select(.target == "/") \
|
||||
| .options \
|
||||
| split(",") \
|
||||
| contains(["non-existent"]) \
|
||||
'
|
||||
""");
|
||||
|
||||
# Verify device composition of filesystems.
|
||||
machine.succeed("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| select(.target == "/") \
|
||||
| .source | split(":") \
|
||||
| contains(["/dev/vda2", "/dev/vdb1", "/dev/vdc1"]) \
|
||||
'
|
||||
""");
|
||||
|
||||
machine.succeed("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| .. \
|
||||
| select(.target? == "/home/Documents") \
|
||||
| .source \
|
||||
| contains("/dev/disk/by-uuid/64e50034-ebe2-eaf8-1f93-cf56266a8d86") \
|
||||
'
|
||||
""");
|
||||
|
||||
machine.fail("""
|
||||
findmnt --json \
|
||||
| jq -e ' \
|
||||
.filesystems[] \
|
||||
| select(.target == "/") \
|
||||
| .source | split(":") \
|
||||
| contains(["/dev/non-existent"]) \
|
||||
'
|
||||
""");
|
||||
'';
|
||||
}
|
||||
+1
-1
@@ -19,7 +19,7 @@ diskoLib.testLib.makeDiskoTest {
|
||||
machine.succeed("mountpoint /zfs_fs");
|
||||
machine.succeed("mountpoint /zfs_legacy_fs");
|
||||
machine.succeed("mountpoint /ext4onzfs");
|
||||
machine.succeed("mountpoint /exfat_on_lvm");
|
||||
machine.succeed("mountpoint /ext4_on_lvm");
|
||||
'';
|
||||
extraSystemConfig = {
|
||||
imports = [
|
||||
|
||||
+2
-2
@@ -19,10 +19,10 @@ diskoLib.testLib.makeDiskoTest {
|
||||
machine.succeed("mountpoint /zfs_fs");
|
||||
machine.succeed("mountpoint /zfs_legacy_fs");
|
||||
machine.succeed("mountpoint /ext4onzfs");
|
||||
machine.succeed("mountpoint /exfat_on_lvm");
|
||||
machine.succeed("mountpoint /ext4_on_lvm");
|
||||
|
||||
|
||||
machine.succeed("test -e /exfat_on_lvm/file-from-postMountHook");
|
||||
machine.succeed("test -e /ext4_on_lvm/file-from-postMountHook");
|
||||
'';
|
||||
extraInstallerConfig = {
|
||||
boot.kernelModules = [
|
||||
|
||||
@@ -55,6 +55,8 @@ pkgs.nixosTest {
|
||||
permission = machine.succeed("stat -c %a /tmp/age.key").strip()
|
||||
assert permission == "600", f"expected permission 600 on /tmp/age.key, got {permission}"
|
||||
|
||||
# Some distros like to automount disks with spaces
|
||||
machine.succeed('mkdir -p "/mnt/with spaces" && mkfs.ext4 /dev/vdb && mount /dev/vdb "/mnt/with spaces"')
|
||||
machine.succeed("${disko}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine")
|
||||
# test idempotency
|
||||
machine.succeed("${disko}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine")
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
{ config, ... }:
|
||||
{
|
||||
documentation.enable = false;
|
||||
system.stateVersion = config.system.nixos.version;
|
||||
system.stateVersion = config.system.nixos.release;
|
||||
disko.checkScripts = true;
|
||||
}
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
{ config, ... }:
|
||||
{
|
||||
documentation.enable = false;
|
||||
system.stateVersion = config.system.nixos.version;
|
||||
system.stateVersion = config.system.nixos.release;
|
||||
disko.memSize = 2048;
|
||||
disko.checkScripts = true;
|
||||
}
|
||||
|
||||
+1
-4
@@ -1,4 +1 @@
|
||||
{
|
||||
version = "1.10.0";
|
||||
released = false;
|
||||
}
|
||||
{ version = "1.12.0"; released = true; }
|
||||
Reference in new issue
Block a user