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.
16 (mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
20 boot.growPartition = mkEnableOption "growing the root partition on boot";
23 config = mkIf config.boot.growPartition {
26 assertion = !config.boot.initrd.systemd.repart.enable && !config.systemd.repart.enable;
27 message = "systemd-repart already grows the root partition and thus you should not use boot.growPartition";
30 systemd.services.growpart = {
31 wantedBy = [ "-.mount" ];
32 after = [ "-.mount" ];
34 "systemd-growfs-root.service"
37 conflicts = [ "shutdown.target" ];
38 unitConfig.DefaultDependencies = false;
41 RemainAfterExit = true;
42 TimeoutSec = "infinity";
43 # growpart returns 1 if the partition is already grown
44 SuccessExitStatus = "0 1";
48 rootDevice="${config.fileSystems."/".device}"
49 rootDevice="$(readlink -f "$rootDevice")"
50 parentDevice="$rootDevice"
51 while [ "''${parentDevice%[0-9]}" != "''${parentDevice}" ]; do
52 parentDevice="''${parentDevice%[0-9]}";
54 partNum="''${rootDevice#"''${parentDevice}"}"
55 if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
56 parentDevice="''${parentDevice%p}"
58 "${pkgs.cloud-utils.guest}/bin/growpart" "$parentDevice" "$partNum"