modified: ri_data.py

zli update
This commit is contained in:
fling
2010-10-15 18:16:59 +08:00
parent c3fa7d0404
commit 4f9ec86571

View File

@@ -40,8 +40,7 @@ p_node - xml node (parent node)'''
class Partition:
''' disk partition '''
unit=''
label=''
list=[]
dict={}
def __init__(self, dev, st, sz, tp, fs, fg, fr):
''' Partition init function '''
self.device = dev
@@ -49,72 +48,86 @@ class Partition:
self.size = sz
self.type = tp
self.filesystem = fs
self.israid = fg
self.flags = fg
self.from_os = fr
Partition.list.append(self)
Partition.dict[dev] = self
@staticmethod
def init_from_os():
''' create a Partition instance from hardware info'''
device_list=[]
Partition.unit='MB'
p = re.compile(r"\s*")
Partition.unit='GB'
cmd_cat = 'cat /proc/partitions'
fs_list=["ext2","ext3","fat32","fat16","ntfs","reiserfs","xfs","jfs","linux-swap"]
st,o = commands.getstatusoutput(cmd_cat)
if st:
print "Error cat : command not found or /proc/partitions dosen't exsit"
return
for s in o.splitlines():
ret = re.search(r".+([sh][a-zA-Z]+)\s*",s)
if ret:
if ret.group(1) in device_list:
continue
device_list.append(ret.group(1))
for d in device_list:
st,o=commands.getstatusoutput('parted /dev/%s unit %s print'%(d,Partition.unit))
if st:
print "Error parted :command not found"
return
for s in o.splitlines():
ret = p.split(s)
if ret[0]=='Partition':
Partition.label=ret[-1]
if len(ret) > 1 and ret[1].isdigit():
# ret[0] is ['']
#Number start end size type file_system flags
# 1 2.0B 33GB 1GB primary raid
# 2 32GB 33GB 1GB primary raid, boot ...
# 3 32GB 33GB 1GB primary boot, raid, lbx ...
# 4 32GB 33GB 1GB primary exit2 raid
# 5 32GB 33GB 1GB primary exit2 raid, boot ...
# 6 3.2kB 33GB 1GB primary
# 7 32GB 33GB 1GB primary linx-swap
# 8 32GB 33GB 1GB primary boot, lbx ...
# 9 32GB 33GB 1GB primary exit2 boot
#ret[1] ret[2] ret[3] ret[4] ret[5] ret[6] ret[7]
ret+=['','','']
ret[2]=ret[2][:-len(Partition.unit)]
ret[3]=ret[3][:-len(Partition.unit)]
ret[4]=ret[4][:-len(Partition.unit)]
if "raid" in ret[6:] or "raid," in ret[6:]:
ret[7]='yes'
if ret[6][:3].lower()=='raid' or ret[6].lower() not in fs_list:
ret[6]=''
else:
ret[7]='no'
if ret[6].lower() not in fs_list:
ret[6]=''
Partition(d+ret[1],ret[2],ret[4],ret[5],ret[6][:10] == "linux-swap" and "swap" or ret[6] ,ret[7],'yes')
# an example of `cat /proc/partitions`
# major minor #blocks name
#
# 8 0 488386584 sda
# 8 1 97659103 sda1
# 8 2 1951897 sda2
# 8 3 97659135 sda3
# 8 4 291113865 sda4
# 8 16 244197527 sdb
# 8 17 62918541 sdb1
# 8 18 4200997 sdb2
# 8 19 1 sdb3
# 8 21 1238016 sdb5
# 9 0 803136 md0
l = [ s.split()[-1] for s in o.splitlines() if re.match(r'\s*\d+', s)]
# devices : 1) sda, sdb(first if ), or md0 (second if)
for d in [ i for i in l if not re.search(r'\d+', i)
or re.search(r'([a-zA-Z]+)', i).group(1) not in l]
st,o=commands.getstatusoutput('parted /dev/%s unit %s print'%(d,Partition.unit))
if st:
print "Error parted execute error"
return
# an example of o's content
# Model: ATA Hitachi HDS72105 (scsi)
# Disk /dev/sda: 500GB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
#
# Number Start End Size Type File system Flags
# 1 0.00GB 100GB 100GB primary ext3 boot
# 2 100GB 102GB 2.00GB primary linux-swap(v1)
# 3 102GB 202GB 100GB primary ext3
# 4 202GB 500GB 298GB primary ext3
located = False
for s in o.splitlines():
# Only match number, start, end, and size, that's enough.
# And when in raid case, no type appears.
title_p = re.compile('Number\s+Start\s+End\s+Size\s')
if title_p.match(s):
nm_i = s.find('Number')
st_i = s.find('Start')
end_i = s.find('End')
sz_i = s.find('Size')
tp_i = s.find('Type')
fs_i = s.find('File system')
flg_i= s.find('Flags')
if tp_i < 0: tp_i=fs_i
located = True
if located:
Partition(d+s[nm_i:st_i].strip(), # device name
s[st_i:end_i].strip(), # start
s[sz_i:tp_i].strip().rstrip(Partition.unit), # size
s[tp_i:fs_i].strip(), # type
re.search('swap', s[fs_i:flg_i]) and 'swap' or s[fs_i:flg_i].strip(), # file system
s[flg_i:].strip() # flags
)
@staticmethod
def init_from_xml(node):
''' create Partition instances from xml node '''
Partition.unit = node.attributes['unit'].value.encode('ascii')
Partition.label = node.attributes['label'].value.encode('ascii')
for p in node.childNodes:
if p.nodeType == node.ELEMENT_NODE and p.nodeName == 'partition':
Partition(p.attributes['device'].value.encode('ascii'),\
@@ -122,7 +135,7 @@ class Partition:
p.attributes['size'].value.encode('ascii'),\
p.attributes['type'].value.encode('ascii'),\
p.attributes['file-system'].value.encode('ascii'),\
p.attributes['israid'].value.encode('ascii'),\
p.attributes['flags'].value.encode('ascii'),\
p.attributes['from_os'].value.encode('ascii'))
@@ -135,9 +148,6 @@ p_node - xml node (parent node)'''
unit_attr = doc.createAttribute('unit')
unit_attr.value = Partition.unit
pts.setAttributeNode(unit_attr)
label_attr = doc.createAttribute('label')
label_attr.value = Partition.label
pts.setAttributeNode(label_attr)
for p in Partition.list:
pt = doc.createElement('partition')
dev_attr = doc.createAttribute('device')
@@ -145,62 +155,52 @@ p_node - xml node (parent node)'''
size_attr = doc.createAttribute('size')
type_attr = doc.createAttribute('type')
fs_attr = doc.createAttribute('file-system')
israid_attr = doc.createAttribute('israid')
flags_attr = doc.createAttribute('flags')
from_attr = doc.createAttribute('from_os')
dev_attr.value = p.device
start_attr.value = p.start
size_attr.value = p.size
type_attr.value = p.type
fs_attr.value = p.filesystem
israid_attr.value = p.israid
flags_attr.value = p.flags
from_attr.value = p.from_os
pt.setAttributeNode(dev_attr)
pt.setAttributeNode(start_attr)
pt.setAttributeNode(size_attr)
pt.setAttributeNode(type_attr)
pt.setAttributeNode(fs_attr)
pt.setAttributeNode(israid_attr)
pt.setAttributeNode(flags_attr)
pt.setAttributeNode(from_attr)
pts.appendChild(pt)
p_node.appendChild(pts)
# ri_tk_cmd.py use
@staticmethod
def get_size(dev):
for p in Partition.list:
if p.device == dev:
return p.size
return Partition.dict[dev].size
class Raid:
''' raid information '''
list = []
def __init__(self, dev, from_os, level, sz, a_devs, s_devs=[]):
dict = {}
def __init__(self, dev, from_os, level, a_devs, s_devs=[]):
''' Raid init function '''
self.device = dev
self.from_os = from_os
self.level = level
self.size = sz
self.active_components = a_devs
self.spare_components = s_devs
Raid.list.append(self)
Raid.dict[dev] = self
@staticmethod
def init_from_os():
cmd = 'cat /proc/mdstat'
st, o = commands.getstatusoutput(cmd)
if st:
return
if st: return
dev_p = re.compile(r'''
^(md\d+)\s*: # md device
\s*active\s* # "active"
(\w+)\s* # raid type
''', re.VERBOSE)
size_p = re.compile(r'''
^\s*(\d+) # block number
\s*blocks
''', re.VERBOSE)
for s in o.splitlines():
dev_res = dev_p.split(s)
if len(dev_res)>1: # matched
@@ -214,15 +214,8 @@ class Raid:
spr_cmpts.append(ss[:ss.index('[')])
else:
act_cmpts.append(ss[:ss.index('[')])
size = size_p.match(s)
if size is not None:
raid_size = size.groups()[0]
# if already detect a raid dev
if raid_dev:
Raid(raid_dev, "yes", raid_level, raid_size, act_cmpts, spr_cmpts)
# clear raid dev
raid_dev=''
Raid(raid_dev, "yes", raid_level, raid_size, act_cmpts, spr_cmpts)
@staticmethod
def add_component(node, l):
@@ -242,7 +235,6 @@ l - list
raid_dev = e.attributes['device'].value
raid_from = e.attributes['from_os'].value
raid_level = e.attributes['level'].value
raid_size = e.attributes['size'].value
act_cmpts = []
spr_cmpts = []
for sub_e in e.childNodes:
@@ -252,7 +244,7 @@ l - list
elif sub_e.nodeName == 'spare':
Raid.add_component(sub_e, spr_cmpts)
Raid(raid_dev, raid_from, raid_level, raid_size, act_cmpts, spr_cmpts)
Raid(raid_dev, raid_from, raid_level, act_cmpts, spr_cmpts)
@staticmethod
def to_xml(doc, p_node):
@@ -275,10 +267,6 @@ p_node - xml node (parent node) '''
rd_level_attr.value = r.level
rd.setAttributeNode(rd_level_attr)
rd_size_attr = doc.createAttribute('size')
rd_size_attr.value = r.size
rd.setAttributeNode(rd_size_attr)
rd_act_elem = doc.createElement('active')
for act_c in r.active_components:
act_c_e = doc.createElement('component')
@@ -301,15 +289,14 @@ p_node - xml node (parent node) '''
#ri_tk_cmd.py use
@staticmethod
def get_size(dev):
for r in Raid.list:
if r.device == dev:
return r.size
# need fill
pass
@staticmethod
def get_next_device():
''' get next available raid device name '''
num_p = re.compile(r'md(\d+)')
numbers = [ int(num_p.match(r.device).groups()[0]) for r in Raid.list ]
numbers = [ int(num_p.match(r).groups()[0]) for r in Raid.dict.keys() ]
max = 0
for n in numbers:
if n > max: max = n
@@ -326,62 +313,52 @@ p_node - xml node (parent node) '''
class MountPoint:
''' mount-points '''
list=[]
def __init__(self, dev, dir='', fs='', fm='no', sz='0'):
dict={}
def __init__(self, dev, dir='', fs='', fm='no'):
self.device = dev
self.directory = dir
self.filesystem = fs
self.format = fm
self.size = sz
MountPoint.list.append(self)
MountPoint.dict[dev]=self
@staticmethod
def change(dev, dir, fs, fm):
for mp in MountPoint.list:
if mp.device == dev:
mp.directory = dir
mp.filesystem = fs
mp.format = fm
mp = MountPoint.dict[dev]
mp.directory = dir
mp.filesystem = fs
mp.format = fm
def device(self):
return self.device
@staticmethod
def init_from_internal():
''' init MountPoint from internal class Partition and class Raid '''
# clean MountPoint.list before update
MountPoint.list=[]
# add partitions in Raid.list
raid_devs=Raid.dev_in_raid()
for p in Partition.list:
if p.device not in raid_devs :
MountPoint(p.device,fs=p.filesystem,sz=p.size)
# add raid device in dev_in_raid
dev_in_raid = Raid.dev_in_raid()
for r in Raid.list:
MountPoint(r.device,sz=r.size)
for dev in set(Partition.dict.keys()).difference(set(dev_in_raid)):
if MountPoint.dict.has_key(dev): continue
MountPoint(dev, fs=Partition.dict[dev].filesystem)
for dev in Raid.dict.keys():
if MountPoint.dict.has_key(dev)
continue
MountPoint(dev)
# now process whether a partition or raid was removed
s1 = set([ m.device for m in MountPoint.list ])
s2 = set([ p.device for p in Partition.list ] + [ r.device for r in Raid.list])
s1 = set(MountPoint.dict.keys())
s2 = set(Partition.dict.keys() + Raid.dict.keys())
for dev in s1-s2:
for i in range(len(MountPoint.list)):
if dev == MountPoint.list[i].device:
del MountPoint.list[i]
break
# sort
MountPoint.list.sort(key=MountPoint.device)
del MountPoint.dict[dev]
@staticmethod
def init_from_xml(node):
''' create MountPoint instances from xml node '''
for m in node.childNodes:
if m.nodeType == node.ELEMENT_NODE and m.nodeName == 'mount-point':
MountPoint(m.attributes['device'].value.encode('ascii'), \
m.attributes['directory'].value.encode('ascii'), \
m.attributes['file-system'].value.encode('ascii'), \
m.attributes['format'].value.encode('ascii'),\
m.attributes['size'].value.encode('ascii'))
m.attributes['directory'].value.encode('ascii'), \
m.attributes['file-system'].value.encode('ascii'), \
m.attributes['format'].value.encode('ascii'))
@staticmethod
def to_xml(doc, p_node):
@@ -395,17 +372,14 @@ p_node - xml node (parent node)'''
dir_attr = doc.createAttribute('directory')
fs_attr = doc.createAttribute('file-system')
fm_attr = doc.createAttribute('format')
sz_attr = doc.createAttribute('size')
dev_attr.value = m.device
dir_attr.value = m.directory
fs_attr.value = m.filesystem
fm_attr.value = m.format
sz_attr.value = m.size
mp.setAttributeNode(dev_attr)
mp.setAttributeNode(dir_attr)
mp.setAttributeNode(fs_attr)
mp.setAttributeNode(fm_attr)
mp.setAttributeNode(sz_attr)
mps.appendChild(mp)
p_node.appendChild(mps)