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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 826 - (hide annotations) (download) (as text)
Sat Aug 20 16:14:46 2005 UTC (15 years, 2 months ago) by agaffney
File MIME type: text/x-python
File size: 23389 byte(s)
global try/except block around fs resizable detection code

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20