direct-io.hg

view tools/python/xen/xend/server/blkif.py @ 12203:70687bcb82dd

[XEND] Compare result of blkdev_name_to_number() explicitly against None.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Nov 02 07:46:06 2006 +0000 (2006-11-02)
parents 33e9c88aab02
children 5bed7bc05c8a
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005, 2006 XenSource Inc.
17 #============================================================================
19 import re
20 import string
22 from xen.util import blkif
23 from xen.util import security
24 from xen.xend import sxp
25 from xen.xend.XendError import VmError
26 from xen.xend.server.DevController import DevController
28 class BlkifController(DevController):
29 """Block device interface controller. Handles all block devices
30 for a domain.
31 """
33 def __init__(self, vm):
34 """Create a block device controller.
35 """
36 DevController.__init__(self, vm)
38 def getDeviceDetails(self, config):
39 """@see DevController.getDeviceDetails"""
40 uname = sxp.child_value(config, 'uname', '')
41 dev = sxp.child_value(config, 'dev', '')
43 if 'ioemu:' in dev:
44 (_, dev) = string.split(dev, ':', 1)
45 try:
46 (dev, dev_type) = string.split(dev, ':', 1)
47 except ValueError:
48 dev_type = "disk"
50 if uname is None:
51 if dev_type == 'cdrom':
52 (typ, params) = ("", "")
53 else:
54 raise VmError(
55 'Block device must have physical details specified')
56 else:
57 try:
58 (typ, params) = string.split(uname, ':', 1)
59 except ValueError:
60 (typ, params) = ("", "")
62 mode = sxp.child_value(config, 'mode', 'r')
63 if mode not in ('r', 'w', 'w!'):
64 raise VmError('Invalid mode')
66 back = { 'dev' : dev,
67 'type' : typ,
68 'params' : params,
69 'mode' : mode
70 }
72 uuid = sxp.child_value(config, 'uuid')
73 if uuid:
74 back['uuid'] = uuid
76 if security.on():
77 (label, ssidref, policy) = security.get_res_security_details(uname)
78 back.update({'acm_label' : label,
79 'acm_ssidref': str(ssidref),
80 'acm_policy' : policy})
82 devid = blkif.blkdev_name_to_number(dev)
83 if devid is None:
84 raise VmError('Unable to find number for device (%s)' % (dev))
86 front = { 'virtual-device' : "%i" % devid,
87 'device-type' : dev_type
88 }
90 return (devid, back, front)
93 def reconfigureDevice(self, _, config):
94 """@see DevController.reconfigureDevice"""
95 (devid, new_back, new_front) = self.getDeviceDetails(config)
97 (dev, mode) = self.readBackend(devid, 'dev', 'mode')
98 dev_type = self.readFrontend(devid, 'device-type')
100 if (dev_type == 'cdrom' and new_front['device-type'] == 'cdrom' and
101 dev == new_back['dev'] and mode == 'r'):
102 self.writeBackend(devid,
103 'type', new_back['type'],
104 'params', new_back['params'])
105 else:
106 raise VmError('Refusing to reconfigure device %s:%d to %s' %
107 (self.deviceClass, devid, config))
110 def getDeviceConfiguration(self, devid):
111 """Returns the configuration of a device.
113 @note: Similar to L{configuration} except it returns a dict.
114 @return: dict
115 """
116 config = DevController.getDeviceConfiguration(self, devid)
117 devinfo = self.readBackend(devid, 'dev', 'type', 'params', 'mode',
118 'uuid')
119 dev, typ, params, mode, uuid = devinfo
121 if dev:
122 dev_type = self.readFrontend(devid, 'device-type')
123 if dev_type:
124 dev += ':' + dev_type
125 config['dev'] = dev
126 if typ and params:
127 config['uname'] = typ +':' + params
128 if mode:
129 config['mode'] = mode
130 if uuid:
131 config['uuid'] = uuid
133 return config
135 def destroyDevice(self, devid):
136 """@see DevController.destroyDevice"""
138 # If we are given a device name, then look up the device ID from it,
139 # and destroy that ID instead. If what we are given is an integer,
140 # then assume it's a device ID and pass it straight through to our
141 # superclass's method.
143 try:
144 DevController.destroyDevice(self, int(devid))
145 except ValueError:
146 devid_end = type(devid) is str and devid.split('/')[-1] or None
148 for i in self.deviceIDs():
149 d = self.readBackend(i, 'dev')
150 if d == devid or (devid_end and d == devid_end):
151 DevController.destroyDevice(self, i)
152 return
153 raise VmError("Device %s not connected" % devid)