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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 916 - (hide annotations) (download) (as text)
Wed Sep 21 03:15:04 2005 UTC (15 years, 1 month ago) by agaffney
File MIME type: text/x-python
File size: 26129 byte(s)
rewrite do_recommended() to be more robust

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20