/[gli]/trunk/src/GLIStorageDevice.py
Gentoo

Diff of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 491 Revision 492
1import commands, string, re, os, parted 1import commands, string, re, os, parted
2
3MEGABYTE = 1024 * 1024
2 4
3class Device: 5class Device:
4 "Class representing a partitionable device." 6 "Class representing a partitionable device."
5 7
6 _device = None 8 _device = None
11 _cylinder_bytes = 0 13 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 14 _sectors_in_cylinder = 0
13 _parted_dev = None 15 _parted_dev = None
14 _parted_disk = None 16 _parted_disk = None
15 _sector_bytes = 0 17 _sector_bytes = 0
18 _total_mb = 0
19 _arch = None
20
21 archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
22 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
23 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
24 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
25 }
16 26
17 def __init__(self, device): 27 def __init__(self, device, arch="x86"):
18 self._device = device 28 self._device = device
19 self._partitions = {} 29 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 30 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
21 self._total_bytes = 0 31 self._total_bytes = 0
22 self._cylinder_bytes = 0 32 self._cylinder_bytes = 0
33 self._arch = arch
23 self._parted_dev = parted.PedDevice.get(self._device) 34 self._parted_dev = parted.PedDevice.get(self._device)
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 35 self._parted_disk = parted.PedDisk.new(self._parted_dev)
25 self.set_disk_geometry_from_disk() 36 self.set_disk_geometry_from_disk()
26 37
27 def set_disk_geometry_from_disk(self): 38 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 39 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
29# if string.strip(commands.getoutput("echo " + self._device + " | grep '/hd'")) == self._device: # IDE
30# proc_dir = "/proc/ide/" + commands.getoutput("echo " + self._device + " | cut -d '/' -f 3")
31# proc_dir = string.strip(proc_dir)
32# heads = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 2")
33# sectors = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 3")
34# total_sectors = commands.getoutput("cat " + proc_dir + "/capacity")
35# cylinders = int(total_sectors) / (int(heads) * int(sectors))
36# self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = int(heads), int(sectors), int(cylinders)
37# else: #SCSI
38 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders 40 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
39
40 self._sector_bytes = self._parted_dev.sector_size 41 self._sector_bytes = self._parted_dev.sector_size
41 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes 42 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes
42# self._total_sectors = self._geometry['cylinders'] * self._geometry['heads'] * self._geometry['sectors']
43 self._total_sectors = self._parted_dev.length 43 self._total_sectors = self._parted_dev.length
44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
45 self._total_mb = int(self._total_bytes / MEGABYTE)
45 46
46 def set_partitions_from_disk(self): 47 def set_partitions_from_disk(self):
48 last_part = 0
49 last_log_part = 4
47 parted_part = self._parted_disk.next_partition() 50 parted_part = self._parted_disk.next_partition()
48 while parted_part != None: 51 while parted_part:
52 part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
49 if parted_part.num < 1: 53 if parted_part.num >= 1:
50 parted_part = self._parted_disk.next_partition(parted_part)
51 continue
52 fs_type = ""
53 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
54 if parted_part.type == 2: fs_type = "extended"
55 self._partitions[int(parted_part.num)] = Partition(self, parted_part.num, '', parted_part.geom.start, parted_part.geom.end, (parted_part.geom.end - parted_part.geom.start), fs_type, format=False, existing=True)
56 parted_part = self._parted_disk.next_partition(parted_part)
57
58 def set_partitions_from_install_profile_structure(self, ips):
59 for part in ips:
60 tmppart = ips[part]
61 existing = False
62 parted_part = self._parted_disk.get_partition(part)
63 if parted_part != None:
64 start = parted_part.geom.start / self._sectors_in_cylinder
65 end = parted_part.geom.end / self._sectors_in_cylinder
66 fs_type = "" 54 fs_type = ""
67 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 55 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
68 if parted_part.type == 2: fs_type = "extended" 56 if parted_part.type == 2: fs_type = "extended"
57 # Insert code to calculate MB here ---------------------------------------v
58 self._partitions[int(parted_part.num)] = Partition(self, parted_part.num, part_mb, parted_part.geom.start, parted_part.geom.end, fs_type, format=False, existing=True)
59 elif parted_part.type_name == "free":
60 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
61 if parent_part:
62 # And here ----------------------------------------------------------------------------v
63 self._partitions[float(last_log_part+0.9)] = Partition(self, float(last_log_part+0.9), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
64 last_log_part += 1
65 else:
66 # And here ----------------------------------------------------------------------------v
67 self._partitions[float(last_log_part+0.1)] = Partition(self, float(last_log_part+0.1), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
68 last_part += 1
69 parted_part = self._parted_disk.next_partition(parted_part)
70
71 def set_partitions_from_install_profile_structure(self, ips):
72 pass
73# for part in ips:
74# tmppart = ips[part]
75# existing = False
76# parted_part = self._parted_disk.get_partition(part)
77# if parted_part != None:
78# start = parted_part.geom.start / self._sectors_in_cylinder
79# end = parted_part.geom.end / self._sectors_in_cylinder
80# fs_type = ""
81# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
82# if parted_part.type == 2: fs_type = "extended"
69 if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False: 83# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
70 existing = True 84# existing = True
71 self._partitions[int(part)] = Partition(self, part, '', tmppart['start'], tmppart['end'], 0, tmppart['type'], mountopts=tmppart['mountopts'], mountpoint=tmppart['mountpoint'], format=tmppart['format'], existing=(not tmppart['format'])) 85# self._partitions[int(part)] = Partition(self, part, '', tmppart['start'], tmppart['end'], 0, tmppart['type'], mountopts=tmppart['mountopts'], mountpoint=tmppart['mountpoint'], format=tmppart['format'], existing=(not tmppart['format']))
72 86
73 def get_device(self): 87 def get_device(self):
74 return self._device 88 return self._device
75 89
76 def clear_partitions(self):
77 self._partitions = {}
78
79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''): 90 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
80 free_start, free_end = self.get_free_space(start) 91 free_minor = float(free_minor)
81 minor = int(minor) 92 new_minor = int(free_minor) + 1
82 if not free_end:
83 return False
84 if self._partitions.has_key(minor): 93 if self._partitions.has_key(new_minor):
85 parts = self._partitions.keys() 94 parts = self._partitions.keys()
86 parts.sort() 95 parts.sort()
87 parts.reverse() 96 parts.reverse()
88 hole_at = 0 97 hole_at = 0
89 for i in range(1, parts[0]+1): 98 for i in range(1, parts[0]+1):
90 if i <= minor: continue 99 if i <= new_minor: continue
91 if not self._partitions.has_key(int(i)): 100 if not self._partitions.has_key(i):
92 hole_at = i 101 hole_at = i
93 break 102 break
94 stopscooting = 0 103 stopscooting = 0
95 for i in parts: 104 for i in parts:
96 if stopscooting: break 105 if stopscooting: break
97 if (i >= hole_at) and (hole_at): continue 106 if i >= hole_at and hole_at: continue
98 if i >= minor: 107 if i >= new_minor:
99 self._partitions[i].set_minor(i+1) 108 self._partitions[i].set_minor(i+1)
100 self._partitions[i+1] = self._partitions[i] 109 self._partitions[i+1] = self._partitions[i]
101 if i == minor: stopscooting = 1 110 if i == new_minor: stopscooting = 1
111 del self._partitions[free_minor]
102 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type, mountpoint=mountpoint, mountopts=mountopts) 112 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
103 113
104 def remove_partition(self, minor): 114 def remove_partition(self, minor):
115 tmppart = self._partitions[int(minor)]
116 free_minor = 0
117 if tmppart.is_logical():
118 free_minor = float(minor)-0.1
119 else:
120 free_minor = float(minor)-0.9
121 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
105 del self._partitions[int(minor)] 122 del self._partitions[int(minor)]
106 123
107 def get_free_space(self, start): 124 def get_free_space(self, start):
108 GAP_SIZE = 100 125 GAP_SIZE = 100
109 parts = self._partitions.keys() 126 parts = self._partitions.keys()
181 198
182 def get_ordered_partition_list(self): 199 def get_ordered_partition_list(self):
183 parts = self._partitions.keys() 200 parts = self._partitions.keys()
184 parts.sort() 201 parts.sort()
185 partlist = [] 202 partlist = []
186 free_start, free_end = self.get_free_space(0) 203 tmppart = None
187 tmppart = None
188 tmppart_log = None 204 tmppart_log = None
189 for part in parts: 205 for part in parts:
190 if part > 4: break 206 if self.archinfo[self._arch]['extended'] and part > 4.1: break
191 tmppart = self._partitions[part] 207 tmppart = self._partitions[part]
192 if free_end < tmppart.get_start() and not free_start == -1:
193 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
194 free_start, free_end = self.get_free_space(free_end)
195 newitem = self._device + str(part) + ": " + str(tmppart.get_start()) + "-" + str(tmppart.get_end())
196 if tmppart.is_extended(): newitem = newitem + " extended"
197 partlist.append(newitem) 208 partlist.append(part)
198 if tmppart.is_extended(): 209 if tmppart.is_extended():
199 for part_log in parts: 210 for part_log in parts:
200 if part_log < 5: continue 211 if part_log < 4.9: continue
201 tmppart_log = self._partitions[part_log] 212 tmppart_log = self._partitions[part_log]
202 if free_end < tmppart_log.get_start() and free_end <= tmppart.get_end() and not free_start == -1:
203 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
204 free_start, free_end = self.get_free_space(free_end)
205 newitem = self._device + str(part_log) + ": " + str(tmppart_log.get_start()) + "-" + str(tmppart_log.get_end()) + " logical"
206 partlist.append(newitem) 213 partlist.append(part_log)
207 if ((tmppart_log == None) or (free_start > tmppart_log.get_end())) and free_start < tmppart.get_end():
208 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ") logical")
209 free_start, free_end = self.get_free_space(free_end)
210 if (tmppart == None) or (free_start > tmppart.get_end()):
211 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
212 return partlist 214 return partlist
213 215
214 def get_install_profile_structure(self): 216 def get_install_profile_structure(self):
215 devdic = {} 217 devdic = {}
216 for part in self._partitions: 218 for part in self._partitions:
217 tmppart = self._partitions[part] 219 tmppart = self._partitions[part]
218 devdic[part] = { 'mb': 0, 'minor': int(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() } 220 devdic[part] = { 'mb': tmppart.get_mb(), 'minor': float(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() }
219 return devdic 221 return devdic
220 222
221 def get_extended_partition(self): 223 def get_extended_partition(self):
222 for part in self._partitions: 224 for part in self._partitions:
223 tmppart = self._partitions[part] 225 tmppart = self._partitions[part]
238 return int(self._geometry['cylinders']) 240 return int(self._geometry['cylinders'])
239 241
240 def get_drive_bytes(self): 242 def get_drive_bytes(self):
241 return int(self._total_bytes) 243 return int(self._total_bytes)
242 244
245 def get_total_mb(self):
246 return self._total_mb
247
243 def get_partitions(self): 248 def get_partitions(self):
244 return self._partitions 249 return self._partitions
245 250
246 def print_partitions(self): 251# def print_partitions(self):
247 for part in self._partitions.keys(): 252# for part in self._partitions.keys():
248 print self._partitions[part].return_info() 253# print self._partitions[part].return_info()
249 254
250 def print_geometry(self): 255 def print_geometry(self):
251 print self._total_bytes, self._geometry 256 print self._total_bytes, self._geometry
252 257
253 def _error(self, message): 258 def _error(self, message):
280 285
281class Partition: 286class Partition:
282 "Class representing a single partition within a Device object" 287 "Class representing a single partition within a Device object"
283 288
284 _device = None 289 _device = None
285 _minor = None 290 _minor = 0
286 _orig_minor = "" 291 _orig_minor = 0
287 _bootflag = None
288 _start = None 292 _start = 0
289 _end = None 293 _end = 0
290 _blocks = None
291 _type = None 294 _type = None
292 _mountpoint = None 295 _mountpoint = None
293 _mountopts = None 296 _mountopts = None
294 _format = None 297 _format = None
295 _resizeable = None 298 _resizeable = None
296 _min_sectors_for_resize = 0 299 _min_sectors_for_resize = 0
300 _mb = 0
297 301
298 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 302 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False):
299 self._device = device 303 self._device = device
300 self._minor = int(minor) 304 self._minor = float(minor)
301 if existing: self._orig_minor = int(minor) 305 if existing: self._orig_minor = int(minor)
302 self._bootflag = bootflag
303 self._start = int(start) 306 self._start = int(start)
304 self._end = int(end) 307 self._end = int(end)
305 self._blocks = int(blocks)
306 if type == "": type = "unknown" 308 self._type = type or "unknown"
307 self._type = type
308 self._mountpoint = mountpoint 309 self._mountpoint = mountpoint
309 self._mountopts = mountopts 310 self._mountopts = mountopts
310 self._format = format 311 self._format = format
311 if blocks == 0: 312 self._mb = mb
312 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
313 if existing: 313 if existing:
314 self._orig_minor = self._minor
314 parted_part = device._parted_disk.get_partition(minor) 315 parted_part = device._parted_disk.get_partition(minor)
315 label_type = device._parted_disk.type.name 316 label_type = device._parted_disk.type.name
316 if label_type == "loop": 317 if label_type == "loop":
317 dev_node = device._device 318 dev_node = device._device
318 else: 319 else:
327 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes) 328 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes)
328 free_sec = free_sec - 2000 # just to be safe 329 free_sec = free_sec - 2000 # just to be safe
329 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec 330 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
330 self._resizeable == True 331 self._resizeable == True
331 else: 332 else:
332 parted_part = self._device._parted_disk.get_partition(self._minor) 333 parted_part = self._device._parted_disk.get_partition(int(self._minor))
333 try: 334 try:
334 parted_fs = parted_part.geom.file_system_open() 335 parted_fs = parted_part.geom.file_system_open()
335 except: 336 except:
336 self._resizeable = False 337 self._resizeable = False
337 return 338 return
346 return True 347 return True
347 else: 348 else:
348 return False 349 return False
349 350
350 def is_logical(self): 351 def is_logical(self):
351 part = self._device.get_partition_at(self._start, ignore_extended=0) 352 if self._type == "free":
352 if part and self._device._partitions[part].is_extended() and not part == self._minor: 353 if int(self._minor) + 0.9 == self._minor:
354 return True
355 else:
356 return False
357 elif self._device.archinfo[self._device._arch]['extended'] and self._minor > 4:
353 return True 358 return True
359 else:
354 return False 360 return False
355 361
356 def get_logicals(self): 362 def get_logicals(self):
357 if not self.is_extended(): 363 if not self.is_extended():
358 return None 364 return None
359 logicals = [] 365 logicals = []
384 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 390 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
385 391
386 def get_end(self): 392 def get_end(self):
387 return int(self._end) 393 return int(self._end)
388 394
395 def get_mb(self):
396 return int(self._mb)
397
389 def set_type(self, type): 398 def set_type(self, type):
390 self._type = type 399 self._type = type
391 400
392 def get_type(self): 401 def get_type(self):
393 return self._type 402 return self._type
394 403
395 def get_device(self): 404 def get_device(self):
396 return self._device 405 return self._device
397 406
398 def set_minor(self, minor): 407 def set_minor(self, minor):
399 self._minor = int(minor) 408 self._minor = float(minor)
400 409
401 def get_minor(self): 410 def get_minor(self):
402 return int(self._minor) 411 return float(self._minor)
403 412
404 def set_orig_minor(self, orig_minor): 413 def set_orig_minor(self, orig_minor):
405 self._orig_minor = int(orig_minor) 414 self._orig_minor = int(orig_minor)
406 415
407 def get_orig_minor(self): 416 def get_orig_minor(self):

Legend:
Removed from v.491  
changed lines
  Added in v.492

  ViewVC Help
Powered by ViewVC 1.1.20