最後更新: 2021-07-15
介紹
libvirt 機乎是 kvm 的 default 管理工具來
它包括一了 API library, 一個 daemon (libvirtd), 及基本的 cli (virsh) 的集合來
目錄
- 安裝
- Settings
- 入門
- blk device(domblklist, domblkinfo, domblkstat)
安裝
CLI Client
yum install libvirt-client # C7
apt-get install libvirt-clients # U18
Daemon
apt-get install libvirt-daemon # U18
獲得:
- /usr/bin/virsh # CLI
- /usr/sbin/libvirtd # daemon <--> /var/run/libvirt/libvirt-sock
其他 libvirt 的 fontend:
- virtinst # Programs to create and clone virtual machines
Settings
non-root user 可以用 virsh:
adduser <youruser> libvirt
有關的設定檔
- /etc/libvirt/qemu.conf
- /etc/libvirt/lxc.conf
- /etc/default/libvirt-bin
- /etc/logrotate.d/libvirtd.qemu
- /etc/logrotate.d/libvirtd.lxc
- /etc/cron.daily/libvirt-bin
- /etc/init/libvirt-bin.conf
入門
看現在的版本
virsh --version
0.9.8
libvirtd --version
libvirtd (libvirt) 0.9.8
系統情況:
virsh nodeinfo
CPU model: x86_64 CPU(s): 2 CPU frequency: 2603 MHz CPU socket(s): 1 Core(s) per socket: 2 Thread(s) per core: 1 NUMA cell(s): 1 <--- Non-Uniform Memory Access (node) 與 CPUSET 有關 Memory size: 3202040 kB
virsh freecell [--all]
Total: 2055032 kB
virsh sysinfo
smbios .................... processor .................... memory_device
學習
virsh help list
應用
virsh 一共有以下 11 大類指令
- Domain Management
- Domain Monitoring
- Host and Hypervisor
- Interface
- Network Filter
- Networking
- Node Device
- Secret
- Snapshot
- Storage Pool
- Storage Volume
進入本地的 Shell:
virsh
virsh --connect qemu:///system
virsh -c qemu:///system
virsh # version Compiled against library: libvir 0.8.3 Using library: libvir 0.8.3 Using API: QEMU 0.8.3 Running hypervisor: QEMU 0.12.5
連接後執行 cmd
virsh -c qemu:///system [cmd]
連接 driver
URL
driver[+transport]://[username@][hostname][:port]/[path][?extraparameters]
Qemu driver:
# 透過 ssh 建立通道及認證
virsh --connect qemu+ssh://[email protected]/system
Vbox driver:
vbox:///session
vbox+ssh://[email protected]/session
LXC driver:
lxc:///
system 與 session 的分別
qemu:///system # system mode daemon
qemu:///session # session mode daemon
查看有什麼 VM:
virsh # list [--all]
Id Name State -------------------------- 27 drbd-b running 28 drbd-a running
vm 的稱呼:
- id
- uuid
- name
domname 27 # drbd-b
domid drbd-b # 27
domuuid drbd-b # fa394a50-25ba-43f4-a2e0-3d89965cc85d
查看 VM 的基本資料:
virsh dominfo < Id | Name >
Example:
virsh dominfo debian1
Id: 4 Name: debian1 UUID: e7f6d616-2aff-a790-f70c-23063d303566 OS Type: hvm State: running CPU(s): 1 CPU time: 8.3s Max memory: 262144 kB Used memory: 262144 kB Persistent: yes Autostart: disable Managed save: no Security model: apparmor Security DOI: 0 Security label: libvirt-e7f6d616-2aff-a790-f70c-23063d303566 (enforcing)
CPU
# vcpuinfo debian1
VCPU: 0 CPU: 1 State: running CPU time: 14.7s CPU Affinity: yy
virsh # vcpucount winxp
maximum config 1 maximum live 1 current config 1 current live 1
virsh # vcpupin winxp
VCPU: CPU Affinity ---------------------------------- 0: 0-1
VM State
virsh # domstate debian1
running
virsh # dommemstat debian1
actual 262144
blk device:
virsh # domblklist debian1
Target Source ------------------------------------------------ vda /mnt/vm/debian1/debian1_sata-2g.qcow2
virsh # domblkinfo debian1 vda
Capacity: 1024000000 Allocation: 1023881216 Physical: 1023881216
virsh # domblkstat debian1 vda
vda rd_req 1 vda rd_bytes 512 vda wr_req 0 vda wr_bytes 0 vda flush_operations 0
Start / Stop / Pause
start
shutdown / destroy
reboot / reset
suspend / resume
注意
reset 不會重讀 vm 修改過的 setting (XML) 的 !!
自動啟動
virsh autostart <domain> [--disable]
Save the RAM state of a running domain (save, restore)
# 與 VM 開關無關
virsh # save drbd-a /root/drbd-a.ram
長時間暫停 VM
# 與 suspend / resume 有所不同, 此方法會釋放出此 VM 在用的 RAM (Save and destroy)
# 下次 start 時可以選擇在保留的那個 state 啟動( paused 或 running)
managedsave domain-id [--paused] [--running]
# Full Boot (不要已保儲好的 stat file)
managedsave-remove domain-id
Machines
kvm -M ?
Supported machines are: pc Standard PC (alias of pc-1.0) pc-1.0 Standard PC (default) pc-0.14 Standard PC pc-0.13 Standard PC pc-0.12 Standard PC pc-0.11 Standard PC, qemu 0.11 pc-0.10 Standard PC, qemu 0.10 isapc ISA-only PC
修改 VM 設定
直接修改:
virsh edit vm-name
匯出, 匯入修改:
virsh dumpxml vm-name > /tmp/vm-name.xml
virsh define /tmp/vm-name.xml
其他有關的設定檔:
/etc/libvirt/qemu/*.xml
Virtual Networks: net-edit, net-dumpxml
Storage Pools: pool-edit, pool-dumpxml
Storage Volumes: vol-edit, vol-dumpxml
Interfaces: iface-edit, iface-dumpxml
當 vm 在行時
ps -e | grep kvm
1149 ? 00:00:00 kvm-irqfd-clean 13740 ? 00:00:18 kvm <-- 多了 13741 ? 00:00:00 kvm-pit-wq <-- 多了
Log
位於
/var/log/libvirt/qemu/$your-vm-name.log
Snapshot
查看 VM 有什麼 snapshot:
snapshot-list debian6a
virsh # snapshot-list debian6a Name Creation Time State --------------------------------------------------- clean 1969-12-31 16:19:38 +0800 shutoff
Create a snapshot
snapshot-create <domain>
example:
snapshot-create centos6
Delete a domain snapshot
snapshot-delete
Revert a domain to a snapshot
snapshot-revert
Rename
snapshot-edit domain <snapshotname> --rename
之後才可改 snapshot 的名稱
Virsh Networking
詳見: link
Storage Pools & Storage Volumes
它們都不是必須的, 為的是方便管理
因為在 Linux 上, 實在有不少 FileSystem 的 Backend,
比如 NFS, iSCSI, LVM 等等, 在掛載時較麻煩, 如果要劃一管理它們實在不易 !
Storage Pools
virsh pool-create pool.xml <--- transient pool, that has (no persistent config on disk)
<--- gone when you call pool-destroy on it
pool.xml:
<pool type='fs'> <name>vm_image</name> <source> <device path='/dev/sda9'/> <format type='auto'/> </source> <target> <path>/var/lib/virt/images</path> </target> </pool>
virsh pool-list
Name State Autostart ----------------------------------------- vm_image active no
pool-info vm_image
Name: vm_image UUID: f06af455-56dc-0944-5d63-2ddf1e9b923e State: running Persistent: no Autostart: no Capacity: 44.61 GB Allocation: 23.60 GB Available: 21.00 GB
pool-dumpxml
pool-define <-- /etc/libvirt/storage
pool-edit
pool-autostart vm_image <-- 只有 persistent 的才可 autostart
<-- 在 /etc/libvirt/storage/autostart 建立 soft-link
pool-refresh vm_image <--- 更新 vm_image.xml 的內容
pool-start
pool-destroy
pool-undefine
pool-delete
Storage Volumes:
virsh vol-???? <pool> <xml> | <name>
<volume> <name>centos6.3.qcow2</name> <allocation unit="G">2</allocation> // 如果沒有設定, 那 image 就會 full allocation <capacity unit="G">10</capacity> // logical capacity </volume>
vol-create vm_image vol.xml
vol-list vm_image
Name Path ----------------------------------------- centos6.3.qcow2 /var/lib/virt/images/centos6.3.qcow2
刪除
所有 VM 都是由 uuid 去操作, 所以如果要為 vm 改名, 那的刪除它先
XML:
<domain type='kvm'> ............. <uuid>d161d64e-ccc0-3774-4907-e6be57089a49</uuid> </domain>
我們不能 undefine 有 snapshot 的 VM !!
如果仍要 undefine 的話, 那就要加上 --snapshots-metadata
e.g.
undefine centos6 --snapshots-metadata
Default hypervisor
查看 Default hypervisor:
virsh uri
qemu:///system
一次性設定
export LIBVIRT_DEFAULT_URI=qemu:///system
永久設定
/etc/libvirt/libvirt.conf
uri_default = "qemu:///system"
aliases
/etc/libvirt/libvirt.conf
uri_aliases = [ "hail=qemu+ssh://[email protected]/system", "sleet=qemu+ssh://[email protected]/system", ]
Migrate
Plain migration: (libvirt will choose a migration port in the range 49152-49215)
Tunneled migration:
virsh migrate <domain> <remote host URI> --migrateuri tcp://<remote host>:<port>
Data transports:
- native transport
- tunnelled over a libvirtd connection
opts:
- --live for live migration
- --copy-storage-all indicates migration with non-shared storage
DestinationURL:
- qemu+ssh://test2.example.com/system
migrate-getspeed
migrate-getspeed domain-id
migrate-setspeed
# Unit: Mbps
migrate-setspeed domain-id bandwidth