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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20