libvirt的virsh命令和qemu参数转换

运行虚拟机有多种方式,其中可以使用qemu-system-x86或者qemu-kvm来运行虚拟。另外还可以使用libvirt的virsh命令从xml文件定义来运行虚拟机。而网上许多的教程或者示例使用qemu命令参数来运行的,如果使用libvirt来运行可能会无从下手,不知道对应的参数。

libvirt从Domain的配置文件启动虚拟机

虚拟机(有时Domain,guest都表示虚拟机的含义。)可以通过Domain xml文件来进行配置虚拟机。以下是一个demo的配置文件。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< domain type = 'qemu' >
   < name >QEmu-fedora-i686</ name >
   < uuid >c7a5fdbd-cdaf-9455-926a-d65c16db1809</ uuid >
   < memory >219200</ memory >
   < currentMemory >219200</ currentMemory >
   < vcpu >2</ vcpu >
   < os >
     < type arch = 'i686' machine = 'pc' >hvm</ type >
     < boot dev = 'cdrom' />
   </ os >
   < devices >
     < emulator >/usr/bin/qemu-system-x86_64</ emulator >
     < disk type = 'file' device = 'cdrom' >
       < source file = '/home/user/boot.iso' />
       < target dev = 'hdc' />
       < readonly />
     </ disk >
     < disk type = 'file' device = 'disk' >
       < source file = '/home/user/fedora.img' />
       
< target dev = 'hda' />
     </ disk >
     < interface type = 'network' >
       < source network = 'default' />
     </ interface >
     < graphics type = 'vnc' port = '-1' />
   </ devices >
</ domain >

然后使用以下命令从xml文件导入到libvirt虚拟机管理软件中。

1
virsh define demo.xml

启动虚拟机。其中QEmu-fedora-i686为xml定义的虚拟机别名。

1
virsh start QEmu-fedora-i686

使用qemu命令行启动虚拟机

也可以使用qemu命令行启动虚拟机,但是每次都要输入那么长的参数会忘记,所以也可以写个shell脚本来保存参数。类似的,qemu启动虚拟机使用如下命令:

1
2
3
4
5
6
7
8
9
/usr/bin/qemu-system-x86_64 --enable-kvm \
   -m 1024 \
   -smp 1 \
   -name QEMUGuest1 \
   -nographic \
   -monitor pty -no-acpi -boot c \
   -drive  file=/dev/HostVG/QEMUGuest1,if=ide,index=0 \
   -net none \
   -serial none -parallel none -usb

看到这样长的参数,输入起来当然麻烦了。而且这样运行虚拟机libvirt还没办法直接查询虚拟机的状态。

在xml文件里使用任意qemu命令行参数

有些命令行的参数可以使用xml直接定义出来,但是有些并不能找到对应的xml的标签。但是libvrit为我们提供了一个qemu:commandline来配置xml文件。

例如:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
< domain type = 'qemu' xmlns:qemu = 'http://libvirt.org/schemas/domain/qemu/1.0' >
   < name >QEmu-fedora-i686</ name >
   < memory >219200</ memory >
   < os >
     < type arch = 'i686' machine = 'pc' >hvm</ type >
   </ os >
   < devices >
     < emulator >/usr/bin/qemu-system-x86_64</ emulator >
   </ devices >
   < qemu:commandline >
     < qemu:arg value = '-newarg' />
     < qemu:env name = 'QEMU_ENV' value = 'VAL' />
   </ qemu:commandline >
</ domain >

Libvirt提供一个XML命名空间和可选类库libvirt-qemu.so来解决qemu的一些特殊情况。当被正确使用时,这些扩展允许测试还没被libvirt的XML和API接口支持qemu的特性。然而,他们是不被支持的,因为该类库不能保证有一个稳定的API接口,滥用这些类库或者xml文件会导致不一致的状态使libvirtd奔溃,升级qemu-kvmlibvirtd会对那么依赖使用qemu特性的虚拟机有破坏性行为。如果你确信需要获得的qemu的特性,请发送RFE给libvirt的mailing list以得到纳入该功能的稳定的libvirt XML和API接口。

这些类库提供两个API:virDomainQemuMonitorCommand,用来发送任意监控命令(monitor command, 包括HMP和QMP格式)给qemu虚拟机 (Since 0.8.3);virDomainQemuAttach用来注册一个人工启动的qemu虚拟机,这样可以使用libvirtd来管理(Since 0.9.4)。

当启动虚拟机的时候,以下的XML扩展允许给qemu命令行参数。为了使用XML扩展,需要增加一个XML namespacehttp://libvirt.org/schemas/domain/qemu/1.0。一般给这namespace取名为qemu,需要增加一个元素<qemu:commandline>,还有两个子元素:

qemu:arg 当启动虚拟机的时候,向qemu进行提供一个命令行参数,将参数放在XML的value属性里。

qemu:env当启动虚拟机的时候,向qemu进行提供一个环境变量, 将 name-value对 放在name属性和可选的vlaue属性里。

将QEMU参数转domain XML配置

命令virsh domxml-from-native提供一个方法将已存在的一组QEMU参数转成可以被libvirt使用Domain XML文件。这个命令的目的用于将先前已经能够从命令行运行的虚拟机转化为用libvirt去管理的XML配置文件。请不要用它来创建一个新虚拟机。新的虚拟机应该由调用libvirt API的程序去创建,或者手工编写XML文件让virsh运行。

需要特别注意的是,在demo.args文件中,不能有换行出现,整个命令要在同一行里。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ cat > demo.args <<EOF
LC_ALL=C PATH=/bin HOME=/home/test USER=test \
LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 \
-nographic -monitor pty -no-acpi -boot c -hda \
/dev/HostVG/QEMUGuest1 -net none -serial none \
-parallel none -usb
EOF
 
$ virsh domxml-from-native qemu-argv demo.args
<domain type='qemu'>
   <uuid>00000000-0000-0000-0000-000000000000</uuid>
   <memory>219136</memory>
   <currentMemory>219136</currentMemory>
   <vcpu>1</vcpu>
   <os>
     <type arch='i686' machine='pc'>hvm</type>
     <boot dev='hd'/>
   </os>
   <clock offset='utc'/>
   <on_poweroff>destroy</on_poweroff>
   <on_reboot>restart</on_reboot>
   <on_crash>destroy</on_crash>
   <devices>
     <emulator>/usr/bin/qemu</emulator>
     <disk type='block' device='disk'>
       <source dev='/dev/HostVG/QEMUGuest1'/>
       <target dev='hda' bus='ide'/>
     </disk>
   </devices>
</domain>

转化domain XML为QEMU参数

命令virsh domain-to-natice可以将libvirt的Domian XML文件转化成一组QEMU参数。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$ cat > demo.xml <<EOF
<domain type='qemu'>
   <name>QEMUGuest1</name>
   <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
   <memory>219200</memory>
   <currentMemory>219200</currentMemory>
   <vcpu>1</vcpu>
   <os>
     <type arch='i686' machine='pc'>hvm</type>
     <boot dev='hd'/>
   </os>
   <clock offset='utc'/>
   <on_poweroff>destroy</on_poweroff>
   <on_reboot>restart</on_reboot>
   <on_crash>destroy</on_crash>
   <devices>
     <emulator>/usr/bin/qemu</emulator>
     <disk type='block' device='disk'>
       <source dev='/dev/HostVG/QEMUGuest1'/>
       <target dev='hda' bus='ide'/>
     </disk>
   </devices>
</domain>
EOF
 
$ virsh domxml-to-native qemu-argv demo.xml
   LC_ALL=C PATH=/usr/bin:/bin HOME=/home/test \
   USER=test LOGNAME=test /usr/bin/qemu -S -M pc \
   -no-kqemu -m 214 -smp 1 -name QEMUGuest1 -nographic \
   -monitor pty -no-acpi -boot c -drive \
   file=/dev/HostVG/QEMUGuest1,if=ide,index=0 -net none \
   -serial none -parallel none -usb

转载地址:https://www.chenyudong.com/archives/qemu-kvm-command-arguments-switch-to-libvirt-xml.html

猜你喜欢

转载自blog.csdn.net/YuZhiHui_No1/article/details/53909925