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