Useful commands for configuring KVM virtual machines


KVM


install KVM

apt install –no-install-recommends qemu-system libvirt-daemon-system virtinst dnsmasq qemu-utils

note: this command is intended for use on Debian 12


Network


list networks

virsh net-list –all

create a temporary network config file

cat <<EOF > /tmp/br0
<network>
  <name>br0</name>
  <bridge name='br0' stp='on'/>
  <ip address='192.168.100.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.100.2' end='192.168.100.254'/>
    </dhcp>
  </ip>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
</network>
EOF

define a network

virsh net-define /tmp/br0

configure a network autostart

virsh net-autostart br0

activate a network

virsh net-start br0

deactivate a network

virsh net-destroy br0

undefine a network

virsh net-undefine br0

attach a network

virsh attach-interface –domain test0 –type bridge –source tmp0 –model virtio –config

detach a network

virsh detach-interface –domain test0 –type bridge –mac 00:11:22:33:44:55 –config


Storage pool


list storage pools

virsh pool-list –all

create a directory for a storage pool

mkdir /mnt/pool0

create a temporary storage pool config file

cat <<EOF > /tmp/pool0
<pool type='dir'>
  <name>pool0</name>
  <target>
    <path>/mnt/pool0</path>
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>
EOF

define a storage pool

virsh pool-define /tmp/pool0

configure a pool autostart

virsh pool-autostart pool0

activate a storage pool

virsh pool-start pool0

deactivate a storage pool

virsh pool-destroy pool0

undefine a storage pool

virsh pool-undefine pool0


Storage volume


create a temporary volume config file

cat <<EOF > /tmp/vol0
<volume type='file'>
  <name>vol0</name>
  <capacity unit='G'>20</capacity>
  <target>
    <format type='qcow2'/>
  </target>
</volume>
EOF

create a volume

virsh vol-create pool0 /tmp/vol0

delete a volume

virsh vol-delete vol0 pool0

attach a volume

virsh attach-disk –domain test0 –source /mnt/pool0/tmp0 –target vdb –driver qemu –subdriver qcow2 –config

detach a volume

virsh detach-disk –domain test0 –target vdb –config


VM


list VMs

virsh list –all

list available OS names

virt-install –osinfo list

create a VM

virt-install –name test0 –osinfo debian11 –cpu host-passthrough –vcpus 4 –memory 4096 –graphics vnc,listen=1.2.3.4 –disk /mnt/pool0/vol0,bus=virtio –network bridge=br0,model=virtio –noautoconsole –noreboot –import

start a VM

virsh start test0

suspend a VM

virsh suspend test0

resume a VM

virsh resume test0

stop a VM gracefully

virsh shutdown test0 –mode acpi

stop a VM forcefully

virsh destroy test0

reboot a VM gracefully

virsh reboot test0

reboot a VM forcefully

virsh reset test0


VNC


get a listening port

virsh dumpxml test0 | grep vnc | sed -rn “s/.port='([0-9])’.*/\1/p”

connect to a VM using any VNC client


ISO


enter a VM config editing mode

virsh edit test0

add the following lines

<device>
  ...
  <disk type='file' device='cdrom'>
    <source file='/home/ISO/debian-12.2.0-amd64-netinst.iso'/>
    <target dev='hda' bus='sata'/>
    <readonly/>
  </disk>
  ...
</device>

edit the following lines

<os>
  ...
  <boot dev='cdrom'/>
  ...
</os>

restart a VM


Cloning


stop a VM

clone a VM

virt-clone –original test0 –name test99 –file /mnt/pool0/vol99

note: if multiple disks are connected to the VM, then “–file” options should be specified in the same order in which these disks are specified in the VM XML description


Snapshotting


list snapshots

virsh snapshot-list test0

create a snapshot config file

cat <<EOF > /tmp/snapshot0
<domainsnapshot>
  <name>snapshot0</name>
</domainsnapshot>
EOF

create a snapshot

virsh snapshot-create –atomic –domain test0 –xmlfile /tmp/snapshot0

apply a snapshot

virsh snapshot-revert –domain test0 –snapshotname snapshot0

delete a snapshot

virsh snapshot-delete –domain test0 –snapshotname snapshot0


Monitoring


vcpu usage

virsh vcpuinfo –domain test0

memory usage

virsh dommemstat –domain test0

network interfaces

virsh domifaddr –domain test0


Links


Network XML format – https://libvirt.org/formatnetwork.html
Storage pool and volume XML format – https://libvirt.org/formatstorage.html
Domain XML format – https://libvirt.org/formatdomain.html
Snapshot XML format – https://libvirt.org/formatsnapshot.html
Manual Snapshots – https://libvirt.org/kbase/snapshots.html

Leave a Reply