vm.py 7.3 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 12
    arch = None
    vm_type = None
    arch = None
    os_boot = None
tarokkk committed
13 14 15 16 17
    vcpu = None
    cpu_share = None
    memory_max = None
    network_list = list()
    disk_list = list()
18 19
    graphics = dict
    context = dict
tarokkk committed
20 21 22 23 24

    def __init__(self,
                 name,
                 vcpu,
                 memory_max,
tarokkk committed
25 26 27 28 29 30
                 cpu_share="100",
                 arch="x86_64",
                 os_boot="hd",
                 vm_type="kvm",
                 network_list=None,
                 disk_list=None,
31 32 33
                 context=None,
                 graphics=None,
                 acpi=True):
tarokkk committed
34
        '''Default Virtual Machine constructor
35 36 37 38 39 40 41 42 43 44 45 46
        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
47 48 49 50 51
        '''
        self.name = name
        self.vcpu = vcpu
        self.cpu_share = cpu_share
        self.memory_max = memory_max
tarokkk committed
52 53 54
        self.arch = arch
        self.os_boot = os_boot
        self.vm_type = vm_type
tarokkk committed
55 56 57
        self.network_list = network_list
        self.disk_list = disk_list
        self.conext = context
58 59
        self.graphics = graphics
        self.acpi = acpi
tarokkk committed
60

tarokkk committed
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 88
    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())
89 90 91 92 93 94 95 96 97 98 99 100 101 102
        # 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
103 104 105 106 107 108 109 110
        return xml_top

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

tarokkk committed
111 112

class VMDisk:
tarokkk committed
113

tarokkk committed
114 115 116 117 118 119 120 121 122
    '''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
123
    target_device = None
tarokkk committed
124 125 126 127 128 129 130 131

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

143
    def build_xml(self):
tarokkk committed
144 145 146 147 148
        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
149 150 151 152 153 154 155 156
        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
157

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

tarokkk committed
164 165

class VMNetwork:
166

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

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

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

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