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为创建工具

最新文章

  1. [转]extjs组件添加事件监听的三种方式
  2. 关于WPF中RichTextBox失去焦点后如何保持高亮显示所选择的内容
  3. 谈谈yii2-gii如何自定义模板
  4. vs2012 发布网站,
  5. 网站迁移时候,发现&lt;head&gt;内容都到body里了
  6. 在GitHub上建立个人主页的方法
  7. GLSL Entry point not found
  8. jquery全选+下拉+单选+事件+挂事件
  9. javascript之值传递与引用传递
  10. zoj 2777 Visible Lattice Points(欧拉函数,基础)
  11. ADO和ADO.NET有什么不同?
  12. C题
  13. 总结windows多线程同步互斥
  14. NEU OJ 1649 GMZ’s Pretty Number
  15. “玲珑杯”ACM比赛 Round #1 题解
  16. MPLS VPN随堂笔记3
  17. Pandas时间处理的一些小方法
  18. PyQt5--QComboBox
  19. 多路复用 阻塞/非阻塞IO模型 网络IO两个阶段
  20. Leetcod--20. Valid Parentheses(极简洁的括号匹配)

热门文章

  1. python爬取小说
  2. 安装keepalived OpenSSL is not properly installed on your system. !!!
  3. MVC过滤器:自定义授权过滤器
  4. Python 爬取大众点评 50 页数据,最好吃的成都火锅竟是它!
  5. Java生鲜电商平台-电商支付流程架构实战
  6. Dynamics 365中计算字段与Now进行计算实体导入报错:You can&#39;t use Now(), which is of type DateTime, with the current function.
  7. HTTP协议解析之Cookie
  8. UITableView总结
  9. HAproxy四层TCP负载均衡配置及测试
  10. oracle并行模式