Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) Citrix Systems Inc. 

2# 

3# This program is free software; you can redistribute it and/or modify 

4# it under the terms of the GNU Lesser General Public License as published 

5# by the Free Software Foundation; version 2.1 only. 

6# 

7# This program is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU Lesser General Public License for more details. 

11# 

12# You should have received a copy of the GNU Lesser General Public License 

13# along with this program; if not, write to the Free Software Foundation, Inc., 

14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

15# 

16# Utility functions to query and list local physical block devices from /sys 

17 

18import os 

19import sys 

20import time 

21 

22 

23def read_whole_file(filename): 

24 f = open(filename, 'r') 

25 try: 

26 return f.readlines() 

27 finally: 

28 f.close() 

29 

30 

31def list(): 

32 """List physical block devices from /sys""" 

33 all = os.listdir("/sys/block") 

34 

35 def is_physical_device(dev): 

36 sys = os.path.join("/sys/block", dev) 

37 device = os.path.join(sys, "device") 

38 return os.path.exists(device) 

39 return filter(is_physical_device, all) 

40 

41 

42def get_usb_node(usb_path): 

43 """ Given a full usb block device path, return the device node part 

44 usb_path: device path 

45 return: the usb device node 

46 example: 

47 1. devices/pci0000:00/0000:00:1a.0/usb1/1-0:1.0 => "" 

48 This is invalid input. This means a block device interface directly 

49 attached to bus. 

50 2. devices/pci0000:00/0000:00:1a.0/usb1/1-1 => "1-1" 

51 This could be valid usb device, but is unexpected and seems truncated. 

52 We should see the interface info. 

53 3. devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1 => "1-1.1" 

54 This could be valid usb device, but is unexpected and seems truncated. 

55 We should see the interface info. 

56 4. devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1:1.0 => "1-1" 

57 This is a valid usb interface, the function return "1-1". 

58 5. devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1/1-1.1:1.0 => "1-1.1" 

59 This is a valid usb interface, the function return "1-1.1". 

60 """ 

61 parts = usb_path.split("/") 

62 node = "" 

63 usb = False 

64 for part in parts: 

65 if usb: 

66 if ":" in part: 

67 return node 

68 else: 

69 node = part 

70 elif part.startswith("usb"): 

71 usb = True 

72 return node 

73 

74 

75def stat(device): 

76 """Given a device name, return a dictionary containing keys: 

77 size: size of device in bytes 

78 bus: bus type (eg USB, IDE) 

79 bus_path: identifier of bus endpoint (eg 1:0:0) 

80 hwinfo: string containing vendor, model, rev and type information""" 

81 results = {} 

82 sys = os.path.join("/sys/block", device) 

83 device = os.path.join(sys, "device") 

84 

85 try: 

86 results["size"] = int(read_whole_file(os.path.join(sys, "size"))[0]) * 512 

87 except: 

88 pass 

89 

90 results["bus"] = "Unrecognised bus type" 

91 results["bus_path"] = "" 

92 try: 

93 device_path = os.readlink(device) 

94 if device_path.find("/usb") != -1: 

95 results["bus"] = "USB" 

96 results["bus_path"] = os.path.basename(device_path) 

97 elif device_path.find("/ide") != -1: 

98 results["bus"] = "IDE" 

99 results["bus_path"] = os.path.basename(device_path) 

100 elif os.readlink(os.path.join(device, "subsystem")).endswith("scsi"): 

101 results["bus"] = "SCSI" 

102 results["bus_path"] = os.path.basename(device_path) 

103 real_path = os.path.realpath(device) 

104 if "/usb" in real_path: 

105 results["usb_path"] = get_usb_node(real_path) 

106 except: 

107 pass 

108 

109 # Work out the vendor/model/rev info 

110 results["hwinfo"] = "" 

111 

112 for field, fmt in [("vendor", "%s"), ("model", "model %s"), ("rev", "rev %s"), ("type", "type %s")]: 

113 try: 

114 value = read_whole_file(os.path.join(device, field))[0].strip() 

115 value = fmt % value 

116 if results["hwinfo"] != "": 

117 results["hwinfo"] = results["hwinfo"] + " " + value 

118 else: 

119 results["hwinfo"] = value 

120 except: 

121 pass 

122 

123 return results