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 [ -e "$rootDevice" ]; then
34 rootDevice="$(readlink -f "$rootDevice")"
35 parentDevice="$(lsblk -npo PKNAME "$rootDevice")"
36 TMPDIR=/run sh $(type -P growpart) "$parentDevice" "''${rootDevice#$parentDevice}"
37 udevadm settle
38 fi
39 '';
40
41 };
42
43}