Skip to content

Commit

Permalink
nixos/hot-resize: init
Browse files Browse the repository at this point in the history
  • Loading branch information
liberodark committed Dec 10, 2024
1 parent 1784028 commit 524027c
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions nixos/modules/services/system/hot-resize.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}

0 comments on commit 524027c

Please sign in to comment.