簡體   English   中英

QEMU-KVM 自動設置 filterref 參數

[英]QEMU-KVM automatically set filterref parameter

如果這是一個簡單的問題,我很抱歉,但我剛剛開始使用 qemu,找不到簡單的方法來做到這一點。

我正在嘗試自動化我的 KVM 部署。 我目前遇到的問題是我找不到自動為 filterref 設置參數的方法。

這就是我的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 版本 output:

   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 上。

又快又臟

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

對 virt-install 的 xml output 進行任意編輯

根據手冊頁,您可以使用 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
#...

virt-install 手冊頁摘錄:

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 output:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM