2020-04-24 23:36:52 +00:00
|
|
|
# This module automatically grows the root partition.
|
|
|
|
# This allows an instance to be created with a bigger root filesystem
|
|
|
|
# than provided by the machine image.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
2023-11-16 04:20:00 +00:00
|
|
|
boot.growPartition = mkEnableOption (lib.mdDoc "growing the root partition on boot");
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.boot.growPartition {
|
2023-11-16 04:20:00 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !config.boot.initrd.systemd.repart.enable && !config.systemd.repart.enable;
|
|
|
|
message = "systemd-repart already grows the root partition and thus you should not use boot.growPartition";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
systemd.services.growpart = {
|
|
|
|
wantedBy = [ "-.mount" ];
|
|
|
|
after = [ "-.mount" ];
|
2024-01-02 11:29:13 +00:00
|
|
|
before = [ "systemd-growfs-root.service" "shutdown.target" ];
|
2023-11-16 04:20:00 +00:00
|
|
|
conflicts = [ "shutdown.target" ];
|
|
|
|
unitConfig.DefaultDependencies = false;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
TimeoutSec = "infinity";
|
|
|
|
# growpart returns 1 if the partition is already grown
|
|
|
|
SuccessExitStatus = "0 1";
|
|
|
|
};
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
rootDevice="${config.fileSystems."/".device}"
|
2020-04-24 23:36:52 +00:00
|
|
|
rootDevice="$(readlink -f "$rootDevice")"
|
|
|
|
parentDevice="$rootDevice"
|
|
|
|
while [ "''${parentDevice%[0-9]}" != "''${parentDevice}" ]; do
|
|
|
|
parentDevice="''${parentDevice%[0-9]}";
|
|
|
|
done
|
|
|
|
partNum="''${rootDevice#''${parentDevice}}"
|
|
|
|
if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
|
|
|
|
parentDevice="''${parentDevice%p}"
|
|
|
|
fi
|
2023-11-16 04:20:00 +00:00
|
|
|
"${pkgs.cloud-utils.guest}/bin/growpart" "$parentDevice" "$partNum"
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|