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 imports = [
11 (mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
12 ];
13
14 options = {
15 boot.growPartition = mkEnableOption "growing the root partition on boot";
16 };
17
18 config = mkIf config.boot.growPartition {
19 assertions = [
20 {
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";
23 }
24 ];
25 systemd.services.growpart = {
26 wantedBy = [ "-.mount" ];
27 after = [ "-.mount" ];
28 before = [ "systemd-growfs-root.service" "shutdown.target" ];
29 conflicts = [ "shutdown.target" ];
30 unitConfig.DefaultDependencies = false;
31 serviceConfig = {
32 Type = "oneshot";
33 RemainAfterExit = true;
34 TimeoutSec = "infinity";
35 # growpart returns 1 if the partition is already grown
36 SuccessExitStatus = "0 1";
37 };
38
39 script = ''
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]}";
45 done
46 partNum="''${rootDevice#''${parentDevice}}"
47 if [ "''${parentDevice%[0-9]p}" != "''${parentDevice}" ] && [ -b "''${parentDevice%p}" ]; then
48 parentDevice="''${parentDevice%p}"
49 fi
50 "${pkgs.cloud-utils.guest}/bin/growpart" "$parentDevice" "$partNum"
51 '';
52 };
53 };
54}