Linux Virsh 控制虚拟机
Virsh 命令对虚拟机操作
参考文档
- https://www.cnblogs.com/potato-chip/p/10484917.html
- https://www.cnblogs.com/longchang/p/12300129.html
virsh define 创建虚拟机
1. 首先需要创建一个稀疏镜像
# 校验镜像是否正确
qemu-img check 镜像名
# 创建一个qcow2的稀疏文件虚拟磁盘文件,就是说刚创建出来的文件并没有8G,它会随着数据的增多慢慢增加,直到80G;
qemu-img create -f qcow2 -b /path/name.qcow2 -o backing_fmt=qcow2 /path/new_name.qcow2 80G
# 参数说明:
# -f: 指定镜像格式,
# -o:指定选项
# /root/kvm.qcow2 表示指定创建的虚拟磁盘路径
# 80G指定磁盘大小;
# backing_fmt, 设置后端镜像的镜像格式
2. 创建一个 xml 文件
- 参考文章
- https://www.cnblogs.com/mrwuzs/p/8037058.html
- https://blog.csdn.net/qq_15437629/article/details/77827033
Python 生成一个 uuid 作为虚拟机的 uuid
import uuid
# 基于时间戳生成一个 uuid
def get_uuid():
instance = uuid.uuid1()
return instance
xml 折叠代码
<domain type='kvm'>
<name>generic</name>
<uuid>f3a9ac9b-32fc-4fe0-bb63-8a970db3b50d</uuid>
<memory unit='KiB'>10485760</memory>
<currentMemory unit='KiB'>10485760</currentMemory>
<vcpu placement='static'>8</vcpu>
<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='partial'>
<model fallback='allow'>Broadwell</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<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>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/generic.qcow2'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='1'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:29:5b:67'/>
<source network='default'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='1'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</domain>
3. 创建虚拟机
virsh define ./instance_xml_name.xml
virsh 其他操作
- 删除虚拟机
# 先关机 在删除
virsh destroy 虚拟机名称
virsh undefine 虚拟机名称
- 关闭虚拟机
virsh shutdown 虚拟机名称
- 强制关闭虚拟机
virsh destory 虚拟机名称
virsh 虚拟机快照
- 创建虚拟机快照
virsh snapshot-create-as 虚拟机名称 快照名称
# domain 是指虚拟机的名字
# snapshot-name 是指快照的名字
- 查看快照列表和详情信息
virsh snapshot-list 虚拟机名称 快照名称
- 恢复快照
virsh snapshot-revert 虚拟机名称 快照名称
- 删除快照
virsh snapshot-delete 虚拟机名称 快照名称
virsh lv卷:作为虚拟机后端存储
查看虚拟机的虚拟磁盘
$ virsh domblklist <虚拟机名称>
挂载一个 LV 作为虚拟机硬盘
$ virsh attach-disk instance_name /dev/vg_name/lv_name vde --driver qemu
$ virsh <虚拟机名字> <挂载的lv> <lv 所在的硬盘>
--derver <设备驱动 qemu>
--mode <挂载的类型> # --mode shareable
OPTIONS
[--domain] <string> domain name, id or uuid
[--source] <string> source of disk device
[--target] <string> target of disk device 一般为vdb,vdc...
--driver <string> driver of disk device
--mode <string> mode of device reading and writing
--subdriver 镜像文件的格式
卸载一块虚拟磁盘
# 卸载某一台虚拟机上的虚拟磁盘
$ virsh detach-device <虚拟机名称> <target:一般为 sdb sdbc>
#
$ virsh domlblist <虚拟机名称>

浙公网安备 33010602011771号