debuggers.hg

view tools/python/xen/util/blkif.py @ 17952:c5875621d79a

Fix up python breakage for blkdev_name_to_number

Signed-off-by: Chris Lalancette <clalance@redhat.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jun 30 10:01:44 2008 +0100 (2008-06-30)
parents 0728459b3c8d
children 81d47e75ce1a
line source
1 import os
2 import re
3 import string
5 def expand_dev_name(name):
6 if not name:
7 return name
8 if re.match( '^/', name ):
9 return name
10 else:
11 return '/dev/' + name
13 def blkdev_name_to_number(name):
14 """Take the given textual block-device name (e.g., '/dev/sda1',
15 'hda') and return the device number used by the OS. """
17 n = expand_dev_name(name)
19 devname = 'virtual-device'
20 devnum = None
22 try:
23 return os.stat(n).st_rdev
24 except Exception, ex:
25 pass
27 scsi_major = [ 8, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135 ]
28 if re.match( '/dev/sd[a-z]([1-9]|1[0-5])?$', n):
29 major = scsi_major[(ord(n[7:8]) - ord('a')) / 16]
30 minor = ((ord(n[7:8]) - ord('a')) % 16) * 16 + int(n[8:] or 0)
31 devnum = major * 256 + minor
32 elif re.match( '/dev/sd[a-i][a-z]([1-9]|1[0-5])?$', n):
33 major = scsi_major[((ord(n[7:8]) - ord('a') + 1) * 26 + (ord(n[8:9]) - ord('a'))) / 16 ]
34 minor = (((ord(n[7:8]) - ord('a') + 1 ) * 26 + (ord(n[8:9]) - ord('a'))) % 16) * 16 + int(n[9:] or 0)
35 devnum = major * 256 + minor
36 elif 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 devnum = major * 256 + minor
41 elif re.match( '/dev/xvd[a-p]([1-9]|1[0-5])?$', n):
42 devnum = (202 << 8) + ((ord(n[8:9]) - ord('a')) << 4) + int(n[9:] or 0)
43 elif re.match('/dev/xvd[q-z]([1-9]|1[0-5])?$', n):
44 devname = 'virtual-device-ext'
45 devnum = (1 << 28) + ((ord(n[8:9]) - ord('a')) << 8) + int(n[9:] or 0)
46 elif re.match('/dev/xvd[a-i][a-z]([1-9]|1[0-5])?$', n):
47 devname = 'virtual-device-ext'
48 devnum = (1 << 28) + (((ord(n[8:9]) - ord('a') + 1) * 26 + (ord(n[9:10]) - ord('a'))) << 8) + int(n[10:] or 0)
49 elif re.match( '^(0x)[0-9a-fA-F]+$', name ):
50 devnum = string.atoi(name, 16)
51 elif re.match('^[0-9]+$', name):
52 devnum = string.atoi(name, 10)
54 return (devname, devnum)
56 def blkdev_segment(name):
57 """Take the given block-device name (e.g. '/dev/sda1', 'hda')
58 and return a dictionary { device, start_sector,
59 nr_sectors, type }
60 device: Device number of the given partition
61 start_sector: Index of first sector of the partition
62 nr_sectors: Number of sectors comprising this partition
63 type: 'Disk' or identifying name for partition type
64 """
65 val = None
66 (name, n) = blkdev_name_to_number(name)
67 if not n is None:
68 val = { 'device' : n,
69 'start_sector' : long(0),
70 'nr_sectors' : long(1L<<63),
71 'type' : 'Disk' }
72 return val
74 def _parse_uname(uname):
75 fn = taptype = None
76 if uname.find(":") != -1:
77 (typ, fn) = uname.split(":", 1)
78 if typ == "phy" and not fn.startswith("/"):
79 fn = "/dev/%s" %(fn,)
80 if typ == "tap":
81 (taptype, fn) = fn.split(":", 1)
82 return (fn, taptype)
84 def blkdev_uname_to_file(uname):
85 """Take a blkdev uname and return the corresponding filename."""
86 return _parse_uname(uname)[0]
88 def blkdev_uname_to_taptype(uname):
89 """Take a blkdev uname and return the blktap type."""
90 return _parse_uname(uname)[1]
92 def mount_mode(name):
93 mode = None
94 name = expand_dev_name(name)
95 lines = os.popen('mount 2>/dev/null').readlines()
96 exp = re.compile('^' + name + ' .*[\(,]r(?P<mode>[ow])[,\)]')
97 for line in lines:
98 pm = exp.match(line)
99 if not pm: continue
100 mode = pm.group('mode')
101 break
102 if mode == 'w':
103 return mode
104 if mode == 'o':
105 mode = 'r'
106 return mode