QEMU-KVM自动设置filterref参数



如果这是一个简单的问题,我很抱歉,但我刚刚开始使用qemu,找不到一个简单的方法来做到这一点。

我正在尝试自动化我的KVM部署。我目前遇到的问题是,我找不到一种方法来自动设置参数的过滤器。

这是我的网络选项virt-install目前的样子,现在工作得很好。

--network type=bridge,network=default,bridge=bridge0,model=e1000e,mac=$mac,filterref=clean-traffic

然而,我找不到任何东西来设置一个参数来定义它应该被锁定的IP地址。这是我想在xml中得到的结果:

<filterref filter='clean-traffic'>
<parameter name='IP' value='XXX.XXX.XXX.XXX'/>
</filterref>

我正在寻找一种方法来自动添加该参数,最好直接与virt-install或在某种程度上,我可以只是运行一个脚本,输入我想要设置的几个变量。此时,VM已经在运行并等待设置完成,并加载了过滤器。基本上,我希望在第一次启动之前加载参数,这样就不会有人试图弄乱ip地址。

这可能吗?

这是整个脚本"我现在只是复制到控制台。

name=WindowsTest
mac=00:50:56:00:05:C5
size=70
ram=6000
vcpus=6
let cores=vcpus/2
virt-install 
--name=$name 
--ram=$ram 
--cpu=host 
--vcpus=$vcpus,maxvcpus=$vcpus,sockets=1,cores=$cores,threads=2 
--os-type=windows 
--os-variant=win10 
--disk path=/var/lib/libvirt/clutchImages/$name.qcow2,size=$size,format=qcow2,bus=virtio 
--cdrom /var/isos/Windows_20H2_English.iso 
--disk /var/isos/virtio-win-0.1.185.iso,device=cdrom 
--network type=bridge,network=default,bridge=bridge0,model=e1000e,mac=$mac,filterref=clean-traffic  
--graphics spice,listen=157.90.2.208  
--graphics vnc

virsh版本输出:

virsh version
Compiled against library: libvirt 6.0.0
Using library: libvirt 6.0.0
Using API: QEMU 6.0.0
Running hypervisor: QEMU 4.2.0

我使用的是CentOS Linux 8.3.2011。

对virt-install的xml输出进行任意编辑

根据手册页,您可以使用XPath直接编辑XML语法。

virt-install 
#...
--network network="${net}",mac="${macaddr},filterref.filter=clean-traffic" 
--xml xpath.create=./devices/interface/filterref/parameter 
--xml xpath.set=./devices/interface/filterref/parameter/@name=IP 
--xml xpath.set=./devices/interface/filterref/parameter/@value=10.0.0.20
#...

man virt-install | grep -m1 -A40 '--xml'
--xml
Syntax: --xml ARGS
Make  direct edits to the generated XML using XPath syntax. Take an ex‐
ample like
virt-install --xml ./@foo=bar --xml ./newelement/subelement=1
This will alter the generated XML to contain:
<domain foo='bar' ...>
...
<newelement>
<subelement>1</subelement>
</newelement>
</domain>
The --xml option has 4 sub options:
--xml xpath.set=XPATH[=VALUE]
The default behavior if no explicit suboption is set. Takes  the
form  XPATH=VALUE unless paired with xpath.value . See below for
how value is interpreted.
--xml xpath.value=VALUE
xpath.set will be interpreted only  as  the  XPath  string,  and
xpath.value  will be used as the value to set. May help sidestep
problems if the string you need to set  contains  a  '='  equals
sign.
If  value  is  empty,  it's treated as unsetting that particular
node.
--xml xpath.create=XPATH
Create the node as an empty element. Needed for boolean elements
like <readonly/>
--xml xpath.delete=XPATH
Delete the entire node specified by the xpath, and all its chil‐
dren

XML结果
<interface type="network">
<!-- ... -->
<filterref filter="clean-traffic">
<parameter name="IP" value="10.0.0.20"/>
</filterref>
</interface>

virsh version输出:

Compiled against library: libvirt 7.7.0
Using library: libvirt 7.7.0
Using API: QEMU 7.7.0
Running hypervisor: QEMU 6.2.0

快捷&肮脏的

name=WindowsTest
mac=00:50:56:00:05:C5
IP=xxx.yyy.zzz.qqq
size=70
ram=6000
vcpus=6
let cores=vcpus/2
virt-install 
--name=$name 
--ram=$ram 
--cpu=host 
--vcpus=$vcpus,maxvcpus=$vcpus,sockets=1,cores=$cores,threads=2 
--os-type=windows 
--os-variant=win10 
--disk path=/var/lib/libvirt/clutchImages/$name.qcow2,size=$size,format=qcow2,bus=virtio 
--cdrom /var/isos/Windows_20H2_English.iso 
--disk /var/isos/virtio-win-0.1.185.iso,device=cdrom 
--network type=bridge,network=default,bridge=bridge0,model=e1000e,mac=$mac,filterref=clean-traffic  
--graphics spice,listen=157.90.2.208  
--graphics vnc
--print-xml  > /tmp/{$name}.xml  &&  
sed -i "s/<filterref.*/<filterref filter='clean-traffic'>n <parameter name='IP' value='${IP}'/>n </filterref>/g" /tmp/{$name}.xml &&  
virsh create /tmp/{$name}.xml

最新更新