qemu:kmv的文本管理工具,包括qemu-kvm、qemu-img
libvirt:是一套免费、开源的支持Linux下主流虚拟化工具的C函数库,libvirtd是运行的守护进程的名称。包括GUI: virt-manager, virt-viewer,CLI: virt-install, virsh
使用virsh测试各命令及创建虚拟机
1. 获取各命令帮助
virsh help KEYWORD
#virsh help list
2. 查看域,–all选项可查看关机的虚拟机域,域id每次开关机后可能不一样
root@localhost ~]# virsh list –all
Id Name State
—————————————————-
– debian8 shut off
3. 查看虚拟机配置文件
注意为xml格式,可以到处到某处查看或以此为模板创建其他虚拟机
虚拟机以域(domain)为单位创建
# virsh dumpxml debian8 > /tmp/mytemplate.xml
4. 创建域
create
virsh create <file> [–console] [–paused] [–autodestroy] [–pass-fds <string>] [–validate]
[–file] <string> file containing an XML domain description
–console attach to console after creation
–paused leave the guest paused after creation
–autodestroy automatically destroy the guest when virsh disconnects
–pass-fds <string> pass file descriptors N,M,… to the guest
–validate validate the XML against the schema
5. 获取域id
[root@localhost ~]# virsh domid debian8
3
6. 获取域uuid
[root@localhost ~]# virsh domuuid debian8
9332c5a4-4abc-4e7f-bec0-faf394950a55
7. 获取域信息
[root@localhost ~]# virsh dominfo debian8
Id: 3
Name: debian8
UUID: 9332c5a4-4abc-4e7f-bec0-faf394950a55
OS Type: hvm
State: running
CPU(s): 2
CPU time: 428.6s
Max memory: 1047552 KiB
Used memory: 1047552 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c327,c602 (enforcing)
8. 登录虚拟机控制台
[root@localhost ~]# virsh console debian8
Connected to domain debian8
Escape character is ^]
使用ctrl+],退出console
9. 开启域
[root@localhost ~]# virsh start debian8
Domain debian8 started
10. 重启域
reboot
11. 关闭域
destory
shutdown
12. 删除域
undefine
13. 暂停域并保存域状态至某文件中
# virsh save debian8 /tmp/debian_save1 –running
–running 下次恢复,直接启动
14. 从保存文件中恢复域
# virsh restore /tmp/debian_save1
管理域的命令:
15. 改变内存大小
不能超出预设值,只能调小,可以当前生效,也可以下次生效
# virsh setmem debian8 786m –current
[root@localhost ~]# virsh dominfo debian8
Id: 5
Name: debian8
UUID: 9332c5a4-4abc-4e7f-bec0-faf394950a55
OS Type: hvm
State: running
CPU(s): 2
CPU time: 1471.5s
Max memory: 1047552 KiB
Used memory: 804864 KiB #此处为改过的值
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c470,c985 (enforcing)
[root@localhost ~]# free -mh
total used free shared buff/cache available
Mem: 977M 741M 73M 4.2M 162M 64M
Swap: 1.9G 1.1G 827M
16. 设定内存最大内存
运行中的域不能修改最大内存值
[root@localhost ~]# virsh setmaxmem debian8 900m –config
下次启动有效
17. 设定vcpu数量
# virsh setvcpus debian8 1 –config
不能实时改,下次启动有效
18. 获取vcpu信息
[root@localhost ~]# virsh vcpuinfo debian8
VCPU: 0 #vcpu
CPU: 0 #在宿主机cpu位置
State: running
CPU time: 675.5s
CPU Affinity: yyyy
VCPU: 1
CPU: 1
State: running
CPU time: 694.2s
CPU Affinity: yyyy
19. 获取域网络接口信息
[root@localhost ~]# virsh domiflist debian8
Interface Type Source Model MAC
——————————————————-
vnet0 network default virtio 52:54:00:82:53:a2
20. 获取域的接口统计信息
[root@localhost ~]# virsh domifstat debian8 vnet0
vnet0 rx_bytes 197810
vnet0 rx_packets 3755
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 13400
vnet0 tx_packets 111
vnet0 tx_errs 0
vnet0 tx_drop 0
21. 获取域块设备信息
[root@localhost ~]# virsh domblklist debian8
Target Source
————————————————
vda /var/lib/libvirt/images/debian8.qcow2
hda –
22. 获取域块设备(存储)统计信息
[root@localhost ~]# virsh domblkstat debian8
rd_req 21908
rd_bytes 670065746
wr_req 1105
wr_bytes 29772800
flush_operations 229
rd_total_times 97947369758
wr_total_times 60546346501
flush_total_times 1534616225
创建及管理磁盘:
23. 创建磁盘
[root@localhost ~]# qemu-img create -f qcow2 -o preallocation=metadata /tmp/test.qcow2 120G 稀疏格式
Formatting '/tmp/test.qcow2', fmt=qcow2 size=128849018880 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off
[root@localhost ~]# du -lh /tmp/test.qcow2
19M /tmp/test.qcow2
[root@localhost ~]# ll -lh /tmp/test.qcow2
-rw-r–r–. 1 root root 121G Jan 12 13:53 /tmp/test.qcow2
24. 增加磁盘大小
[root@localhost ~]# qemu-img resize /tmp/test.qcow2 150G
Image resized.
[root@localhost ~]# ll -h /tmp/test.qcow2
-rw-r–r–. 1 root root 121G Jan 12 13:57 /tmp/test.qcow2
[root@localhost ~]# du -lh /tmp/test.qcow2
19M /tmp/test.qcow2
25. 附加磁盘到域
# qemu-img create -f qcow2 -o preallocation=metadata /tmp/mytest.img 20G
[root@localhost ~]# virsh attach-disk debian8 /tmp/mytest.img vdb
Disk attached successfully
26. 拆除磁盘
[root@localhost ~]# virsh detach-disk debian8 vdb
Disk detached successfully
网卡管理
网桥查看命令
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.525400571a76 yes virbr0-nic
vnet0
27.添加域网卡到宿主机桥上
[root@localhost ~]# virsh attach-interface debian8 bridge virbr0 为宿主机nat网桥
Interface attached successfully
[root@localhost ~]# virsh domiflist debian8
Interface Type Source Model MAC
——————————————————-
vnet0 network default virtio 52:54:00:82:53:a2
vnet1 bridge virbr0 rtl8139 52:54:00:ca:04:d3
vnet2 bridge br0 rtl8139 52:54:00:89:3b:1d
28. 删除域网卡
[root@localhost ~]# virsh detach-interface debian8 bridge –mac 52:54:00:89:3b:1d
Interface detached successfully
使用qemu命令手动创建虚拟机
qemu-kvm为创建工具