From 3ff504c70b291d6c41fdd2acbc15c58982f0e847 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Tue, 2 Apr 2024 15:14:29 -0300 Subject: Feat: adds the command --- kvmx | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'kvmx') diff --git a/kvmx b/kvmx index 3a2e666..ddd9498 100755 --- a/kvmx +++ b/kvmx @@ -2457,6 +2457,85 @@ function kvmx_usb_detach { sudo chown root /dev/bus/usb/$bus/$device } +# Grow a partition +# This will restart the guest a few times +# Inspired by https://ahelpme.com/linux/online-resize-of-a-root-ext4-file-system-increase-the-space/ +function kvmx_growpart { + local device="$1" + local partition="$2" + local size="$3" + + # Syntax check + if [ -z "$size" ]; then + echo "usage: $BASENAME growpart $VM " + echo "example: $BASENAME growpart test /dev/vda 2 5G" + exit 1 + fi + + # Ensure the guest is running + if ! kvmx_running; then + echo "Powering up guest..." + kvmx_up + fi + + # Check for $partition + echo "Checking for partition ${device}${partition}..." + echo /bin/test -e ${device}${partition} | kvmx_ssh + if [ "$?" != "0" ]; then + echo "Could not determine ${device}${partition} existence, aborting." + exit 1 + fi + + # Ensure cloud-guest-utils is installed + echo "Checking for cloud-guest-utils availability..." + echo which growpart | kvmx_ssh &> /dev/null + if [ "$?" != "0" ]; then + + echo which apt-get | kvmx_ssh &> /dev/null + if [ "$?" == "0" ]; then + kvmx_ssh sudo apt-get install -y cloud-guest-utils + else + echo "Please install cloud-guest-utils in the guest first" + exit 1 + fi + fi + + # Now make sure the guest is off + echo "Powering off guest" + kvmx_poweroff + + # Resize the image + echo "Resizing the image to an additional ${size}" + qemu-img resize $image +${size} + + # Power up + echo "Powering up the guest again" + kvmx_up + + # Resize virtual machine root partition - while the filesystem is mounted! + # this parted command currently need to be done manually + # + # Check https://unix.stackexchange.com/questions/373063/auto-expand-last-partition-to-use-all-unallocated-space-using-parted-in-batch-m + # https://unix.stackexchange.com/questions/190317/gnu-parted-resizepart-in-script#202872 + # https://bugs.launchpad.net/ubuntu/+source/parted/+bug/1270203 + # https://techtitbits.com/2018/12/using-parteds-resizepart-non-interactively-on-a-busy-partition/ + # https://serverfault.com/questions/870594/resize-partition-to-maximum-using-parted-in-non-interactive-mode + # + #echo resizepart 2 -1 | kvmx ssh $guest sudo parted /dev/vda + #kvmx_ssh sudo parted /dev/vda resizepart 2 -1 Yes + echo "Growing ${device}${partition}..." + kvmx_ssh sudo growpart ${device} ${partition} + + # Resize the file system and schedule a fsck for the next reboot + echo "Resizing the ${device}${partition} filesystem..." + kvmx_ssh sudo resize2fs ${device}${partition} + kvmx_ssh sudo touch /forcefsck + + # Restart + echo "Restarting the guest..." + kvmx_restart +} + # Dispatch if type kvmx_$ACTION 2> /dev/null | grep -q "kvmx_$ACTION ()"; then __kvmx_initialize $* -- cgit v1.2.3