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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 628 - (show annotations) (download) (as text)
Wed Jun 8 03:29:59 2005 UTC (15 years, 4 months ago) by agaffney
File MIME type: text/x-python
File size: 25542 byte(s)
Minor fix recommended by pychecker

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20