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 = {
|
2022-09-09 14:08:57 +00:00
|
|
|
boot.growPartition = mkEnableOption (lib.mdDoc "grow the root partition on boot");
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.boot.growPartition {
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
assertions = [{
|
|
|
|
assertion = !config.boot.initrd.systemd.enable;
|
|
|
|
message = "systemd stage 1 does not support 'boot.growPartition' yet.";
|
|
|
|
}];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
boot.initrd.extraUtilsCommands = ''
|
|
|
|
copy_bin_and_libs ${pkgs.gawk}/bin/gawk
|
|
|
|
copy_bin_and_libs ${pkgs.gnused}/bin/sed
|
2020-11-24 20:58:05 +00:00
|
|
|
copy_bin_and_libs ${pkgs.util-linux}/sbin/sfdisk
|
|
|
|
copy_bin_and_libs ${pkgs.util-linux}/sbin/lsblk
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
substitute "${pkgs.cloud-utils.guest}/bin/.growpart-wrapped" "$out/bin/growpart" \
|
2020-04-24 23:36:52 +00:00
|
|
|
--replace "${pkgs.bash}/bin/sh" "/bin/sh" \
|
|
|
|
--replace "awk" "gawk" \
|
|
|
|
--replace "sed" "gnused"
|
|
|
|
|
|
|
|
ln -s sed $out/bin/gnused
|
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
rootDevice="${config.fileSystems."/".device}"
|
|
|
|
if waitDevice "$rootDevice"; then
|
|
|
|
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
|
|
|
|
TMPDIR=/run sh $(type -P growpart) "$parentDevice" "$partNum"
|
|
|
|
udevadm settle
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|