debuggers.hg

view tools/python/xen/util/blkif.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 5c0bf00e371d
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 scsi_major = [ 8, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135 ]
27 if re.match( '/dev/sd[a-z]([1-9]|1[0-5])?$', n):
28 major = scsi_major[(ord(n[7:8]) - ord('a')) / 16]
29 minor = ((ord(n[7:8]) - ord('a')) % 16) * 16 + int(n[8:] or 0)
30 return major * 256 + minor
31 if re.match( '/dev/sd[a-i][a-z]([1-9]|1[0-5])?$', n):
32 major = scsi_major[((ord(n[7:8]) - ord('a') + 1) * 26 + (ord(n[8:9]) - ord('a'))) / 16 ]
33 minor = (((ord(n[7:8]) - ord('a') + 1 ) * 26 + (ord(n[8:9]) - ord('a'))) % 16) * 16 + int(n[9:] or 0)
34 return major * 256 + minor
36 if re.match( '/dev/hd[a-t]([1-9]|[1-5][0-9]|6[0-3])?', n):
37 ide_majors = [ 3, 22, 33, 34, 56, 57, 88, 89, 90, 91 ]
38 major = ide_majors[(ord(n[7:8]) - ord('a')) / 2]
39 minor = ((ord(n[7:8]) - ord('a')) % 2) * 64 + int(n[8:] or 0)
40 return major * 256 + minor
42 if re.match( '/dev/xvd[a-p]([1-9]|1[0-5])?', n):
43 return 202 * 256 + 16 * (ord(n[8:9]) - ord('a')) + int(n[9:] or 0)
45 if re.match( '^(0x)[0-9a-fA-F]+$', name ):
46 return string.atoi(name,16)
48 if re.match('^[0-9]+$', name):
49 return string.atoi(name, 10)
51 return None
53 def blkdev_segment(name):
54 """Take the given block-device name (e.g. '/dev/sda1', 'hda')
55 and return a dictionary { device, start_sector,
56 nr_sectors, type }
57 device: Device number of the given partition
58 start_sector: Index of first sector of the partition
59 nr_sectors: Number of sectors comprising this partition
60 type: 'Disk' or identifying name for partition type
61 """
62 val = None
63 n = blkdev_name_to_number(name)
64 if not n is None:
65 val = { 'device' : n,
66 'start_sector' : long(0),
67 'nr_sectors' : long(1L<<63),
68 'type' : 'Disk' }
69 return val
71 def _parse_uname(uname):
72 fn = taptype = None
73 if uname.find(":") != -1:
74 (typ, fn) = uname.split(":", 1)
75 if typ == "phy" and not fn.startswith("/"):
76 fn = "/dev/%s" %(fn,)
77 if typ == "tap":
78 (taptype, fn) = fn.split(":", 1)
79 return (fn, taptype)
81 def blkdev_uname_to_file(uname):
82 """Take a blkdev uname and return the corresponding filename."""
83 return _parse_uname(uname)[0]
85 def blkdev_uname_to_taptype(uname):
86 """Take a blkdev uname and return the blktap type."""
87 return _parse_uname(uname)[1]
89 def mount_mode(name):
90 mode = None
91 name = expand_dev_name(name)
92 lines = os.popen('mount 2>/dev/null').readlines()
93 exp = re.compile('^' + name + ' .*[\(,]r(?P<mode>[ow])[,\)]')
94 for line in lines:
95 pm = exp.match(line)
96 if not pm: continue
97 mode = pm.group('mode')
98 break
99 if mode == 'w':
100 return mode
101 if mode == 'o':
102 mode = 'r'
103 return mode