KVM

KVM on Debian 12, tested on the N100 Mini PC.

Install

Some of these tools will require an X11 server which can be VxSrv on Windows, for example.

apt install --no-install-recommends qemu-system libvirt-clients libvirt-daemon-system virt-viewer virtinst virt-manager qemu-utils

qemu

To allow a user to create a virtual network permissions must be altered for a qemu script.

If it doesn't exist, create a script `/etc/rc.local'.

#! /bin/bash
chmod u+s /usr/lib/qemu/qemu-bridge-helper
exit 0

Make the script executable and run it.

chmod +x /etc/rc.local && /etc/rc.local

To make this script run at every boot with systemd add the following to `/etc/systemd/system/rc-local.service'

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/rc.local

[Install]
WantedBy=multi-user.target

Enable the service with

systemctl enable rc-local.service

User

Add login to group(s).

Debian says add your login to group libvirt and other guides recommend group kvm as well. The latter group requirement is unknown.

adduser `login' libvirt
adduser `login' kvm

Test

Export the hostname of your X11 server and on that host allow access via xhost.

setenv DISPLAY excellent:0.0
virt-manager

Directories

VM storage

mkdir -p /opt/VM
chown 0:0 /opt/VM
chmod 1777 /opt/VM

ISO storage

mkdir -p /opt/ISO
cd /opt/ISO
wget https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-12.4.0-amd64-DVD-1.iso
sha256sum debian-12.4.0-amd64-DVD-1.iso
3a85e04f0bbdf7ba0cdd7537dd781c583f70754db67e5a757f113442cade56ea  debian-12.4.0-amd64-DVD-1.iso

VM

Create

Interactive

This example uses a fixed MAC address.

#! /bin/sh

export PATH=/bin:/sbin

NAME="debian12"
DVD="/opt/ISO/debian-12.4.0-amd64-DVD-1.iso"
DISK="/opt/VM/${NAME}.qcow2,size=20,format=qcow2"

virsh destroy ${NAME}
virsh undefine ${NAME}
rm -f ${DISK}

virt-install \
--name "${NAME}" \
--ram 1024 \
--disk path="${DISK}" \
--vcpus 1 \
--os-variant debian11 \
--network bridge=br0,mac=02:00:c0:a8:00:6e \
--graphics none \
--console pty,target_type=serial \
--location "${DVD}" \
--extra-args 'console=ttyS0'

Non interactive

Append the following to the interactive script above

--initrd-inject preseed.cfg --noautoconsole --wait

This preseed file uses a fixed IP address.

preeseed.cfg

List
virsh list --all
Start
virsh start debian12
Auto start
virsh autostart debian12
No auto start
virsh autostart --disable debian12
Information
virsh dominfo debian12
Console
virsh console debian12
Stop
virsh shutdown debian12

NB there are optional modes

Force stop
virsh destroy debian12
Delete
virsh undefine debian12
Export
virsh dumpxml debian12 > debian12.xml
Import
virsh define debian12.xml

Disk

Tools
apt install guestfs-tools
Info
qemu-img info vm1.qcow2
image: vm1.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 3.42 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false
Compact
virt-sparsify --in-place vm1.qcow2
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   6.4] Trimming /dev/sda1
[   6.8] Clearing Linux swap on /dev/sda5
[   7.1] Sparsify in-place operation completed with no errors
Resize
qemu-img resize vm1.qcow2 40G
qemu-img info vm1.qcow2
image: vm1.qcow2
file format: qcow2
virtual size: 40 GiB (42949672960 bytes)
disk size: 2.49 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false

VM

sudo apt-get install gparted

Run gparted to re-arrange and resize the partitions as appropriate

Resources

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies