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

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

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

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

# by the Free Software Foundation; version 2.1 only. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  

# GNU Lesser General Public License for more details. 

# 

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

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

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

# 

# LVM cache (for minimizing the number of lvs commands) 

# 

 

import os 

import util 

import lvutil 

import lvhdutil 

from lock import Lock 

from refcounter import RefCounter 

 

class LVInfo: 

    def __init__(self, name): 

        self.name = name 

        self.size = 0 

        self.active = False 

        self.open = 0 

        self.readonly = False 

        self.tags = [] 

 

    def toString(self): 

        return "%s, size=%d, active=%s, open=%s, ro=%s, tags=%s" % \ 

                (self.name, self.size, self.active, self.open, self.readonly, \ 

                self.tags) 

 

def lazyInit(op): 

    def wrapper(self, *args): 

        if not self.initialized: 

            util.SMlog("LVMCache: will initialize now") 

            self.refresh() 

            #util.SMlog("%s(%s): %s" % (op, args, self.toString())) 

        try: 

            ret = op(self, *args) 

        except KeyError: 

            util.logException("LVMCache") 

            util.SMlog("%s(%s): %s" % (op, args, self.toString())) 

            raise 

        return ret 

    return wrapper 

 

 

class LVMCache: 

    """Per-VG object to store LV information. Can be queried for cached LVM 

    information and refreshed""" 

 

    def __init__(self, vgName): 

        """Create a cache for VG vgName, but don't scan the VG yet""" 

        self.vgName = vgName 

        self.vgPath = "/dev/%s" % self.vgName 

        self.lvs = dict() 

        self.tags = dict() 

        self.initialized = False 

        util.SMlog("LVMCache created for %s" % vgName) 

 

    def refresh(self): 

        """Get the LV information for the VG using "lvs" """ 

        util.SMlog("LVMCache: refreshing") 

        #cmd = lvutil.cmd_lvm([lvutil.CMD_LVS, "--noheadings", "--units", 

        #                    "b", "-o", "+lv_tags", self.vgPath]) 

        #text = util.pread2(cmd) 

 

        cmd = [lvutil.CMD_LVS, "--noheadings", "--units", 

                               "b", "-o", "+lv_tags", self.vgPath] 

 

        text = lvutil.cmd_lvm(cmd) 

        self.lvs.clear() 

        self.tags.clear() 

        for line in text.split('\n'): 

            if not line: 

                continue 

            fields = line.split() 

            lvName = fields[0] 

            lvInfo = LVInfo(lvName) 

            lvInfo.size = long(fields[3].replace("B","")) 

            lvInfo.active = (fields[2][4] == 'a') 

            if (fields[2][5] == 'o'): 

                lvInfo.open = 1 

            lvInfo.readonly = (fields[2][1] == 'r') 

            self.lvs[lvName] = lvInfo 

            if len(fields) >= 5: 

                tags = fields[4].split(',') 

                for tag in tags: 

                    self._addTag(lvName, tag) 

        self.initialized = True 

 

    # 

    # lvutil functions 

    # 

    @lazyInit 

    def create(self, lvName, size, tag = None): 

        lvutil.create(lvName, size, self.vgName, tag) 

        lvInfo = LVInfo(lvName) 

        lvInfo.size = size 

        lvInfo.active = True 

        self.lvs[lvName] = lvInfo 

        if tag: 

            self._addTag(lvName, tag) 

 

    @lazyInit 

    def remove(self, lvName): 

        path = self._getPath(lvName) 

        lvutil.remove(path) 

        for tag in self.lvs[lvName].tags: 

            self._removeTag(lvName, tag) 

        del self.lvs[lvName] 

 

    @lazyInit 

    def rename(self, lvName, newName): 

        path = self._getPath(lvName) 

        lvutil.rename(path, newName) 

        lvInfo = self.lvs[lvName] 

        del self.lvs[lvName] 

        lvInfo.name = newName 

        self.lvs[newName] = lvInfo 

 

    @lazyInit 

    def setSize(self, lvName, newSize): 

        path = self._getPath(lvName) 

        size = self.getSize(lvName) 

        lvutil.setSize(path, newSize, (newSize < size)) 

        self.lvs[lvName].size = newSize 

 

    @lazyInit 

    def activate(self, ns, ref, lvName, binary): 

        lock = Lock(ref, ns) 

        lock.acquire() 

        try: 

            count = RefCounter.get(ref, binary, ns) 

            if count == 1: 

                try: 

                    self.activateNoRefcount(lvName) 

                except util.CommandException: 

                    RefCounter.put(ref, binary, ns) 

                    raise 

        finally: 

            lock.release() 

 

    @lazyInit 

    def deactivate(self, ns, ref, lvName, binary): 

        lock = Lock(ref, ns) 

        lock.acquire() 

        try: 

            count = RefCounter.put(ref, binary, ns) 

            if count > 0: 

                return 

            refreshed = False 

            while True: 

                lvInfo = self.getLVInfo(lvName) 

                if len(lvInfo) != 1: 

                    raise util.SMException("LV info not found for %s" % ref) 

                info = lvInfo[lvName] 

                if info.open: 

                    if refreshed: 

                        # should never happen in normal conditions but in some  

                        # failure cases the recovery code may not be able to  

                        # determine what the correct refcount should be, so it  

                        # is not unthinkable that the value might be out of  

                        # sync 

                        util.SMlog("WARNING: deactivate: LV %s open" % lvName) 

                        return 

                    # check again in case the cached value is stale 

                    self.refresh() 

                    refreshed = True 

                else: 

                    break 

            try: 

                self.deactivateNoRefcount(lvName) 

            except util.CommandException: 

                self.refresh() 

                if self.getLVInfo(lvName): 

                    util.SMlog("LV %s could not be deactivated" % lvName) 

                    if lvInfo[lvName].active: 

                        util.SMlog("Reverting the refcount change") 

                        RefCounter.get(ref, binary, ns) 

                    raise 

                else: 

                    util.SMlog("LV %s not found" % lvName) 

        finally: 

            lock.release() 

 

    @lazyInit 

    def activateNoRefcount(self, lvName, refresh = False): 

        path = self._getPath(lvName) 

        lvutil.activateNoRefcount(path, refresh) 

        self.lvs[lvName].active = True 

 

    @lazyInit 

    def deactivateNoRefcount(self, lvName): 

        path = self._getPath(lvName) 

        if self.checkLV(lvName): 

            lvutil.deactivateNoRefcount(path) 

            self.lvs[lvName].active = False 

        else: 

            util.SMlog("LVMCache.deactivateNoRefcount: no LV %s" % lvName) 

            lvutil._lvmBugCleanup(path) 

 

    @lazyInit 

    def setHidden(self, lvName, hidden=True): 

        path = self._getPath(lvName) 

        if hidden: 

            lvutil.setHidden(path) 

            self._addTag(lvName, lvutil.LV_TAG_HIDDEN) 

        else: 

            lvutil.setHidden(path, hidden=False) 

            self._removeTag(lvName, lvutil.LV_TAG_HIDDEN) 

 

    @lazyInit 

    def setReadonly(self, lvName, readonly): 

        path = self._getPath(lvName) 

        if self.lvs[lvName].readonly != readonly: 

            uuids = util.findall_uuid(path) 

            ns = lvhdutil.NS_PREFIX_LVM + uuids[0] 

            # Taking this lock is needed to avoid a race condition 

            # with tap-ctl open (which is now taking the same lock) 

            lock = Lock("lvchange-p", ns) 

            lock.acquire() 

            lvutil.setReadonly(path, readonly) 

            lock.release() 

            self.lvs[lvName].readonly = readonly 

 

    @lazyInit 

    def changeOpen(self, lvName, inc): 

        """We don't actually open or close the LV, just mark it in the cache""" 

        self.lvs[lvName].open += inc 

 

 

    # 

    # cached access 

    # 

    @lazyInit 

    def checkLV(self, lvName): 

        return self.lvs.get(lvName) 

 

    @lazyInit 

    def getLVInfo(self, lvName = None): 

        result = dict() 

        lvs = [] 

        if lvName == None: 

            lvs = self.lvs.keys() 

        elif self.lvs.get(lvName): 

            lvs = [lvName] 

        for lvName in lvs: 

            lvInfo = self.lvs[lvName] 

            lvutilInfo = lvutil.LVInfo(lvName) 

            lvutilInfo.size = lvInfo.size 

            lvutilInfo.active = lvInfo.active 

            lvutilInfo.open = (lvInfo.open > 0) 

            lvutilInfo.readonly = lvInfo.readonly 

            if lvutil.LV_TAG_HIDDEN in lvInfo.tags: 

                lvutilInfo.hidden = True 

            result[lvName] = lvutilInfo 

        return result 

 

    @lazyInit 

    def getSize(self, lvName): 

        return self.lvs[lvName].size 

 

    @lazyInit 

    def getHidden(self, lvName): 

        return (lvutil.LV_TAG_HIDDEN in self.lvs[lvName].tags) 

 

    @lazyInit 

    def getTagged(self, tag): 

        lvList = self.tags.get(tag) 

        if not lvList: 

            return [] 

        return lvList 

 

    @lazyInit 

    def is_active(self, lvname): 

        return self.lvs[lvname].active 

 

    # 

    # private 

    # 

    def _getPath(self, lvName): 

        return os.path.join(self.vgPath, lvName) 

 

    def _addTag(self, lvName, tag): 

        self.lvs[lvName].tags.append(tag) 

        if self.tags.get(tag): 

            self.tags[tag].append(lvName) 

        else: 

            self.tags[tag] = [lvName] 

 

    def _removeTag(self, lvName, tag): 

        self.lvs[lvName].tags.remove(tag) 

        self.tags[tag].remove(lvName) 

 

    def toString(self): 

        result = "LVM Cache for %s: %d LVs" % (self.vgName, len(self.lvs)) 

        for lvName, lvInfo in self.lvs.iteritems(): 

            result += "\n%s" % lvInfo.toString() 

        return result