direct-io.hg

view tools/python/xen/util/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 b1a68f065fbd
line source
1 import os
2 import re
3 import string
5 from xen.xend.XendLogging import log
7 def expand_dev_name(name):
8 if not name:
9 return name
10 if re.match( '^/', name ):
11 return name
12 else:
13 return '/dev/' + name
15 def blkdev_name_to_number(name):
16 """Take the given textual block-device name (e.g., '/dev/sda1',
17 'hda') and return the device number used by the OS. """
19 n = expand_dev_name(name)
21 try:
22 return os.stat(n).st_rdev
23 except Exception, ex:
24 pass
26 if re.match( '/dev/sd[a-p]([1-9]|1[0-5])?', n):
27 return 8 * 256 + 16 * (ord(n[7:8]) - ord('a')) + int(n[8:] or 0)
29 if re.match( '/dev/hd[a-t]([1-9]|[1-5][0-9]|6[0-3])?', n):
30 ide_majors = [ 3, 22, 33, 34, 56, 57, 88, 89, 90, 91 ]
31 major = ide_majors[(ord(n[7:8]) - ord('a')) / 2]
32 minor = ((ord(n[7:8]) - ord('a')) % 2) * 64 + int(n[8:] or 0)
33 return major * 256 + minor
35 if re.match( '/dev/xvd[a-p]([1-9]|1[0-5])?', n):
36 return 202 * 256 + 16 * (ord(n[8:9]) - ord('a')) + int(n[9:] or 0)
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 not n is None:
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(":", 1)
67 if typ == "phy" and not fn.startswith("/"):
68 fn = "/dev/%s" %(fn,)
69 if typ == "tap":
70 (typ, fn) = fn.split(":", 1)
71 return fn
73 def mount_mode(name):
74 mode = None
75 name = expand_dev_name(name)
76 lines = os.popen('mount 2>/dev/null').readlines()
77 exp = re.compile('^' + name + ' .*[\(,]r(?P<mode>[ow])[,\)]')
78 for line in lines:
79 pm = exp.match(line)
80 if not pm: continue
81 mode = pm.group('mode')
82 break
83 if mode == 'w':
84 return mode
85 if mode == 'o':
86 mode = 'r'
87 return mode