ia64/xen-unstable

view tools/python/xen/util/blkif.py @ 4935:9685aab52173

bitkeeper revision 1.1418 (4288b194SVDMgqu_H0b_Hwg8OcoZkA)

blkif.py:
Import XendLogging.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Mon May 16 14:43:32 2005 +0000 (2005-05-16)
parents f85eb51dc313
children 6866a8788b06
line source
1 import os
2 import re
3 import string
5 from xen.xend.XendLogging import log
7 from xen.util.ip import _readline, _readlines
9 def expand_dev_name(name):
10 if not name:
11 return name
12 if re.match( '^/dev/', name ):
13 return name
14 else:
15 return '/dev/' + name
17 def blkdev_name_to_number(name):
18 """Take the given textual block-device name (e.g., '/dev/sda1',
19 'hda') and return the device number used by the OS. """
21 n = expand_dev_name(name)
23 try:
24 return os.stat(n).st_rdev
25 except Exception, ex:
26 log.debug("exception looking up device number for %s: %s", name, ex)
27 pass
29 if re.match( '/dev/sd[a-p]([0-9]|1[0-5])', n):
30 return 8 * 256 + 16 * (ord(n[7:8]) - ord('a')) + int(n[8:])
32 if re.match( '/dev/hd[a-t]([1-9]|[1-5][0-9]|6[0-3])?', n):
33 ide_majors = [ 3, 22, 33, 34, 56, 57, 88, 89, 90, 91 ]
34 major = ide_majors[(ord(n[7:8]) - ord('a')) / 2]
35 minor = ((ord(n[7:8]) - ord('a')) % 2) * 64 + int(n[8:] or 0)
36 return major * 256 + minor
38 # see if this is a hex device number
39 if re.match( '^(0x)?[0-9a-fA-F]+$', name ):
40 return string.atoi(name,16)
42 return None
44 def blkdev_segment(name):
45 """Take the given block-device name (e.g. '/dev/sda1', 'hda')
46 and return a dictionary { device, start_sector,
47 nr_sectors, type }
48 device: Device number of the given partition
49 start_sector: Index of first sector of the partition
50 nr_sectors: Number of sectors comprising this partition
51 type: 'Disk' or identifying name for partition type
52 """
53 val = None
54 n = blkdev_name_to_number(name)
55 if n:
56 val = { 'device' : n,
57 'start_sector' : long(0),
58 'nr_sectors' : long(1L<<63),
59 'type' : 'Disk' }
60 return val
62 def blkdev_uname_to_file(uname):
63 """Take a blkdev uname and return the corresponding filename."""
64 fn = None
65 if uname.find(":") != -1:
66 (typ, fn) = uname.split(":")
67 if typ == "phy" and not fn.startswith("/dev/"):
68 fn = "/dev/%s" %(fn,)
69 return fn
71 def mount_mode(name):
72 mode = None
73 name = expand_dev_name(name)
74 lines = _readlines(os.popen('mount 2>/dev/null'))
75 exp = re.compile('^' + name + ' .*[\(,]r(?P<mode>[ow])[,\)]')
76 for line in lines:
77 pm = exp.match(line)
78 if not pm: continue
79 mode = pm.group('mode')
80 break
81 if mode == 'w':
82 return mode
83 if mode == 'o':
84 mode = 'r'
85 return mode