1# This is a test utility that automatically formats
2# `config.virtualisation.rootDevice` in the initrd.
3# Note that when you are using
4# `boot.initrd.systemd.enable = true`, you can use
5# `virtualisation.fileSystems."/".autoFormat = true;`
6# instead.
7
8{
9 lib,
10 config,
11 pkgs,
12 ...
13}:
14
15let
16 rootDevice = config.virtualisation.rootDevice;
17in
18{
19
20 boot.initrd.extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
21 # We need mke2fs in the initrd.
22 copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs
23 '';
24
25 boot.initrd.postDeviceCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
26 # If the disk image appears to be empty, run mke2fs to
27 # initialise.
28 FSTYPE=$(blkid -o value -s TYPE ${rootDevice} || true)
29 PARTTYPE=$(blkid -o value -s PTTYPE ${rootDevice} || true)
30 if test -z "$FSTYPE" -a -z "$PARTTYPE"; then
31 mke2fs -t ext4 ${rootDevice}
32 fi
33 '';
34}