Exemple #1
0
    def testDisks(self):
        """Generate XML from a VM and verify the disk specifications"""
        test_vm = vm.VM(
            name='something',
            memory=1000000000,
            disks=['/home/ebroder/block-dev', '/home/ebroder/file.img'],
            network='debmarshal-1',
            mac='AA:BB:CC:DD:EE:FF',
            arch='x86_64',
            extra={})

        self.mox.StubOutWithMock(utils, 'diskIsBlockDevice')
        utils.diskIsBlockDevice('/home/ebroder/block-dev').AndReturn(True)
        utils.diskIsBlockDevice('/home/ebroder/file.img').AndReturn(False)

        self.mox.ReplayAll()

        xml = base.Hypervisor.domainXML(test_vm)

        self.assertEqual(len(xml.xpath('/domain/devices/disk')), 2)
        disk = xml.xpath('/domain/devices/disk[target/@dev="hda"]')[0]
        self.assertEqual(disk.xpath('string(@type)'), 'block')
        self.assertEqual(len(disk.xpath('source')), 1)
        self.assertEqual(disk.xpath('string(source/@dev)'), test_vm.disks[0])

        disk = xml.xpath('/domain/devices/disk[target/@dev="hdb"]')[0]
        self.assertEqual(len(disk.xpath('source')), 1)
        self.assertEqual(disk.xpath('string(@type)'), 'file')
        self.assertEqual(len(disk.xpath('source')), 1)
        self.assertEqual(disk.xpath('string(source/@file)'), test_vm.disks[1])
Exemple #2
0
  def testDisks(self):
    """Generate XML from a VM and verify the disk specifications"""
    test_vm = vm.VM(name='something',
                    memory=1000000000,
                    disks=['/home/ebroder/block-dev',
                           '/home/ebroder/file.img'],
                    network='debmarshal-1',
                    mac='AA:BB:CC:DD:EE:FF',
                    arch='x86_64',
                    extra={})

    self.mox.StubOutWithMock(utils, 'diskIsBlockDevice')
    utils.diskIsBlockDevice('/home/ebroder/block-dev').AndReturn(True)
    utils.diskIsBlockDevice('/home/ebroder/file.img').AndReturn(False)

    self.mox.ReplayAll()

    xml = base.Hypervisor.domainXML(test_vm)

    self.assertEqual(len(xml.xpath('/domain/devices/disk')), 2)
    disk = xml.xpath('/domain/devices/disk[target/@dev="hda"]')[0]
    self.assertEqual(disk.xpath('string(@type)'), 'block')
    self.assertEqual(len(disk.xpath('source')), 1)
    self.assertEqual(disk.xpath('string(source/@dev)'), test_vm.disks[0])

    disk = xml.xpath('/domain/devices/disk[target/@dev="hdb"]')[0]
    self.assertEqual(len(disk.xpath('source')), 1)
    self.assertEqual(disk.xpath('string(@type)'), 'file')
    self.assertEqual(len(disk.xpath('source')), 1)
    self.assertEqual(disk.xpath('string(source/@file)'), test_vm.disks[1])
Exemple #3
0
  def testFile(self):
    self.mox.StubOutWithMock(os, 'stat')
    os.stat('/home/ebroder/root.img').AndReturn(posix.stat_result([
      0100644, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

    self.mox.ReplayAll()

    self.assertEqual(utils.diskIsBlockDevice('/home/ebroder/root.img'),
                     False)
    def testFile(self):
        self.mox.StubOutWithMock(os, 'stat')
        os.stat('/home/ebroder/root.img').AndReturn(
            posix.stat_result([0100644, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

        self.mox.ReplayAll()

        self.assertEqual(utils.diskIsBlockDevice('/home/ebroder/root.img'),
                         False)
Exemple #5
0
  def testBlockDevice(self):
    self.mox.StubOutWithMock(os, 'stat')
    os.stat('/home/ebroder/root.img').AndReturn(posix.stat_result([
      # st_mode is the first argument
      060644, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

    self.mox.ReplayAll()

    self.assertEqual(utils.diskIsBlockDevice('/home/ebroder/root.img'),
                     True)
Exemple #6
0
  def domainXML(cls, vm):
    """Generate the XML to pass to libvirt to create a new domain.

    This will generate the portions of the XML that are common to all
    hypervisors; subclasses of Hypervisor will add on elements and
    properties to the XML tree returned from their superclass.

    Args:
      vm: an instance of debmarshal.vm.VM with the parameters for the
        VM

    Returns:
      The XML specification for the domain specified by vm as an
        lxml.etree.Element object
    """
    xml = etree.Element('domain')
    etree.SubElement(xml, 'name').text = vm.name
    etree.SubElement(xml, 'memory').text = str(vm.memory)

    xml_os = etree.SubElement(xml, 'os')
    if 'kernel' in vm.extra:
      etree.SubElement(xml_os, 'kernel').text = vm.extra['kernel']
      etree.SubElement(xml_os, 'initrd').text = vm.extra['initrd']
      if vm.extra.get('cmdline'):
        etree.SubElement(xml_os, 'cmdline').text = vm.extra['cmdline']

    if 'on_poweroff' in vm.extra:
      etree.SubElement(xml, 'on_poweroff').text = vm.extra['on_poweroff']
    if 'on_reboot' in vm.extra:
      etree.SubElement(xml, 'on_reboot').text = vm.extra['on_reboot']

    devices = etree.SubElement(xml, 'devices')
    for disk_num, disk in enumerate(vm.disks):
      xml_disk = etree.SubElement(devices, 'disk')

      if utils.diskIsBlockDevice(disk):
        xml_disk.set('type', 'block')
        etree.SubElement(xml_disk, 'source', dev=disk)
      else:
        xml_disk.set('type', 'file')
        etree.SubElement(xml_disk, 'source', file=disk)

      disk_letter = chr(disk_num + ord('a'))
      etree.SubElement(xml_disk, 'target', dev='hd%s' % disk_letter)

    xml_net = etree.SubElement(devices, 'interface', type='network')
    etree.SubElement(xml_net, 'source', network=vm.network)
    etree.SubElement(xml_net, 'mac', address=vm.mac)

    return xml
Exemple #7
0
    def domainXML(cls, vm):
        """Generate the XML to pass to libvirt to create a new domain.

    This will generate the portions of the XML that are common to all
    hypervisors; subclasses of Hypervisor will add on elements and
    properties to the XML tree returned from their superclass.

    Args:
      vm: an instance of debmarshal.vm.VM with the parameters for the
        VM

    Returns:
      The XML specification for the domain specified by vm as an
        lxml.etree.Element object
    """
        xml = etree.Element('domain')
        etree.SubElement(xml, 'name').text = vm.name
        etree.SubElement(xml, 'memory').text = str(vm.memory)

        xml_os = etree.SubElement(xml, 'os')
        if 'kernel' in vm.extra:
            etree.SubElement(xml_os, 'kernel').text = vm.extra['kernel']
            etree.SubElement(xml_os, 'initrd').text = vm.extra['initrd']
            if vm.extra.get('cmdline'):
                etree.SubElement(xml_os, 'cmdline').text = vm.extra['cmdline']

        if 'on_poweroff' in vm.extra:
            etree.SubElement(xml, 'on_poweroff').text = vm.extra['on_poweroff']
        if 'on_reboot' in vm.extra:
            etree.SubElement(xml, 'on_reboot').text = vm.extra['on_reboot']

        devices = etree.SubElement(xml, 'devices')
        for disk_num, disk in enumerate(vm.disks):
            xml_disk = etree.SubElement(devices, 'disk')

            if utils.diskIsBlockDevice(disk):
                xml_disk.set('type', 'block')
                etree.SubElement(xml_disk, 'source', dev=disk)
            else:
                xml_disk.set('type', 'file')
                etree.SubElement(xml_disk, 'source', file=disk)

            disk_letter = chr(disk_num + ord('a'))
            etree.SubElement(xml_disk, 'target', dev='hd%s' % disk_letter)

        xml_net = etree.SubElement(devices, 'interface', type='network')
        etree.SubElement(xml_net, 'source', network=vm.network)
        etree.SubElement(xml_net, 'mac', address=vm.mac)

        return xml
    def testBlockDevice(self):
        self.mox.StubOutWithMock(os, 'stat')
        os.stat('/home/ebroder/root.img').AndReturn(
            posix.stat_result([
                # st_mode is the first argument
                060644,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            ]))

        self.mox.ReplayAll()

        self.assertEqual(utils.diskIsBlockDevice('/home/ebroder/root.img'),
                         True)