diff options
Diffstat (limited to 'kvmx')
-rwxr-xr-x | kvmx | 94 |
1 files changed, 94 insertions, 0 deletions
@@ -881,6 +881,21 @@ function kvmx_ssh { $ssh_env $SSH_COMMAND -p $SSH 127.0.0.1 $* } +# Enhanced SSH login into the guest +function kvmx_login { + # This allows the usage of a custom login command + # + # It's not implemented directly in kvmx_ssh because it conflicts with use + # cases where SSH commands are read from stdin and not from positional + # arguments, like `echo something | kvmx_ssh`. + if [ -z "$1" ] && [ ! -z "$ssh_login_command" ]; then + # This needs the -t option to request a pseudo-terminal + kvmx_ssh -t $ssh_login_command + else + kvmx_ssh $* + fi +} + # Get guest SSH key fingerprints function kvmx_ssh_finger { if ls $DATADIR/ssh/*.pub.* &> /dev/null; then @@ -2457,6 +2472,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 <device> <partition> <additional_size>" + 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 $* |