From 524027cc5296779380f9bf63219edfc18d7623ae Mon Sep 17 00:00:00 2001 From: liberodark Date: Wed, 20 Nov 2024 11:00:42 +0100 Subject: [PATCH] nixos/hot-resize: init --- nixos/modules/services/system/hot-resize.nix | 84 ++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 nixos/modules/services/system/hot-resize.nix diff --git a/nixos/modules/services/system/hot-resize.nix b/nixos/modules/services/system/hot-resize.nix new file mode 100644 index 00000000000000..a5a5764295236d --- /dev/null +++ b/nixos/modules/services/system/hot-resize.nix @@ -0,0 +1,84 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + cfg = config.services.hotResize; +in +{ + options.services.hotResize = { + enable = lib.mkEnableOption "the hot resize service for filesystems"; + + device = lib.mkOption { + type = lib.types.str; + example = "/dev/sda1"; + description = "Device to resize"; + }; + + fsType = lib.mkOption { + type = lib.types.enum [ + "ext4" + "xfs" + "btrfs" + ]; + default = "ext4"; + description = "Filesystem type (supported: ext4, xfs, btrfs)"; + }; + + mountPoint = lib.mkOption { + type = lib.types.str; + example = "/"; + description = "Mount point of the filesystem to resize"; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.hotResize = { + description = "Hot resize service for filesystems"; + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = false; + ExecStart = pkgs.writeShellScript "resize-fs" '' + ${lib.getExe' pkgs.parted "partprobe"} + + DISK=$(echo ${cfg.device} | sed 's/[0-9]*$//') + PART_NUM=$(echo ${cfg.device} | grep -o '[0-9]*$') + + echo "Growing partition..." + ${lib.getExe' pkgs.cloud-utils "growpart"} $DISK $PART_NUM || true + + echo "Resizing filesystem..." + case "${cfg.fsType}" in + "ext4") + ${lib.getExe' pkgs.e2fsprogs "resize2fs"} ${cfg.device} + ;; + "xfs") + ${lib.getExe' pkgs.xfsprogs "xfs_growfs"} ${cfg.mountPoint} + ;; + "btrfs") + ${lib.getExe' pkgs.btrfs-progs "btrfs"} filesystem resize max ${cfg.mountPoint} + ;; + esac + + echo "Current size:" + ${lib.getExe' pkgs.coreutils "df"} -h ${cfg.mountPoint} + ''; + }; + }; + + environment.systemPackages = + [ + pkgs.parted + pkgs.cloud-utils + pkgs.coreutils + ] + ++ lib.optional (cfg.fsType == "ext4") pkgs.e2fsprogs + ++ lib.optional (cfg.fsType == "xfs") pkgs.xfsprogs + ++ lib.optional (cfg.fsType == "btrfs") pkgs.btrfs-progs; + }; +}