From 19890539087a2c5387aa03a26e0aa45f6dacc7c5 Mon Sep 17 00:00:00 2001 From: kalle Date: Mon, 5 Jan 2026 22:11:35 +0100 Subject: [PATCH] feat: add auto-grow-root service for live disk resize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds systemd service that automatically grows the root filesystem on boot: - Detects partition layout and grows partition if needed (growpart) - Supports both ext4 (resize2fs) and btrfs filesystems - Works with VIRTIO-SCSI disk (/dev/sda) - Enables seamless live disk resize from the hosting platform 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- configuration.nix | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/configuration.nix b/configuration.nix index 1716f0b..61ed7e7 100644 --- a/configuration.nix +++ b/configuration.nix @@ -89,6 +89,38 @@ # Enable the OpenSSH daemon. services.openssh.enable = true; + # Auto-grow root filesystem on boot (for live disk resize support) + # Works with VIRTIO-SCSI (/dev/sda) - detects partition layout automatically + systemd.services.auto-grow-root = { + description = "Auto-grow root filesystem"; + wantedBy = [ "multi-user.target" ]; + after = [ "local-fs.target" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + path = with pkgs; [ cloud-utils parted e2fsprogs btrfs-progs util-linux ]; + script = '' + set -euo pipefail + + ROOT_DEV=$(findmnt -n -o SOURCE /) + ROOT_DISK=$(lsblk -no PKNAME "$ROOT_DEV" | head -1) + + # If partitioned, grow partition first + if [[ "$ROOT_DEV" =~ [0-9]$ ]]; then + PART_NUM=$(echo "$ROOT_DEV" | grep -oE '[0-9]+$') + growpart "/dev/$ROOT_DISK" "$PART_NUM" 2>/dev/null || true + fi + + # Grow filesystem + FSTYPE=$(findmnt -n -o FSTYPE /) + case "$FSTYPE" in + ext4) resize2fs "$ROOT_DEV" 2>/dev/null || true ;; + btrfs) btrfs filesystem resize max / 2>/dev/null || true ;; + esac + ''; + }; + # This value determines the NixOS release from which the default # settings for stateful data, like file locations and database versions