| title | Virtual Machine Management |
|---|---|
| description | Creating, Maintaining, and Destroying Virtual Machines |
| published | true |
| date | 2023-08-25 15:34:44 UTC |
| tags | infra |
| editor | markdown |
| dateCreated | 2021-06-02 14:47:57 UTC |
Sometimes the initial size of a VM's storage was too small and it is necessary to increase it. If we host the virtual machine we can resize it ourselves. Production applications will require IS&S involvement to add more space.
If the partition table uses logical partitions inside an Extended partition (partition number starts with 5 or higher - e.g., /dev/sda5), you cannot use virt-resize. Unfortunately, this is how the Ubuntu 20.04 image from virt-builder is set up, so partitions on those machines have to be resized manually.
This is an example partition table that virt-resize CAN expand:
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 12580863 12578816 6G 83 Linux
This is an example partition table that virt-resize CANNOT expand:
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 1050623 1048576 512M b W95 FAT32
/dev/vda2 1050624 41943039 40892416 19.5G 5 Extended
/dev/vda5 1050626 41943039 40892414 19.5G 83 Linux
You can get a view into the disk image without booting the VM with virt-filesystems:
root@fee:/vm/vm0# virt-filesystems --partitions --long -a seedMain.img
Name Type MBR Size Parent
/dev/sda1 partition 0b 536870912 /dev/sda
/dev/sda2 partition 05 1024 /dev/sda
/dev/sda5 partition 83 20936915968 /dev/sda
- Create a new image with the desired size in the qcow2 format:
qemu-img create -f qcow2 -o preallocation=metadata newdisk.img 20G - Resize the old image into the new image, expanding the given partition to fill the new space:
virt-resize --expand /dev/sda2 olddisk.img newdisk.img - Rename the old image (for backup purposes) and rename the new image to replace the old one.
- Boot the virtual machine - it may take awhile the first time
- Create a new image with the desired size in the qcow2 format:
qemu-img create -f qcow2 -o preallocation=metadata newdisk.img 20G - Copy the old image into the new image. The partitions will be the same size.
virt-resize olddisk.img newdisk.img - Rename the old image (for backup purposes) and rename the new image to replace the old one.
- Boot into the VM
- To resize without rebooting, follow the steps in https://medium.com/100-days-of-linux/how-to-resize-a-linux-root-file-system-af3e5096b4e4
a. Using
fdisk, delete and recreate the main filesystem partition (the Extended and Linux partition) b. Make sure the starting sector of the new Linux partition matches the old one. You may need to adjust the starting sector of the new partition in expert mode so that it matches the starting sector of the old partition. https://unix.stackexchange.com/a/320447 c. Usepartprobeto reload the partition table d. Resize the filesystem in the new partition to use the new available space - Reboot the virtual machine - it may take awhile the first time. Cross your fingers.
References virt-resize man page with examples Resize Linux Root Filesystem Extend Logical Partition inside Extended Partition virt-filesystems man page Red Hat virt-resize guide
echo "domain1 domain2etc " | xargs -d " " -L1 virsh destroy
virsh list --inactive | cut -c 8-38 | tr -d [:blank:] | grep -v Base | grep -v '^base$' | tail -n +3 > inactive_domains.txt
Remove domains from inactive_domains.txt if you wish to keep them.
cat inactive_domains.txt | xargs -L1 virsh undefine
cat inactive_domains.txt | awk '$0="/vm/vm0/"$0".img"' | xargs rm