使用libvirt管理KVM(一)

一. 安裝和配置libvirt,源碼下載http://www.qemu-project.org/download/#source.


二. 從包和源碼包進行安裝libvirt。

1. 在ubuntu系統上,安裝包以下

xiodi@c720132:~$ sudo apt-get updateshell

xiodi@c720132:~$ sudo apt install libvirt-binubuntu


2. 檢查libvirt守護進程是否處於運行狀態。

xiodi@c720132:~$ sudo pgrep -lfa libvirtd
10131 /usr/sbin/libvirtdvim


3. 檢查這個默認配置

xiodi@c720132:~$ cat /etc/libvirt/libvirtd.conf | grep -vi "#"  | sed '/^$/d'
unix_sock_group = "libvirtd"
unix_sock_ro_perms = "0777"
unix_sock_rw_perms = "0770"
auth_unix_ro = "none"
auth_unix_rw = "none"api


4. 經過編輯qemu配置文件,關閉QEMU的安全驅動。

xiodi@c720132:~$ sudo vim /etc/libvirt/qemu.conf 安全

image


5. 重啓libvirt守護進程

xiodi@c720132:~$ /etc/init.d/libvirt-bin restart
[....] Restarting libvirt-bin (via systemctl): libvirt-bin.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'libvirt-bin.service'.
Authenticating as: xiodi,,, (xiodi)
Password:
==== AUTHENTICATION COMPLETE ===
. ok 服務器


6. 在libvirt目錄中檢查全部配置文件 。

xiodi@c720132:~$ ls -al /etc/libvirt/
total 92
drwxr-xr-x  5 root root  4096 Jun 29 15:21 .
drwxr-xr-x 96 root root  4096 Jun 29 15:16 ..
drwxr-xr-x  2 root root  4096 May 24 03:02 hooks
-rw-r--r--  1 root root   450 May 24 03:03 libvirt-admin.conf
-rw-r--r--  1 root root   547 May 24 03:03 libvirt.conf
-rw-r--r--  1 root root 15276 May 24 03:03 libvirtd.conf
-rw-r--r--  1 root root   834 May 24 03:03 libxl.conf
-rw-r--r--  1 root root  2169 May 24 03:03 libxl-lockd.conf
-rw-r--r--  1 root root  1235 May 24 03:03 lxc.conf
drwxr-xr-x  2 root root  4096 Jun 29 15:16 nwfilter
drwxr-xr-x  3 root root  4096 Jun 29 15:16 qemu
-rw-------  1 root root 19536 May 24 03:03 qemu.conf
-rw-r--r--  1 root root  2169 May 24 03:03 qemu-lockd.conf
-rw-r--r--  1 root root  2134 May 24 03:03 virtlockd.conf
-rw-r--r--  1 root root  1802 May 24 03:03 virtlogd.conf
-rw-r--r--  1 root root  1217 May 24 03:03 virt-login-shell.conf網絡


7. 介紹幾個重要的配置文件

  • libvirt.conf: 針對virsh命令主要是關於客戶端的配置文件 。 通常默認的就知足要求了。
  • libvirtd.conf :主要是針對服務器端的配置文件 ,它提供了不一樣的安全選項,請求限制和日誌選項。
  • qemu.conf: 主要是針對QEMU 設備 的主要配置文件 ,好比VNC服務器地址的配置選項等 。
  • 在建立完QEMU/KVM虛擬機後,/etc/libvirtd/qemu目錄將會包含虛擬機的配置定義。
  • /etc/libvirt/qemu/networks/包含了網絡相關的配置文件 。


三. 定義KVM實例

1. 定義本虛擬化主機上的全部虛擬機。

[root@kvm-1 ~]# virsh list --all
  Id    Name                           State
----------------------------------------------------
  1     c720220                        running
  2     c720203                        running
  4     c720101                        running
  10    c720104                        runningapp


2. 定義下面的XML文件。

[root@kvm-1 qemu]# cat c720135.xml dom

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
   virsh edit c720135
or other application using the libvirt API.
-->socket

<domain type='kvm'>
   <name>c720135</name>
   <uuid>56468610-cb3e-457a-b493-a6845bd9a09e</uuid>
   <memory unit='KiB'>4194304</memory>
   <currentMemory unit='KiB'>4194304</currentMemory>
   <vcpu placement='static'>8</vcpu>
   <os>
     <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
     <boot dev='hd'/>
   </os>
   <features>
     <acpi/>
     <apic/>
   </features>
   <cpu mode='custom' match='exact' check='partial'>
     <model fallback='allow'>Westmere</model>
   </cpu>
   <clock offset='utc'>
     <timer name='rtc' tickpolicy='catchup'/>
     <timer name='pit' tickpolicy='delay'/>
     <timer name='hpet' present='no'/>
   </clock>
   <on_poweroff>destroy</on_poweroff>
   <on_reboot>restart</on_reboot>
   <on_crash>destroy</on_crash>
   <pm>
     <suspend-to-mem enabled='no'/>
     <suspend-to-disk enabled='no'/>
   </pm>
   <devices>
     <emulator>/usr/libexec/qemu-kvm</emulator>
     <disk type='file' device='disk'>
       <driver name='qemu' type='qcow2'/>
       <source file='/kvm/sys-disk/c720135.qcow2'/>
       <target dev='vda' bus='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
     </disk>
     <disk type='file' device='cdrom'>
       <driver name='qemu' type='raw'/>
       <target dev='hda' bus='ide'/>
       <readonly/>
       <address type='drive' controller='0' bus='0' target='0' unit='0'/>
     </disk>
     <controller type='usb' index='0' model='ich9-ehci1'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci1'>
       <master startport='0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci2'>
       <master startport='2'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci3'>
       <master startport='4'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
     </controller>
     <controller type='pci' index='0' model='pci-root'/>
     <controller type='ide' index='0'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
     </controller>
     <controller type='virtio-serial' index='0'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
     </controller>
     <interface type='bridge'>
       <mac address='52:54:00:48:7d:06'/>
       <source bridge='br0'/>
       <model type='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     </interface>
     <interface type='bridge'>
       <mac address='52:54:00:67:d2:50'/>
       <source bridge='br1'/>
       <model type='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     </interface>
     <serial type='pty'>
       <target port='0'/>
     </serial>
     <console type='pty'>
       <target type='serial' port='0'/>
     </console>
     <channel type='unix'>
       <target type='virtio' name='org.qemu.guest_agent.0'/>
       <address type='virtio-serial' controller='0' bus='0' port='1'/>
     </channel>
     <input type='tablet' bus='usb'>
       <address type='usb' bus='0' port='1'/>
     </input>
     <input type='mouse' bus='ps2'/>
     <input type='keyboard' bus='ps2'/>
     <memballoon model='virtio'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
     </memballoon>
   </devices>
</domain>


3. 定義虛擬機

[root@kvm-1 qemu]# virsh define kvm1.xml

經過寫XML文件去配置虛擬機,是至關繁瑣和容易出錯的。一個簡單 的建立虛擬機的方式是從已經存在的鏡像或者安裝介質建立。 是使用virt-install工具。


(1)安裝virtinst包

xiodi@c720132:~$ sudo apt install virtinst


(2)使用virt-install命令定義和開啓新的實例。

[root@kvm-1 tmp]#  virt-install --name test --ram 1024 --disk path=/tmp/CentOS-7-x86_64-GenericCloud-1705.raw,format=raw --graphics vnc,listen=192.168.20.254 --noautoconsole --hvm --import


(3)查看定義的主機

[root@kvm-1 tmp]# virsh list --all
  Id    Name                           State
----------------------------------------------------
  1     c720220                        running
  2     c720203                        running
  4     c720101                        running
  10    c720104                        running
  39    c720133                        running
  40    c720134                        running
  41    c720135                        running
  42    c720136                        running
  44    test                           running


(4)從下面命令能夠看出自動產生的xml文件 。

[root@kvm-1 tmp]# cat /etc/libvirt/qemu/test.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
   virsh edit test
or other application using the libvirt API.
-->

<domain type='kvm'>
   <name>test</name>
   <uuid>acb7bbe0-0f47-4517-be83-2cf14be50811</uuid>
   <memory unit='KiB'>1048576</memory>
   <currentMemory unit='KiB'>1048576</currentMemory>
   <vcpu placement='static'>1</vcpu>
   <os>
     <type arch='x86_64' machine='pc-i440fx-rhel7.4.0'>hvm</type>
     <boot dev='hd'/>
   </os>
   <features>
     <acpi/>
     <apic/>
   </features>
   <cpu mode='custom' match='exact' check='partial'>
     <model fallback='allow'>Westmere</model>
   </cpu>
   <clock offset='utc'>
     <timer name='rtc' tickpolicy='catchup'/>
     <timer name='pit' tickpolicy='delay'/>
     <timer name='hpet' present='no'/>
   </clock>
   <on_poweroff>destroy</on_poweroff>
   <on_reboot>restart</on_reboot>
   <on_crash>destroy</on_crash>
   <pm>
     <suspend-to-mem enabled='no'/>
     <suspend-to-disk enabled='no'/>
   </pm>
   <devices>
     <emulator>/usr/libexec/qemu-kvm</emulator>
     <disk type='file' device='disk'>
       <driver name='qemu' type='raw'/>
       <source file='/tmp/CentOS-7-x86_64-GenericCloud-1705.raw'/>
       <target dev='hda' bus='ide'/>
       <address type='drive' controller='0' bus='0' target='0' unit='0'/>
     </disk>
     <controller type='usb' index='0' model='ich9-ehci1'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci1'>
       <master startport='0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci2'>
       <master startport='2'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci3'>
       <master startport='4'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
     </controller>
     <controller type='pci' index='0' model='pci-root'/>
     <controller type='ide' index='0'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
     </controller>
     <interface type='network'>
       <mac address='52:54:00:c5:25:91'/>
       <source network='default'/>
       <model type='rtl8139'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     </interface>
     <serial type='pty'>
       <target port='0'/>
     </serial>
     <console type='pty'>
       <target type='serial' port='0'/>
     </console>
     <input type='mouse' bus='ps2'/>
     <input type='keyboard' bus='ps2'/>
     <graphics type='vnc' port='-1' autoport='yes' listen='192.168.20.254'>
       <listen type='address' address='192.168.20.254'/>
     </graphics>
     <video>
       <model type='cirrus' vram='16384' heads='1' primary='yes'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
     </video>
     <memballoon model='virtio'>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
     </memballoon>
   </devices>
</domain>


四. 開始,中止,和移除KVM實例

1. 列出全部的虛擬機

[root@kvm-1 qemu]# virsh list –all


2. 啓動虛擬機並進行校驗

[root@kvm-1 qemu]# virsh start c720094
Domain c720094 started


列出全部虛擬機。

[root@kvm-1 qemu]# virsh list --all
  Id    Name                           State
----------------------------------------------------
  1     c720220                        running
  2     c720203                        running
  4     c720101                        running
  10    c720104                        running
  39    c720133                        running
  40    c720134                        running
  41    c720135                        running
  42    c720136                        running
  43    c720094                        running
  -     c720103                        shut off


3. 檢查虛擬機運行的進程

[root@kvm-1 qemu]# pgrep -lfa qemu
1822 /usr/libexec/qemu-kvm -name guest=c720101,debug-threads=on -S -object secret,id=masterKey0,format=raw,file=/var/lib/libvirt/qemu/domain-4-c720101/master-key.aes -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Westmere -m 4096 -realtime mlock=off -smp 8,sockets=8,cores=1,threads=1 -


4. 中止VM和確保它的狀態從運行狀態轉變成shut off:

[root@kvm-1 qemu]# virsh destroy c720094
Domain c720094 destroyed

[root@kvm-1 qemu]# virsh list --all
  Id    Name                           State
----------------------------------------------------
  1     c720220                        running
  2     c720203                        running
  4     c720101                        running
  10    c720104                        running
  39    c720133                        running
  40    c720134                        running
  41    c720135                        running
  42    c720136                        running
  -     c720094                        shut off
  -     c720103                        shut off


5. 移除定義的實例

[root@kvm-1 qemu]# virsh undefine c720094


五. 檢查和編輯KVM配置

轉儲虛擬機的配置文件 到磁盤,去檢查或者備份。 使用virsh命令也能夠更新配置。


1. 檢查運行的虛擬機實例

[root@kvm-1 qemu]# virsh list
  Id    Name                           State
----------------------------------------------------
  1     c720220                        running
  2     c720203                        running
  4     c720101                        running
  10    c720104                        running
  39    c720133                        running
  40    c720134                        running
  41    c720135                        running
  42    c720136                        running


2. 轉儲實例配置文件到標準輸出。

[root@kvm-1 qemu]# virsh dumpxml c720136
<domain type='kvm' id='42'>
   <name>c720136</name>
   <uuid>d9605a81-38b8-47ed-8984-21dea6cb0d88</uuid>
   <memory unit='KiB'>4194304</memory>
   <currentMemory unit='KiB'>4194304</currentMemory>
   <vcpu placement='static'>8</vcpu>
   <resource>
     <partition>/machine</partition>
   </resource>
   <os>
     <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
     <boot dev='hd'/>
   </os>
   <features>
     <acpi/>
     <apic/>
   </features>
   <cpu mode='custom' match='exact' check='full'>
     <model fallback='forbid'>Westmere</model>
     <feature policy='require' name='pclmuldq'/>
     <feature policy='require' name='x2apic'/>
     <feature policy='require' name='hypervisor'/>
   </cpu>
   <clock offset='utc'>
     <timer name='rtc' tickpolicy='catchup'/>
     <timer name='pit' tickpolicy='delay'/>
     <timer name='hpet' present='no'/>
   </clock>
   <on_poweroff>destroy</on_poweroff>
   <on_reboot>restart</on_reboot>
   <on_crash>destroy</on_crash>
   <pm>
     <suspend-to-mem enabled='no'/>
     <suspend-to-disk enabled='no'/>
   </pm>
   <devices>
     <emulator>/usr/libexec/qemu-kvm</emulator>
     <disk type='file' device='disk'>
       <driver name='qemu' type='qcow2'/>
       <source file='/kvm/sys-disk/c72136.qcow2'/>
       <backingStore/>
       <target dev='vda' bus='virtio'/>
       <alias name='virtio-disk0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
     </disk>
     <disk type='file' device='cdrom'>
       <driver name='qemu' type='raw'/>
       <backingStore/>
       <target dev='hda' bus='ide'/>
       <readonly/>
       <alias name='ide0-0-0'/>
       <address type='drive' controller='0' bus='0' target='0' unit='0'/>
     </disk>
     <controller type='usb' index='0' model='ich9-ehci1'>
       <alias name='usb'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci1'>
       <alias name='usb'/>
       <master startport='0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci2'>
       <alias name='usb'/>
       <master startport='2'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
     </controller>
     <controller type='usb' index='0' model='ich9-uhci3'>
       <alias name='usb'/>
       <master startport='4'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
     </controller>
     <controller type='pci' index='0' model='pci-root'>
       <alias name='pci.0'/>
     </controller>
     <controller type='ide' index='0'>
       <alias name='ide'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
     </controller>
     <controller type='virtio-serial' index='0'>
       <alias name='virtio-serial0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
     </controller>
     <interface type='bridge'>
       <mac address='52:54:00:6d:a0:8b'/>
       <source bridge='br0'/>
       <target dev='vnet14'/>
       <model type='virtio'/>
       <alias name='net0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     </interface>
     <interface type='bridge'>
       <mac address='52:54:00:4a:9e:ea'/>
       <source bridge='br1'/>
       <target dev='vnet15'/>
       <model type='virtio'/>
       <alias name='net1'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     </interface>
     <serial type='pty'>
       <source path='/dev/pts/5'/>
       <target port='0'/>
       <alias name='serial0'/>
     </serial>
     <console type='pty' tty='/dev/pts/5'>
       <source path='/dev/pts/5'/>
       <target type='serial' port='0'/>
       <alias name='serial0'/>
     </console>
     <channel type='unix'>
       <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-42-c720136/org.qemu.guest_agent.0'/>
       <target type='virtio' name='org.qemu.guest_agent.0' state='disconnected'/>
       <alias name='channel0'/>
       <address type='virtio-serial' controller='0' bus='0' port='1'/>
     </channel>
     <input type='tablet' bus='usb'>
       <alias name='input0'/>
       <address type='usb' bus='0' port='1'/>
     </input>
     <input type='mouse' bus='ps2'>
       <alias name='input1'/>
     </input>
     <input type='keyboard' bus='ps2'>
       <alias name='input2'/>
     </input>
     <memballoon model='virtio'>
       <alias name='balloon0'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
     </memballoon>
   </devices>
   <seclabel type='none' model='none'/>
   <seclabel type='dynamic' model='dac' relabel='yes'>
     <label>+107:+107</label>
     <imagelabel>+107:+107</imagelabel>
   </seclabel>
</domain>


3. 保存配置文件到新的文件 。

[root@kvm-1 qemu]# virsh dumpxml c720136 > c720136.xm

[root@kvm-1 qemu]# head c720136.xml
<domain type='kvm' id='42'>
   <name>c720136</name>
   <uuid>d9605a81-38b8-47ed-8984-21dea6cb0d88</uuid>
   <memory unit='KiB'>4194304</memory>
   <currentMemory unit='KiB'>4194304</currentMemory>
   <vcpu placement='static'>8</vcpu>
   <resource>
     <partition>/machine</partition>
   </resource>
   <os>


4. 在適當的地方編輯配置文件 。

[root@kvm-1 qemu]# virsh edit c720136.xml

相關文章
相關標籤/搜索