CCP1 Day6
Page content
LAB
Part 1
Verify environment
ubuntu@source:~$ lsmod | grep kvm
kvm_intel 180224 0
kvm 561152 1 kvm_intel
irqbypass 16384 1 kvm
ubuntu@source:~$ sudo virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
ubuntu@source:~$ sudo virsh net-dumpxml default
<network>
<name>default</name>
<uuid>652aa9c9-84a8-4765-be6b-47437a87d0d6</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:05:3b:c8'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
my_vm.xml
ubuntu@source:~$ more my_vm.xml
<domain type='kvm'>
<name>my_vm</name>
<memory>512000</memory>
<vcpu placement='static' current='1'>4</vcpu>
<os>
<type>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='yes' timeout='10000'/>
</os>
<features>
<acpi/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2"/>
<source file="/home/ubuntu/my_vm.img"/>
<target dev="vda" bus="virtio"/>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"/>
<source file="/home/ubuntu/ubuntu-16.04.3-server-amd64.iso"/>
<target dev="hdc" bus="ide"/>
<readonly/>
</disk>
<interface type='bridge'>
<source bridge='virbr0'/>
</interface>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport="yes" listen='0.0.0.0' passwd='1234'/>
<console type='pty'>
<target port='0'/>
</console>
</devices>
</domain>
Diff between my_vm.xml and my_vm_precreated.xml
ubuntu@source:~$ diff my_vm.xml my_vm_precreated.xml
7d6
< <boot dev='cdrom'/>
24,29d22
< </disk>
< <disk type="file" device="cdrom">
< <driver name="qemu" type="raw"/>
< <source file="/home/ubuntu/ubuntu-16.04.3-server-amd64.iso"/>
< <target dev="hdc" bus="ide"/>
< <readonly/>
ubuntu@source:~$ sudo virsh domxml-to-native qemu-argv my_vm.xml
LC_ALL=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin QEMU_AUDIO_DRV=none /usr/bin/kvm -name my_vm -machine pc-i440fx-xenial,accel=kvm,usb=off -m 500 -realtime mlock=off -smp 1,maxcpus=4,sockets=4,cores=1,threads=1 -uuid e13c8054-fa68-441f-89d0-6e4b3cdc3566 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-my_vm/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot order=dc,menu=on,splash-time=10000,strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/home/ubuntu/my_vm.img,format=qcow2,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0 -drive file=/home/ubuntu/ubuntu-16.04.3-server-amd64.iso,format=raw,if=none,id=drive-ide0-1-0,readonly=on -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:e1:10:ad,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 0.0.0.0:0,password -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5 -msg timestamp=on
create empty disk
ubuntu@source:~$ sudo qemu-img create -f qcow2 my_vm_temp.img 5G
Formatting 'my_vm_temp.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
create VM
ubuntu@source:~$ sudo virsh create my_vm_precreated.xml
Domain my_vm created from my_vm_precreated.xml
ubuntu@source:~$ sudo virsh list
Id Name State
----------------------------------------------------
1 my_vm running
ubuntu@source:~$ sudo virsh domblklist my_vm
Target Source
------------------------------------------------
vda /home/ubuntu/my_vm.img
ubuntu@source:~$ sudo virsh dumpxml my_vm
<domain type='kvm' id='1'>
<name>my_vm</name>
<uuid>e8a3dce6-467a-40e0-86d4-af3bb2fdddf9</uuid>
<memory unit='KiB'>512000</memory>
<currentMemory unit='KiB'>512000</currentMemory>
<vcpu placement='static' current='1'>4</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-xenial'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes' timeout='10000'/>
</os>
<features>
<acpi/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/home/ubuntu/my_vm.img'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>
<controller type='usb' index='0'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:40:6e:3b'/>
<source bridge='virbr0'/>
<target dev='vnet0'/>
<model type='rtl8139'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/2'>
<source path='/dev/pts/2'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1'/>
<alias name='video0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<alias name='balloon0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
<seclabel type='dynamic' model='apparmor' relabel='yes'>
<label>libvirt-e8a3dce6-467a-40e0-86d4-af3bb2fdddf9</label>
<imagelabel>libvirt-e8a3dce6-467a-40e0-86d4-af3bb2fdddf9</imagelabel>
</seclabel>
</domain>
ubuntu@source:~$ sudo virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
ubuntu@source:~$ sudo virsh domiflist my_vm
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge virbr0 rtl8139 52:54:00:40:6e:3b
ubuntu@source:~$ sudo arp -e
Address HWtype HWaddress Flags Mask Iface
host-10-11-0-1.openstac ether fa:16:3e:c2:b8:d2 C ens3
host-10-11-0-118.openst ether fa:16:3e:0d:d2:a9 C ens3
10.11.0.100 ether fa:16:3e:8f:83:85 C ens3
192.168.122.2 ether 52:54:00:40:6e:3b C virbr0
ssh into vm
ubuntu@source:~$ ssh user@192.168.122.2
user@192.168.122.2's password:
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-87-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
255 packages can be updated.
171 updates are security updates.
Last login: Thu Oct 12 12:42:48 2017
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
Command List
$sudo virsh list
$sudo virsh reboot my_vm
$sudo virsh suspend my_vm
$sudo virsh resume my_vm
$sudo virsh snapshot-create-as --domain my_vm --name my_vm_s0 --description "Test snapshot" #create snapshot
$sudo virsh snapshot-revert --domain my_vm --snapshotname my_vm_s0 #back to previous snapshot
#### Basic Monitoring
```bash
ubuntu@source:~$ virsh help monitor
Domain Monitoring (help keyword 'monitor'):
domblkerror Show errors on block devices
domblkinfo domain block device size information
domblklist list all domain blocks
domblkstat get device block stats for a domain
domcontrol domain control interface state
domif-getlink get link state of a virtual interface
domifaddr Get network interfaces' addresses for a running domain
domiflist list all domain virtual interfaces
domifstat get network interface stats for a domain
dominfo domain information
dommemstat get memory statistics for a domain
domstate domain state
domstats get statistics about one or multiple domains
domtime domain time
list list domains
ubuntu@source:~$ sudo virsh dominfo my_vm
Id: 1
Name: my_vm
UUID: e8a3dce6-467a-40e0-86d4-af3bb2fdddf9
OS Type: hvm
State: running
CPU(s): 1
CPU time: 36.9s
Max memory: 512000 KiB
Used memory: 512000 KiB
Persistent: no
Autostart: disable
Managed save: no
Security model: apparmor
Security DOI: 0
Security label: libvirt-e8a3dce6-467a-40e0-86d4-af3bb2fdddf9 (enforcing)
ubuntu@source:~$ virsh domblklist my_vm
Target Source
------------------------------------------------
vda /home/ubuntu/my_vm.img
ubuntu@source:~$ sudo virsh domblkinfo my_vm vda
Capacity: 10737418240
Allocation: 2600468480
Physical: 2600411136
$ifconfig
...
vnet0 Link encap:Ethernet HWaddr fe:54:00:40:6e:3b
inet6 addr: fe80::fc54:ff:fe40:6e3b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2205 errors:0 dropped:0 overruns:0 frame:0
TX packets:11278 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:190075 (190.0 KB) TX bytes:13585096 (13.5 MB)
ubuntu@source:~$ sudo virsh domifaddr my_vm vnet0
Name MAC address Protocol Address
-------------------------------------------------------------------------------
ubuntu@source:~$ virsh domifstat my_vm vnet0
vnet0 rx_bytes 13585720
vnet0 rx_packets 11290
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 190075
vnet0 tx_packets 2205
vnet0 tx_errs 0
vnet0 tx_drop 0
ubuntu@source:~$ virsh net-dumpxml default
<network>
<name>default</name>
<uuid>652aa9c9-84a8-4765-be6b-47437a87d0d6</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:05:3b:c8'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
Question: If I destroy the VM process, is my data also destroyed? Do I have to reinstall the VM again?
sudo virsh destroy my_vm
Data is persistant even if VM is destroyed. You have to create the vm again.
virsh create my_vm_precreated.xml
Part 2
Scaling
add CPU
ubuntu@source:~$ virsh setvcpus my_vm 2
ubuntu@source:~$ virsh dominfo my_vm
Id: 4
Name: my_vm
UUID: 967e0f9b-a161-46c8-8e97-940c62b865d7
OS Type: hvm
State: running
CPU(s): 2
CPU time: 16.8s
Max memory: 512000 KiB
Used memory: 512000 KiB
Persistent: no
Autostart: disable
Managed save: no
Security model: apparmor
Security DOI: 0
Security label: libvirt-967e0f9b-a161-46c8-8e97-940c62b865d7 (enforcing)
reduce/add memory
virsh setmem my_vm 256000
ubuntu@source:~$ virsh setmaxmem my_vm 256000
error: Unable to change MaxMemorySize
error: Requested operation is not valid: cannot resize the maximum memory on an active domain
ubuntu@source:~$ virsh dominfo my_vm
...
Max memory: 512000 KiB
Used memory: 256000 KiB
...
attach new drive (created in Part 1)
sudo virsh attach-disk my_vm --source /home/ubuntu/my_vm_temp.img --target vdb