vm.py 7.29 KB
Newer Older
tarokkk committed
1 2 3 4 5 6 7 8
#!/usr/bin/env python
import lxml.etree as ET

# VM Instance class


class VMInstance:
    name = None
tarokkk committed
9 10 11
    arch = None
    vm_type = None
    os_boot = None
tarokkk committed
12 13 14 15 16
    vcpu = None
    cpu_share = None
    memory_max = None
    network_list = list()
    disk_list = list()
17 18
    graphics = dict
    context = dict
tarokkk committed
19 20 21 22 23

    def __init__(self,
                 name,
                 vcpu,
                 memory_max,
tarokkk committed
24 25 26 27 28 29
                 cpu_share="100",
                 arch="x86_64",
                 os_boot="hd",
                 vm_type="kvm",
                 network_list=None,
                 disk_list=None,
30 31 32
                 context=None,
                 graphics=None,
                 acpi=True):
tarokkk committed
33
        '''Default Virtual Machine constructor
34 35 36 37 38 39 40 41 42 43 44 45
        name    - unique name for the instance
        vcpu    - nubmer of processors
        memory_max  - maximum virtual memory (actual memory maybe add late)
        cpu_share   - KVM process priority (0-100)
        arch        - libvirt arch parameter default x86_64
        os_boot     - boot device default hd
        vm_type     - hypervisor type default kvm
        network_list    - VMNetwork list
        disk_list   - VMDIsk list
        context  -   Key-Value pars (not used)
        graphics    - Dict that keys are: type, listen, port, passwd
        acpi        - True/False to enable acpi
tarokkk committed
46 47 48 49 50
        '''
        self.name = name
        self.vcpu = vcpu
        self.cpu_share = cpu_share
        self.memory_max = memory_max
tarokkk committed
51 52 53
        self.arch = arch
        self.os_boot = os_boot
        self.vm_type = vm_type
tarokkk committed
54 55 56
        self.network_list = network_list
        self.disk_list = disk_list
        self.conext = context
57 58
        self.graphics = graphics
        self.acpi = acpi
tarokkk committed
59

tarokkk committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    def build_xml(self):
        '''Return the root Element Tree object
        '''
        ET.register_namespace(
            'qemu', 'http://libvirt.org/schemas/domain/qemu/1.0')
        xml_top = ET.Element(
            'domain',
            attrib={
                'type': self.vm_type
            })
        # Basic virtual machine paramaters
        ET.SubElement(xml_top, 'name').text = self.name
        ET.SubElement(xml_top, 'vcpu').text = self.vcpu
        ET.SubElement(xml_top, 'memory').text = self.memory_max
        # Cpu tune
        cputune = ET.SubElement(xml_top, 'cputune')
        ET.SubElement(cputune, 'shares').text = self.cpu_share
        # Os specific options
        os = ET.SubElement(xml_top, 'os')
        ET.SubElement(os, 'type', attrib={'arch': self.arch}).text = "hvm"
        ET.SubElement(os, 'boot', attrib={'dev': self.os_boot})
        # Devices
        devices = ET.SubElement(xml_top, 'devices')
        ET.SubElement(devices, 'emulator').text = '/usr/bin/kvm'
        for disk in self.disk_list:
            devices.append(disk.build_xml())
        for network in self.network_list:
            devices.append(network.build_xml())
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        # Console/graphics section
        if self.graphics is not None:
            ET.SubElement(devices,
                          'graphics',
                          attrib={
                              'type': self.graphics['type'],
                              'listen': self.graphics['listen'],
                              'port': self.graphics['port'],
                              'passwd': self.graphics['passwd'],
                          })
        # Features
        features = ET.SubElement(xml_top, 'features')
        if self.acpi:
            ET.SubElement(features, 'acpi')
tarokkk committed
102 103 104 105 106 107 108 109
        return xml_top

    def dump_xml(self):
        return ET.tostring(self.build_xml(),
                           encoding='utf8',
                           method='xml',
                           pretty_print=True)

tarokkk committed
110 111

class VMDisk:
tarokkk committed
112

tarokkk committed
113 114 115 116 117 118 119 120 121
    '''Virtual MAchine disk representing class
    '''
    name = None
    source = None
    disk_type = None
    disk_device = None
    driver_name = None
    driver_type = None
    driver_cache = None
tarokkk committed
122
    target_device = None
tarokkk committed
123 124 125 126 127 128 129 130

    def __init__(self,
                 name,
                 source,
                 disk_type="file",
                 disk_device="disk",
                 driver_name="qemu",
                 driver_type="qcow2",
131
                 driver_cache="default",
132
                 target_device="vda"):
tarokkk committed
133 134 135 136 137 138 139
        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
tarokkk committed
140
        self.target_device = target_device
tarokkk committed
141

142
    def build_xml(self):
tarokkk committed
143 144 145 146 147
        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})
tarokkk committed
148 149 150 151 152 153 154 155
        ET.SubElement(xml_top, 'target',
                      attrib={'dev': self.target_device})
        ET.SubElement(xml_top, 'driver',
                      attrib={
                          'name': self.driver_name,
                          'type': self.driver_type,
                          'cache': self.driver_cache})
        return xml_top
tarokkk committed
156

157 158 159 160 161 162
    def dump_xml(self):
        return ET.tostring(self.build_xml(),
                           encoding='utf8',
                           method='xml',
                           pretty_print=True)

tarokkk committed
163 164

class VMNetwork:
165

tarokkk committed
166 167
    ''' Virtual Machine network representing class
    name            -- network device name
tarokkk committed
168
    bridge          -- bridg for the port
tarokkk committed
169
    mac             -- the MAC address of the quest interface
170 171
    ipv4            -- the IPv4 address of virtual machine (Flow control)
    ipv6            -- the IPv6 address of virtual machine (Flow controlo)
tarokkk committed
172
    vlan            -- Port VLAN configuration
tarokkk committed
173 174 175 176 177 178 179 180
    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
tarokkk committed
181
    bridge = None
tarokkk committed
182 183 184 185 186 187
    network_type = None
    mac = None
    model = None
    QoS = None
    script_exec = '/bin/true'
    comment = None
tarokkk committed
188
    vlan = 0
189 190
    ipv4 = None
    ipv6 = None
tarokkk committed
191 192 193

    def __init__(self,
                 name,
tarokkk committed
194
                 bridge,
tarokkk committed
195
                 mac,
196 197
                 ipv4=None,
                 ipv6=None,
tarokkk committed
198 199
                 network_type='ethernet',
                 model='virtio',
tarokkk committed
200 201
                 QoS=None,
                 vlan=0):
tarokkk committed
202
        self.name = name
tarokkk committed
203
        self.bridge = bridge
tarokkk committed
204 205
        self.network_type = network_type
        self.mac = mac
206 207
        self.ipv4 = ipv4
        self.ipv6 = ipv6
tarokkk committed
208 209
        self.model = model
        self.QoS = QoS
tarokkk committed
210
        self.vlan = vlan
tarokkk committed
211 212

    # XML dump
213
    def build_xml(self):
tarokkk committed
214 215 216 217 218
        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})
219
        ET.SubElement(xml_top, 'rom', attrib={'bar': 'off'})
tarokkk committed
220
        return xml_top
221 222 223 224

    def dump_xml(self):
        return ET.tostring(self.build_xml(), encoding='utf8',
                           method='xml', pretty_print=True)