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 boot.initrd.extraUtilsCommands = ''
21 copy_bin_and_libs ${pkgs.gawk}/bin/gawk
22 copy_bin_and_libs ${pkgs.gnused}/bin/sed
23 copy_bin_and_libs ${pkgs.util-linux}/sbin/sfdisk
24 copy_bin_and_libs ${pkgs.util-linux}/sbin/lsblk
25
26 substitute "${pkgs.cloud-utils.guest}/bin/.growpart-wrapped" "$out/bin/growpart" \
27 --replace "${pkgs.bash}/bin/sh" "/bin/sh" \
28 --replace "awk" "gawk" \
29 --replace "sed" "gnused"
30
31 ln -s sed $out/bin/gnused
32 '';
33
34 boot.initrd.postDeviceCommands = ''
35 rootDevice="${config.fileSystems."/".device}"
36 if waitDevice "$rootDevice"; then
37 rootDevice="$(readlink -f "$rootDevice")"
38 parentDevice="$rootDevice"
39 while [ "''${parentDevice%[0-9]}" != "''${parentDevice}" ]; do
40 parentDevice="''${parentDevice%[0-9]}";
41 done
42 partNum="''${rootDevice#''${parentDevice}}"
43 if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
44 parentDevice="''${parentDevice%p}"
45 fi
46 TMPDIR=/run sh $(type -P growpart) "$parentDevice" "$partNum"
47 udevadm settle
48 fi
49 '';
50
51 };
52
53}