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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 631 - (hide annotations) (download) (as text)
Thu Jun 9 05:57:56 2005 UTC (15 years, 4 months ago) by agaffney
File MIME type: text/x-python
File size: 25818 byte(s)
Removed start/end from XML output and added mkfsopts to partitioning info.

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 631 devdic[part] = { 'mb': tmppart.get_mb(), 'minor': float(part), 'origminor': tmppart.get_orig_minor(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format(), 'mkfsopts': tmppart.get_mkfsopts() }
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 631 _mkfsopts = None
443 agaffney 107
444 agaffney 509 ##
445 agaffney 514 # Initialization function for the Partition class
446     # @param device Parent GLIStorageDevice object
447     # @param minor Minor of partition
448     # @param mb Parameter Size of partition in MB
449     # @param start Parameter Start sector of partition
450     # @param end Parameter Start sector of partition
451     # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
452     # @param mountpoint='' Mountpoint of partition
453     # @param mountopts='' Mount options of partition
454     # @param format=True Format partition
455     # @param existing=False This partition exists on disk
456 agaffney 631 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False, origminor=0, mkfsopts=''):
457 agaffney 107 self._device = device
458 agaffney 492 self._minor = float(minor)
459 agaffney 107 self._start = int(start)
460     self._end = int(end)
461 agaffney 492 self._type = type or "unknown"
462 agaffney 107 self._mountpoint = mountpoint
463     self._mountopts = mountopts
464     self._format = format
465 agaffney 492 self._mb = mb
466 agaffney 596 self._orig_minor = origminor
467 agaffney 631 self._mkfsopts = mkfsopts
468 agaffney 596 if type != "free":
469     if existing and not origminor:
470     self._orig_minor = self._minor
471     self._minor = int(self._minor)
472     self._orig_minor = int(self._orig_minor)
473 agaffney 231 if existing:
474 agaffney 596 parted_part = device._parted_disk.get_partition(self._orig_minor)
475 agaffney 463 label_type = device._parted_disk.type.name
476     if label_type == "loop":
477 agaffney 466 dev_node = device._device
478     else:
479 agaffney 596 dev_node = device._device + str(self._orig_minor)
480 codeman 616 # print "dev_node = " + dev_node
481 agaffney 231 if type == "ntfs":
482 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 .\+//'"))
483 agaffney 523 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
484 agaffney 529 self._resizeable = True
485 agaffney 231 elif type == "ext2" or type == "ext3":
486 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\+//'")))
487     free_blocks = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
488 agaffney 523 free_bytes = int(block_size * free_blocks)
489     # can't hurt to pad (the +50) it a bit since this is really just a guess
490     self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50
491 agaffney 529 self._resizeable = True
492 agaffney 231 else:
493 agaffney 596 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
494 agaffney 301 try:
495     parted_fs = parted_part.geom.file_system_open()
496     except:
497     self._resizeable = False
498     return
499 agaffney 231 resize_constraint = parted_fs.get_resize_constraint()
500 agaffney 523 min_bytes = resize_constraint.min_size * self._device._sector_bytes
501     self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
502 agaffney 231 self._resizeable = True
503 agaffney 107
504 agaffney 509 ##
505 agaffney 514 # Returns whether or not the partition is extended
506 agaffney 107 def is_extended(self):
507     if self._type == "extended":
508     return True
509     else:
510     return False
511    
512 agaffney 509 ##
513 agaffney 514 # Returns whether or not the partition is logical
514 agaffney 107 def is_logical(self):
515 agaffney 492 if self._type == "free":
516 agaffney 530 if int(self._minor) + 0.9 == self._minor:
517 agaffney 492 return True
518     else:
519     return False
520 agaffney 497 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
521 agaffney 107 return True
522 agaffney 492 else:
523     return False
524 agaffney 107
525 agaffney 509 ##
526 agaffney 514 # Returns a list of logical partitions if this is an extended partition
527 agaffney 107 def get_logicals(self):
528 agaffney 184 if not self.is_extended():
529     return None
530 agaffney 107 logicals = []
531 agaffney 184 parts = self._device._partitions.keys()
532     parts.sort()
533     for part in parts:
534     if part < 5: continue
535 agaffney 107 logicals.append(part)
536     logicals.sort()
537     return logicals
538    
539 agaffney 509 ##
540 agaffney 514 # Returns the extened parent partition if this is a logical partition (no longer used)
541 agaffney 107 def get_extended_parent(self):
542 agaffney 184 if not self.is_logical():
543     return None
544     else:
545     return self._device.get_partition_at(self._start, ignore_extended=0)
546 agaffney 107
547 agaffney 509 ##
548 agaffney 631 # Sets the options passed to mkfs
549     # @param mkfsopts Options passed to mkfs
550     def set_mkfsopts(self, mkfsopts):
551     self._mkfsopts = mkfsopts
552    
553     ##
554     # Returns the options passes to mkfs
555     def get_mkfsopts(self):
556     return self._mkfsopts
557    
558     ##
559 agaffney 514 # Sets the start sector for the partition
560     # @param start Start sector
561 agaffney 107 def set_start(self, start):
562     self._start = int(start)
563    
564 agaffney 509 ##
565 agaffney 514 # Returns the start sector for the partition
566 agaffney 107 def get_start(self):
567     return int(self._start)
568    
569 agaffney 509 ##
570 agaffney 514 # Sets the end sector of the partition
571     # @param end End sector
572 agaffney 107 def set_end(self, end):
573     self._end = int(end)
574    
575 agaffney 509 ##
576 agaffney 514 # Returns end sector for the partition
577 agaffney 107 def get_end(self):
578     return int(self._end)
579    
580 agaffney 509 ##
581 agaffney 514 # Returns size of partition in MB
582 agaffney 492 def get_mb(self):
583     return int(self._mb)
584    
585 agaffney 509 ##
586 agaffney 514 # Sets size of partition in MB
587 agaffney 509 # @param mb Parameter description
588 agaffney 494 def set_mb(self, mb):
589     self._mb = int(mb)
590    
591 agaffney 509 ##
592 agaffney 514 # Sets type of partition
593 agaffney 509 # @param type Parameter description
594 agaffney 107 def set_type(self, type):
595     self._type = type
596    
597 agaffney 509 ##
598 agaffney 514 # Returns type of partition
599 agaffney 107 def get_type(self):
600     return self._type
601    
602 agaffney 509 ##
603 agaffney 514 # Returns parent GLIStorageDevice object
604 agaffney 107 def get_device(self):
605     return self._device
606    
607 agaffney 509 ##
608 agaffney 514 # Sets minor of partition
609     # @param minor New minor
610 agaffney 107 def set_minor(self, minor):
611 agaffney 492 self._minor = float(minor)
612 agaffney 107
613 agaffney 509 ##
614 agaffney 514 # Returns minor of partition
615 agaffney 107 def get_minor(self):
616 agaffney 492 return float(self._minor)
617 agaffney 107
618 agaffney 509 ##
619 agaffney 514 # Sets the original minor of the partition
620 agaffney 509 # @param orig_minor Parameter description
621 agaffney 386 def set_orig_minor(self, orig_minor):
622     self._orig_minor = int(orig_minor)
623    
624 agaffney 509 ##
625 agaffney 514 # Returns the original minor of the partition
626 agaffney 386 def get_orig_minor(self):
627     return self._orig_minor
628    
629 agaffney 509 ##
630 agaffney 514 # Sets the mountpoint for the partition
631     # @param mountpoint Mountpoint
632 agaffney 107 def set_mountpoint(self, mountpoint):
633     self._mountpoint = mountpoint
634    
635 agaffney 509 ##
636 agaffney 514 # Returns the mountpoint for the partition
637 agaffney 107 def get_mountpoint(self):
638     return self._mountpoint
639    
640 agaffney 509 ##
641 agaffney 514 # Sets the mount options for the partition
642     # @param mountopts Mount options
643 agaffney 107 def set_mountopts(self, mountopts):
644     self._mountopts = mountopts
645    
646 agaffney 509 ##
647 agaffney 514 # Returns the mount options for the partition
648 agaffney 107 def get_mountopts(self):
649     return self._mountopts
650    
651 agaffney 509 ##
652 agaffney 514 # Set whether to format the partition
653 agaffney 523 # @param format Format this partition (True/False)
654 agaffney 107 def set_format(self, format):
655     self._format = format
656    
657 agaffney 509 ##
658 agaffney 514 # Returns whether to format the partition
659 agaffney 107 def get_format(self):
660     return self._format
661    
662 agaffney 509 ##
663 agaffney 523 # Returns whether the partition is resizeable
664     def is_resizeable(self):
665     return self._resizeable
666    
667     ##
668     # Returns minimum MB for resize
669     def get_min_mb_for_resize(self):
670     # if self.is_extended():
671     # min_size = self._start
672     # for part in self._device._partitions:
673     # if part < 5: continue
674     # if part.get_end > min_size: min_size = part.get_end()
675     # return min_size
676     # else:
677     if self._resizeable:
678     return self._min_mb_for_resize
679 agaffney 107 else:
680 agaffney 523 return -1
681 agaffney 107
682 agaffney 509 ##
683 agaffney 523 # Returns maximum MB for resize
684     def get_max_mb_for_resize(self):
685 agaffney 544 if self._resizeable:
686     free_minor = 0
687     if self.is_logical():
688     free_minor = self._minor + 0.9
689     else:
690     free_minor = self._minor + 0.1
691     if free_minor in self._device._partitions:
692     return self._mb + self._device._partitions[free_minor]._mb
693     else:
694     return self._mb
695 agaffney 523 else:
696 agaffney 544 return -1
697 agaffney 107
698 agaffney 509 ##
699 agaffney 544 # Resizes the partition
700     # @param mb New size in MB
701     def resize(self, mb):
702     free_minor = self._minor
703 agaffney 107 if self.is_logical():
704 agaffney 544 free_minor += 0.9
705     else:
706     free_minor += 0.1
707     if mb < self._mb:
708     # Shrinking
709     if not free_minor in self._device._partitions:
710     self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
711     self._device._partitions[free_minor]._mb += self._mb - mb
712     self._mb = mb
713     elif mb == self._mb + self._device._partitions[free_minor]._mb:
714     # Using all available unallocated space
715     del self._device._partitions[free_minor]
716     self._mb = mb
717     elif mb > self._mb:
718     # Growing
719     self._device._partitions[free_minor]._mb = mb - self._mb
720     self._mb = mb
721 agaffney 107
722 agaffney 514 ##
723     # Returns a list of detected partitionable devices
724 agaffney 107 def detect_devices():
725     devices = []
726    
727     # Make sure sysfs exists
728     if not os.path.exists("/sys/bus"):
729 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
730 agaffney 107 # Make sure /proc/partitions exists
731     if not os.path.exists("/proc/partitions"):
732 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
733 agaffney 107
734     # Load /proc/partitions into the variable 'partitions'
735     partitions = []
736     for line in open("/proc/partitions"):
737 agaffney 556 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
738 agaffney 107 continue
739    
740     # Get the major, minor and device name
741     major = line.split()[0]
742     minor = line.split()[1]
743     device = "/dev/" + line.split()[3]
744    
745     if not major.isdigit() or not minor.isdigit():
746     continue
747    
748     major = int(major)
749     minor = int(minor)
750    
751     # If there is no /dev/'device_name', then scan
752     # all the devices in /dev to try and find a
753     # devices with the same major and minor
754     if not os.path.exists(device):
755     device = None
756     for path, dirs, files in os.walk("/dev"):
757 agaffney 535 for d_file in files:
758     full_file = os.path.join(path, d_file)
759 agaffney 107 if not os.path.exists(full_file):
760     continue
761     statres = os.stat(full_file)
762     fmaj = os.major(statres.st_rdev)
763     fmin = os.minor(statres.st_rdev)
764     if fmaj == major and fmin == minor:
765     device = full_file
766     break
767     if not device:
768     continue
769    
770     partitions.append(( major, minor, device ))
771    
772     # Scan sysfs for the devices of type 'x'
773     # 'x' being a member of the list below:
774     for dev_type in [ "ide", "scsi" ]: # Other device types? usb? fw?
775     if os.path.exists("/sys/bus/" + dev_type):
776     sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
777    
778     # For each device in the devices on that bus
779     for sysfs_device in sysfs_devices:
780 agaffney 556 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
781 agaffney 107
782     # If the file is not a block device, loop
783     if not os.path.exists(dev_file):
784     continue
785    
786     # Get the major and minor info
787     try:
788     major, minor = open(dev_file).read().split(":")
789     major = int(major)
790     minor = int(minor)
791     except:
792 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
793 agaffney 107
794     # Find a device listed in /proc/partitions
795     # that has the same minor and major as our
796     # current block device.
797     for record in partitions:
798     if major == record[0] and minor == record[1]:
799     devices.append(record[2])
800 agaffney 595
801     # For testing the partitioning code
802     if GLIUtility.is_file("/tmp/disk.img"):
803     devices.append("/tmp/disk.img")
804    
805 agaffney 107 # We have assembled the list of devices, so return it
806     return devices

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20