libvirt创建虚拟机

本文详细介绍了使用Libvirt创建虚拟机的过程,包括虚拟机XML配置文件的各个元素及其作用,如虚拟机类型、内存配置、CPU配置、磁盘与网络设置等。此外还提供了实用的虚拟机和磁盘XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Libvirt create domain

Domain xml

<domain type='xen' id='3'>虚拟机种类:"xen", "kvm", "qemu", "lxc" and "kqemu"Id为运行虚拟机的编号。

  <name>fv0</name>一台主机下唯一。

  <uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid>全局唯一

  <title>A short description - title - of the domain</title>

  <description>Some human readable description</description>

  <metadata>用户数据。用户可以在xml中存储自己的数据。

    <app1:foo xmlns:app1="http://app1.org/app1/">..</app1:foo>

    <app2:bar xmlns:app2="http://app1.org/app2/">..</app2:bar>

  </metadata>

  <os>虚拟机boot参数设置

    <type>hvm</type>hvm表示full virtualization。

    <loader>/usr/lib/xen/boot/hvmloader</loader>

    <boot dev='hd'/>启动顺序。dev内容:"fd", "hd", "cdrom" or "network"

    <boot dev='cdrom'/>

    <bootmenu enable='yes'/>启动时是否显示用户交互界面

    <smbios mode='sysinfo'/>

    <bios useserial='yes'/>

  </os>

<vcpu placement='static' cpuset="1-4,^3,6" current="1">2</vcpu>cpu配置。1-4:物理cpu列表,虚拟cpu和这些物理cpu绑定。

Placement:static表示根据后面的定义配置cpu。Auto表示忽略自定义,系统自动配置。

  <cputune>

    <vcpupin vcpu="0" cpuset="1-4,^2"/>

    <vcpupin vcpu="1" cpuset="0,1"/>

    <vcpupin vcpu="2" cpuset="2,3"/>

    <vcpupin vcpu="3" cpuset="0,4"/>

    <shares>2048</shares>

    <period>1000000</period>

    <quota>-1</quota>

  </cputune>

  <memory unit='KiB'>524288</memory>启动时分配的内存最大值

  <currentMemory unit='KiB'>524288</currentMemory>实际分配的内存,可以小于最大值。

  <memoryBacking>页定义。不采用默认的分页大小。

    <hugepages/>

  </memoryBacking>

  <memtune>

    <hard_limit unit='G'>1</hard_limit>

    <soft_limit unit='M'>128</soft_limit>

    <swap_hard_limit unit='G'>2</swap_hard_limit>

    <min_guarantee unit='bytes'>67108864</min_guarantee>

  </memtune>

  <on_poweroff>destroy</on_poweroff>

  <on_reboot>restart</on_reboot>

  <on_crash>restart</on_crash>

  <devices>

    <disk type='file' snapshot='external'>

      <driver name="tap" type="aio" cache="default"/>

      <source file='/var/lib/xen/images/fv0' startupPolicy='optional'>

        <seclabel relabel='no'/>

      </source>

      <target dev='hda' bus='ide'/>

      <iotune>

        <total_bytes_sec>10000000</total_bytes_sec>

        <read_iops_sec>400000</read_iops_sec>

        <write_iops_sec>100000</write_iops_sec>

      </iotune>

      <boot order='2'/>

      <encryption type='...'>

        ...

      </encryption>

      <shareable/>

      <serial>

        ...

      </serial>

    </disk>

      ...

    <disk type='network'>

      <driver name="qemu" type="raw" io="threads" ioeventfd="on" event_idx="off"/>

      <source protocol="sheepdog" name="image_name">

        <host name="hostname" port="7000"/>

      </source>

      <target dev="hdb" bus="ide"/>

      <boot order='1'/>

      <transient/>

      <address type='drive' controller='0' bus='1' unit='0'/>

    </disk>

    <disk type='network'>

      <driver name="qemu" type="raw"/>

      <source protocol="rbd" name="image_name2">

        <host name="hostname" port="7000"/>

      </source>

      <target dev="hdd" bus="ide"/>

      <auth username='myuser'>

        <secret type='ceph' usage='mypassid'/>

      </auth>

    </disk>

    <disk type='block' device='cdrom'>

      <driver name='qemu' type='raw'/>

      <target dev='hdc' bus='ide' tray='open'/>

      <readonly/>

    </disk>

    <disk type='block' device='lun'>

      <driver name='qemu' type='raw'/>

      <source dev='/dev/sda'/>

      <target dev='sda' bus='scsi'/>

      <address type='drive' controller='0' bus='0' target='3' unit='0'/>

    </disk>

  </devices>

  <devices>

    <interface type='bridge'>

      <source bridge='xenbr0'/>

      <mac address='00:16:3e:5d:c7:9e'/>

      <script path='vif-bridge'/>

      <boot order='1'/>

      <rom bar='off'/>

    </interface>

  </devices>

  <devices>

    <serial type="file">

      <source path="/var/log/vm/vm-serial.log"/>

      <target port="1"/>

    </serial>

  </devices>

Disk xml

      <volume>

        <name>sparse.img</name>磁盘在pool中的唯一的名字

        <key>/var/lib/xen/images/sparse.img</key>提供全局唯一标识,自动生成,创建disk的时候不要指定

        <allocation>0</allocation>

        <capacity unit="T">1</capacity>

        <target>磁盘在本地主机上的描述信息

          <path>/var/lib/virt/images/sparse.img</path>只读。创建磁盘的时候不要指定。

          <format type='qcow2'/>分区类型或文件格式化类型

          <permissions>

            <owner>107</owner>

            <group>107</group>

            <mode>0744</mode>

            <label>virt_image_t</label>

          </permissions>

        </target>

Pool xml

这个比较简单,就不列举了。



好用的domain xml

<?xml version="1.0" encoding="UTF-8"?>
<domain type='kvm'>
  <name>test</name>
  <memory>524288</memory>
  <currentMemory>524288</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-0.14'>hvm</type>
    <boot dev='hd'/>
    <bootmenu enable='no'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='localtime'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/test.img'/><!--运行的镜像-->
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </disk>
    <interface type='network'>
      <mac address='52:54:00:19:25:7b'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>  //这个必须设置,否则建立的虚拟机运行时状态为:未知。
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'/>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </memballoon>
  </devices>
</domain>


disk xml

<?xml version="1.0" encoding="UTF-8"?>
<volume>
  <name></name>
  <key>/var/lib/libvirt/images/temp.ss</key>
  <source>
  </source>
  <capacity>8589934592</capacity>
  <allocation>139264</allocation>
  <target>
    <path>/var/lib/libvirt/images/temp.ss</path> <!--新卷的地址-->
    <format type='qcow2'/>
    <permissions>
      <mode>0600</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
   <backingStore>
    <path>/var/lib/libvirt/images/WinXP.img</path><!--关联的镜像-->
    <format type='qcow2'/>
    <permissions>
      <mode>0600</mode>
      <owner>107</owner>
      <group>107</group>
    </permissions>
  </backingStore>
</volume>


libvirt没有模板的概念,所以创建虚拟机的时候每次都要新安装操作系统和配置其他属性。

这里有一种设计理念:

1 创建出一个虚拟机,并完成所有配置。操作系统安装到一个磁盘中。

2 将虚拟机停止,虚拟机的xml和磁盘作为模板。

3 如果通过此模板创建虚拟机,则利用此xml和磁盘创建。拷贝磁盘到一个新的路径,并在xml中指定。通过此xml创建虚拟机。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值