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

Diff of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 530 Revision 596
1import commands, string, re, os, parted 1import commands, string, os, parted
2#from decimal import Decimal 2from GLIException import *
3import GLIUtility
3 4
4MEGABYTE = 1024 * 1024 5MEGABYTE = 1024 * 1024
5 6
6archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False }, 7archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
7 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False }, 8 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
8 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True }, 9 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
10 'amd64': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
9 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False } 11 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
10 } 12 }
11 13
12## 14##
13# This class provides a partitioning abstraction for the frontends 15# This class provides a partitioning abstraction for the frontends
24 _parted_dev = None 26 _parted_dev = None
25 _parted_disk = None 27 _parted_disk = None
26 _sector_bytes = 0 28 _sector_bytes = 0
27 _total_mb = 0 29 _total_mb = 0
28 _arch = None 30 _arch = None
31 _disklabel = None
29 32
30 ## 33 ##
31 # Initialization function for GLIStorageDevice class 34 # Initialization function for GLIStorageDevice class
32 # @param device Device node (e.g. /dev/hda) of device being represented 35 # @param device Device node (e.g. /dev/hda) of device being represented
33 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now) 36 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
34 def __init__(self, device, arch="x86"): 37 def __init__(self, device, arch="x86", set_geometry=True):
35 self._device = device 38 self._device = device
36 self._partitions = {} 39 self._partitions = {}
37 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 40 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
38 self._total_bytes = 0 41 self._total_bytes = 0
39 self._cylinder_bytes = 0 42 self._cylinder_bytes = 0
40 self._arch = arch 43 self._arch = arch
41 self._parted_dev = parted.PedDevice.get(self._device) 44 self._parted_dev = parted.PedDevice.get(self._device)
45 try:
42 self._parted_disk = parted.PedDisk.new(self._parted_dev) 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:
43 self.set_disk_geometry_from_disk() 51 self.set_disk_geometry_from_disk()
44 52
45 ## 53 ##
46 # Sets disk geometry info from disk. This function is used internally by __init__() 54 # Sets disk geometry info from disk. This function is used internally by __init__()
47 def set_disk_geometry_from_disk(self): 55 def set_disk_geometry_from_disk(self):
48 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 56 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
82 90
83 ## 91 ##
84 # Imports partition info from the install profile partition structure (currently does nothing) 92 # Imports partition info from the install profile partition structure (currently does nothing)
85 # @param ips Parameter structure returned from install_profile.get_partition_tables() 93 # @param ips Parameter structure returned from install_profile.get_partition_tables()
86 def set_partitions_from_install_profile_structure(self, ips): 94 def set_partitions_from_install_profile_structure(self, ips):
87 pass
88# for part in ips: 95 for part in ips:
89# tmppart = ips[part] 96 tmppart = ips[part]
90# existing = False 97 existing = False
91# parted_part = self._parted_disk.get_partition(part) 98 if tmppart['origminor'] and not tmppart['format']:
92# if parted_part != None:
93# start = parted_part.geom.start / self._sectors_in_cylinder
94# end = parted_part.geom.end / self._sectors_in_cylinder
95# fs_type = ""
96# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
97# if parted_part.type == 2: fs_type = "extended"
98# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
99# existing = True 99 existing = True
100# 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 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=['mountopts'])
101 101
102 ## 102 ##
103 # Returns name of device (e.g. /dev/hda) being represented 103 # Returns name of device (e.g. /dev/hda) being represented
104 def get_device(self): 104 def get_device(self):
105 return self._device 105 return self._device
125 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb()) 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] 126 del self._partitions[part_log]
127 else: 127 else:
128 if not last_log_free: 128 if not last_log_free:
129 last_log_free = last_log_minor + 0.9 129 last_log_free = last_log_minor + 0.9
130 else:
131 lost_log_free = part_log
130 tmppart_log.set_minor(last_log_free) 132 tmppart_log.set_minor(last_log_free)
131 self._partitions[last_log_free] = tmppart_log 133 self._partitions[last_log_free] = tmppart_log
132 if part_log != last_log_free: del self._partitions[part_log] 134 if part_log != last_log_free: del self._partitions[part_log]
133 continue 135 continue
134 last_log_free = part_log
135 else: 136 else:
136 if part_log > (last_log_minor + 1): 137 if part_log > (last_log_minor + 1):
137 tmppart_log.set_minor(last_log_minor + 1) 138 tmppart_log.set_minor(last_log_minor + 1)
138 last_log_minor = last_log_minor + 1 139 last_log_minor = last_log_minor + 1
139 self._partitions[last_log_minor] = tmppart_log 140 self._partitions[last_log_minor] = tmppart_log
140 del self._partitions[part_log] 141 del self._partitions[part_log]
141 continue 142 continue
143 else:
142 last_log_minor = part_log 144 last_log_minor = part_log
143 if tmppart.get_type() == "free": 145 if tmppart.get_type() == "free":
144 if last_minor < last_free: 146 if last_minor < last_free:
145 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb()) 147 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
146 del self._partitions[part] 148 del self._partitions[part]
147 else: 149 else:
148 if not last_free: 150 if not last_free:
149 last_free = last_minor + 0.1 151 last_free = last_minor + 0.1
152 else:
153 last_free = part
150 tmppart.set_minor(last_free) 154 tmppart.set_minor(last_free)
151 self._partitions[last_free] = tmppart 155 self._partitions[last_free] = tmppart
152 if part != last_free: del self._partitions[part] 156 if part != last_free: del self._partitions[part]
153 continue 157 continue
154 last_free = part
155 else: 158 else:
156 if part > (last_minor + 1): 159 if part > (last_minor + 1):
157 tmppart.set_minor(last_minor + 1) 160 tmppart.set_minor(last_minor + 1)
158 last_minor = last_minor + 1 161 last_minor = last_minor + 1
159 self._partitions[last_minor] = tmppart 162 self._partitions[last_minor] = tmppart
160 del self._partitions[part] 163 del self._partitions[part]
161 continue 164 continue
165 else:
162 last_minor = part 166 last_minor = part
163 167
164 ## 168 ##
165 # Adds a new partition to the partition info 169 # Adds a new partition to the partition info
166 # @param free_minor minor of unallocated space partition is being created in 170 # @param free_minor minor of unallocated space partition is being created in
167 # @param mb size of partition in MB 171 # @param mb size of partition in MB
320 tmppart = self._partitions[part] 324 tmppart = self._partitions[part]
321 partlist.append(part) 325 partlist.append(part)
322 if tmppart.is_extended(): 326 if tmppart.is_extended():
323 for part_log in parts: 327 for part_log in parts:
324 if part_log < 4.9: continue 328 if part_log < 4.9: continue
325 tmppart_log = self._partitions[part_log]
326 partlist.append(part_log) 329 partlist.append(part_log)
327 return partlist 330 return partlist
328 331
329 ## 332 ##
330 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables() 333 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
343 if tmppart.is_extended(): 346 if tmppart.is_extended():
344 return part 347 return part
345 return 0 348 return 0
346 349
347 ## 350 ##
351 # Sets the disklabel type
352 def set_disklabel(self, disklabel):
353 self._disklabel = disklabel
354
355 ##
356 # Returns the disklabel type
357 def get_disklabel(self):
358 return self._disklabel
359
360 ##
348 # Returns the number of sectors on the device 361 # Returns the number of sectors on the device
349 def get_num_sectors(self): 362 def get_num_sectors(self):
350 return int(self._total_sectors) 363 return int(self._total_sectors)
351 364
352 ## 365 ##
382 ## 395 ##
383 # Prints disk geometry to STDOUT (no longer used) 396 # Prints disk geometry to STDOUT (no longer used)
384 def print_geometry(self): 397 def print_geometry(self):
385 print self._total_bytes, self._geometry 398 print self._total_bytes, self._geometry
386 399
387 ##
388 # Utility function for raising an exception
389 # @param message Error message
390 def _error(self, message):
391 "Raises an exception"
392 raise "DeviceObjectError", message
393
394 ## 400 ##
395 # Utility function for running a command and returning it's output as a list 401 # Utility function for running a command and returning it's output as a list
396 # @param cmd Command to run 402 # @param cmd Command to run
397 def _run(self, cmd): 403 def _run(self, cmd):
398 "Runs a command and returns the output" 404 "Runs a command and returns the output"
445 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.) 451 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
446 # @param mountpoint='' Mountpoint of partition 452 # @param mountpoint='' Mountpoint of partition
447 # @param mountopts='' Mount options of partition 453 # @param mountopts='' Mount options of partition
448 # @param format=True Format partition 454 # @param format=True Format partition
449 # @param existing=False This partition exists on disk 455 # @param existing=False This partition exists on disk
450 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False): 456 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False, origminor=0):
451 self._device = device 457 self._device = device
452 self._minor = float(minor) 458 self._minor = float(minor)
453 if existing: self._orig_minor = int(minor)
454 self._start = int(start) 459 self._start = int(start)
455 self._end = int(end) 460 self._end = int(end)
456 self._type = type or "unknown" 461 self._type = type or "unknown"
457 self._mountpoint = mountpoint 462 self._mountpoint = mountpoint
458 self._mountopts = mountopts 463 self._mountopts = mountopts
459 self._format = format 464 self._format = format
460 self._mb = mb 465 self._mb = mb
466 self._orig_minor = origminor
467 if type != "free":
468 if existing and not origminor:
469 self._orig_minor = self._minor
470 self._minor = int(self._minor)
471 self._orig_minor = int(self._orig_minor)
461 if existing: 472 if existing:
462 self._orig_minor = self._minor
463 parted_part = device._parted_disk.get_partition(minor) 473 parted_part = device._parted_disk.get_partition(self._orig_minor)
464 label_type = device._parted_disk.type.name 474 label_type = device._parted_disk.type.name
465 if label_type == "loop": 475 if label_type == "loop":
466 dev_node = device._device 476 dev_node = device._device
467 else: 477 else:
468 dev_node = device._device + str(minor) 478 dev_node = device._device + str(self._orig_minor)
479 print "dev_node = " + dev_node
469 if type == "ntfs": 480 if type == "ntfs":
470 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 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 .\+//'"))
471 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1 482 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
472 self._resizeable = True 483 self._resizeable = True
473 elif type == "ext2" or type == "ext3": 484 elif type == "ext2" or type == "ext3":
476 free_bytes = int(block_size * free_blocks) 487 free_bytes = int(block_size * free_blocks)
477 # can't hurt to pad (the +50) it a bit since this is really just a guess 488 # can't hurt to pad (the +50) it a bit since this is really just a guess
478 self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50 489 self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50
479 self._resizeable = True 490 self._resizeable = True
480 else: 491 else:
481 parted_part = self._device._parted_disk.get_partition(int(self._minor)) 492 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
482 try: 493 try:
483 parted_fs = parted_part.geom.file_system_open() 494 parted_fs = parted_part.geom.file_system_open()
484 except: 495 except:
485 self._resizeable = False 496 self._resizeable = False
486 return 497 return
514 # Returns a list of logical partitions if this is an extended partition 525 # Returns a list of logical partitions if this is an extended partition
515 def get_logicals(self): 526 def get_logicals(self):
516 if not self.is_extended(): 527 if not self.is_extended():
517 return None 528 return None
518 logicals = [] 529 logicals = []
519 start = self._start
520 parts = self._device._partitions.keys() 530 parts = self._device._partitions.keys()
521 parts.sort() 531 parts.sort()
522 for part in parts: 532 for part in parts:
523 if part < 5: continue 533 if part < 5: continue
524 logicals.append(part) 534 logicals.append(part)
658 return -1 668 return -1
659 669
660 ## 670 ##
661 # Returns maximum MB for resize 671 # Returns maximum MB for resize
662 def get_max_mb_for_resize(self): 672 def get_max_mb_for_resize(self):
673 if self._resizeable:
663 free_minor = 0 674 free_minor = 0
675 if self.is_logical():
676 free_minor = self._minor + 0.9
677 else:
678 free_minor = self._minor + 0.1
679 if free_minor in self._device._partitions:
680 return self._mb + self._device._partitions[free_minor]._mb
681 else:
682 return self._mb
683 else:
684 return -1
685
686 ##
687 # Resizes the partition
688 # @param mb New size in MB
689 def resize(self, mb):
690 free_minor = self._minor
664 if self.is_logical(): 691 if self.is_logical():
665 free_minor = self._minor + 0.9 692 free_minor += 0.9
666 else: 693 else:
667 free_minor = self._minor + 0.1 694 free_minor += 0.1
695 if mb < self._mb:
696 # Shrinking
668 if free_minor in self._device._partitions: 697 if not free_minor in self._device._partitions:
698 self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
699 self._device._partitions[free_minor]._mb += self._mb - mb
700 self._mb = mb
669 return self._mb + self._device._partitions[free_minor]._mb 701 elif mb == self._mb + self._device._partitions[free_minor]._mb:
670 else: 702 # Using all available unallocated space
671 return self._mb 703 del self._device._partitions[free_minor]
672 704 self._mb = mb
673 ## 705 elif mb > self._mb:
674 # Resizes the partition (ignore) 706 # Growing
675 # @param start New start sector 707 self._device._partitions[free_minor]._mb = mb - self._mb
676 # @param end New end sector 708 self._mb = mb
677 def resize(self, start, end):
678 part_at_start = self._device.get_partition_at(int(start))
679 part_at_end = self._device.get_partition_at(int(end))
680 logicals = None
681 if self.is_logical():
682 parent = self.get_extended_parent()
683 parentstart = int(self._device._partitions[parent].get_start())
684 parentend = int(self._device._partitions[parent].get_end())
685 if (start < parentstart) or (end > parentend): return 0
686 if self.is_extended():
687 logicals = self.get_logicals()
688 if len(logicals):
689 logicals_start = self._device._partitions[logicals[0]].get_start()
690 logicals_end = self._device._partitions[logicals[len(logicals)-1]].get_end()
691 if (start > logicals_start) or (end < logicals_end): return 0
692 if part_at_start in logicals: part_at_start = 0
693 if part_at_end in logicals: part_at_end = 0
694 if ((not part_at_start == 0) and (part_at_start != self._minor)) or ((not part_at_end == 0) and (part_at_end != self._minor)):
695 return 0
696 self.set_start(start)
697 self.set_end(end)
698 return 1
699
700 ##
701 # Utility function to raise an exception
702 # @param message Error message
703 def _error(self, message):
704 raise "PartitionObjectError", message
705 709
706## 710##
707# Returns a list of detected partitionable devices 711# Returns a list of detected partitionable devices
708def detect_devices(): 712def detect_devices():
709 devices = [] 713 devices = []
710 714
711 # Make sure sysfs exists 715 # Make sure sysfs exists
712 if not os.path.exists("/sys/bus"): 716 if not os.path.exists("/sys/bus"):
713 raise Exception, "no sysfs found (you MUST use a kernel >2.6)" 717 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
714 # Make sure /proc/partitions exists 718 # Make sure /proc/partitions exists
715 if not os.path.exists("/proc/partitions"): 719 if not os.path.exists("/proc/partitions"):
716 raise Exception, "/proc/partitions does not exist!" 720 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
717 721
718 # Load /proc/partitions into the variable 'partitions' 722 # Load /proc/partitions into the variable 'partitions'
719 partitions = [] 723 partitions = []
720 for line in open("/proc/partitions"): 724 for line in open("/proc/partitions"):
721 if len(line.split()) < 4 or not line.split()[0].isdigit() or \ 725 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
722 not line.split()[1].isdigit():
723 continue 726 continue
724 727
725 # Get the major, minor and device name 728 # Get the major, minor and device name
726 major = line.split()[0] 729 major = line.split()[0]
727 minor = line.split()[1] 730 minor = line.split()[1]
737 # all the devices in /dev to try and find a 740 # all the devices in /dev to try and find a
738 # devices with the same major and minor 741 # devices with the same major and minor
739 if not os.path.exists(device): 742 if not os.path.exists(device):
740 device = None 743 device = None
741 for path, dirs, files in os.walk("/dev"): 744 for path, dirs, files in os.walk("/dev"):
742 for file in files: 745 for d_file in files:
743 full_file = os.path.join(path, file) 746 full_file = os.path.join(path, d_file)
744 if not os.path.exists(full_file): 747 if not os.path.exists(full_file):
745 continue 748 continue
746 statres = os.stat(full_file) 749 statres = os.stat(full_file)
747 fmaj = os.major(statres.st_rdev) 750 fmaj = os.major(statres.st_rdev)
748 fmin = os.minor(statres.st_rdev) 751 fmin = os.minor(statres.st_rdev)
760 if os.path.exists("/sys/bus/" + dev_type): 763 if os.path.exists("/sys/bus/" + dev_type):
761 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices") 764 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
762 765
763 # For each device in the devices on that bus 766 # For each device in the devices on that bus
764 for sysfs_device in sysfs_devices: 767 for sysfs_device in sysfs_devices:
765 dev_file = "/sys/bus/" + dev_type + "/devices/"\ 768 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
766 + sysfs_device + "/block/dev"
767 769
768 # If the file is not a block device, loop 770 # If the file is not a block device, loop
769 if not os.path.exists(dev_file): 771 if not os.path.exists(dev_file):
770 continue 772 continue
771 773
773 try: 775 try:
774 major, minor = open(dev_file).read().split(":") 776 major, minor = open(dev_file).read().split(":")
775 major = int(major) 777 major = int(major)
776 minor = int(minor) 778 minor = int(minor)
777 except: 779 except:
778 raise Exception, "invalid major minor in "\ 780 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
779 + dev_file
780 781
781 # Find a device listed in /proc/partitions 782 # Find a device listed in /proc/partitions
782 # that has the same minor and major as our 783 # that has the same minor and major as our
783 # current block device. 784 # current block device.
784 for record in partitions: 785 for record in partitions:
785 if major == record[0] and minor == record[1]: 786 if major == record[0] and minor == record[1]:
786 devices.append(record[2]) 787 devices.append(record[2])
787 788
789 # For testing the partitioning code
790 if GLIUtility.is_file("/tmp/disk.img"):
791 devices.append("/tmp/disk.img")
792
788 # We have assembled the list of devices, so return it 793 # We have assembled the list of devices, so return it
789 return devices 794 return devices

Legend:
Removed from v.530  
changed lines
  Added in v.596

  ViewVC Help
Powered by ViewVC 1.1.20