Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

KVM: Configure libvirt Network

September 24, 2023 — Gregg Szumowski

You can update the network configuration for your KVM installation using the command line using the virsh command.

To list all of the available network enter the following command. The --all will is used to include the inactive networks:

# virsh net-list --all
Name State Autostart
-----------------------------------------
default active yes
NattedNetwork active yes

Then, edit the network you wish to update:

# EDITOR="vi" virsh net-edit NattedNetwork

Add host configuration(s) or whatever changes you wish to make to XML file:

<network>
<name>NattedNetwork</name>
<uuid>8483028d-667b-47e7-9a8e-f269783a8246</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:ad:b9:ed'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>

Once you’re done, restart the network for the changes to take effect:

# virsh net-destroy NattedNetwork
# virsh net-start NattedNetwork

Tags: cli, libvirt, kvm, network, motd

KVM: Importing an OVA appliance

September 09, 2023 — Gregg Szumowski

You may or may not be aware if it, but an OVA file is just a tar archive containing an .ovf and a .vmdk files, respectively the VM configuration and disk.

$ ls *.ova
HTAOE.ova
$ tar tf HTAOE.ova
HTAOE.ovf
HTAOE-disk001.vmdk
HTAOE.mf

So, you can simply extract the files:

$ tar xvf HTAOE.ova

And convert to a format appropriate for QEMU/KVM:

List the available formats

$ qemu-img -h | tail -n4
Supported formats: blkdebug blklogwrites blkverify bochs cloop compress copy-before-write copy-on-read dmg file ftp ftps host_cdrom host_device http https luks nbd null-aio null-co nvme parallels preallocate qcow qcow2 qed quorum raw replication ssh throttle vdi vhdx vmdk vpc vvfat

See <https://qemu.org/contribute/report-a-bug> for how to report bugs.
More information on the QEMU project at <https://qemu.org>.

Do the actual conversion (I chose qcow2 here)

$ qemu-img convert -O qcow2 HTAOE.vmdk HTAOE.qcow2

Have a look at the .ovf too, for information on expected machine configuration, resources (eg. memory and cpu), etc.

After the conversion, simply create a new VM and make it use the newly created disk as the primary disk.

Tags: cli, kvm, ova, virtualization, qemu, motd

Find the IP Addresses of KVM Virtual Machines (Command Line)

July 22, 2023 — Gregg Szumowski

To find details about the virtual network you can use these commands:

root@slacker:~# virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes

root@slacker:~# virsh net-info default
Name: default
UUID: 14a90f27-9a85-42ca-b434-6ce6c142690c
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0

root@slacker:~# virsh net-dhcp-leases default
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
------------------------------------------------------------------------------------------------------------
2023-07-22 16:18:45 52:54:00:dd:7b:62 ipv4 192.168.122.216/24 centos7-bbk -

You will find the IP address and hostname listed in the last command’s output.

Optionally, to find the network interfaces’s addresses for a running domain called centos7-bbk:

root@slacker:~# virsh list
Id Name State
-----------------------------
3 centos7-bbk running

root@slacker:~# virsh domifaddr centos7-bbk
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet2 52:54:00:dd:7b:62 ipv4 192.168.122.216/24

root@slacker:~#

Tags: cli, kvm, virsh, networking, motd

Create a QEMU/KVM Virtual Machine from the Command Line

July 15, 2023 — Gregg Szumowski

You can use a combination of command line tools to create and configure a virtual machine. Here we will use few tools from the QEMU and libvirt packages to do this.

Use QEMU to create a 15GB QCOW disk image:

$ qemu-img create -f qcow2 /home/user/KVM/CentOS-Stream-9.qcow2 15G
Formatting '/home/user/KVM/CentOS-Stream-9.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=16106127360 lazy_refcounts=off refcount_bits=16

Start the installation:

$ sudo virt-install --name=CentOS-Stream-9 --vcpus=1 --memory=1024 --location=/home/user/Downloads/CentOS-Stream-9-20230704.1-x86_64-boot.iso --os-variant=centos8 --network bridge:virbr0 --disk path=/home/user/KVM/CentOS-Stream-9.qcow2 --disk size=15
Password:
WARNING Requested memory 1024 MiB is less than the recommended 1536 MiB for OS centos8

Starting install...
Retrieving 'vmlinuz' | 0 B 00:00:00 ...
Retrieving 'initrd.img' | 0 B 00:00:00 ...
Allocating 'CentOS-Stream-9.qcow2' | 2.5 MB 00:00:03 ...
WARNING Overriding memory to 3072 MiB needed for centos8 network install.
Creating domain... | 0 B 00:00:00
Running graphical console command: virt-viewer --connect qemu:///system --wait CentOS-Stream-9

Once this gets to this point another window should open up by the virt-viewer application (which you should install if you didn’t already) and you can complete the installation and reboot. You’ll use the same virt-viewer window to use the shell or desktop (depending upon the distro you installed).

When you are done and close the virt-viewer GUI you can find the VM in the running state using virsh:

$ sudo virsh list --all
Password:
Id Name State
------------------------------------
5 CentOS-Stream-9 running
- slackware-current shut off

Then you can shut it down:

$ sudo virsh shutdown 5
Password:
Domain '5' is being shutdown
$ sudo virsh list --all
Password:
Id Name State
------------------------------------
- CentOS-Stream-9 shut off
- slackware-current shut off

Optionally, you can delete the VM domain and the QCOW file using the undefine parameter:

$ sudo virsh undefine --domain CentOS-Stream-9 --remove-all-storage
Password:
Domain 'CentOS-Stream-9' has been undefined
Volume 'vda'(/home/user/KVM/CentOS-Stream-9.qcow2) removed.

Tags: cli, qemu, kvm, motd

How to convert VirtualBox VDI to KVM qcow2

May 10, 2023 — Gregg Szumowski

It is easy to convert a VirtualBox VDI image to a KVM qcow2 file. You have to use the RAW file format as an intermediate.

Make sure the VirtualBox machine is shutdown.

  1. Convert the VDI to a raw disk image.
    Note: VDIs are compressed and raw images are not, so you will need to leave enough disk space for entire uncompressed disk.

    $ VBoxManage clonehd --format RAW vm.vdi vm.img

  2. Then on your KVM host:
    $ qemu-img convert -f raw vm.img -O qcow2 vm.qcow2

Tags: virtualization, virtualbox, kvm, qcow2, motd