User Tools

Site Tools


computing:virtmanagerhell

  • virtmanagerhell
  • Jonathan Haack
  • Haack's Networking
  • netcmnd@jonathanhaack.com

To make a VM from the command line, do the following. Note that this recipe assumes you have already created your virtual switch, br0. It also presumes you have already created your virtual disk, and if you have not, simply run sudo qemu-img create -f raw new.img 1000G.

sudo virt-install --name=new.img \
--os-type=Linux \
--os-variant=debian10 \
--vcpu=1 \
--ram=2048 \
--disk path=/mnt/vms/students/new.img \
--graphics spice \
--location=/mnt/vms/isos/debian-11.4.0-amd64-netinst.iso \
--network bridge:br0

To clone an existing image, do the following:

virt-clone \
--original=clean \
--name=sequoia \
--file=/mnt/vms/students/sequoia.img

If you have a legacy image that needs to be larger, then install a few tools and the proceed to expand the virtual hard disk as follows:

apt install qemu-img kpartx
qemu-img resize debian10.img +50G

After expanding the virtual hard disk, open gparted in X passthrough / command line and expand the existing partition into as much of the the new space as you prefer. And to rename a VM (domain), do the following:

virsh shutdown <old-name>
virsh domrename <old-name> <new-name>

To delete a domain, virt-manager uses the undefine command. To remove the accompanying storage with it, parse the command as follows:

virsh undefine guest1 --remove-all-storage

If you need to force stop a machine, virt-manager uses the destroy command, as follows:

virsh destroy guest1

I prefer the raw format (.img) but if I change my mind later, perhaps because I want snapshots, then I can easily shutdown the machine and convert the image as follows. If I change my mind, I can also go in reverse back to raw.

qemu-img convert -p -f raw -O qcow2 guest1.img guest1.qcow2
qemu-img convert -p -f qcow2 -O raw guest1.qcow2 guest1.raw

If you do end up using this, then you will need to edit the virtual machine's xml file. To do that, open the file and edit the disk type. Change type and source file= to match the new type and extension. If you also moved the location, you can edit that now as well.

virsh edit guest1
<driver name='qemu' type='raw' cache='none'/>
<source file='/mnt/vms/students/guest1.qcow2'/>

Another essential tool is the ability to create snapshots and, when necessary, revert to them. Here are the commands to create snapshot1 and then later revert to that specific snapshot.

virsh snapshot-create-as guest1 snapshot1 --description "first snapshot 11-13-22"
virsh snapshot-revert guest1 snapshot1

If you don't care about naming the snapshot or customizing the description, you may optionally create the snapshot by excuting this:

virsh snapshot-create guest1

To create an external snapshot, append the following arguments:

virsh snapshot-create-as guest1 snapshot1 --description "first snapshot 11-13-22" --diskspec vda,snapshot=external

If you want to revert to the latest snapshot, then execute this:

virsh snapshot-revert guest1 --current

If you need to delete a particular snapshot, or delete all children of a snapshot, execute:

virsh snapshot-delete guest1 snapshot1
virsh snapshot-delete guest1 --current --children-only

To list all snapshots for a particular guestOS, execute this:

virsh snapshot-list guest1

To get information about the snapshot, these commands help:

virsh snapshot-info guest1 snapshot
virsh snapshot-dumpxml guest1 snapshot1

If you need to make a live backup, do the following (Note: make sure that sudo apt install qemu-guest-agent is installed on the guest OS):

virsh domfsfreeze guest1.qcow2
qemu-img create -f qcow2 -b guest1.qcow2 snapshot.qcow2
virsh domfsthaw guest1.qcow2

At times you may need to resize or gather information about a particular virtual disk. If they are in the qcow2 format, gather information as follows:

qemu-img info disk.qcow2

To regain space that is being used needlessly, you can sparsify the qcow2 disk. Note that you must install virt-sparsify separately with sudo apt install libguestfs-tools.

virt-sparsify --in-place disk.qcow2

Most importantly, there's often a need to expand a qcow2 disk. For that, I first recommend you backup the disk. Then, you can either specify the desired or target size, or you specify an increment of space to add on to the disk.

cp -ar --sparse=always disk.qcow2 disk.bk.qcow2
qemu-img resize disk.qcow2 1000G
qemu-img resize disk.qcow2 +10G

Okay, so another big issue with qcow2 images is them growing over time from writes/deletes and then those blocks taking up space on the hypervisor while not actually being used on the guestOS any longer. To stop this, enable unmap on the virtIO disk in virsh, and then schedule a timer within the guest OS to trim those underlying blocks.

sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer

You can also manually run fstrim and then power down the qcow2 image and convert it. You may optionally use compression to save more space, but it takes very long.

fstrim -v /
qemu-img convert -O qcow2 guest.qcow2 guest-trimmed.qcow2
qemu-img convert -O qcow2 -c guest.qcow2 guest-trimmed.qcow2

To test trimming functionality, I use a combination of ``ncdu`` which shows actually consumed space on the host OS, along with the following dd command to easily consume and delete space:

dd if=/dev/urandom of=file bs=4M count=500

To create a backup volume inside a guest you create the volume, attach it, and then shell into the guest and format, mount, and create an fstab entry. First, on the hostOS:

cd /mnt/vms/backups/vm1-backup-dir/
qemu-img create -f qcow2 vm1-backup.qcow2 32G
virsh attach-disk guestOS.qcow2 \
  --source /mnt/vms/backups/vm1-backup-dir/vm1-backup \
  --target vdb \
  --persistent

Then, on the guestOS:

mkdir /mnt/backup
mkfs.ext4 /dev/vdb
mount -t auto /dev/vdb /mnt/backup
nano /etc/fstab
/dev/vdb /mnt/backup ext4 defaults, 0 0

The rest from here on out is my attempt at resizing an .img virtual disk using tools exclusively from virsh / virt-manager. These are highly risky moves and totally not needed for day to day operations. It was more of a mission I was on and based on a tutorial I used nearly 15 years ago when expanding a Windows VM I used for teaching software that was only on that VM. At any rate, I have only succeeded twice doing this, and often get confused looking at the l00ps. Proceed with caution!

sudo apt install libguestfs-tools

After that, confirm the location of your virtual machine. You need to know the actual name of the .img file for this to work. Usually (unless it was changed by someone), this is the name that appears in the virt-manager window.

virsh dumpxml putnameofimagehere | xpath -e /domain/devices/disk/source

Navigate to the directory it spit out, and back up the image and rename it before you proceed:

cd /var/lib/libvirt/images/
sudo cp /var/lib/libvirt/images/putnameofimagehere.img /var/lib/libvirt/images/putnameofimagehere.img.bak

Once you know the name of the .img file and the location of it, prepare the virtual file systems for expansion:

virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/putnameofimagehere.img

Build a new, larger, container for the final disk, and set it aside for the merge/expansion that the tools are going to provide:

truncate -s 128G /var/lib/libvirt/images/outdisk

Now it is time to resize the existing image, using the container you just created:

virt-resize --expand /dev/sda2 /var/lib/libvirt/images/putnameofimagehere.img /var/lib/libvirt/images/outdisk

Okay, now we need to rename the new image we created to the name that virt-manager is looking for, i.e., the name of the original image. Remember, you should have the .bak file you made earlier in case this does not work. This is done as follows:

cp /var/lib/libvirt/images/putnameofimagerhere.img /var/lib/libvirt/images/putnameofimagerhere.img.old
mv /var/lib/libvirt/images/outdisk /var/lib/libvirt/images/putnameofimagehere.img

Made yet another attempt to do by command line in 2022. Install packages required for resizing virtual hard disks and for listing partitions inside virtual hard disks.

apt install qemu-img kpartx

Add 50G of size to the emulated physical hard disk. This is not to be confused with adding space to, or extending, the partition inside it.

qemu-img resize debian10.img +50G

Mount the image file in the present directory, and show all of its mappings.

kpartx -v -a debian10.img

Now, it is time to extend the partition. Your loops might have different #s after, use tab to find the right one (prolly largest #).

parted /dev/loopXX print

You will get output like this:

Model: Unknown (unknown)
Disk /dev/loopXX: 142GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 
Number  Start  End    Size   File system  Flags
 1      0.00B  142GB  142GB  ext4
 

Remove the appropriate mapping. In my case, I am extending the boot volume of a virtual machine, so it was entry 1.

parted /dev/loopXX rm 1

Extend the partition from the start of the mapping you removed to the upper size limit listed in Disk.

parted /dev/loopXX mkpart primary ext4 0.00B 142GB

Verify the new mapping.

parted /dev/loopXX print

Run fsck before resizing the file system. You here appending “p1” to the name in order to make a new partition or mapping.

e2fsck -f /dev/mapper/loopXXp1

Extend the file system inside the partition to the entirety of the partition table.

resize2fs /dev/mapper/loopXXp1 

Unmount the image

kpartx -d debian10.img

oemb1905 2023/01/15 10:31

computing/virtmanagerhell.txt · Last modified: 2023/01/15 17:47 by oemb1905