diff --git a/kvm-install-vm b/kvm-install-vm index 3397d1d..2038757 100755 --- a/kvm-install-vm +++ b/kvm-install-vm @@ -589,6 +589,14 @@ ssh_authorized_keys: - ${KEY} timezone: ${TIMEZONE} + +# Disk and filesystem growth +growpart: + mode: auto + devices: ['/'] + ignore_growroot_disabled: false + +resize_rootfs: true _EOF_ if [ ! -z "${SCRIPTNAME+x}" ]; then @@ -624,23 +632,11 @@ _EOF_ if $RESIZE_DISK; then outputn "Resizing the disk to $DISK_SIZE" - # Workaround to prevent virt-resize from renumbering partitions and breaking grub - # See https://bugzilla.redhat.com/show_bug.cgi?id=1472039 - # Ubuntu will automatically grow the partition to the new size on its first boot - case "$DISTRO" in - ubuntu* | amazon2) - qemu-img resize $DISK $DISK_SIZE &>> ${VMNAME}.log && - ok || - die "Could not resize disk." - ;; - *) - qemu-img create -f qcow2 \ - -o preallocation=metadata $DISK.new $DISK_SIZE &>> ${VMNAME}.log && - virt-resize --quiet --expand /dev/sda1 $DISK $DISK.new &>> ${VMNAME}.log && - (mv $DISK.new $DISK && ok) || - die "Could not resize disk." - ;; - esac + # Cloud-init will automatically grow the partition and filesystem on first boot + # via the growpart and resize_rootfs modules configured in user-data + qemu-img resize $DISK $DISK_SIZE &>> ${VMNAME}.log && + ok || + die "Could not resize disk." fi # Create new storage pool for new VM diff --git a/tests/check_disk_expansion.bats b/tests/check_disk_expansion.bats new file mode 100644 index 0000000..f26689d --- /dev/null +++ b/tests/check_disk_expansion.bats @@ -0,0 +1,23 @@ +#!/usr/bin/env bats + +teardown() { + # Clean up specific VMs that THIS test might have created + ./kvm-install-vm remove "${VMPREFIX}"-expand-15g 2>/dev/null || true +} + +@test "VM creation with -d 15 expands disk to 15GB" { + # Create VM with 15GB disk + run timeout $TIMEOUT ./kvm-install-vm create -d 15 "${VMPREFIX}"-expand-15g + [ "$status" -eq 0 ] + [[ "${output}" =~ "Resizing the disk to 15G" ]] + + # Shut down VM to release disk lock for qemu-img + virsh shutdown "${VMPREFIX}"-expand-15g + sleep 10 + + # Verify disk size using qemu-img info + DISK_PATH="${VMDIR}/${VMPREFIX}-expand-15g/${VMPREFIX}-expand-15g.qcow2" + run qemu-img info "$DISK_PATH" + [ "$status" -eq 0 ] + [[ "${output}" =~ "virtual size: 15 GiB" ]] +}