VM with libvirt

    Requirements:

    apt-get install qemu qemu-kvm libvirt-clients libvirt-daemon-system virtinst bridge-utils
    

    Note: on debian12, qemu becomes qemu-sytem

    And vmx compatibility: grep -E --color 'vmx|svm' /proc/cpuinfo

    Build an image

    with Virt-builder

    Debian package: libguestfs-tools

    virt-builder debian-11 -o test.img --size 100G --root-password password:XXX --hostname test.parmentier.io --install "net-tools" --append-line '/etc/resolv.conf:nameserver 213.186.33.99'
    

    Notes:

    • you can use --firstboot-command 'apt-get install -y ...'
    • --append-line '/etc/hosts:192.168.225.1 server.ostechnix.local'

    source

    Install an image

    Debian package: virtinst

    virt-install \
    --name test.parmentier.io \
    --ram 4096 \
    --disk path=/var/lib/libvirt/images/test.img \
    --vcpus 2 \
    --os-variant debian10 \
    --network bridge=br0,mac=xxx \
    --graphics none \
    --noautoconsole \
    --boot hd \
    --import 
    

    Notes:

    • there is no --os-variant debian11 yet, but debian10 should be fine?
    • if you use a bridge network, ensure it is configured on host, and you might have to specify a virutal mac address (its the case for ovh vm within dedicated).
    • If you are trying to install an iso from scratch, you will certainly need --cdrom=/path/to/file.iso and --graphics vnc see example

    source

    Start vm

    virsh --connect qemu:///system start test.parmentier.io
    

    Auto start

    virsh autostart test.parmentier.io
    

    Get into vm

    virsh console test.parmentier.io
    

    Edit configuration

    virsh edit test.parmentier.io
    

    Note: in some configuration (e.g. cpu) you will have to shutdown/start. See also examples

    Uninstall

    virsh shutdown test.parmentier.io
    virsh undefine test.parmentier.io
    

    Reboot safely

    virsh reboot test.parmentier.io
    

    Hard reboot

    virsh reset test.parmentier.io
    

    Other useful commands

    • suspend: froze on memoryy
    • shutdown
    • destroy: try shutdown first
    • dominfo: get info such as cpu, memory

    Network (host)

    On debian 11, ensure apt-get install -y bridge-utils:

    iface eno3 inet manual
    
    auto br0
    iface br0 inet static
        address xx/24
        gateway yyy
        bridge-ports eno3
        bridge-stp off
        bridge-fd 0
        hwaddress 0a:xx:00
    

    Note:

    • on debian 11, without mac address, it did not work. You can check available bridges with brctl show
    • on debian 12 (ovh template) I was not able to make it works. I played with source 1 and source 2 unsucessfully. It was like if interfaces was not taken in consideration, as well as cloud.cfg in networksection. I will try it when I have dedicated time.

    Also, ensure you have ip_forward=1, you cna check value with sysctl net.ipv4.ip_forward, and edit with sysctl net.ipv4.ip_forward=1. To make it persistant: edit /etc/sysctl.conf, find the line ip_forward=, then run sysctl -p /etc/sysctl.conf.

    Network (vm)

    auto enp1s0
    iface enp1s0 inet static
            address xx
            netmask 255.255.255.255
            broadcast xx
            post-up ip route add yy/32 dev enp1s0
            post-up ip route add default via yy
            pre-down ip route del yy/32 dev enp1s0
            pre-down ip route del default via yy
    
    

    Note: replace xx and yy

    Filesystems

    virt-filesystems -a test.img --all --long -h
    

    Export/Import VM

    Export

    # virsh configuration file
    virsh dumpxml test.parmentier.io >test.parmentier.io.xml
    
    # list of image(s) 
    virsh domblklist test.parmentier.io
    

    Transfert on other machine

    1. reduce sparse space with tar cvf parmentier.img >parmentier.img.tar
    2. use --inplace with rsync

    source

    Note: on case you did not realized this step, you can use virt-sparsify (you can use --in-place if it is not used)

    Import

    virsh define test.parmentier.io.xml
    

    Note: you will certainly need to update network (post-up ip route add rule) in order to use the host bridge ip.

    Web UI

    cockpit

    Expanding or Shrinking an image

    Note: I only try to shrink an image, and it did not work.

    See current usage:

    virt-df -h -a /var/lib/libvirt/images/test.img
    

    Resize it:

    virt-resize --resize /dev/sda1=-50G test.img new-test.img
    

    Note: backup the image is highly recommended before any action

    source 1

    source 2

    source 3