Outils pour utilisateurs

Outils du site


tech:notes_libvirt

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
tech:notes_libvirt [2025/03/24 15:06] – créée - modification externe 127.0.0.1tech:notes_libvirt [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Brouillon}}
 +
 +# Notes libvirt
 +
 +Voir :
 +* [[todo-kvm|Notes KVM]]
 +* https://ubuntu.com/tutorials/multipass-and-libvirt-vms-on-ubuntu-managed-via-landscape#2-install-libvirt
 +
 +Voir aussi :
 +* https://kubevirt.io/
 +* https://developers.redhat.com/articles/2024/02/21/virtio-live-migration-technical-deep-dive
 +
 +
 +## Install
 +
 +~~~bash
 +apt-get install virt-manager virtinst qemu-utils
 +~~~
 +
 +Install Ubuntu
 +~~~bash
 +virt-install --connect qemu:///system \
 +--name ttest02 --ram 4096 --vcpus 1 
 +--vnc --virt-type kvm --machine pc \
 +--location http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/ \
 +--extra-args ksdevice=link,lang=,url=http://192.168.1.5/cblr/svc/op/ks/system/ttest02,text,auto-install/enable=true,priority=critical,ipv6.disable=1 \
 +--arch x86_64 --os-variant virtio26 --disk path=/var/lib/libvirt/images/ttest02-disk0,size=15,format=raw \
 +--network bridge=br0,mac='52:54:2e:f9:98:23' \
 +--wait -1 --noautoconsole
 +~~~
 +
 +Network install Debian Preseed
 +~~~bash
 +virt-install \
 +--name ttest02\
 +--ram 4096  \
 +--location=http://ftp.us.debian.org/debian/dists/stable/main/installer-amd64/ \
 +--initrd-inject=/tmp/preseed.cfg \
 +--extra-args="console=ttyS0,115200n8 serial locale=fr_FR console-setup/ask_detect=false keyboard-configuration/layoutcode=fr hostname=virtual domain=unassigned-domain interface=auto" \
 +--vcpu=1 \
 +--vnc \
 +--vnclisten=0.0.0.0 \
 +--os-type=linux \
 +--os-variant=debianwheezy \
 +-w bridge=virbr0 \
 +-w bridge=br1 \
 +-w network=default \
 +--console pty,target_type=serial \
 +--disk format=qcow2,size=8,bus=virtio,path=/var/lib/libvirt/images/vm1.qcow2
 +~~~
 +
 +Install ISO Debian Preseed
 +~~~bash
 +virt-install \
 +--name vm1 \
 +--ram 1024 \
 +--cdrom /tmp/debian10ps-kvm.iso \
 +--vcpu=1 \
 +--vnc \
 +--vnclisten=0.0.0.0 \
 +--os-type=linux \
 +--os-variant=debiantesting \
 +-w bridge=virbr0 \
 +-w network=default \
 +--console pty,target_type=serial \
 +--disk format=qcow2,size=20,bus=virtio,path=/var/lib/libvirt/images/vm1.qcow2 \
 +--boot uefi \
 +--wait=-1 --noautoconsole
 +~~~
 +
 +
 +
 +## Interface d'admin
 +
 +### WebUI - Cockpit
 +
 +Voir :
 +* [[notes_cockpit]]
 +
 +~~~bash
 +apt-get install cockpit cockpit-machines
 +~~~
 +
 +
 +## Console graphique VNC
 +
 +Connaitre le port
 +~~~bash
 +Connexion VNC
 +virsh dumpxml ttest02 | grep vnc
 +~~~
 +
 +
 +## Effacement
 +
 +~~~bash
 +virsh destroy ttest02
 +virsh undefine ttest02
 +virsh undefine ttest02 --nvram
 +~~~
 +
 +
 +Pb effacement 
 +~~~
 +virsh # undefine --remove-all-storage --delete-snapshots vmplop
 +error: Failed to undefine domain vmplop
 +error: Requested operation is not valid: cannot delete inactive domain with 1 snapshots
 +~~~
 +
 +Solution
 +~~~bash
 +snapshot-delete --metadata --current vmplop
 +~~~
 +
 +
 +
 +
 +## Réseau
 +
 +### DHCP avec adresses statiques
 +
 +Tout d'abord, recherchez les adresses MAC des machines virtuelles auxquelles vous souhaitez attribuer des adresses IP statiques:
 +
 +~~~bash
 +virsh  dumpxml  $VM_NAME | grep 'mac address'
 +~~~
 +
 +Puis éditez le réseau
 +~~~bash
 +virsh  net-list
 +virsh  net-edit  $NETWORK_NAME    # Probably "default"
 +~~~
 +
 +Trouvez le <dhcp> section, restreignez la plage dynamique et ajoutez des entrées d'hôte pour vos machines virtuelles
 +
 +~~~xml
 +<dhcp>
 +  <range start='192.168.122.100' end='192.168.122.254'/>
 +  <Host mac='52:54:00:6c:3c:01' name='vm1' ip='192.168.122.11'/>
 +  <Host mac='52:54:00:6c:3c:02' name='vm2' ip='192.168.122.12'/>
 +  <Host mac='52:54:00:6c:3c:03' name='vm3' ip='192.168.122.12'/>
 +</dhcp>
 +~~~
 +
 +
 +Recharger la conf réseau
 +~~~bash
 +virsh  net-destroy  $NETWORK_NAME  
 +virsh  net-start    $NETWORK_NAME  
 +~~~
 +
 +Source : https://www.it-swarm.dev/fr/dhcp/kvm-libvirt-comment-configurer-des-adresses-ip-dinvites-statiques-sur-lhote-de-virtualisation/960375558/
 +
 +
 +### kvm host to guest connections
 +
 +Source : https://wiki.libvirt.org/page/TroubleshootMacvtapHostFail
 +
 +`/tmp/isolated.xml`
 +~~~xml
 +   <network>
 +     <name>isolated</name>
 +       <ip address='192.168.254.1' netmask='255.255.255.0'>
 +       <dhcp>
 +         <range start='192.168.254.2' end='192.168.254.254' />
 +       </dhcp>
 +     </ip>
 +   </network>
 +~~~
 +
 +~~~bash
 +virsh net-define /tmp/isolated.xml
 +virsh net-start isolated
 +virsh net-autostart isolated
 +~~~
 +
 +~~~bash
 +virsh edit vm-plop
 +~~~
 +
 +~~~xml
 +   <interface type='network'>
 +     <source network='isolated'/>
 +     <model type='virtio'/> <-- This line is optional.
 +   </interface>
 +~~~
 +
 +
 +### Bridge direct
 +
 +~~~bash
 +virsh dumpxml vm1 | xpath -e '/domain/devices/interface[@type="direct"]'
 +~~~
 +
 +~~~xml
 +<interface type="direct">
 +      <mac address="52:54:00:c1:02:bb" />
 +      <source dev="eno4" mode="bridge" />
 +      <target dev="macvtap0" />
 +      <model type="virtio" />
 +      <alias name="net0" />
 +      <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0" />
 +</interface>
 +~~~
 +
 +
 +## Disque
 +
 +### redimensionnement à chaud
 +
 +~~~
 +# virsh dumpxml vm1 | xpath -q -e "/domain/devices/disk[@type='file']/alias"
 +<alias name="virtio-disk0" />
 +<alias name="sata0-0-0" />
 +~~~
 +
 +~~~bash
 +virsh qemu-monitor-command vm1  block_resize drive-virtio-disk0 50G --hmp
 +~~~
 +
 +
 +### Redimensionnement à froid
 +
 +~~~
 +# virsh dumpxml vm1 | xpath -q -e "/domain/devices/disk[@type='file']/source/@file[1]"
 + file="/var/lib/libvirt/images/vm1.qcow2"
 +~~~
 +
 +~~~bash
 +qemu-img resize /var/lib/libvirt/images/vm1.qcow2 +10G
 +~~~
 +
 +### Lister les disques et leurs tailles
 +
 +~~~bash
 +for VM in $(virsh list --all | awk '/^ [0-9-]/ {print $2}');  
 +do 
 + echo VM_NAME: "$VM";
 + virsh dumpxml $VM | xpath -q -e "/domain/devices/disk[@type='file']/source/@file[1]" | sed -e 's%^ file=%%g' | tr "\n" " " | xargs du -h
 +  echo --------------;
 +done
 +
 +~~~
 +
 +
 +## VM
 +
 +### Clone
 +
 +Cloner une VM
 +~~~bash
 +virt-clone --original vm1 --name vm2 --auto-clone
 +~~~
 +
 +### Virsh reload xml config file
 +
 +~~~bash
 +virsh define foo.xml
 +~~~
 +
 +### Connaître l'IP d'une VM
 +
 +Voir aussi : https://www.cyberciti.biz/faq/find-ip-address-of-linux-kvm-guest-virtual-machine/
 +
 +~~~
 +# virsh domiflist FRANCE_ACME_POC2
 +Interface  Type       Source     Model       MAC
 +-------------------------------------------------------
 +macvtap2   direct     enp2s0f0   virtio      52:54:00:c8:c0:b2
 +vnet2      network    ACME_FRANCE_V rtl8139     52:54:00:c8:b8:03
 +
 +# tcpdump -nn ether host 52:54:00:c8:c0:b2
 +tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
 +listening on enp2s0f0, link-type EN10MB (Ethernet), capture size 262144 bytes
 +21:08:36.739541 IP 192.168.98.2.80 > 192.168.219.18.50777: Flags [P.], seq 2750428795:2750428825, ack 2413241637, win 119, length 30: HTTP
 +21:08:36.778206 IP 192.168.98.2.80 > 192.168.219.18.50772: Flags [P.], seq 3075508646:3075508995, ack 4236506059, win 119, length 349: HTTP
 +21:08:36.779166 IP 192.168.219.18.50772 > 192.168.98.2.80: Flags [.], ack 349, win 251, length 0
 +21:08:36.893545 IP 192.168.98.2.80 > 192.168.219.18.50772: Flags [P.], seq 349:373, ack 1, win 119, length 24: HTTP
 +21:08:36.939877 IP 192.168.219.18.50777 > 192.168.98.2.80: Flags [.], ack 30, win 256, length 0
 +~~~
 +
 +~~~bash
 +arp -e
 +~~~
 +
 +Voir aussi :
 +* `virsh domifaddr`
 +* `virsh net-dhcp-leases`
 +
 +
 +## Pb
 +
 +~~~
 +ERROR    Guest name 'ttest02' is already in use.
 +~~~
 +
 +Solution : Effacer !
 +~~~bash
 +virsh undefine ttest02
 +~~~
 +
 +## IOMMU PCI_Passthrough
 +
 +Voir :
 +* https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF
 +* http://www.virtualopensystems.com/fr/services/vfio-full-gpu-virtualization/
 +* https://clayfreeman.github.io/gpu-passthrough/#preparing-the-virtual-machine
 +
 +Pour NVidia il faut cacher le PCI_Passthrough
 +~~~xml
 + <kvm>
 + <hidden state='on'/>
 + </kvm>
 +~~~
 +
 +`/etc/default/grub`
 +~~~ini
 +#GRUB_CMDLINE_LINUX_DEFAULT="quiet"
 +GRUB_CMDLINE_LINUX="quiet intel_iommu=on iommu=pt pci-stub.ids=10de:1b81,10de:10f1"
 +~~~
 +
 +
 +## Err /usr/bin/qemu-system-x86_64: No such file or directory
 +
 +~~~
 +Error starting domain: Cannot check QEMU binary /usr/bin/qemu-system-x86_64: No such file or directory
 +~~~
 +
 +Solution
 +~~~bash
 +apt-get update
 +apt-get purge qemu-system-x86
 +apt-get install qemu-kvm
 +apt-get install -f
 +~~~
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki