博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
virt命令
阅读量:4042 次
发布时间:2019-05-24

本文共 10199 字,大约阅读时间需要 33 分钟。

QEMU的主要功能:

处理器模拟器
仿真IO设备
关联模拟的设备至真实设备;
调试器
与模拟器交互的用户接口
qemu:kmv的文本管理工具,包括qemu-kvm、qemu-img
libvirt:是一套免费、开源的支持Linux下主流虚拟化工具的C函数库,libvirtd是运行的守护进程的名称。包括GUI: virt-manager, virt-viewer,CLI: virt-install, virsh
安装步骤:
(1) 确保CPU支持HVM

grep -E –color=auto “(vmx|svm)” /proc/cpuinfo

(2) 装载模块

modprobe kvm

lsmod | grep kvm

(3)验证

ls -l /dev/kvm
(4)安装libvirt
yum install libvirt -y
(5)安装其他管理组件
yum install -y virt-manager virt-viewer virt-install

yum install -y qemu-kvm

(6)启动libvirtd守护进程

systemctl start libvirtd
验证:ifconfig多了virbr0虚拟网桥(只能是nat模式)
[root@localhost ~]# ifconfig
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:57:1a:76 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(7)启动virt tui

virt-manager

ps aux

root 14996 13.9 8.7 1197240 87980 ? Ssl 23:41 0:01 /usr/bin/python2 /usr/share/virt-manager/virt-manager
使用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

  1. 创建域
    create
    virsh create [–console] [–paused] [–autodestroy] [–pass-fds ] [–validate]
    [–file] 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 pass file descriptors N,M,… to the guest
    –validate validate the XML against the schema
  2. 获取域id
    [root@localhost ~]# virsh domid debian8
    3
  3. 获取域uuid
    [root@localhost ~]# virsh domuuid debian8
    9332c5a4-4abc-4e7f-bec0-faf394950a55
  4. 获取域信息
    [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)
  5. 登录虚拟机控制台
    [root@localhost ~]# virsh console debian8
    Connected to domain debian8
    Escape character is ^]
    使用ctrl+],退出console
  6. 开启域
    [root@localhost ~]# virsh start debian8
    Domain debian8 started
  7. 重启域
    reboot
  8. 关闭域
    destory
    shutdown
  9. 删除域
    undefine
  10. 暂停域并保存域状态至某文件中

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

命令:

help 打印帮助
attach-device 从一个XML文件附加装置
attach-disk 附加磁盘设备
attach-interface 获得网络界面
autostart 自动开始一个域
capabilities 性能
cd change the current directory
connect 连接(重新连接)到 hypervisor
console 连接到客户会话
cpu-baseline compute baseline CPU
cpu-compare compare host CPU with a CPU described by an XML file
create 从一个 XML 文件创建一个域
start 开始一个(以前定义的)非活跃的域
destroy 删除一个域
detach-device 从一个 XML 文件分离设备
detach-disk 分离磁盘设备
detach-interface 分离网络界面
define 从一个 XML 文件定义(但不开始)一个域
domid 把一个域名或 UUID 转换为域 id
domuuid 把一个域名或 id 转换为域 UUID
dominfo 域信息
domjobinfo domain job information
domjobabort abort active domain job
domname 将域 id 或 UUID 转换为域名
domstate 域状态
domblkstat 获得域设备块状态
domifstat 获得域网络接口状态
dommemstat get memory statistics for a domain
domblkinfo domain block device size information
domxml-from-native Convert native config to domain XML
domxml-to-native Convert domain XML to native config
dumpxml XML 中的域信息
edit 编辑某个域的 XML 配置
find-storage-pool-sources 发现潜在存储池源
find-storage-pool-sources-as 找到潜在存储池源
freecell NUMA可用内存
hostname 打印管理程序主机名
list 列出域
migrate 将域迁移到另一个主机中
migrate-setmaxdowntime set maximum tolerable downtime
net-autostart 自动开始网络
net-create 从一个 XML 文件创建一个网络
net-define 从一个 XML 文件定义(但不开始)一个网络
net-destroy 删除一个网络
net-dumpxml XML 中的网络信息
net-edit 为网络编辑 XML 配置
net-list 列出网络
net-name 把一个网络UUID 转换为网络名
net-start 开始一个(以前定义的)不活跃的网络
net-undefine 取消定义一个非活跃的网络
net-uuid 把一个网络名转换为网络UUID
iface-list list physical host interfaces
iface-name convert an interface MAC address to interface name
iface-mac convert an interface name to interface MAC address
iface-dumpxml interface information in XML
iface-define define (but don’t start) a physical host interface from an XML file
iface-undefine undefine a physical host interface (remove it from configuration)
iface-edit edit XML configuration for a physical host interface
iface-start start a physical host interface (enable it / “if-up”)
iface-destroy destroy a physical host interface (disable it / “if-down”)
managedsave managed save of a domain state
nodeinfo 节点信息
nodedev-list 这台主机中中的枚举设备
nodedev-dumpxml XML 中的节点设备详情
nodedev-dettach dettach node device from its device driver
nodedev-reattach reattach node device to its device driver
nodedev-reset 重置节点设备
nodedev-create create a device defined by an XML file on the node
nodedev-destroy destroy a device on the node
nwfilter-define define or update a network filter from an XML file
nwfilter-undefine undefine a network filter
nwfilter-dumpxml network filter information in XML
nwfilter-list list network filters
nwfilter-edit edit XML configuration for a network filter
pool-autostart 自动启动某个池
pool-build 建立池
pool-create 从一个 XML 文件中创建一个池
pool-create-as 从一组变量中创建一个池
pool-define 在一个 XML 文件中定义(但不启动)一个池
pool-define-as 在一组变量中定义池
pool-destroy 销毁池
pool-delete 删除池
pool-dumpxml XML 中的池信息
pool-edit 为存储池编辑 XML 配置
pool-info 存储池信息
pool-list 列出池
pool-name 将池 UUID 转换为池名称
pool-refresh 刷新池
pool-start 启动一个(以前定义的)非活跃的池
pool-undefine 取消定义一个不活跃的池
pool-uuid 把一个池名称转换为池 UUID
secret-define define or modify a secret from an XML file
secret-dumpxml secret attributes in XML
secret-set-value set a secret value
secret-get-value Output a secret value
secret-undefine undefine a secret
secret-list list secrets
pwd print the current directory
quit 退出这个非交互式终端
exit 退出这个非交互式终端
reboot 重新启动一个域
restore 从一个存在一个文件中的状态恢复一个域
resume 重新恢复一个域
save 把一个域的状态保存到一个文件
schedinfo 显示/设置日程安排变量
dump 把一个域的内核 dump 到一个文件中以方便分析
shutdown 关闭一个域
setmem 改变内存的分配
setmaxmem 改变最大内存限制值
setvcpus 改变虚拟 CPU 的号
suspend 挂起一个域
ttyconsole tty 控制台
undefine 取消定义一个非活跃的域
update-device update device from an XML file
uri 打印管理程序典型的URI
vol-create 从一个 XML 文件创建一个卷
vol-create-from create a vol, using another volume as input
vol-create-as 从一组变量中创建卷
vol-clone clone a volume.
vol-delete 删除卷
vol-wipe wipe a vol
vol-dumpxml XML 中的卷信息
vol-info 存储卷信息
vol-list 列出卷
vol-pool returns the storage pool for a given volume key or path
vol-path returns the volume path for a given volume name or key
vol-name returns the volume name for a given volume key or path
vol-key returns the volume key for a given volume name or path
vcpuinfo 域 vcpu 的信息
vcpupin 控制域 vcpu affinity
version 显示版本
vncdisplay vnc 显示
snapshot-create Create a snapshot
snapshot-current Get the current snapshot
snapshot-delete Delete a domain snapshot
snapshot-dumpxml Dump XML for a domain snapshot
snapshot-list List snapshots for a domain
snapshot-revert Revert a domain to a snapshot

转载地址:http://mvodi.baihongyu.com/

你可能感兴趣的文章
Improved&nbsp;logging&nbsp;in&nbsp;Objective-C&nbsp;…
查看>>
ShareKit&nbsp;分享到FaceBook,&nbsp;Twitter…
查看>>
Facebook&nbsp;Share&nbsp;iOS&nbsp;Tutorial
查看>>
判断系统版本(用宏,非UIDevice)以…
查看>>
unsigned&nbsp;int的最大值及int的最大…
查看>>
Property&nbsp;List&nbsp;支持的数据类型(us…
查看>>
Shell编程基础《转》
查看>>
Xcode&nbsp;调试加入参数《Apple&nbsp;Dev&nbsp;Do…
查看>>
修改mysql编码《转》
查看>>
Posting&nbsp;images&nbsp;using&nbsp;TWRequest《…
查看>>
mac系统如何显示和隐藏文件&nbsp;《转》
查看>>
iphone开发常用代码(不断更…
查看>>
OpenCV&nbsp;学习笔记
查看>>
UIColor&nbsp;转换为&nbsp;UIImage&nbsp;《转》
查看>>
&lt;iOS4&gt;Switching&nbsp;between&nbsp;front&nbsp;an…
查看>>
opencv&nbsp;pca
查看>>
OpenCV做PCA的一个详尽的介…
查看>>
iOS&nbsp;中用代码写字体,并加入…
查看>>
《转》深入浅出&nbsp;Cocoa&nbsp;多线程编程…
查看>>
多target
查看>>