Projet

Général

Profil

Libvirt (ancien) » Historique » Version 20

Version 19 (Laurent GUERBY, 29/04/2013 13:34) → Version 20/61 (Joris Michaux, 06/05/2013 17:19)

h1. Libvirt

h2. Resources/Liens

* Virtualisation du routage IPv4 pour les VM http://lists.tetaneutral.net/pipermail/technique/2013-April/000810.html

* libvirt

http://builder.virt-tools.org/artifacts/libvirt-virshcmdref/html/index.html
http://docs.fedoraproject.org/en-US/Fedora/13/html/Virtualization_Guide/chap-Virtualization-Managing_guests_with_virsh.html

* virt-install et preseed sans server web et pxe:

http://honk.sigxcpu.org/con/Preseeding_Debian_virtual_machines_with_virt_install.html

h2. Installation VM sans bridge avec routage static (en /32 pour l'ipv4)

Note: tous les fichiers utilisés et créés ne sont peut être pas à jour, voir ici pour les dernières version: http://git.tetaneutral.net/libvirt.git/

Le répertoire /etc/libvirt/ttnn cité plustard est géré dans ce git.

h3. Configuration de la libvirt

On créé un réseau libvirt qui fait rien (déjà fait sur n7):

<pre>
cat > libvirt_dump_network.xml <<EOF
<network ipv6='yes'>
<name>dump-net</name>
<bridge name='dumpbr0' />
</network>
EOF

virsh net-define libvirt_dump_network.xml
virsh net-autostart dump-net
virsh net-start dump-net
</pre>

On créé un hook libvirt (déjà fait sur n7):
<pre>
mkdir -p /etc/libvirt/ttnn/hooks
ln -s /etc/libvirt/ttnn/hooks /etc/libvirt/hooks
vi /etc/libvirt/hooks/qemu
----8<--------------->8----
#!/bin/bash

name="$1"
operation="$2"
suboperation="$3"

[ "$operation" != "start" ] && exit 0
[ "$suboperation" != "begin" ] && exit 0

# get interface from the libvirt xml
interface=$(cat - | sed -ne "/vnet/s/.*dev='\([^']*\)'.*/\1/gp")

[ -f /etc/libvirt/ttnn/${name}.conf ] && source /etc/libvirt/ttnn/${name}.conf

[ "$ip" -o "$ip6" ] && brctl delif dumpbr0 $interface
[ "$ip" ] && ip route add $ip/32 dev $interface
[ "$ip6" ] && ip route add $ip6/56 dev $interface
[ "$ip6" ] && ip -6 addr add fe80::31/64 dev $interface
exit 0
----8<--------------->8----
</pre>

h3. Preparation de l'installer

Pour une squeeze on met à jour virtinst (déjà fait sur n7):

<pre>
wget http://ftp.us.debian.org/debian/pool/main/v/virtinst/virtinst_0.600.3-3_all.deb
dpkg -i virtinst_0.600.3-3_all.deb
</pre>

On install le preseed.cfg attaché à la page dans /etc/libvirt/preseed.cfg (déjà fait sur n7)

h4. Installation d'une VM

Creation du disk (déjà fait sur n7):
<pre>
lvcreate -n picttn -L 50G /dev/raid5
</pre>

Configuration du routage static:

<pre>
vi /etc/libvirt/ttnn/picttn.conf
# le /32 ipv4
ip="91.224.148.252"
# le /56 ipv6
ip6="2a01:6600:8080:FC00::"
</pre>

Lancement de l'installation via le réseau natter de libvirt
<pre>
virt-install -n picttn -r 2048 --os-type=linux \
--autostart \
--disk /dev/raid5/picttn,bus=virtio,cache=none,io=native \
--network network=default,model=virtio \
--nographics \
--noreboot \
--wait -1 \
--location http://ftp.debian.org/debian/dists/wheezy/main/installer-amd64/ \
--serial pty \
--initrd-inject /etc/libvirt/ttnn/preseed.cfg \
--initrd-inject /etc/libvirt/ttnn/picttn.conf \
--initrd-inject /etc/libvirt/ttnn/postinstall.sh \
--extra-args "console=ttyS0,9600n8 DEBIAN_FRONTEND=text auto priority=critical hostname=picttn domain=le-pic.org"
</pre>

Une fois l'installation terminée, on change la conf réseau:

<pre>
EDITOR="sed -ie s/default/dump-net/g" virsh edit picttn
</pre>

On peux démarrer la VM
<pre>
virsh start picttn
virsh console picttn
</pre>

Ensuite on peux se connecte à la VM
<pre>
ssh root@2a01:6600:8080:FC00::1
</pre>

Le /etc/network/interfaces IPv4 après installation

<pre>
iface eth0 inet static
address 91.224.148.xxx
netmask 32
up ip route add 91.224.148.0/32 dev eth0
up ip route add default via 91.224.148.0
</pre>

h2. Misc

<H4Mm3r> si tu lance la création de la VM sans avoir créé le disk --disk /vmroot/testnat.qcow2,format=qcow2,size=12,bus=virtio,cache=writeback
<H4Mm3r> le disk se créé mais dans le XML le format est à RAW
<H4Mm3r> si tu le créé à l'avance avec qemu-img create -f qcow2
<H4Mm3r> et que tu lance ta commande --disk /vmroot/testnat.qcow2,format=qcow2
<guerby> ok donc c'etait bien ça :)
<guerby> bbl (miam)
<H4Mm3r> dans le fichier XML le format est à RAW
<H4Mm3r> il faut en effet dumper un XML, modifier la valeur à QCOW2
<H4Mm3r> et créer la VM depuis ce XML
<H4Mm3r> virsh create file.xml
<H4Mm3r> pourqu'enfin il comprenne que c'est du QCOW...
<H4Mm3r> et là mon disk fait bien 265ko, mais est détecté comme 12G :D

h2. HOWTO by H4Mm3r (under contruction)

h3. Usefull links

#### Misc

#OS Type
http://rhevdup.blogspot.fr/2012/05/list-available-os-types-for-kvm-on-rhel.html

#Libvirt TTN
http://chiliproject.tetaneutral.net/projects/tetaneutral/wiki/Libvirt

#### KVM management

#Install QEMU KVM + LIBVIRT
http://www.howtoforge.com/virtualization-with-kvm-on-a-debian-squeeze-server

#Edit XML file
http://vladnevzorov.com/2011/02/11/how-to-change-configuration-of-kvm-virtual-machine/

#Clone a KVM
https://help.ubuntu.com/community/KVM/CreateGuests#Cloning_a_virtual_machine
http://linux.die.net/man/1/virt-clone

#### Network

#NAT
http://www.linuxquestions.org/questions/linux-networking-3/kvm-qemu-and-nat-on-the-host-machine-mini-tutorial-697980/
http://wiki.libvirt.org/page/Networking
http://libvirt.org/formatnetwork.html#examplesNAT
https://help.ubuntu.com/community/KVM/Networking

#Bridge
http://toast.djw.org.uk/qemu.html
http://www.mediaonfire.com/en/tech_tips/QEMUNetworking.html (bridge, nat, arp)

#### Improve performance
http://blog.bodhizazen.net/linux/improve-kvm-performance/
http://www.linux-kvm.org/page/Tuning_KVM
http://www.ezunix.org/index.php?title=Boost_performance_of_KVM_guests

#### Storage
http://doc.opensuse.org/products/draft/SLES/SLES-kvm_sd_draft/cha.libvirt.storage.html

h3. HOWTO

<pre>
[H] = Host
[G] = Guest
[XML] = XML content
"command"
</pre>

#### Diskfile Storage

#Create qcow2 disk
<pre>
[H] "qemu-img create -f qcow2 /path/to/disk.qcow2 $SIZE" (ex: 20G)
[H] "chmod +x /path/to/disk.qcow2"
</pre>

#Convert raw to qcow2
<pre>
[H] "qemu-img convert -O qcow2 disk.img disk.qcow2"
</pre>

#Disable cache to improve performance
<pre>
[XML] <driver name='qemu' type='qcow2' cache='none'/>
</pre>

#### LVM Storage

<pre>
[H] Create "/etc/libvirt/storage/vmdata.xml" file :
<pool type="logical">
<name>my_pool</name>
<source>
<device path="/path/to/dev"/>
</source>
<target>
<path>/target/path/path> (LV will be created there)
</target>
</pool>
[H] "virsh pool-define /etc/libvirt/storage/my_pool.xml"
[H] "virsh pool-start my_pool"
[H] "virsh pool-autostart my_pool"
[H] "virsh pool-info my_pool"
[H] "virsh vol-create-as my_pool my_logical_volume 12G"
[H] You must create a LV to be able to give it to a KVM "virsh vol-create-as my_pool my_vol 12G --format=lvm2"
</pre>

h3. Create KVM

#Bridged with TAP + LVM
<pre>
[H] "virt-install -d --hvm --vnc --name=my_kvm --vcpus=4 --cpuset=0-4 --ram 2048 --disk vol=my_pool/my_vol,bus=virtio,cache=none --network bridge=my_bridge,model=virtio -c /path/to/distrib.iso --keymap=fr --os-type=linux --noautoconsole"
</pre>

#TAP (if you want to rename the default "vnetX" interface by "tapX"
<pre>
[H] "virsh dumpxml my_guest > my_guest.xml
[H] "virsh undefine my_guest"
[H] Edit the my_guest.xml file to rename VNETX by TAPX
[H] "virsh define my_guest.xml"
[H] "virsh start my_guest"
</pre>

#NATed + LVM
<pre>
[H] "virt-install -d --hvm --vnc --name=my_kvm --vcpus=4 --cpuset=0-4 --ram 2048 --disk vol=my_pool/my_vol,bus=virtio,cache=none --network network=my_network,model=virtio -c /path/to/distrib.iso --keymap=fr --os-type=linux --noautoconsole"
</pre>

#Clone VM
<pre>
[H] Create the LVM dest
[H] Pause the KVM "virsh suspend my_kvm"
[H] virt-clone -o my_orig_kvm -n my_cloned_kvm -f=/path/to/lvm/dev
[G] Go and change the hostname and resolv.conf
</pre>

h3. Command explanation

#Bridged with tap
<pre>
virt-install -d --hvm --vnc --name=testlvm \
--vcpus=4 --cpuset=0-4 \
--ram 2048 \
--disk vol=vmdata/testlvm,bus=virtio,cache=none \
--network bridge=br0,model=virtio \
-c /data/isos/debian-testing-amd64-netinst.iso \
--keymap=fr --os-type=linux --os-variant=debiansqueeze --noautoconsole
</pre>

#NATed
<pre>
virt-install -d --hvm --vnc --name=testlvm \
--vcpus=4 --cpuset=0-4 \
--ram 2048 \
--disk vol=vmdata/testlvm,bus=virtio,cache=none \
--network network=frp_network,model=virtio \
-c /data/isos/debian-testing-amd64-netinst.iso \
--keymap=fr --os-type=linux --os-variant=debiansqueeze --noautoconsole
</pre>

[OLD QCOW2]
<pre>
[H] "virt-install --connect qemu:///system -n puppetnode1 -r 1024 --vcpus=4 --cpuset=4-7 --disk path=/vmroot/puppetnode1.qcow2 -c /data/isos/debian-6.0.6-amd64-netinst.iso --vnc --keymap=fr --noautoconsole --os-type linux --os-variant debiansqueeze --accelerate --network network=default --hvm"
[H] "virt-install --connect qemu:///system -n testnat -r 1024 --vcpus=2 --cpuset=2-3 --disk /vmroot/testnat.qcow2,format=qcow2,size=12,bus=virtio,cache=writeback -c /data/isos/debian-6.0.6-amd64-netinst.iso --vnc --keymap=fr --noautoconsole --os-type linux --os-variant debiansqueeze --accelerate --network bridge=br0 --hvm"
</pre>

#Start a KVM
<pre>
[H] "virsh start my_kvm"
</pre>

#Stop node
<pre>
[H] Soft : "virsh shutdown my_kvm"
[H] Hard : "virsh destroy my_kvm"
</pre>

#Suppr node del
<pre>
[H] "virsh undefine node" (+ remove files)
</pre>

#Rename node
<pre>
[H] "virsh dumpxml my_kvm > foo.xml"
[H] <edit foo.xml, change the name, move storage>
[H] "virsh undefine my_kvm"
[H] "virsh define foo.xml"
</pre>

# Connect to virsh qemu
<pre>
[H] "virsh --connect qemu:///system"
[H] "virsh # help"
</pre>

#Start a network
<pre>
[H] "virsh net-start my_network"
</pre>

#Configure VNC

<pre>
[H] Edit /etc/libvirt/qemu.conf and set vnc_listen = "your.ip.addd"
[H] You need to restart service "/etc/init.d/libvirtd restart"
[H] "ps aux | grep -i kvm" (find vnc display for your KVM)
</pre>

#Connect to a KVM through VNC

<pre>
tightvncviewer -compresslevel 7 -encodings "copyrect tight hextile zlib corre rre raw" x.xxx.xxx.xxx:x
</pre>

#Keyboard FR for KVM
<pre>
[H] "virsh edit my_kvm"
[XML] <graphics type='vnc' port='-1' autoport='yes' keymap='fr'/>
</pre>

#Create your own NAT network
<pre>
[H] Create your_nat.xml file :
[XML] <network>
<name>frp_network</name>
<forward mode='nat'/>
<bridge name='br1' stp='on' delay='0' />
<ip address='10.16.42.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.16.42.2' end='10.16.42.50' />
</dhcp>
</ip>
</network>
[H] "virsh net-define your_nat.xml"
[H] "virsh net-start your_nat.xml"
[H] "virsh net-autostart your_nat.xml"
[XML] Network type "interface type = network, source network=your_nat"
</pre>

#IBM advisements to improve performance
<pre>
[H] Use LVM (or other direct access to HDD) for VMs storage (from 45 to 3' to install a VM)
[H] Set the swappiness value to 0 ("echo 0 > /proc/sys/vm/swappiness" && edit "/etc/sysctl.conf" and add "vm.swappiness=0")
[H] Disable zone_reclaim ("echo 0 > /proc/sys/vm/zone_reclaim_mode" && edit "/etc/sysctl.conf" and add "vm.zone_reclaim_mode=0")
[H] Enable KSM ("echo 1 > /sys/kernel/mm/ksm/run") and set the check time to 200ms ("echo 200 > /sys/kernel/mm/ksm/sleep_millisecs")
</pre>