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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 628 - (hide annotations) (download) (as text)
Wed Jun 8 03:29:59 2005 UTC (15 years, 4 months ago) by agaffney
File MIME type: text/x-python
File size: 25542 byte(s)
Minor fix recommended by pychecker

1 agaffney 535 import commands, string, os, parted
2 agaffney 556 from GLIException import *
3 agaffney 595 import GLIUtility
4 agaffney 107
5 agaffney 492 MEGABYTE = 1024 * 1024
6    
7 agaffney 497 archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
8     'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
9     'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
10 agaffney 595 'amd64': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
11 agaffney 497 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
12     }
13    
14 agaffney 513 ##
15     # This class provides a partitioning abstraction for the frontends
16 agaffney 107 class Device:
17     "Class representing a partitionable device."
18    
19     _device = None
20     _partitions = None
21     _geometry = None
22     _total_bytes = 0
23     _total_sectors = 0
24     _cylinder_bytes = 0
25     _sectors_in_cylinder = 0
26     _parted_dev = None
27     _parted_disk = None
28 agaffney 212 _sector_bytes = 0
29 agaffney 492 _total_mb = 0
30     _arch = None
31 agaffney 595 _disklabel = None
32 agaffney 492
33 agaffney 509 ##
34 agaffney 513 # Initialization function for GLIStorageDevice class
35     # @param device Device node (e.g. /dev/hda) of device being represented
36     # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
37 agaffney 596 def __init__(self, device, arch="x86", set_geometry=True):
38 agaffney 107 self._device = device
39     self._partitions = {}
40     self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
41     self._total_bytes = 0
42     self._cylinder_bytes = 0
43 agaffney 492 self._arch = arch
44 agaffney 107 self._parted_dev = parted.PedDevice.get(self._device)
45 agaffney 595 try:
46     self._parted_disk = parted.PedDisk.new(self._parted_dev)
47     except:
48     self._parted_disk = self._parted_dev.disk_new_fresh(parted.disk_type_get(archinfo[self._arch]['disklabel']))
49     self._disklabel = self._parted_disk.type.name
50 agaffney 596 if set_geometry:
51     self.set_disk_geometry_from_disk()
52 agaffney 107
53 agaffney 509 ##
54 agaffney 513 # Sets disk geometry info from disk. This function is used internally by __init__()
55 agaffney 107 def set_disk_geometry_from_disk(self):
56     self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
57 agaffney 205 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
58 agaffney 212 self._sector_bytes = self._parted_dev.sector_size
59     self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes
60     self._total_sectors = self._parted_dev.length
61 agaffney 107 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
62 agaffney 492 self._total_mb = int(self._total_bytes / MEGABYTE)
63 agaffney 107
64 agaffney 509 ##
65 agaffney 513 # Sets partition info from disk.
66 agaffney 107 def set_partitions_from_disk(self):
67 agaffney 492 last_part = 0
68     last_log_part = 4
69 agaffney 107 parted_part = self._parted_disk.next_partition()
70 agaffney 492 while parted_part:
71     part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
72     if parted_part.num >= 1:
73 agaffney 113 fs_type = ""
74     if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
75     if parted_part.type == 2: fs_type = "extended"
76 agaffney 501 if archinfo[self._arch]['extended'] and parted_part.num > 4:
77     last_log_part = parted_part.num
78     else:
79     last_part = parted_part.num
80 agaffney 492 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)
81     elif parted_part.type_name == "free":
82     parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
83     if parent_part:
84 agaffney 530 self._partitions[last_log_part+0.9] = Partition(self, last_log_part+0.9, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
85 agaffney 492 last_log_part += 1
86     else:
87 agaffney 530 self._partitions[last_part+0.1] = Partition(self, last_part+0.1, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
88 agaffney 492 last_part += 1
89     parted_part = self._parted_disk.next_partition(parted_part)
90 agaffney 107
91 agaffney 509 ##
92 agaffney 513 # Imports partition info from the install profile partition structure (currently does nothing)
93     # @param ips Parameter structure returned from install_profile.get_partition_tables()
94 agaffney 492 def set_partitions_from_install_profile_structure(self, ips):
95 agaffney 596 for part in ips:
96     tmppart = ips[part]
97     existing = False
98     if tmppart['origminor'] and not tmppart['format']:
99     existing = True
100 agaffney 611 self._partitions[tmppart['minor']] = Partition(self, tmppart['minor'], tmppart['mb'], tmppart['start'], tmppart['end'], tmppart['type'], format=tmppart['format'], origminor=tmppart['origminor'], existing=existing, mountpoint=tmppart['mountpoint'], mountopts=tmppart['mountopts'])
101 agaffney 492
102 agaffney 509 ##
103 agaffney 513 # Returns name of device (e.g. /dev/hda) being represented
104 agaffney 107 def get_device(self):
105     return self._device
106    
107 agaffney 509 ##
108 agaffney 513 # Combines free space and closes gaps in minor numbers. This is used internally
109 agaffney 494 def tidy_partitions(self):
110     last_minor = 0
111     last_log_minor = 4
112     last_free = 0
113     last_log_free = 0
114     parts = self._partitions.keys()
115     parts.sort()
116     for part in parts:
117 agaffney 497 if archinfo[self._arch]['extended'] and part > 4: break
118 agaffney 494 tmppart = self._partitions[part]
119     if tmppart.get_type() == "extended":
120     for part_log in parts:
121 agaffney 530 if part_log < 4.9: continue
122 agaffney 494 tmppart_log = self._partitions[part_log]
123     if tmppart_log.get_type() == "free":
124 agaffney 495 if last_log_minor < last_log_free:
125     self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
126     del self._partitions[part_log]
127 agaffney 494 else:
128 agaffney 495 if not last_log_free:
129 agaffney 530 last_log_free = last_log_minor + 0.9
130 agaffney 544 else:
131 agaffney 627 last_log_free = part_log
132 agaffney 495 tmppart_log.set_minor(last_log_free)
133     self._partitions[last_log_free] = tmppart_log
134     if part_log != last_log_free: del self._partitions[part_log]
135 agaffney 494 else:
136     if part_log > (last_log_minor + 1):
137     tmppart_log.set_minor(last_log_minor + 1)
138 agaffney 495 last_log_minor = last_log_minor + 1
139     self._partitions[last_log_minor] = tmppart_log
140     del self._partitions[part_log]
141 agaffney 544 else:
142     last_log_minor = part_log
143 agaffney 627 elif tmppart.get_type() == "free":
144 agaffney 495 if last_minor < last_free:
145     self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
146     del self._partitions[part]
147 agaffney 494 else:
148 agaffney 495 if not last_free:
149 agaffney 530 last_free = last_minor + 0.1
150 agaffney 544 else:
151     last_free = part
152 agaffney 495 tmppart.set_minor(last_free)
153     self._partitions[last_free] = tmppart
154     if part != last_free: del self._partitions[part]
155 agaffney 494 else:
156     if part > (last_minor + 1):
157     tmppart.set_minor(last_minor + 1)
158 agaffney 495 last_minor = last_minor + 1
159     self._partitions[last_minor] = tmppart
160     del self._partitions[part]
161 agaffney 544 else:
162     last_minor = part
163 agaffney 494
164 agaffney 509 ##
165 agaffney 513 # Adds a new partition to the partition info
166     # @param free_minor minor of unallocated space partition is being created in
167     # @param mb size of partition in MB
168     # @param start Start sector (only used for existing partitions)
169     # @param end End sector (only used for existing partitions)
170     # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
171     # @param mountpoint='' Partition mountpoint
172     # @param mountopts='' Partition mount options
173 agaffney 492 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
174     new_minor = int(free_minor) + 1
175     if self._partitions.has_key(new_minor):
176 agaffney 107 parts = self._partitions.keys()
177     parts.sort()
178     parts.reverse()
179     hole_at = 0
180     for i in range(1, parts[0]+1):
181 agaffney 492 if i <= new_minor: continue
182     if not self._partitions.has_key(i):
183 agaffney 107 hole_at = i
184     break
185     stopscooting = 0
186     for i in parts:
187     if stopscooting: break
188 agaffney 492 if i >= hole_at and hole_at: continue
189     if i >= new_minor:
190 agaffney 107 self._partitions[i].set_minor(i+1)
191     self._partitions[i+1] = self._partitions[i]
192 agaffney 492 if i == new_minor: stopscooting = 1
193 agaffney 494 if mb != self._partitions[free_minor].get_mb():
194     old_free_mb = self._partitions[free_minor].get_mb()
195     del self._partitions[free_minor]
196 agaffney 497 if archinfo[self._arch]['extended'] and new_minor >= 5:
197 agaffney 530 free_minor = new_minor + 0.9
198 agaffney 494 else:
199 agaffney 530 free_minor = new_minor + 0.1
200 agaffney 494 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
201     # print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
202     else:
203     del self._partitions[free_minor]
204 agaffney 492 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
205 agaffney 494 if type == "extended":
206 agaffney 530 self._partitions[4.9] = Partition(self, 4.9, mb, 0, 0, "free")
207 agaffney 494 self.tidy_partitions()
208 agaffney 107
209 agaffney 509 ##
210 agaffney 513 # Removes partition from partition info
211     # @param minor Minor of partition to remove
212 agaffney 107 def remove_partition(self, minor):
213 agaffney 492 tmppart = self._partitions[int(minor)]
214     free_minor = 0
215     if tmppart.is_logical():
216 agaffney 530 free_minor = minor-0.1
217 agaffney 492 else:
218 agaffney 530 free_minor = minor-0.9
219 agaffney 492 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
220 agaffney 107 del self._partitions[int(minor)]
221 agaffney 494 self.tidy_partitions()
222 agaffney 107
223 agaffney 509 ##
224 agaffney 513 # Returns free space (no longer used)
225     # @param start Start sector for search
226 agaffney 107 def get_free_space(self, start):
227 agaffney 207 GAP_SIZE = 100
228 agaffney 107 parts = self._partitions.keys()
229     parts.sort()
230     lastend_pri = 0
231     lastend_log = 0
232     free_start = -1
233     free_end = -1
234 agaffney 205 if start > self._total_sectors: return (-1, -1)
235 agaffney 107 for part in parts:
236     if part > 4: break
237     tmppart = self._partitions[part]
238 agaffney 207 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
239 agaffney 107 free_start = lastend_pri
240     free_end = tmppart.get_start() - 1
241     break
242     if tmppart.is_extended() and start < tmppart.get_end():
243     lastend_log = tmppart.get_start()
244     for part_log in parts:
245     if part_log < 5: continue
246     tmppart_log = self._partitions[part_log]
247 agaffney 207 if (tmppart_log.get_start() > (lastend_log + GAP_SIZE)) and (lastend_log >= start):
248 agaffney 107 free_start = lastend_log
249     free_end = tmppart_log.get_start() - 1
250     break
251     lastend_log = tmppart_log.get_end() + 1
252     if free_start == -1 and lastend_log < tmppart.get_end():
253     free_start = lastend_log
254     free_end = tmppart.get_end()
255 agaffney 112 break
256 agaffney 107 lastend_pri = tmppart.get_end() + 1
257 agaffney 205 if free_start == -1 and lastend_pri < self._total_sectors:
258 agaffney 107 free_start = lastend_pri
259 agaffney 205 free_end = self._total_sectors
260 agaffney 107 return (free_start, free_end)
261    
262 agaffney 509 ##
263 agaffney 513 # Gets partition containing a certain sector (no longer used)
264     # @param sector Sector to look at
265     # @param ignore_extended=1 Ignore extended partitions
266 agaffney 205 def get_partition_at(self, sector, ignore_extended=1):
267 agaffney 107 parts = self._partitions.keys()
268     parts.sort()
269     for part in parts:
270     tmppart = self._partitions[part]
271     if ignore_extended and tmppart.is_extended(): continue
272 agaffney 205 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
273 agaffney 107 return part
274     return 0
275    
276 agaffney 509 ##
277 agaffney 513 # Returns free minor (no longer used)
278 agaffney 509 # @param start Parameter description
279     # @param end Parameter description
280 agaffney 107 def get_free_minor_at(self, start, end):
281     parts = self._partitions.keys()
282     parts.sort()
283     minor = 1
284     lastpart = 0
285     for part in parts:
286     if part > 4: break
287     tmppart = self._partitions[part]
288     if end < tmppart.get_start():
289     minor = part
290     if (minor - 1) > lastpart: minor = lastpart + 1
291     break
292     if tmppart.is_extended() and start < tmppart.get_end():
293     minor = 5
294     lastpart = 4
295     for part_log in parts:
296     if part_log < 5: continue
297     tmppart_log = self._partitions[part_log]
298     if end < tmppart_log.get_start():
299     minor = part_log
300     if (minor - 1) > lastpart: minor = lastpart + 1
301     break
302     minor = part_log + 1
303     lastpart = part_log
304     break
305     minor = part + 1
306     lastpart = part
307     return minor
308    
309 agaffney 509 ##
310 agaffney 513 # Returns an ordered list (disk order) of partition minors
311 agaffney 107 def get_ordered_partition_list(self):
312 codeman 625 parts = self._partitions.keys()
313     parts.sort()
314     partlist = []
315 agaffney 492 tmppart = None
316     for part in parts:
317 agaffney 530 if archinfo[self._arch]['extended'] and part > 4.1: break
318 agaffney 107 tmppart = self._partitions[part]
319 agaffney 492 partlist.append(part)
320 agaffney 107 if tmppart.is_extended():
321     for part_log in parts:
322 agaffney 530 if part_log < 4.9: continue
323 agaffney 492 partlist.append(part_log)
324 agaffney 107 return partlist
325    
326 agaffney 509 ##
327 agaffney 513 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
328 agaffney 107 def get_install_profile_structure(self):
329     devdic = {}
330     for part in self._partitions:
331     tmppart = self._partitions[part]
332 agaffney 492 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() }
333 agaffney 107 return devdic
334    
335 agaffney 509 ##
336 agaffney 513 # Returns the minor of the extended partition, if any
337 agaffney 183 def get_extended_partition(self):
338     for part in self._partitions:
339     tmppart = self._partitions[part]
340     if tmppart.is_extended():
341     return part
342 agaffney 184 return 0
343 agaffney 183
344 agaffney 509 ##
345 agaffney 624 # Returns the drive model
346     def get_model(self):
347     return self._parted_dev.model
348    
349     ##
350 agaffney 596 # Sets the disklabel type
351     def set_disklabel(self, disklabel):
352     self._disklabel = disklabel
353    
354     ##
355     # Returns the disklabel type
356     def get_disklabel(self):
357     return self._disklabel
358    
359     ##
360 agaffney 513 # Returns the number of sectors on the device
361 agaffney 107 def get_num_sectors(self):
362     return int(self._total_sectors)
363    
364 agaffney 509 ##
365 agaffney 513 # Returns the size of a cylinder in bytes
366 agaffney 107 def get_cylinder_size(self):
367     return int(self._cylinder_bytes)
368    
369 agaffney 509 ##
370 agaffney 513 # Returns the size of a sector in bytes
371 agaffney 212 def get_sector_size(self):
372     return int(self._sector_bytes)
373    
374 agaffney 509 ##
375 agaffney 513 # Returns the number of cylinders
376 agaffney 107 def get_num_cylinders(self):
377     return int(self._geometry['cylinders'])
378    
379 agaffney 509 ##
380 agaffney 513 # Returns the total number of bytes on the device
381 agaffney 107 def get_drive_bytes(self):
382     return int(self._total_bytes)
383    
384 agaffney 509 ##
385 agaffney 513 # Returns the total number of MB on the device
386 agaffney 492 def get_total_mb(self):
387     return self._total_mb
388    
389 agaffney 509 ##
390 agaffney 513 # Returns partition info dictionary
391 agaffney 107 def get_partitions(self):
392     return self._partitions
393    
394 agaffney 509 ##
395 agaffney 513 # Prints disk geometry to STDOUT (no longer used)
396 agaffney 107 def print_geometry(self):
397     print self._total_bytes, self._geometry
398    
399 agaffney 509 ##
400 agaffney 513 # Utility function for running a command and returning it's output as a list
401     # @param cmd Command to run
402 agaffney 107 def _run(self, cmd):
403     "Runs a command and returns the output"
404    
405     # Run command
406     output_string = commands.getoutput(cmd)
407    
408     # What we will return
409     output_list = []
410    
411     # As long as there is a new line in the output_string
412     while output_string.find("\n") != -1:
413    
414     # Find the \n in the string
415     index = output_string.find("\n") + 1
416    
417     # Add the line to the output and remove it from
418     # the output_string
419     output_list.append(output_string[:index])
420     output_string = output_string[index:]
421    
422     # return output
423     return output_list
424    
425 agaffney 513 ##
426     # This class represents a partition within a GLIStorageDevice object
427 agaffney 107 class Partition:
428     "Class representing a single partition within a Device object"
429    
430     _device = None
431 agaffney 492 _minor = 0
432     _orig_minor = 0
433     _start = 0
434     _end = 0
435 agaffney 107 _type = None
436     _mountpoint = None
437     _mountopts = None
438     _format = None
439     _resizeable = None
440 agaffney 523 _min_mb_for_resize = 0
441 agaffney 492 _mb = 0
442 agaffney 107
443 agaffney 509 ##
444 agaffney 514 # Initialization function for the Partition class
445     # @param device Parent GLIStorageDevice object
446     # @param minor Minor of partition
447     # @param mb Parameter Size of partition in MB
448     # @param start Parameter Start sector of partition
449     # @param end Parameter Start sector of partition
450     # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
451     # @param mountpoint='' Mountpoint of partition
452     # @param mountopts='' Mount options of partition
453     # @param format=True Format partition
454     # @param existing=False This partition exists on disk
455 agaffney 596 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False, origminor=0):
456 agaffney 107 self._device = device
457 agaffney 492 self._minor = float(minor)
458 agaffney 107 self._start = int(start)
459     self._end = int(end)
460 agaffney 492 self._type = type or "unknown"
461 agaffney 107 self._mountpoint = mountpoint
462     self._mountopts = mountopts
463     self._format = format
464 agaffney 492 self._mb = mb
465 agaffney 596 self._orig_minor = origminor
466     if type != "free":
467     if existing and not origminor:
468     self._orig_minor = self._minor
469     self._minor = int(self._minor)
470     self._orig_minor = int(self._orig_minor)
471 agaffney 231 if existing:
472 agaffney 596 parted_part = device._parted_disk.get_partition(self._orig_minor)
473 agaffney 463 label_type = device._parted_disk.type.name
474     if label_type == "loop":
475 agaffney 466 dev_node = device._device
476     else:
477 agaffney 596 dev_node = device._device + str(self._orig_minor)
478 codeman 616 # print "dev_node = " + dev_node
479 agaffney 231 if type == "ntfs":
480 agaffney 463 min_bytes = int(commands.getoutput("ntfsresize -f --info " + dev_node + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
481 agaffney 523 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
482 agaffney 529 self._resizeable = True
483 agaffney 231 elif type == "ext2" or type == "ext3":
484 agaffney 530 block_size = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")))
485     free_blocks = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
486 agaffney 523 free_bytes = int(block_size * free_blocks)
487     # can't hurt to pad (the +50) it a bit since this is really just a guess
488     self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50
489 agaffney 529 self._resizeable = True
490 agaffney 231 else:
491 agaffney 596 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
492 agaffney 301 try:
493     parted_fs = parted_part.geom.file_system_open()
494     except:
495     self._resizeable = False
496     return
497 agaffney 231 resize_constraint = parted_fs.get_resize_constraint()
498 agaffney 523 min_bytes = resize_constraint.min_size * self._device._sector_bytes
499     self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
500 agaffney 231 self._resizeable = True
501 agaffney 107
502 agaffney 509 ##
503 agaffney 514 # Returns whether or not the partition is extended
504 agaffney 107 def is_extended(self):
505     if self._type == "extended":
506     return True
507     else:
508     return False
509    
510 agaffney 509 ##
511 agaffney 514 # Returns whether or not the partition is logical
512 agaffney 107 def is_logical(self):
513 agaffney 492 if self._type == "free":
514 agaffney 530 if int(self._minor) + 0.9 == self._minor:
515 agaffney 492 return True
516     else:
517     return False
518 agaffney 497 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
519 agaffney 107 return True
520 agaffney 492 else:
521     return False
522 agaffney 107
523 agaffney 509 ##
524 agaffney 514 # Returns a list of logical partitions if this is an extended partition
525 agaffney 107 def get_logicals(self):
526 agaffney 184 if not self.is_extended():
527     return None
528 agaffney 107 logicals = []
529 agaffney 184 parts = self._device._partitions.keys()
530     parts.sort()
531     for part in parts:
532     if part < 5: continue
533 agaffney 107 logicals.append(part)
534     logicals.sort()
535     return logicals
536    
537 agaffney 509 ##
538 agaffney 514 # Returns the extened parent partition if this is a logical partition (no longer used)
539 agaffney 107 def get_extended_parent(self):
540 agaffney 184 if not self.is_logical():
541     return None
542     else:
543     return self._device.get_partition_at(self._start, ignore_extended=0)
544 agaffney 107
545 agaffney 509 ##
546 agaffney 514 # Sets the start sector for the partition
547     # @param start Start sector
548 agaffney 107 def set_start(self, start):
549     self._start = int(start)
550    
551 agaffney 509 ##
552 agaffney 514 # Returns the start sector for the partition
553 agaffney 107 def get_start(self):
554     return int(self._start)
555    
556 agaffney 509 ##
557 agaffney 514 # Sets the end sector of the partition
558     # @param end End sector
559 agaffney 107 def set_end(self, end):
560     self._end = int(end)
561    
562 agaffney 509 ##
563 agaffney 514 # Returns end sector for the partition
564 agaffney 107 def get_end(self):
565     return int(self._end)
566    
567 agaffney 509 ##
568 agaffney 514 # Returns size of partition in MB
569 agaffney 492 def get_mb(self):
570     return int(self._mb)
571    
572 agaffney 509 ##
573 agaffney 514 # Sets size of partition in MB
574 agaffney 509 # @param mb Parameter description
575 agaffney 494 def set_mb(self, mb):
576     self._mb = int(mb)
577    
578 agaffney 509 ##
579 agaffney 514 # Sets type of partition
580 agaffney 509 # @param type Parameter description
581 agaffney 107 def set_type(self, type):
582     self._type = type
583    
584 agaffney 509 ##
585 agaffney 514 # Returns type of partition
586 agaffney 107 def get_type(self):
587     return self._type
588    
589 agaffney 509 ##
590 agaffney 514 # Returns parent GLIStorageDevice object
591 agaffney 107 def get_device(self):
592     return self._device
593    
594 agaffney 509 ##
595 agaffney 514 # Sets minor of partition
596     # @param minor New minor
597 agaffney 107 def set_minor(self, minor):
598 agaffney 492 self._minor = float(minor)
599 agaffney 107
600 agaffney 509 ##
601 agaffney 514 # Returns minor of partition
602 agaffney 107 def get_minor(self):
603 agaffney 492 return float(self._minor)
604 agaffney 107
605 agaffney 509 ##
606 agaffney 514 # Sets the original minor of the partition
607 agaffney 509 # @param orig_minor Parameter description
608 agaffney 386 def set_orig_minor(self, orig_minor):
609     self._orig_minor = int(orig_minor)
610    
611 agaffney 509 ##
612 agaffney 514 # Returns the original minor of the partition
613 agaffney 386 def get_orig_minor(self):
614     return self._orig_minor
615    
616 agaffney 509 ##
617 agaffney 514 # Sets the mountpoint for the partition
618     # @param mountpoint Mountpoint
619 agaffney 107 def set_mountpoint(self, mountpoint):
620     self._mountpoint = mountpoint
621    
622 agaffney 509 ##
623 agaffney 514 # Returns the mountpoint for the partition
624 agaffney 107 def get_mountpoint(self):
625     return self._mountpoint
626    
627 agaffney 509 ##
628 agaffney 514 # Sets the mount options for the partition
629     # @param mountopts Mount options
630 agaffney 107 def set_mountopts(self, mountopts):
631     self._mountopts = mountopts
632    
633 agaffney 509 ##
634 agaffney 514 # Returns the mount options for the partition
635 agaffney 107 def get_mountopts(self):
636     return self._mountopts
637    
638 agaffney 509 ##
639 agaffney 514 # Set whether to format the partition
640 agaffney 523 # @param format Format this partition (True/False)
641 agaffney 107 def set_format(self, format):
642     self._format = format
643    
644 agaffney 509 ##
645 agaffney 514 # Returns whether to format the partition
646 agaffney 107 def get_format(self):
647     return self._format
648    
649 agaffney 509 ##
650 agaffney 523 # Returns whether the partition is resizeable
651     def is_resizeable(self):
652     return self._resizeable
653    
654     ##
655     # Returns minimum MB for resize
656     def get_min_mb_for_resize(self):
657     # if self.is_extended():
658     # min_size = self._start
659     # for part in self._device._partitions:
660     # if part < 5: continue
661     # if part.get_end > min_size: min_size = part.get_end()
662     # return min_size
663     # else:
664     if self._resizeable:
665     return self._min_mb_for_resize
666 agaffney 107 else:
667 agaffney 523 return -1
668 agaffney 107
669 agaffney 509 ##
670 agaffney 523 # Returns maximum MB for resize
671     def get_max_mb_for_resize(self):
672 agaffney 544 if self._resizeable:
673     free_minor = 0
674     if self.is_logical():
675     free_minor = self._minor + 0.9
676     else:
677     free_minor = self._minor + 0.1
678     if free_minor in self._device._partitions:
679     return self._mb + self._device._partitions[free_minor]._mb
680     else:
681     return self._mb
682 agaffney 523 else:
683 agaffney 544 return -1
684 agaffney 107
685 agaffney 509 ##
686 agaffney 544 # Resizes the partition
687     # @param mb New size in MB
688     def resize(self, mb):
689     free_minor = self._minor
690 agaffney 107 if self.is_logical():
691 agaffney 544 free_minor += 0.9
692     else:
693     free_minor += 0.1
694     if mb < self._mb:
695     # Shrinking
696     if not free_minor in self._device._partitions:
697     self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
698     self._device._partitions[free_minor]._mb += self._mb - mb
699     self._mb = mb
700     elif mb == self._mb + self._device._partitions[free_minor]._mb:
701     # Using all available unallocated space
702     del self._device._partitions[free_minor]
703     self._mb = mb
704     elif mb > self._mb:
705     # Growing
706     self._device._partitions[free_minor]._mb = mb - self._mb
707     self._mb = mb
708 agaffney 107
709 agaffney 514 ##
710     # Returns a list of detected partitionable devices
711 agaffney 107 def detect_devices():
712     devices = []
713    
714     # Make sure sysfs exists
715     if not os.path.exists("/sys/bus"):
716 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
717 agaffney 107 # Make sure /proc/partitions exists
718     if not os.path.exists("/proc/partitions"):
719 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
720 agaffney 107
721     # Load /proc/partitions into the variable 'partitions'
722     partitions = []
723     for line in open("/proc/partitions"):
724 agaffney 556 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
725 agaffney 107 continue
726    
727     # Get the major, minor and device name
728     major = line.split()[0]
729     minor = line.split()[1]
730     device = "/dev/" + line.split()[3]
731    
732     if not major.isdigit() or not minor.isdigit():
733     continue
734    
735     major = int(major)
736     minor = int(minor)
737    
738     # If there is no /dev/'device_name', then scan
739     # all the devices in /dev to try and find a
740     # devices with the same major and minor
741     if not os.path.exists(device):
742     device = None
743     for path, dirs, files in os.walk("/dev"):
744 agaffney 535 for d_file in files:
745     full_file = os.path.join(path, d_file)
746 agaffney 107 if not os.path.exists(full_file):
747     continue
748     statres = os.stat(full_file)
749     fmaj = os.major(statres.st_rdev)
750     fmin = os.minor(statres.st_rdev)
751     if fmaj == major and fmin == minor:
752     device = full_file
753     break
754     if not device:
755     continue
756    
757     partitions.append(( major, minor, device ))
758    
759     # Scan sysfs for the devices of type 'x'
760     # 'x' being a member of the list below:
761     for dev_type in [ "ide", "scsi" ]: # Other device types? usb? fw?
762     if os.path.exists("/sys/bus/" + dev_type):
763     sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
764    
765     # For each device in the devices on that bus
766     for sysfs_device in sysfs_devices:
767 agaffney 556 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
768 agaffney 107
769     # If the file is not a block device, loop
770     if not os.path.exists(dev_file):
771     continue
772    
773     # Get the major and minor info
774     try:
775     major, minor = open(dev_file).read().split(":")
776     major = int(major)
777     minor = int(minor)
778     except:
779 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
780 agaffney 107
781     # Find a device listed in /proc/partitions
782     # that has the same minor and major as our
783     # current block device.
784     for record in partitions:
785     if major == record[0] and minor == record[1]:
786     devices.append(record[2])
787 agaffney 595
788     # For testing the partitioning code
789     if GLIUtility.is_file("/tmp/disk.img"):
790     devices.append("/tmp/disk.img")
791    
792 agaffney 107 # We have assembled the list of devices, so return it
793     return devices

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20