Python libvirt API-创建虚拟机 [英] Python libvirt API - create a Virtual Machine

查看:933
本文介绍了Python libvirt API-创建虚拟机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个python脚本来处理基本的VM操作,例如:创建VM,删除VM,启动,停止等.

I am trying to create a python script to handle basic VM operations like: create a VM, delete a VM, start, stop, etc.

目前,我宁可卡在" create

Currently I'm rather "stuck" on create

在命令行中,您将执行以下操作:

From the command line you would do something like:

qemu-img create -f qcow2 vdisk.img <size>
virt-install --virt-type kvm --name testVM --ram 1024
 --cdrom=ubuntu.iso  --disk /path/to/virtual/drive,size=10,format=qcow2
 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
 --os-type=linux

这将创建一个名为testVM的新VM,并将其安装在先前定义的vdisk.img

And that will create a new VM called testVM and install it on the previously defined vdisk.img

但是我想用python来完成这一切;我知道如何处理第二部分:

But I want to do this all in python; I know how to handle the second part:

  1. 从VM的XML模板开始
  2. 打开libvirt连接并使用连接处理程序创建VM

  1. start with an XML template for the VM
  2. open a libvirt connection and use the connection handler to create the VM

但是我想知道第一部分,您必须在其中创建虚拟磁盘.

But I'm wondering about the first part, where you have to create the virtual disk.

您可以使用任何libvirt API calls吗?

或者,您必须对qemu-img create进行系统调用才能创建虚拟磁盘?

OR, you have to put in a system call to qemu-img create to create the virtual disk?

推荐答案

我终于找到并回答了我的问题-因此,如果有人遇到相同的问题,我将在此处发布解决方案.

I finally found and answer to my problems- so I'm posting the solution here in case anyone ever hits the same problem.

libvirt连接对象可以与存储池一起使用.

The libvirt connection object can work with storage pools.

在libvirt.org中:"存储池是管理员(通常是专门的存储管理员)为虚拟机使用而预留的存储量.存储池按存储分为存储卷管理员或系统管理员,然后将卷作为块设备分配给VM."

From the libvirt.org: "A storage pool is a quantity of storage set aside by an administrator, often a dedicated storage administrator, for use by virtual machines. Storage pools are divided into storage volumes either by the storage administrator or the system administrator, and the volumes are assigned to VMs as block devices."

基本上,卷是quemu-img create创建的.在同一目录中创建所有.img(使用qemu-img创建的)文件的存储池后;用qemu-img创建的文件被视为卷.

Basically a volume is what quemu-img create creates. Once you create a storage pool in the same directory where all the .img (created using qemu-img) files are; the files created with qemu-img are seen as volumes.

以下代码将列出所有现有卷,包括使用qemu-img

The following code will list all existing volumes, including the ones created with qemu-img

conn = libvirt.open()

pools = conn.listAllStoragePools(0)

for pool in pools:              

    #check if pool is active
    if pool.isActive() == 0:
        #activate pool
        pool.create()

    stgvols = pool.listVolumes()
    print('Storage pool: '+pool.name())
    for stgvol in stgvols :
        print('  Storage vol: '+stgvol)

创建存储池:

def createStoragePool(conn):        
        xmlDesc = """
        <pool type='dir'>
          <name>guest_images_storage_pool</name>
          <uuid>8c79f996-cb2a-d24d-9822-ac7547ab2d01</uuid>
          <capacity unit='bytes'>4306780815</capacity>
          <allocation unit='bytes'>237457858</allocation>
          <available unit='bytes'>4069322956</available>
          <source>
          </source>
          <target>
            <path>/path/to/guest_images</path>
            <permissions>
              <mode>0755</mode>
              <owner>-1</owner>
              <group>-1</group>
            </permissions>
          </target>
        </pool>"""


        pool = conn.storagePoolDefineXML(xmlDesc, 0)

        #set storage pool autostart     
        pool.setAutostart(1)
        return pool

创建卷:

def createStoragePoolVolume(pool, name):    
        stpVolXml = """
        <volume>
          <name>"""+name+""".img</name>
          <allocation>0</allocation>
          <capacity unit="G">10</capacity>
          <target>
            <path>/path/to/guest_images/"""+name+""".img</path>
            <permissions>
              <owner>107</owner>
              <group>107</group>
              <mode>0744</mode>
              <label>virt_image_t</label>
            </permissions>
          </target>
        </volume>"""

        stpVol = pool.createXML(stpVolXml, 0)   
        return stpVol

这篇关于Python libvirt API-创建虚拟机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆