direct-io.hg

view tools/python/xen/xend/server/blkif.py @ 11470:2b8dc69744e3

This patch adds syntax checking for "xm block-attach" command.

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
author Ewan Mellor <ewan@xensource.com>
date Thu Sep 14 07:55:28 2006 +0100 (2006-09-14)
parents f66bae594691
children f5321161c649 16946dcbf14b
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 #============================================================================
20 import re
21 import string
23 from xen.util import blkif
24 from xen.util import security
25 from xen.xend import sxp
26 from xen.xend.XendError import VmError
28 from xen.xend.server.DevController import DevController
31 class BlkifController(DevController):
32 """Block device interface controller. Handles all block devices
33 for a domain.
34 """
36 def __init__(self, vm):
37 """Create a block device controller.
38 """
39 DevController.__init__(self, vm)
42 def getDeviceDetails(self, config):
43 """@see DevController.getDeviceDetails"""
44 uname = sxp.child_value(config, 'uname')
46 dev = sxp.child_value(config, 'dev')
48 if 'ioemu:' in dev:
49 (_, dev) = string.split(dev, ':', 1)
50 try:
51 (dev, dev_type) = string.split(dev, ':', 1)
52 except ValueError:
53 dev_type = "disk"
55 if uname is None:
56 if dev_type == 'cdrom':
57 (typ, params) = ("", "")
58 else:
59 raise VmError(
60 'Block device must have physical details specified')
61 else:
62 try:
63 (typ, params) = string.split(uname, ':', 1)
64 except ValueError:
65 (typ, params) = ("", "")
67 mode = sxp.child_value(config, 'mode', 'r')
68 if mode not in ('r', 'w', 'w!'):
69 raise VmError('Invalid mode')
71 back = { 'dev' : dev,
72 'type' : typ,
73 'params' : params,
74 'mode' : mode
75 }
77 if security.on():
78 (label, ssidref, policy) = security.get_res_security_details(uname)
79 back.update({'acm_label' : label,
80 'acm_ssidref': str(ssidref),
81 'acm_policy' : policy})
83 devid = blkif.blkdev_name_to_number(dev)
84 front = { 'virtual-device' : "%i" % devid,
85 'device-type' : dev_type
86 }
88 return (devid, back, front)
91 def reconfigureDevice(self, _, config):
92 """@see DevController.reconfigureDevice"""
93 (devid, new_back, new_front) = self.getDeviceDetails(config)
95 (dev, mode) = self.readBackend(devid, 'dev', 'mode')
96 dev_type = self.readFrontend(devid, 'device-type')
98 if (dev_type == 'cdrom' and new_front['device-type'] == 'cdrom' and
99 dev == new_back['dev'] and mode == 'r'):
100 self.writeBackend(devid,
101 'type', new_back['type'],
102 'params', new_back['params'])
103 else:
104 raise VmError('Refusing to reconfigure device %s:%d to %s' %
105 (self.deviceClass, devid, config))
108 def configuration(self, devid):
109 """@see DevController.configuration"""
111 result = DevController.configuration(self, devid)
113 (dev, typ, params, mode) = self.readBackend(devid,
114 'dev', 'type', 'params',
115 'mode')
117 if dev:
118 (dev_type) = self.readFrontend(devid, 'device-type')
119 if dev_type:
120 dev += ":" + dev_type
121 result.append(['dev', dev])
122 if typ and params:
123 result.append(['uname', typ + ":" + params])
124 if mode:
125 result.append(['mode', mode])
127 return result
130 def destroyDevice(self, devid):
131 """@see DevController.destroyDevice"""
133 # If we are given a device name, then look up the device ID from it,
134 # and destroy that ID instead. If what we are given is an integer,
135 # then assume it's a device ID and pass it straight through to our
136 # superclass's method.
138 try:
139 DevController.destroyDevice(self, int(devid))
140 except ValueError:
141 devid_end = type(devid) is str and devid.split('/')[-1] or None
143 for i in self.deviceIDs():
144 d = self.readBackend(i, 'dev')
145 if d == devid or (devid_end and d == devid_end):
146 DevController.destroyDevice(self, i)
147 return
148 raise VmError("Device %s not connected" % devid)