-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1784028
commit 524027c
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |