typioca: 2.7.0 -> 2.8.0
[NixPkgs.git] / nixos / modules / system / boot / grow-partition.nix
blob897602f9826ab32bb14dfc8c05af50b7b3d44f68
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, ... }:
7 with lib;
10   imports = [
11     (mkRenamedOptionModule [ "virtualisation" "growPartition" ] [ "boot" "growPartition" ])
12   ];
14   options = {
15     boot.growPartition = mkEnableOption (lib.mdDoc "growing the root partition on boot");
16   };
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" ];
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       };
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   };