virsh命令

virsh 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
virsh list # 列出所有运行的虚拟机                                       
virsh list --all # 列出所有虚拟机

virsh hostname # 列出真机主机名
virsh sysinfo # 列出真机的详细信息
virsh nodecpustats # 得到真机cpu状态
virsh nodememstats # 得到真机内存状态
virsh node-memory-tune # 允许你设置的node内存参数
virsh capabilities # 打印虚拟机的特点
virsh freecell # 打印虚拟机中可用内存
virsh shutdown dns # 关闭虚拟机dns
virsh start dns # 启动虚拟机dns
virsh dumpxml dns > dns.xml # 导出dns虚拟机的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
virsh --help                                     #查看命令帮忙
virsh list #显示正在运行的虚拟机
virsh list --all #显示所有的虚拟机
virsh start vm-node1 #启动vm-node1虚拟机
virsh shutdown vm-node1 #关闭vm-node1虚拟机
virsh destroy vm-node1 #虚拟机vm-node1强制断电
virsh suspend vm-node1 #挂起vm-node1虚拟机
virsh resume vm-node1 #恢复挂起的虚拟机
virsh undefine vm-node1 #删除虚拟机,慎用
virsh dominfo vm-node1 #查看虚拟机的配置信息
virsh domiflist #查看网卡配置信息
virsh domblklist vm-node1 #查看该虚拟机的磁盘位置
virsh edit vm-node1 #修改vm-node1的xml配置文件
virsh dumpxml vm-node1 #查看KVM虚拟机当前配置
virsh dumpxml vm-node1 > vm-node1.bak.xml #备份vm-node1虚拟机的xml文件,原文件默认路径/etc/libvirt/qemu/vm-node1.xml
virsh autostart vm-node1 #KVM物理机开机自启动虚拟机,配置后会在此目录生成配置文件/etc/libvirt/qemu/autostart/vm-node1.xml
virsh autostart --disable vm-node1 #取消开机自启动
virsh console vm-node1 #进入虚拟机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<domain type='kvm' id='13'>
<name>demo-guest1</name>
<uuid>409fc837-ef54-48c9-9a47-c62fe383d33d</uuid>
<!-- guset可以使用的最大内存,以KB为单位 -->
<memory unit='KiB'>2097152</memory>
<!-- guest启动时内存,可以通过virsh setmem来调整内存,但不能大于最大可使用内存 -->
<currentMemory unit='KiB'>2097152</currentMemory>
<!-- 分配的虚拟cpu -->
<vcpu placement='static'>2</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'>Skylake-Server-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
<feature policy='require' name='hypervisor'/>
<feature policy='disable' name='arat'/>
</cpu>
<!-- 时钟, 使用本地时间: utc -->
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<!-- 定义了在kvm环境中crash时的默认的动作分别为destroy和restart。
destroy:停止该虚拟机。相当于关闭电源。
restart: 重启虚拟机 -->
<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>
<!-- 模拟器,此处路径为qemu-kvm绝对路径 -->
<emulator>/usr/libexec/qemu-kvm</emulator>
<!-- 用于kvm启动或者存储的文件,启动设备为:/image/UCarApp1/UCarApp1_clone-disk1.raw -->
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/image/UCarApp1/UCarApp1_clone-disk1.raw'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<!-- 可通过--disk 修改磁盘总线类型为ide --disk UCarApp1_clone-disk1.raw,bus=ide -->
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' 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='0x04' 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='0x04' 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='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<controller type='virtio-serial' index='0'>
<alias name='virtio-serial0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<!-- 采用普通的驱动,即硬盘和网卡都采用默认配置情况下,网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。采用 virtio 驱动后,网卡工作在 1000M 的模式下 -->
<interface type='bridge'>
<mac address='52:54:00:bc:1b:74'/>
<source bridge='br0'/>
<target dev='vnet6'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/11'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/11'>
<source path='/dev/pts/11'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-13-demo-guest1/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>
<!-- 定义与guset交互的图形设备。在这个例子中,使用vnc协议。listen的地址为0.0.0.0的地址。prot为5903,通过以下的命令查找端口号: virsh vncdisplay guestName -->
<graphics type='vnc' port='5903' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<alias name='video0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<alias name='balloon0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<alias name='rng0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</rng>
<!-- 设备定义结束 -->
</devices>
<seclabel type='dynamic' model='selinux' relabel='yes'>
<label>system_u:system_r:svirt_t:s0:c47,c808</label>
<imagelabel>system_u:object_r:svirt_image_t:s0:c47,c808</imagelabel>
</seclabel>
<seclabel type='dynamic' model='dac' relabel='yes'>
<label>+107:+107</label>
<imagelabel>+107:+107</imagelabel>
</seclabel>
</domain>

课程中心

Qianj.51CTO