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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 535 - (show annotations) (download) (as text)
Fri Apr 29 06:40:30 2005 UTC (15 years, 6 months ago) by agaffney
File MIME type: text/x-python
File size: 25670 byte(s)
Minor fixes to
  GLIStorageDevice as suggested by pychecker.

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20