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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

"""Fake scsiutil module""" 

 

import os 

import re 

 

from . import f_exceptions 

from . import util 

from . import mpath_cli 

 

SECTOR_SHIFT = 9 

 

def match_dm(s): 

    regex = re.compile("mapper/") 

    return regex.search(s, 0) 

 

 

def getdev(path): 

    realpath = os.path.realpath(path) 

    if match_dm(realpath): 

        newpath = realpath.replace("/dev/mapper/","/dev/disk/by-id/scsi-") 

    else: 

        newpath = path 

    return os.path.realpath(newpath).split('/')[-1] 

 

 

def getsize(path): 

    dev = getdev(path) 

    sysfs = os.path.join('/sys/block',dev,'size') 

    size = 0 

    if os.path.exists(sysfs): 

        try: 

            f=open(sysfs, 'r') 

            size = (int(f.readline()) << SECTOR_SHIFT) 

            f.close() 

        except: 

            pass 

    return size 

 

 

def get_vendor(device_path): 

    vendor_command = ['sginfo', '-M', device_path] 

    (rc, stdout, _) = util.doexec(vendor_command) 

    if rc != 0: 

        raise f_exceptions.XenError( 

            'SCSIRead', 'sginfo failed {}, {}'.format(device_path, rc)) 

 

    match = re.search(r'(^|.*\n)Vendor:\s+(.*)[$|\n]', stdout) 

    if not match: 

        raise f_exceptions.XenError( 

            'SCSIRead', 'sginfo no vendor match {}'.format(device_path)) 

    return match.group(2).strip() 

 

 

def get_serial(device_path): 

    serial_command = ['sginfo', '-s', device_path] 

    (rc, stdout, _) = util.doexec(serial_command) 

    if rc != 0: 

        raise f_exceptions.XenError( 

            'SCSIRead', 'sginfo failed {}, {}'.format(device_path, rc)) 

 

    match = re.search(r'(^|.*\n)Serial Number\s+\'([^\']*)\'[$|\n]', stdout) 

    if not match: 

        raise f_exceptions.XenError( 

            'SCSIRead', 'sginfo no serial match {}'.format(device_path)) 

    return match.group(2).strip() 

 

 

def get_devices_by_SCSIid(SCSIid): 

    devices = os.listdir(os.path.join('/dev/disk/by-scsid', SCSIid)) 

    if 'mapper' in devices: 

        devices.remove('mapper') 

    return devices 

 

 

def sg_readcap(device): 

    device = os.path.join('/dev', getdev(device)) 

    readcapcommand = ['/usr/bin/sg_readcap', '-b', device] 

    (rc,stdout,stderr) = util.doexec(readcapcommand) 

    if rc == 6: 

        # retry one time for "Capacity data has changed" 

        (rc,stdout,stderr) = util.doexec(readcapcommand) 

    if rc != 0: 

        raise f_exceptions.XenError("SCSIRead", 

                                    "scsiutil.sg_readcap(%s) failed" % (device)) 

    match = re.search('(^|.*\n)(0x[0-9a-fA-F]+) (0x[0-9a-fA-F]+)\n$', stdout) 

    if not match: 

        raise f_exceptions.XenError("SCSIRead", 

                                    "scsiutil.sg_readcap(%s) failed to parse: %s" 

                                     % (device, stdout)) 

    (blockcount, blocksize) = match.group(2, 3) 

    return (int(blockcount, 0) * int(blocksize, 0)) 

 

 

def refreshdev(pathlist): 

    """ 

    Refresh block devices given a path list 

    """ 

    for path in pathlist: 

        dev = getdev(path) 

        util.SMlog("Refreshing size for device %s (%s)" % (path, dev)) 

        sysfs = os.path.join('/sys/block',dev,'device/rescan') 

        if os.path.exists(sysfs): 

            try: 

                f = os.open(sysfs, os.O_WRONLY) 

                os.write(f,'1') 

                os.close(f) 

            except: 

                pass 

 

 

def refresh_lun_size_by_SCSIid(SCSIid): 

    """ 

    Refresh all devices for the SCSIid. 

    Returns True if all known devices and the mapper device are up to date. 

    """ 

    def get_primary_device(SCSIid): 

        mapperdevice = os.path.join('/dev/mapper', SCSIid) 

        if os.path.exists(mapperdevice): 

            return mapperdevice 

        else: 

            devices = get_devices_by_SCSIid(SCSIid) 

            if devices: 

                return devices[0] 

            else: 

                return None 

 

    def get_outdated_size_devices(currentcapacity, devices): 

        devicesthatneedrefresh = [] 

        for device in devices: 

            if getsize(device) != currentcapacity: 

                devicesthatneedrefresh.append(device) 

        return devicesthatneedrefresh 

 

    def refresh_devices_if_needed(primarydevice, SCSIid, currentcapacity): 

        devices = get_devices_by_SCSIid(SCSIid) 

        if "/dev/mapper/" in primarydevice: 

            devices = set(devices + mpath_cli.list_paths(SCSIid)) 

        devicesthatneedrefresh = get_outdated_size_devices(currentcapacity, 

                                                           devices) 

        if devicesthatneedrefresh: 

            # timing out avoids waiting for min(dev_loss_tmo, fast_io_fail_tmo) 

            # if one or multiple devices don't answer 

            util.timeout_call(10, refreshdev, devicesthatneedrefresh) 

            if get_outdated_size_devices(currentcapacity, 

                                         devicesthatneedrefresh): 

                # in this state we shouldn't force resizing the mapper dev 

                raise f_exceptions.XenError("DeviceResize", 

                                            "Failed to get %s to agree on the " 

                                            "current capacity." 

                                            % devicesthatneedrefresh) 

 

    def refresh_mapper_if_needed(primarydevice, SCSIid, currentcapacity): 

        if ("/dev/mapper/" in primarydevice 

           and get_outdated_size_devices(currentcapacity, [primarydevice])): 

            util.SMlog("Resizing multpath map for device %s" % SCSIid) 

            mpath_cli.resize_map(SCSIid) 

            if get_outdated_size_devices(currentcapacity, [primarydevice]): 

                raise f_exceptions.XenError("DeviceResize", 

                                            "Failed to get the mapper dev to " 

                                            "agree on the current capacity.") 

 

    try: 

        primarydevice = get_primary_device(SCSIid) 

        if primarydevice: 

            currentcapacity = sg_readcap(primarydevice) 

            refresh_devices_if_needed(primarydevice, SCSIid, currentcapacity) 

            refresh_mapper_if_needed(primarydevice, SCSIid, currentcapacity) 

        else: 

            util.SMlog("scsiutil.refresh_lun_size_by_SCSIid(%s) could not " 

                       "find any devices for the SCSIid." % SCSIid) 

        return True 

    except: 

        util.SMlog("Error in scsiutil.refresh_lun_size_by_SCSIid(%s)" % SCSIid) 

        return False