-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathbcachefs_filesystem.nix
More file actions
339 lines (336 loc) · 12.8 KB
/
bcachefs_filesystem.nix
File metadata and controls
339 lines (336 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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}";''}
mount \
-t bcachefs \
-o "${lib.concatStringsSep "," (lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions))}" \
"/dev/disk/by-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
# Attempt to create the subvolume otherwise.
mkdir -p -- "$(dirname -- "$SUBVOL_ABS_PATH")";
bcachefs subvolume create "$SUBVOL_ABS_PATH";
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}";''}
mount \
-t bcachefs \
-o "${
lib.concatStringsSep "," (
lib.unique (
[
"X-mount.mkdir"
"X-mount.subdir=${lib.removePrefix "/" subvolume.name}"
]
++ subvolume.mountOptions
)
)
}" \
"/dev/disk/by-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}";''}
mount \
-t bcachefs \
-o "${lib.concatStringsSep "," (lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions))}" \
"/dev/disk/by-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.";
};
};
}