Commit 83d949f1 by tarokkk

initial commit

parents
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>one-2273</name>
<vcpu>1</vcpu>
<cputune>
<shares>512</shares>
</cputune>
<memory>1048576</memory>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
</os>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'>
<source file='/datastore/0/2273/disk.0'/>
<target dev='vda'/>
<driver name='qemu' type='qcow2' cache='default'/>
</disk>
<disk type='file' device='cdrom'>
<source file='/datastore/0/2273/disk.1'/>
<target dev='hda'/>
<readonly/>
<driver name='qemu' type='raw'/>
</disk>
<interface type='ethernet'>
<target dev='nw-2273-0'/>
<mac address='02:00:0a:09:01:2d'/>
<model type='virtio'/>
</interface>
<graphics type='vnc' listen='0.0.0.0' port='8173' passwd='usxdfmnkfk'/>
</devices>
<features>
<acpi/>
</features>
<cpu><topology sockets='1' cores='1' threads='1'/></cpu>
</domain>
import libvirt
import sys
# Open libvirt connection (local)
connection = libvirt.open(None)
# XML sample for testing
xml_sample = '''
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>one-2273</name>
<vcpu>1</vcpu>
<cputune>
<shares>512</shares>
</cputune>
<memory>1048576</memory>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
</os>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'>
<source file='/datastore/0/2273/disk.0'/>
<target dev='vda'/>
<driver name='qemu' type='qcow2' cache='default'/>
</disk>
<disk type='file' device='cdrom'>
<source file='/datastore/0/2273/disk.1'/>
<target dev='hda'/>
<readonly/>
<driver name='qemu' type='raw'/>
</disk>
<interface type='bridge'>
<source bridge='cloud'/>
<mac address='02:00:0a:09:01:2d'/>
<model type='virtio'/>
</interface>
<graphics type='vnc' listen='0.0.0.0' port='8173' passwd='usxdfmnkfk'/>
</devices>
<features>
<acpi/>
</features>
<cpu><topology sockets='1' cores='1' threads='1'/></cpu>
</domain>
'''
if connection == None:
print "Fail to connect to libvirt daemon."
sys.exit(1)
try
names = connection.listDefinedDomains()
print names
connection.defineXML(xml_sample)
print connection.listDefinedDomains()
# conn.undefineXML()
#!/usr/bin/env python
import lxml.etree as ET
# VM Instance class
class VMInstance:
name = None
vcpu = None
cpu_share = None
memory_max = None
network_list = list()
disk_list = list()
context = dict()
def __init__(self,
name,
vcpu,
cpu_share,
memory_max,
network_list,
disk_list,
context):
'''Default Virtual Machine constructor
'''
self.name = name
self.vcpu = vcpu
self.cpu_share = cpu_share
self.memory_max = memory_max
self.network_list = network_list
self.disk_list = disk_list
self.conext = context
class VMDisk:
'''Virtual MAchine disk representing class
'''
name = None
source = None
disk_type = None
disk_device = None
driver_name = None
driver_type = None
driver_cache = None
def __init__(self,
name,
source,
disk_type="file",
disk_device="disk",
driver_name="qemu",
driver_type="qcow2",
driver_cache="normal"):
self.name = name
self.source = source
self.disk_type = disk_type
self.disk_device = disk_device
self.driver_name = driver_name
self.driver_type = driver_type
self.driver_cache = driver_cache
def dump_xml(self):
xml_top = ET.Element('disk',
attrib={'type': self.disk_type,
'device': self.disk_device})
ET.SubElement(xml_top, 'source',
attrib={self.disk_type: self.source})
class VMNetwork:
''' Virtual Machine network representing class
name -- network device name
mac -- the MAC address of the quest interface
network_type -- need to be "ethernet" by default
model -- available models in libvirt
QoS -- CIRCLE QoS class?
comment -- Any comment
script -- Executable network script /bin/true by default
'''
# Class attributes
name = None
network_type = None
mac = None
model = None
QoS = None
script_exec = '/bin/true'
comment = None
def __init__(self,
name,
mac,
network_type='ethernet',
model='virtio',
QoS=None):
self.name = name
self.network_type = network_type
self.mac = mac
self.model = model
self.QoS = QoS
# XML dump
def dump_xml(self):
xml_top = ET.Element('interface', attrib={'type': self.network_type})
ET.SubElement(xml_top, 'target', attrib={'dev': self.name})
ET.SubElement(xml_top, 'mac', attrib={'address': self.mac})
ET.SubElement(xml_top, 'model', attrib={'type': self.model})
ET.SubElement(xml_top, 'script', attrib={'path': self.script_exec})
return ET.tostring(xml_top,
encoding='utf8',
method='xml',
pretty_print=True)
a = VMNetwork(name="vm-77", mac="010101")
print a.dump_xml()
import xml.etree.ElementTree as ET
top = ET.Element('domain',attrib={'type':'kvm', 'xmlns:qemu':'http://libvirt.org/schemas/domain/qemu/1.0'})
cpu = ET.SubElement(top,'cpu')
#vcpu =
#cputune =
#devices =
xml_dump = ET.tostring(top)
print xml_dump
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment