To show available os for guest
$ virt-install --osinfo list | grep arch
archlinux
To use bridge add /etc/qemu/bridge.conf if not available and chmod file /usr/lib/qemu/qemu-bridge-helper
# mkdir /etc/qemu
# touch /etc/qemu/bridge.conf
# echo "allow br0" >> /etc/qemu/bridge.conf
# chmod u+s /usr/lib/qemu/qemu-bridge-helper
To create guest with name=guest01, disk size 10GB, Ram 2Gb (2048), virtual processor 2, os variant debian 12 (not available, we use debian11), boot from iso file:
$ virt-install \
--name guest01 \
--memory 2048 \
--vcpus 2 \
--disk path=/home/dedetok/guests/guest01.qcoe2,size=10,bus=virtio \
--cdrom /home/dedetok/Downloads/debian-12.11.0-amd64-netinst.iso \
--nonetworks \
--os-variant debian11 \
--virt-type kvm
Parameters:
- --name: name to identify guest
- --ram: guest memory in megabytes
- --vcpus: number of cpu for guest
- --disk: path=<path_to_disk_image>,size=<disk_size_in_gb>,bus=virtio
virtio is standard interface for virtual machines, it improve vm network performance
- --cdrom: install from iso file or CD/DVD/USB
- --nonetworks: no update or install from internet
or
--network bridge=br0,model=virtio to use bridge network, see part 2
For bridge see Network Bridge section
Options:
- Graphics option
- --graphics vnc: Enables VNC for graphical access. If virt-viewer is installed, it will automatically launch. If not, you'll need to manually connect using a VNC client like vinagre or remmina.
- --graphics spice: Enables SPICE for graphical access. SPICE is generally considered more modern and efficient than VNC.
- --graphics none: Disables graphical access and forces a text-mode installation using the serial console.
- Disk option
- Default folder for virtual disk /var/lib/libvirt/images/
qcow2 offers features that raw (or img) doesn't:
- Snapshots: qcow2 allows you to create snapshots of your virtual machine's disk, enabling easy rollback to previous states.
- Compression: It can compress the disk image, potentially saving storage space.
- Sparse files: qcow2 supports sparse files, meaning it only allocates disk space for used portions of the image, which can be more efficient.
- raw format (or just img when using virt-install) has no special features:
It simply represents the raw data of the disk, which can be less flexible and potentially wasteful of disk space.
To show version
$ virsh version
Compiled against library: libvirt 9.0.0
Using library: libvirt 9.0.0
Using API: QEMU 9.0.0
Running hypervisor: QEMU 7.2.17
Managing VM
To List guest cm
$ virsh list --all
Connect to vm
$ virsh console [vm_name]
or using virt-viewer
$ virt-viewer [vm_name]
To edit vm
$ virsh edit [vm_name]
To start vm
$ virsh start [vm_name]
to restart vm
$ virsh reboot [vm_name] --mode initctl
to force stoping vm
$ virsh destroy
[vm_name]
to force shutdown vm
$ virsh shutdown
[vm_name]
--mode acpi
to suspend vm
$ virsh suspend
[vm_name]
to resume vm after suspend
$ virsh resume
[vm_name]
to reset vm (similiar to pressing reset button on physical PC)
$ virsh reset
[vm_name]
Restarting KVM Daemon
# systemctl restart libvirtd
to remove vm and its storage permanently
$ virsh undefine --managed-save --remove-all-storage
[vm_name]
To make vm auto run after host restart (run once)
$ virsh autostart
[vm_name]
Network bridge
Network configuration file:
- /etc/libvirt/qemu/networks/default.xmlfir active configuration
- /usr/share/libvirt/networks/default.xml for template
To enable network bridge for guest:
- turn up bridge interface
- list all network using virsh net-list
- if list is empty, define default network and edit default network to use existing bridge
- start network bridge
To turn up bridge interface
# ifup br0
To show bridge interface
# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.9aa237b1bcc8 no enp2s0
To show network bridge (if list empty, see define default network)
$ virsh net-list --all
To define default network
$ virsh net-define /usr/share/libvirt/networks/default.xml
To undefined default network
$ virsh net-undefine default
To edit default network
$ virsh net-edit default
Change default network using edit default netowrk
<network>
<name>default</name>
<uuid>84b29e1f-b2c3-4230-bc21-fba0143c026c</uuid>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
To start network bridge
$ virsh net-start default
To auto start network bridge
$ virsh net-autostart default
To add manually bridge network into vm edit vm and add
<domain type='kvm'>
...
<devices>
<interface type='bridge'>
<mac address='52:54:00:87:65:f6'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
...
References:
- wiki.debian.org/KVM
- wiki.debian.org/DebianInstaller/Preseed
- wiki.debian.org/BridgeNetworkConnections