VM with libvirt

    Requirements:

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

    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/Import 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
    

    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: do uninstall first

    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.

    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