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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 509 - (show annotations) (download) (as text)
Thu Apr 14 15:44:03 2005 UTC (15 years, 6 months ago) by agaffney
File MIME type: text/x-python
File size: 27120 byte(s)
  Use blackace's one-liner to add comments for all function for use with
  pythondoc.

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20