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.
5 { config, lib, pkgs, ... }:
11 (mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
15 boot.growPartition = mkEnableOption (lib.mdDoc "growing the root partition on boot");
18 config = mkIf config.boot.growPartition {
21 assertion = !config.boot.initrd.systemd.repart.enable && !config.systemd.repart.enable;
22 message = "systemd-repart already grows the root partition and thus you should not use boot.growPartition";
25 systemd.services.growpart = {
26 wantedBy = [ "-.mount" ];
27 after = [ "-.mount" ];
28 before = [ "systemd-growfs-root.service" ];
29 conflicts = [ "shutdown.target" ];
30 unitConfig.DefaultDependencies = false;
33 RemainAfterExit = true;
34 TimeoutSec = "infinity";
35 # growpart returns 1 if the partition is already grown
36 SuccessExitStatus = "0 1";
40 rootDevice="${config.fileSystems."/".device}"
41 rootDevice="$(readlink -f "$rootDevice")"
42 parentDevice="$rootDevice"
43 while [ "''${parentDevice%[0-9]}" != "''${parentDevice}" ]; do
44 parentDevice="''${parentDevice%[0-9]}";
46 partNum="''${rootDevice#''${parentDevice}}"
47 if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
48 parentDevice="''${parentDevice%p}"
50 "${pkgs.cloud-utils.guest}/bin/growpart" "$parentDevice" "$partNum"