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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 640 - (hide annotations) (download) (as text)
Fri Jun 10 04:00:31 2005 UTC (15 years, 4 months ago) by agaffney
File MIME type: text/x-python
File size: 23261 byte(s)
Fix call to get_partition_at()

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 596 def __init__(self, device, arch="x86", set_geometry=True):
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     if self.get_extended_partition() and parted_part.geom.start >= self._partitions[self.get_extended()].get_start() and parted_part.geom.end <= self._partitions[self.get_extended()].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 596 parted_part = device._parted_disk.get_partition(self._orig_minor)
373 agaffney 463 label_type = device._parted_disk.type.name
374     if label_type == "loop":
375 agaffney 466 dev_node = device._device
376     else:
377 agaffney 596 dev_node = device._device + str(self._orig_minor)
378 codeman 616 # print "dev_node = " + dev_node
379 agaffney 231 if type == "ntfs":
380 agaffney 635 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 .\+//'"))
381     self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
382 agaffney 529 self._resizeable = True
383 agaffney 231 elif type == "ext2" or type == "ext3":
384 agaffney 635 block_size = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")))
385     free_blocks = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
386     free_bytes = long(block_size * free_blocks)
387 agaffney 523 # can't hurt to pad (the +50) it a bit since this is really just a guess
388 agaffney 635 self._min_mb_for_resize = self._mb - long(free_bytes / MEGABYTE) + 50
389 agaffney 529 self._resizeable = True
390 agaffney 231 else:
391 agaffney 596 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
392 agaffney 301 try:
393     parted_fs = parted_part.geom.file_system_open()
394     except:
395     self._resizeable = False
396     return
397 agaffney 231 resize_constraint = parted_fs.get_resize_constraint()
398 agaffney 523 min_bytes = resize_constraint.min_size * self._device._sector_bytes
399 agaffney 635 self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
400 agaffney 231 self._resizeable = True
401 agaffney 107
402 agaffney 509 ##
403 agaffney 514 # Returns whether or not the partition is extended
404 agaffney 107 def is_extended(self):
405     if self._type == "extended":
406     return True
407     else:
408     return False
409    
410 agaffney 509 ##
411 agaffney 514 # Returns whether or not the partition is logical
412 agaffney 107 def is_logical(self):
413 agaffney 492 if self._type == "free":
414 agaffney 639 if int(self._minor) + FREE_MINOR_FRAC_LOG == self._minor:
415 agaffney 492 return True
416     else:
417     return False
418 agaffney 497 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
419 agaffney 107 return True
420 agaffney 492 else:
421     return False
422 agaffney 107
423 agaffney 509 ##
424 agaffney 514 # Returns a list of logical partitions if this is an extended partition
425 agaffney 107 def get_logicals(self):
426 agaffney 184 if not self.is_extended():
427     return None
428 agaffney 107 logicals = []
429 agaffney 184 parts = self._device._partitions.keys()
430     parts.sort()
431     for part in parts:
432     if part < 5: continue
433 agaffney 107 logicals.append(part)
434     logicals.sort()
435     return logicals
436    
437 agaffney 509 ##
438 agaffney 514 # Returns the extened parent partition if this is a logical partition (no longer used)
439 agaffney 107 def get_extended_parent(self):
440 agaffney 184 if not self.is_logical():
441     return None
442     else:
443     return self._device.get_partition_at(self._start, ignore_extended=0)
444 agaffney 107
445 agaffney 509 ##
446 agaffney 631 # Sets the options passed to mkfs
447     # @param mkfsopts Options passed to mkfs
448     def set_mkfsopts(self, mkfsopts):
449     self._mkfsopts = mkfsopts
450    
451     ##
452     # Returns the options passes to mkfs
453     def get_mkfsopts(self):
454     return self._mkfsopts
455    
456     ##
457 agaffney 514 # Sets the start sector for the partition
458     # @param start Start sector
459 agaffney 107 def set_start(self, start):
460 agaffney 635 self._start = long(start)
461 agaffney 107
462 agaffney 509 ##
463 agaffney 514 # Returns the start sector for the partition
464 agaffney 107 def get_start(self):
465 agaffney 635 return long(self._start)
466 agaffney 107
467 agaffney 509 ##
468 agaffney 514 # Sets the end sector of the partition
469     # @param end End sector
470 agaffney 107 def set_end(self, end):
471 agaffney 635 self._end = long(end)
472 agaffney 107
473 agaffney 509 ##
474 agaffney 514 # Returns end sector for the partition
475 agaffney 107 def get_end(self):
476 agaffney 635 return long(self._end)
477 agaffney 107
478 agaffney 509 ##
479 agaffney 514 # Returns size of partition in MB
480 agaffney 492 def get_mb(self):
481 agaffney 635 return long(self._mb)
482 agaffney 492
483 agaffney 509 ##
484 agaffney 514 # Sets size of partition in MB
485 agaffney 509 # @param mb Parameter description
486 agaffney 494 def set_mb(self, mb):
487 agaffney 635 self._mb = long(mb)
488 agaffney 494
489 agaffney 509 ##
490 agaffney 514 # Sets type of partition
491 agaffney 509 # @param type Parameter description
492 agaffney 107 def set_type(self, type):
493     self._type = type
494    
495 agaffney 509 ##
496 agaffney 514 # Returns type of partition
497 agaffney 107 def get_type(self):
498     return self._type
499    
500 agaffney 509 ##
501 agaffney 514 # Returns parent GLIStorageDevice object
502 agaffney 107 def get_device(self):
503     return self._device
504    
505 agaffney 509 ##
506 agaffney 514 # Sets minor of partition
507     # @param minor New minor
508 agaffney 107 def set_minor(self, minor):
509 agaffney 492 self._minor = float(minor)
510 agaffney 107
511 agaffney 509 ##
512 agaffney 514 # Returns minor of partition
513 agaffney 107 def get_minor(self):
514 agaffney 492 return float(self._minor)
515 agaffney 107
516 agaffney 509 ##
517 agaffney 514 # Sets the original minor of the partition
518 agaffney 509 # @param orig_minor Parameter description
519 agaffney 386 def set_orig_minor(self, orig_minor):
520     self._orig_minor = int(orig_minor)
521    
522 agaffney 509 ##
523 agaffney 514 # Returns the original minor of the partition
524 agaffney 386 def get_orig_minor(self):
525     return self._orig_minor
526    
527 agaffney 509 ##
528 agaffney 514 # Sets the mountpoint for the partition
529     # @param mountpoint Mountpoint
530 agaffney 107 def set_mountpoint(self, mountpoint):
531     self._mountpoint = mountpoint
532    
533 agaffney 509 ##
534 agaffney 514 # Returns the mountpoint for the partition
535 agaffney 107 def get_mountpoint(self):
536     return self._mountpoint
537    
538 agaffney 509 ##
539 agaffney 514 # Sets the mount options for the partition
540     # @param mountopts Mount options
541 agaffney 107 def set_mountopts(self, mountopts):
542     self._mountopts = mountopts
543    
544 agaffney 509 ##
545 agaffney 514 # Returns the mount options for the partition
546 agaffney 107 def get_mountopts(self):
547     return self._mountopts
548    
549 agaffney 509 ##
550 agaffney 514 # Set whether to format the partition
551 agaffney 523 # @param format Format this partition (True/False)
552 agaffney 107 def set_format(self, format):
553     self._format = format
554    
555 agaffney 509 ##
556 agaffney 514 # Returns whether to format the partition
557 agaffney 107 def get_format(self):
558     return self._format
559    
560 agaffney 509 ##
561 agaffney 523 # Returns whether the partition is resizeable
562     def is_resizeable(self):
563     return self._resizeable
564    
565     ##
566     # Returns minimum MB for resize
567     def get_min_mb_for_resize(self):
568     # if self.is_extended():
569     # min_size = self._start
570     # for part in self._device._partitions:
571     # if part < 5: continue
572     # if part.get_end > min_size: min_size = part.get_end()
573     # return min_size
574     # else:
575     if self._resizeable:
576     return self._min_mb_for_resize
577 agaffney 107 else:
578 agaffney 523 return -1
579 agaffney 107
580 agaffney 509 ##
581 agaffney 523 # Returns maximum MB for resize
582     def get_max_mb_for_resize(self):
583 agaffney 544 if self._resizeable:
584     free_minor = 0
585     if self.is_logical():
586 agaffney 639 free_minor = self._minor + FREE_MINOR_FRAC_LOG
587 agaffney 544 else:
588 agaffney 639 free_minor = self._minor + FREE_MINOR_FRAC_PRI
589 agaffney 544 if free_minor in self._device._partitions:
590     return self._mb + self._device._partitions[free_minor]._mb
591     else:
592     return self._mb
593 agaffney 523 else:
594 agaffney 544 return -1
595 agaffney 107
596 agaffney 509 ##
597 agaffney 544 # Resizes the partition
598     # @param mb New size in MB
599     def resize(self, mb):
600     free_minor = self._minor
601 agaffney 107 if self.is_logical():
602 agaffney 639 free_minor += FREE_MINOR_FRAC_LOG
603 agaffney 544 else:
604 agaffney 639 free_minor += FREE_MINOR_FRAC_PRI
605 agaffney 544 if mb < self._mb:
606     # Shrinking
607     if not free_minor in self._device._partitions:
608     self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
609     self._device._partitions[free_minor]._mb += self._mb - mb
610     self._mb = mb
611     elif mb == self._mb + self._device._partitions[free_minor]._mb:
612     # Using all available unallocated space
613     del self._device._partitions[free_minor]
614     self._mb = mb
615     elif mb > self._mb:
616     # Growing
617     self._device._partitions[free_minor]._mb = mb - self._mb
618     self._mb = mb
619 agaffney 107
620 agaffney 514 ##
621     # Returns a list of detected partitionable devices
622 agaffney 107 def detect_devices():
623     devices = []
624    
625     # Make sure sysfs exists
626     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     for dev_type in [ "ide", "scsi" ]: # Other device types? usb? fw?
673     if os.path.exists("/sys/bus/" + dev_type):
674     sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
675    
676     # For each device in the devices on that bus
677     for sysfs_device in sysfs_devices:
678 agaffney 556 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
679 agaffney 107
680     # If the file is not a block device, loop
681     if not os.path.exists(dev_file):
682     continue
683    
684     # Get the major and minor info
685     try:
686     major, minor = open(dev_file).read().split(":")
687     major = int(major)
688     minor = int(minor)
689     except:
690 agaffney 556 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
691 agaffney 107
692     # Find a device listed in /proc/partitions
693     # that has the same minor and major as our
694     # current block device.
695     for record in partitions:
696     if major == record[0] and minor == record[1]:
697     devices.append(record[2])
698 agaffney 595
699     # For testing the partitioning code
700     if GLIUtility.is_file("/tmp/disk.img"):
701     devices.append("/tmp/disk.img")
702    
703 agaffney 107 # We have assembled the list of devices, so return it
704     return devices

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20