[iscsi] On améliore encore, parce que c'est un peu buggué.

This commit is contained in:
Pierre-Elliott Bécue 2013-05-15 23:03:07 +02:00
parent a42896d8b3
commit bdbc0b5b4d
2 changed files with 55 additions and 44 deletions

View file

@ -18,8 +18,14 @@ from config import ISCSI_MAP_FILE
def getname(device): def getname(device):
map_file = ISCSI_MAP_FILE map_file = ISCSI_MAP_FILE
if not device.isalpha():
block = "".join([i for i in device if i.isalpha()])
part = "_part" + device.replace(block, "")
else:
block = device
part = ""
# Rechreche le nom complet du périphérique dans /sys # Rechreche le nom complet du périphérique dans /sys
dev = os.readlink("/sys/block/%s/device" % device) dev = os.readlink("/sys/block/%s/device" % block)
# L'identifiant est de la forme "../../../0:0:0:42", où 42 (j'ai perdu) # L'identifiant est de la forme "../../../0:0:0:42", où 42 (j'ai perdu)
# est le lun. # est le lun.
@ -36,7 +42,7 @@ def getname(device):
globals()['map'] = {} globals()['map'] = {}
execfile(map_file, globals()) execfile(map_file, globals())
return map.get(lun, "lun%d" % lun) return map.get(lun, "lun%d" % lun) + part
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) != 2: if len(sys.argv) != 2:

View file

@ -29,19 +29,24 @@ def store_iscsi_volumes():
if line.startswith('ip-'): if line.startswith('ip-'):
device = os.readlink(PATH+line) # de la forme ../../sdb42 device = os.readlink(PATH+line) # de la forme ../../sdb42
device = device.rsplit('/', 1)[1] device = device.rsplit('/', 1)[1]
if line.rsplit('-', 1)[1][0:4] == "part":
symlink = 'iscsi_' + ugin.getname("".join([i for i in device if i.isalpha()])) + "_part" + line.rsplit('-', 1)[1][4:]
else:
symlink = 'iscsi_' + ugin.getname(device) symlink = 'iscsi_' + ugin.getname(device)
line = line.rsplit('-', 1)[1] if line.rsplit('-', 1)[1][0:4] == "part":
_ = links.setdefault(line, [symlink, device]) lun = line.rsplit('-', 2)[1]
else:
lun = line.rsplit('-', 1)[1]
if links.has_key(lun):
if not (True in [(symlink in tup) for tup in links[lun]]):
links[lun].append((symlink, device))
else:
links[lun] = [(symlink, device)]
return links return links
def make_link(couple): def make_link(couple):
"""Crée le symlink /dev/iscsi_nom -> /dev/sdb42""" """Crée le symlink /dev/iscsi_nom -> /dev/sdb42"""
os.chdir("/dev/") os.chdir("/dev/")
sym, dev = couple res = []
for (sym, dev) in couple:
diskstatus = subprocess.Popen(['file', '-sb', '/dev/' + dev], stdout=subprocess.PIPE) diskstatus = subprocess.Popen(['file', '-sb', '/dev/' + dev], stdout=subprocess.PIPE)
diskstatus = diskstatus.stdout.readlines()[0] diskstatus = diskstatus.stdout.readlines()[0]
if not (os.path.islink(sym)) and not 'empty' in diskstatus and not 'ERROR' in diskstatus: if not (os.path.islink(sym)) and not 'empty' in diskstatus and not 'ERROR' in diskstatus:
@ -49,20 +54,20 @@ def make_link(couple):
try: try:
os.symlink(dev, sym) os.symlink(dev, sym)
sys.stdout.write(coul("OK", 'vert')) sys.stdout.write(coul("OK", 'vert'))
res = True res.append(True)
except: except:
sys.stdout.write(coul("ECHEC", 'rouge')) sys.stdout.write(coul("ECHEC", 'rouge'))
res = False res.append(False)
sys.stdout.write('\n') sys.stdout.write('\n')
elif os.path.islink(sym) and ('empty' in diskstatus or 'ERROR' in diskstatus): elif os.path.islink(sym) and ('empty' in diskstatus or 'ERROR' in diskstatus):
sys.stdout.write("Destruction du lien /dev/" + sym + "") sys.stdout.write("Destruction du lien /dev/" + sym + "")
try: try:
os.remove(sym) os.remove(sym)
sys.stdout.write(coul("OK", 'vert')) sys.stdout.write(coul("OK", 'vert'))
res = True res.append(True)
except: except:
sys.stdout.write(coul("ECHEC", 'rouge')) sys.stdout.write(coul("ECHEC", 'rouge'))
res = False res.append(False)
sys.stdout.write('\n') sys.stdout.write('\n')
elif os.path.islink(sym) and os.readlink(sym) != dev: elif os.path.islink(sym) and os.readlink(sym) != dev:
sys.stdout.write("Mise à jour du lien /dev/" + sym + " -> /dev/" + dev + "") sys.stdout.write("Mise à jour du lien /dev/" + sym + " -> /dev/" + dev + "")
@ -70,13 +75,13 @@ def make_link(couple):
os.remove(sym) os.remove(sym)
os.symlink(dev, sym) os.symlink(dev, sym)
sys.stdout.write(coul("OK", 'vert')) sys.stdout.write(coul("OK", 'vert'))
res = True res.ppend(True)
except: except:
sys.stdout.write(coul("ECHEC", 'rouge')) sys.stdout.write(coul("ECHEC", 'rouge'))
res = False res.append(False)
sys.stdout.write('\n') sys.stdout.write('\n')
else: else:
res = None res.append(None)
return res return res
def clean_iscsi_links(): def clean_iscsi_links():
@ -91,7 +96,7 @@ def clean_iscsi_links():
if line.startswith('iscsi_'): if line.startswith('iscsi_'):
device = os.readlink('/dev/'+line) # de la forme ../../sdb42 device = os.readlink('/dev/'+line) # de la forme ../../sdb42
symlink = line symlink = line
make_link((symlink, device)) make_link([(symlink, device)])
return links return links
if __name__ == '__main__': if __name__ == '__main__':
@ -105,7 +110,7 @@ if __name__ == '__main__':
links = store_iscsi_volumes() links = store_iscsi_volumes()
for path in links.keys(): for path in links.keys():
cmd = make_link(links[path]) cmd = make_link(links[path])
if cmd != None: if True in cmd or False in cmd:
nothing = False nothing = False
if nothing: if nothing: