1# This module automatically grows the root partition.
2# This allows an instance to be created with a bigger root filesystem
3# than provided by the machine image.
4
5{ config, lib, pkgs, ... }:
6
7with lib;
8
9{
10 imports = [
11 (mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
12 ];
13
14 options = {
15 boot.growPartition = mkEnableOption (lib.mdDoc "grow the root partition on boot");
16 };
17
18 config = mkIf config.boot.growPartition {
19
20 assertions = [{
21 assertion = !config.boot.initrd.systemd.enable;
22 message = "systemd stage 1 does not support 'boot.growPartition' yet.";
23 }];
24
25 boot.initrd.extraUtilsCommands = ''
26 copy_bin_and_libs ${pkgs.gawk}/bin/gawk
27 copy_bin_and_libs ${pkgs.gnused}/bin/sed
28 copy_bin_and_libs ${pkgs.util-linux}/sbin/sfdisk
29 copy_bin_and_libs ${pkgs.util-linux}/sbin/lsblk
30
31 substitute "${pkgs.cloud-utils.guest}/bin/.growpart-wrapped" "$out/bin/growpart" \
32 --replace "${pkgs.bash}/bin/sh" "/bin/sh" \
33 --replace "awk" "gawk" \
34 --replace "sed" "gnused"
35
36 ln -s sed $out/bin/gnused
37 '';
38
39 boot.initrd.postDeviceCommands = ''
40 rootDevice="${config.fileSystems."/".device}"
41 if waitDevice "$rootDevice"; then
42 rootDevice="$(readlink -f "$rootDevice")"
43 parentDevice="$rootDevice"
44 while [ "''${parentDevice%[0-9]}" != "''${parentDevice}" ]; do
45 parentDevice="''${parentDevice%[0-9]}";
46 done
47 partNum="''${rootDevice#''${parentDevice}}"
48 if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
49 parentDevice="''${parentDevice%p}"
50 fi
51 TMPDIR=/run sh $(type -P growpart) "$parentDevice" "$partNum"
52 udevadm settle
53 fi
54 '';
55
56 };
57
58}