direct-io.hg

changeset 13595:959e79bfe913

Fix device reordering that occurs when the config gets read into dictionaries.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Jan 24 16:48:41 2007 +0000 (2007-01-24)
parents 30af6cfdb05c
children 2932d0e7c554
files tools/python/xen/xend/XendConfig.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/image.py tools/python/xen/xend/tests/test_XendConfig.py
line diff
     1.1 --- a/tools/python/xen/xend/XendConfig.py	Wed Jan 24 16:33:19 2007 +0000
     1.2 +++ b/tools/python/xen/xend/XendConfig.py	Wed Jan 24 16:48:41 2007 +0000
     1.3 @@ -654,19 +654,11 @@ class XendConfig(dict):
     1.4  
     1.5          # set device references in the configuration
     1.6          self['devices'] = cfg.get('devices', {})
     1.7 -        
     1.8 -        self['console_refs'] = []
     1.9 -        self['vif_refs'] = []
    1.10 -        self['vbd_refs'] = []
    1.11 -        self['vtpm_refs'] = []
    1.12 -        for dev_uuid, (dev_type, dev_info) in self['devices'].items():
    1.13 -            if dev_type == 'vif':
    1.14 -                self['vif_refs'].append(dev_uuid)
    1.15 -            elif dev_type in ('vbd','tap'):
    1.16 -                self['vbd_refs'].append(dev_uuid)
    1.17 -            elif dev_type in ('vtpm',):
    1.18 -                self['vtpm_refs'].append(dev_uuid)
    1.19 -        
    1.20 +        self['console_refs'] = cfg.get('console_refs', [])
    1.21 +        self['vif_refs'] = cfg.get('vif_refs', [])
    1.22 +        self['vbd_refs'] = cfg.get('vbd_refs', [])
    1.23 +        self['vtpm_refs'] = cfg.get('vtpm_refs', [])
    1.24 +
    1.25  
    1.26      def _sxp_to_xapi_unsupported(self, sxp_cfg):
    1.27          """Read in an SXP configuration object and populate
    1.28 @@ -989,8 +981,8 @@ class XendConfig(dict):
    1.29                  
    1.30                  dev_uuid = cfg_xenapi.get('uuid', uuid.createString())
    1.31                  dev_info['uuid'] = dev_uuid
    1.32 -                self['devices'][dev_uuid] = (dev_type, dev_info)
    1.33 -                self['vif_refs'].append(dev_uuid)
    1.34 +                target['devices'][dev_uuid] = (dev_type, dev_info)
    1.35 +                target['vif_refs'].append(dev_uuid)
    1.36                  return dev_uuid
    1.37              
    1.38              elif dev_type in ('vbd', 'tap'):
    1.39 @@ -1013,8 +1005,8 @@ class XendConfig(dict):
    1.40  
    1.41                  dev_uuid = cfg_xenapi.get('uuid', uuid.createString())
    1.42                  dev_info['uuid'] = dev_uuid
    1.43 -                self['devices'][dev_uuid] = (dev_type, dev_info)
    1.44 -                self['vbd_refs'].append(dev_uuid)                
    1.45 +                target['devices'][dev_uuid] = (dev_type, dev_info)
    1.46 +                target['vbd_refs'].append(dev_uuid)                
    1.47                  return dev_uuid
    1.48  
    1.49              elif dev_type == 'vtpm':
    1.50 @@ -1023,8 +1015,8 @@ class XendConfig(dict):
    1.51  
    1.52                  dev_uuid = cfg_xenapi.get('uuid', uuid.createString())
    1.53                  dev_info['uuid'] = dev_uuid
    1.54 -                self['devices'][dev_uuid] = (dev_type, dev_info)
    1.55 -                self['vtpm_refs'].append(dev_uuid)
    1.56 +                target['devices'][dev_uuid] = (dev_type, dev_info)
    1.57 +                target['vtpm_refs'].append(dev_uuid)
    1.58                  return dev_uuid
    1.59  
    1.60          return ''
    1.61 @@ -1073,6 +1065,14 @@ class XendConfig(dict):
    1.62  
    1.63          return sxpr
    1.64  
    1.65 +    def ordered_device_refs(self):
    1.66 +        result = (self['console_refs'] +
    1.67 +                  self['vbd_refs'] +
    1.68 +                  self['vif_refs'] +
    1.69 +                  self['vtpm_refs'])
    1.70 +        result.extend([u for u in self['devices'].keys() if u not in result])
    1.71 +        return result
    1.72 +
    1.73      def all_devices_sxpr(self):
    1.74          """Returns the SXPR for all devices in the current configuration."""
    1.75          sxprs = []
    1.76 @@ -1081,7 +1081,9 @@ class XendConfig(dict):
    1.77          if 'devices' not in self:
    1.78              return sxprs
    1.79          
    1.80 -        for dev_type, dev_info in self['devices'].values():
    1.81 +        ordered_refs = self.ordered_device_refs()
    1.82 +        for dev_uuid in ordered_refs:
    1.83 +            dev_type, dev_info = self['devices'][dev_uuid]
    1.84              if dev_type == 'pci': # special case for pci devices
    1.85                  sxpr = [['uuid', dev_info['uuid']]]
    1.86                  for pci_dev_info in dev_info['devs']:
     2.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Wed Jan 24 16:33:19 2007 +0000
     2.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Wed Jan 24 16:48:41 2007 +0000
     2.3 @@ -1216,8 +1216,10 @@ class XendDomainInfo:
     2.4  
     2.5          @raise: VmError for invalid devices
     2.6          """
     2.7 -        for (devclass, config) in self.info.get('devices', {}).values():
     2.8 -            if devclass in XendDevices.valid_devices():            
     2.9 +        ordered_refs = self.info.ordered_device_refs()
    2.10 +        for dev_uuid in ordered_refs:
    2.11 +            devclass, config = self.info['devices'][dev_uuid]
    2.12 +            if devclass in XendDevices.valid_devices():
    2.13                  log.info("createDevice: %s : %s" % (devclass, scrub_password(config)))
    2.14                  dev_uuid = config.get('uuid')
    2.15                  devid = self._createDevice(devclass, config)
     3.1 --- a/tools/python/xen/xend/image.py	Wed Jan 24 16:33:19 2007 +0000
     3.2 +++ b/tools/python/xen/xend/image.py	Wed Jan 24 16:48:41 2007 +0000
     3.3 @@ -325,7 +325,7 @@ class HVMImageHandler(ImageHandler):
     3.4              raise VmError("HVM guest support is unavailable: is VT/AMD-V "
     3.5                            "supported by your CPU and enabled in your BIOS?")
     3.6  
     3.7 -        self.dmargs = self.parseDeviceModelArgs(imageConfig, deviceConfig)
     3.8 +        self.dmargs = self.parseDeviceModelArgs(vmConfig)
     3.9          self.device_model = imageConfig['hvm'].get('device_model')
    3.10          if not self.device_model:
    3.11              raise VmError("hvm: missing device model")
    3.12 @@ -375,11 +375,11 @@ class HVMImageHandler(ImageHandler):
    3.13  
    3.14      # Return a list of cmd line args to the device models based on the
    3.15      # xm config file
    3.16 -    def parseDeviceModelArgs(self, imageConfig, deviceConfig):
    3.17 +    def parseDeviceModelArgs(self, vmConfig):
    3.18          dmargs = [ 'boot', 'fda', 'fdb', 'soundhw',
    3.19                     'localtime', 'serial', 'stdvga', 'isa',
    3.20                     'acpi', 'usb', 'usbdevice', 'keymap' ]
    3.21 -        hvmDeviceConfig = imageConfig['hvm']['devices']
    3.22 +        hvmDeviceConfig = vmConfig['image']['hvm']['devices']
    3.23  
    3.24          ret = ['-vcpus', str(self.vm.getVCpuCount())]
    3.25  
    3.26 @@ -410,29 +410,32 @@ class HVMImageHandler(ImageHandler):
    3.27          ret = ret + ["-domain-name", str(self.vm.info['name_label'])]
    3.28          nics = 0
    3.29          
    3.30 -        for devuuid, (devtype, devinfo) in deviceConfig.items():
    3.31 -            if devtype == 'vbd':
    3.32 -                uname = devinfo.get('uname')
    3.33 -                if uname is not None and 'file:' in uname:
    3.34 -                    (_, vbdparam) = string.split(uname, ':', 1)
    3.35 -                    if not os.path.isfile(vbdparam):
    3.36 -                        raise VmError('Disk image does not exist: %s' %
    3.37 -                                      vbdparam)
    3.38 -            if devtype == 'vif':
    3.39 -                dtype = devinfo.get('type', 'ioemu')
    3.40 -                if dtype != 'ioemu':
    3.41 -                    continue
    3.42 -                nics += 1
    3.43 -                mac = devinfo.get('mac')
    3.44 -                if mac == None:
    3.45 -                    mac = randomMAC()
    3.46 -                bridge = devinfo.get('bridge', 'xenbr0')
    3.47 -                model = devinfo.get('model', 'rtl8139')
    3.48 -                ret.append("-net")
    3.49 -                ret.append("nic,vlan=%d,macaddr=%s,model=%s" %
    3.50 -                           (nics, mac, model))
    3.51 -                ret.append("-net")
    3.52 -                ret.append("tap,vlan=%d,bridge=%s" % (nics, bridge))
    3.53 +        for devuuid in vmConfig['vbd_refs']:
    3.54 +            devinfo = vmConfig['devices'][devuuid][1]
    3.55 +            uname = devinfo.get('uname')
    3.56 +            if uname is not None and 'file:' in uname:
    3.57 +                (_, vbdparam) = string.split(uname, ':', 1)
    3.58 +                if not os.path.isfile(vbdparam):
    3.59 +                    raise VmError('Disk image does not exist: %s' %
    3.60 +                                  vbdparam)
    3.61 +
    3.62 +        for devuuid in vmConfig['vif_refs']:
    3.63 +            devinfo = vmConfig['devices'][devuuid][1]
    3.64 +            dtype = devinfo.get('type', 'ioemu')
    3.65 +            if dtype != 'ioemu':
    3.66 +                continue
    3.67 +            nics += 1
    3.68 +            mac = devinfo.get('mac')
    3.69 +            if mac is None:
    3.70 +                mac = randomMAC()
    3.71 +            bridge = devinfo.get('bridge', 'xenbr0')
    3.72 +            model = devinfo.get('model', 'rtl8139')
    3.73 +            ret.append("-net")
    3.74 +            ret.append("nic,vlan=%d,macaddr=%s,model=%s" %
    3.75 +                       (nics, mac, model))
    3.76 +            ret.append("-net")
    3.77 +            ret.append("tap,vlan=%d,bridge=%s" % (nics, bridge))
    3.78 +
    3.79          return ret
    3.80  
    3.81      def configVNC(self, imageConfig):
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/python/xen/xend/tests/test_XendConfig.py	Wed Jan 24 16:48:41 2007 +0000
     4.3 @@ -0,0 +1,42 @@
     4.4 +import unittest
     4.5 +
     4.6 +import xen.xend.XendConfig as XendConfig
     4.7 +
     4.8 +
     4.9 +class test_XendConfig(unittest.TestCase):
    4.10 +
    4.11 +    def testParseFromSXP(self):
    4.12 +        cfg = XendConfig.XendConfig(
    4.13 +            sxp_obj = (
    4.14 +            ['vm',
    4.15 +             ['bootloader_args', '-q --default_args="root=/dev/sda1 ro" --extra_args="quiet" /images/VM1.sda'],
    4.16 +                       ['bootloader', '/usr/bin/pygrub'],
    4.17 +             ['device', ['vif', ['mac', '00:16:3E:4C:D1:00'], ['script', 'vif-bridge'], ['bridge', 'xenbr0']]],
    4.18 +             ['device', ['vif', ['mac', '00:16:3E:48:56:26'], ['script', 'vif-bridge'], ['bridge', 'vbridge0']]],
    4.19 +             ['device', ['vbd', ['uname', 'phy:/images/VM1.sda'], ['dev', 'sda'], ['mode', 'w']]],
    4.20 +             ['device', ['vbd', ['uname', 'phy:/images/VM1.sdb'], ['dev', 'sdb'], ['mode', 'w']]],
    4.21 +             ['memory', '256'], ['name', 'VM1'], ['on_crash', 'restart'],
    4.22 +             ['uuid', '10927a76-fe27-49b2-8f57-2970b7bbed6c'], ['vcpus', '1']
    4.23 +             ]))
    4.24 +
    4.25 +        self.assertEqual(cfg['uuid'], '10927a76-fe27-49b2-8f57-2970b7bbed6c')
    4.26 +        self.assertEqual(cfg['name_label'], 'VM1')
    4.27 +        self.assertEqual(cfg['memory_static_max'], 256)
    4.28 +
    4.29 +        ordered_refs = cfg.ordered_device_refs()
    4.30 +        self.assertEqual(cfg['devices'][ordered_refs[0]][0], 'vbd')
    4.31 +        self.assertEqual(cfg['devices'][ordered_refs[1]][0], 'vbd')
    4.32 +        self.assertEqual(cfg['devices'][ordered_refs[2]][0], 'vif')
    4.33 +        self.assertEqual(cfg['devices'][ordered_refs[3]][0], 'vif')
    4.34 +        self.assertEqual(cfg['devices'][ordered_refs[0]][1]['uname'],
    4.35 +                         'phy:/images/VM1.sda')
    4.36 +        self.assertEqual(cfg['devices'][ordered_refs[1]][1]['uname'],
    4.37 +                         'phy:/images/VM1.sdb')
    4.38 +        self.assertEqual(cfg['devices'][ordered_refs[2]][1]['mac'],
    4.39 +                         '00:16:3E:4C:D1:00')
    4.40 +        self.assertEqual(cfg['devices'][ordered_refs[3]][1]['mac'],
    4.41 +                         '00:16:3E:48:56:26')
    4.42 +
    4.43 +
    4.44 +def test_suite():
    4.45 +    return unittest.makeSuite(test_XendConfig)