add from os to Partition and Raid

modify install_ng.xml
This commit is contained in:
fling
2010-09-25 18:42:40 +08:00
parent 00d3c6545f
commit 5fca45eb13
3 changed files with 118 additions and 66 deletions

View File

@@ -10,7 +10,7 @@ install_xml = '../xml/install.xml'
config_xml = '../xml/config.xml'
def to_xml_attr(doc, node, cls, name):
''' This method is called by to_xml. Its function is to create an attribute, and set its value based on Network data member
''' This method is called by to_xml. Its function is to create an attribute, and set its value based on Network data member
doc - xml document
node - xml node
cls - python class/python instance
@@ -29,7 +29,7 @@ class SerialNumber:
@staticmethod
def to_xml(doc, p_node):
''' write SerialNumber into xml
''' write SerialNumber into xml
doc - xml document instance
p_node - xml node (parent node)'''
sn = doc.createElement('serial-number')
@@ -40,88 +40,119 @@ p_node - xml node (parent node)'''
class Partition:
''' disk partition '''
unit=''
label=''
list=[]
def __init__(self, dev, st, sz, id, fr):
def __init__(self, dev, st, sz, tp, fs, fg, fr):
''' Partition init function '''
self.device = dev
self.start = st
self.size = sz
self.id = id
self.from_os = fr
self.device = dev
self.start = st
self.size = sz
self.type = tp
self.filesystem = fs
self.flags = fg
self.from_os = fr
Partition.list.append(self)
@staticmethod
def init_from_os():
''' create a Partition instance from hardware info'''
# cmd = 'sfdisk -d'
# has problem on raid detection
cmd = 'cat /home/zhi/work/new_install/python/mine/sfdisk.txt'
st, o = commands.getstatusoutput(cmd)
device_list=[]
Partition.unit='GB'
cmd_cat = 'cat /proc/partitions'
st,o = commands.getstatusoutput(cmd_cat)
if st:
print "Error cat : command not found or /proc/partitions dosen't exsit"
return
p0 = re.compile(r'unit:\s*(\w+)')
p = re.compile(r'''
\s*(\w+)\s*: # device
\s*start=\s*(\d+)\s*, # start
\s*size=\s*(\d+)\s*, # size
\s*Id=\s*(\d+) # id
''', re.VERBOSE)
p = re.compile(r"\s*")
for s in o.splitlines():
res0 = p0.search(s)
if res0:
Partition.unit = res0.groups()[0]
res = p.search(s)
if res:
dev, start, size, id = res.groups()
Partition(dev.split('/')[-1], start, size, id, True)
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)>=5 and ret[0] == '':
# Not enough to prevent of ret
tmp =[]
ret=ret+['','','']
for l in ret[7].split(","):
tmp.append(l.strip())
if "raid" in tmp:
ret[7]="yes"
else:
ret[7]="no"
Partition(d+ret[1],ret[2],ret[4],ret[5],ret[6][:10] == "linux-swap" and "swap" or ret[6] ,ret[7],'yes')
@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'),\
p.attributes['start'].value.encode('ascii'), \
p.attributes['size'].value.encode('ascii'), \
p.attributes['id'].value.encode('ascii'), \
p.attributes['from_os'].value.encode('ascii'))
p.attributes['start'].value.encode('ascii'),\
p.attributes['size'].value.encode('ascii'),\
p.attributes['type'].value.encode('ascii'),\
p.attributes['file-system'].value.encode('ascii'),\
p.attributes['flags'].value.encode('ascii'),\
p.attributes['from_os'].value.encode('ascii'))
@staticmethod
def to_xml(doc, p_node):
''' write all Partition instance into xml
''' write all Partition instance into xml
doc - xml document instance
p_node - xml node (parent node)'''
pts = doc.createElement("partitions")
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')
start_attr = doc.createAttribute('start')
size_attr = doc.createAttribute('size')
id_attr = doc.createAttribute('id')
from_attr = doc.createAttribute('from_os')
dev_attr.value = p.device
start_attr.value = p.start
size_attr.value = p.size
id_attr.value = p.id
from_attr.value = p.from_os
dev_attr = doc.createAttribute('device')
start_attr = doc.createAttribute('start')
size_attr = doc.createAttribute('size')
type_attr = doc.createAttribute('type')
fs_attr = doc.createAttribute('file-system')
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
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(id_attr)
pt.setAttributeNode(type_attr)
pt.setAttributeNode(fs_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
class Raid:
''' raid information '''
list = []
@@ -137,20 +168,20 @@ class Raid:
@staticmethod
def init_from_os():
#cmd = 'cat /proc/mdstat'
cmd = 'cat /home/zhi/work/new_install/python/mine/raid.txt'
cmd = 'cat /proc/mdstat'
st, o = commands.getstatusoutput(cmd)
if st:
return
dev_p = re.compile(r'''
^(md\d+)\s*: # md device
\s*active\s* # "active"
(\w+)\s* # raid type
''', re.VERBOSE)
^(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)
^\s*(\d+) # block number
\s*blocks
''', re.VERBOSE)
for s in o.splitlines():
@@ -166,13 +197,12 @@ class Raid:
spr_cmpts.append(ss[:ss.index('[')])
else:
act_cmpts.append(ss[:ss.index('[')])
continue
if size_p.match(s).groups():
raid_size = size_p.match(s).groups()[0]
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, True, raid_level, raid_size, act_cmpts, spr_cmpts)
Raid(raid_dev, "yes", raid_level, raid_size, act_cmpts, spr_cmpts)
# clear raid dev
raid_dev=''
@@ -251,7 +281,7 @@ p_node - xml node (parent node) '''
raids.appendChild(rd)
p_node.appendChild(raids)
#ri_tk_cmd.py use
@staticmethod
def get_size(dev):
for r in Raid.list:
@@ -286,6 +316,7 @@ class MountPoint:
mp.directory = dir
mp.filesystem = fs
mp.format = fm
def device(self):
return self.device
@@ -293,14 +324,23 @@ class MountPoint:
@staticmethod
def init_from_internal():
''' init MountPoint from internal class Partition and class Raid '''
# add raid device in dev_in_raid
dev_in_raid = set()
for r in Raid.list:
dev_in_raid.update(r.active_components)
dev_in_raid.update(r.spare_components)
devs = [ m.device for m in MountPoint.list ]
for p in Partition.list:
if p.id != 'fd' and p.device not in devs:
MountPoint(p.device, sz=p.size)
if p.device not in devs and p.device not in dev_in_raid :
MountPoint(p.device,fs=p.filesystem,sz=p.size)
for r in Raid.list:
if r.device not in devs:
MountPoint(r.device, sz=r.size)
if r.device not in devs and r.from_os == 'yes':
f_s = [p.filesystem for p in Partition.list if p.device == r.active_components[0]]
MountPoint(r.device, fs=f_s[0],sz=r.size)
elif r.device not in devs:
MountPoint(r.device,sz=r.size)
# now process whether a partition or raid was removed
s1 = set([ m.device for m in MountPoint.list ])
@@ -321,7 +361,8 @@ class MountPoint:
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['format'].value.encode('ascii'),\
m.attributes['size'].value.encode('ascii'))
@staticmethod
def to_xml(doc, p_node):
@@ -335,17 +376,20 @@ 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)
class Network:
''' network '''
hostname =''

View File

@@ -250,7 +250,7 @@ def raid_raw_init(w):
dev_in_raid.update(r.spare_components)
raw_devs = [ p.device for p in ri_data.Partition.list
if p.id == 'fd' and p.device not in dev_in_raid ]
if p.flags=='yes' and p.device not in dev_in_raid ]
display.var_dict['raid_raw_devs'].set(value=tuple(raw_devs))
def list_to_list(list_from, var_from, var_to):

View File

@@ -26,15 +26,23 @@
<define name="partitions">
<element name='partitions'>
<attribute name='unit'/>
<attribute name='label'>
<choice>
<value>msdos</value>
<value>gpt</value>
</choice>
</attribute>
<oneOrMore>
<element name='partition'>
<attribute name='device'/>
<attribute name='start'/>
<attribute name='size'/>
<attribute name='id'/>
<attribute name='from_os'>
<ref name='mybool'/>
</attribute>
<attribute name='file-system'/>
<attribute name='flags'/>
<attribute name='type'/>
</element>
</oneOrMore>
</element>