Browse Source

fix: use cloud-init to grow partition and resize fs (#107)

main 1.4.5
Giovanni Torres 4 months ago committed by GitHub
parent
commit
636585db7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 30
      kvm-install-vm
  2. 23
      tests/check_disk_expansion.bats

30
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

23
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" ]]
}
Loading…
Cancel
Save