def __init__(self, device_path, arch="x86"): self._arch = arch self._path = "" self._device = None self._model = "" self._disk = None self._partitions = {} self._disklabel = "" self._length = 0 # total sectors self._sector_size = 0 self._parted_type = deviceType dev = parted.PedDevice.get(device_path) self._model = dev.model self._length = dev.length self._sector_size = dev.sector_size self._dev = dev try: self._disk = parted.PedDisk.new(dev) except: label = archinfo[self._arch]["disklabel"] disk_type = parted.disk_type_get(label) self._disk = self._dev.disk_new_fresh(disk_type) self._disklabel = self._disk.type.name self._path = device_path self.update()
def single_partition_device_1_x(device, vars, log): lvm_flag = parted.partition_flag_get_by_name("lvm") try: log.write("Using pyparted 1.x\n") # wipe the old partition table utils.sysexec("dd if=/dev/zero of=%s bs=512 count=1" % device, log) # get the device dev = parted.PedDevice.get(device) # create a new partition table disk = dev.disk_new_fresh(parted.disk_type_get("msdos")) # create one big partition on each block device constraint = dev.constraint_any() new_part = disk.partition_new(parted.PARTITION_PRIMARY, parted.file_system_type_get("ext2"), 0, 1) # make it an lvm partition new_part.set_flag(lvm_flag, 1) # actually add the partition to the disk disk.add_partition(new_part, constraint) disk.maximize_partition(new_part, constraint) disk.commit() del disk except BootManagerException, e: log.write("BootManagerException while running: %s\n" % str(e)) return 0
def __init__(self, partitions=None, device=None, scan=False): if partitions == None: self.partitions = [] else: self.partitions = partitions # XXX - if a disk label isn't initialized, go ahead and initialize it # this could cause some ramifications if we detect a disk with a disk # label that parted doesn't understand which we may want to prompt # for. Ideally we should warn the user here that the disk they're # trying to partition can not be used. if device: self.partedDevice = device.partedDevice try: self.partedDisk = parted.PedDisk.new(self.partedDevice) except parted.error: try: self.partedDisk = self.partedDevice.disk_new_fresh( parted.disk_type_get("msdos")) self.partedDisk.commit() except parted.error, msg: log.error("Couldn't initialize %s: %s." % (device.path, msg)) return
def getPartitionsOfDevice(device_path): """Returns all partitions of a given device but swap partition""" def getPartitionInfo(part): partition = {} if part.num >= 1: fs_name = "" if part.fs_type and part.fs_type.name != 'linux-swap(new)': if part.fs_type.name == 'fat16' or part.fs_type.name == 'fat32': part_type = 'vfat' elif part.fs_type.name == 'ntfs': part_type = 'ntfs-3g' else: part_type = part.fs_type.name return (device_path + str(part.num), {"mount_point": '', "file_system": part_type, "options": '', "dump_freq": '0', "fs_pass_no": '0'}) return partition dev = parted.PedDevice.get(device_path) try: disk = parted.PedDisk.new(dev) except: label = archinfo['x86']["disklabel"] disk_type = parted.disk_type_get(label) disk = dev.disk_new_fresh(disk_type) part = disk.next_partition() while part: info = getPartitionInfo(part) if info: yield info part = disk.next_partition(part)
def createPartitions(self, devicename, partitions, samesize): import parted #IDEA compare the partition configurations for update #1. delete all aprtitions # old way RHEL4/5 dev=parted.PedDevice.get(devicename) disk=parted.PedDisk.new(dev) try: disk.delete_all() except parted.error: #FIXME use generic disk types disk=dev.disk_new_fresh(parted.disk_type_get("msdos")) # create new partitions for part in partitions: partedtype=part.getPartedType() if samesize: size=self.getSectorSize(part.getAttribute("size"), dev) else: size=self.getSectorSizeOptimum(part.getAttribute("size"), dev) flags=part.getFlags() partedflags=list() for flag in flags: partedflags.append(flag.getFlagPartedNum()) self.log.debug("creating partition: size: %i" % size ) self.add_partition(disk, partedtype, size, partedflags) disk.commit()
def getPartitionsOfDevice(device_path): """Returns all partitions of a given device but swap partition""" def getPartitionInfo(part): partition = {} if part.num >= 1: fs_name = "" if part.fs_type and part.fs_type.name != 'linux-swap': return (device_path + str(part.num), {"mount_point": None, "file_system": part.fs_type.name, "options": None, "dump_freq": None, "fs_pass_no": None}) return partition dev = parted.PedDevice.get(device_path) try: disk = parted.PedDisk.new(dev) except: disk_type = parted.disk_type_get("msdos") disk = dev.disk_new_fresh(disk_type) part = disk.next_partition() while part: info = getPartitionInfo(part) if info: yield info part = disk.next_partition(part)
def create_partition_table(self): self.primarynum = 1 self.extnum = 5 self.isext = 0 self.disk = self.drive.disk_create(parted.disk_type_get("msdos")) return self.get_freespace()
def __init__(self, device, arch="x86", set_geometry=True, local_device=True): self._device = device self._partitions = [] self._geometry = { 'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512 } self._total_bytes = 0 self._cylinder_bytes = 0 self._arch = arch self._local_device = local_device if self._local_device: self._parted_dev = parted.PedDevice.get(self._device) try: self._parted_disk = parted.PedDisk.new(self._parted_dev) except: self._parted_disk = self._parted_dev.disk_new_fresh( parted.disk_type_get(archinfo[self._arch])) self._disklabel = self._parted_disk.type.name else: self._disklabel = archinfo[self._arch] self._labelinfo = labelinfo[self._disklabel] if set_geometry: self.set_disk_geometry_from_disk()
def __init__(self, device_path, arch="x86"): self._arch = arch self._path = "" self._device = None self._model = "" self._disk = None self._partitions = [] self._disklabel = "" self._length = 0 # total sectors self._sector_size = 0 self._parted_type = deviceType self._needs_commit = False dev = parted.PedDevice.get(device_path) self._model = dev.model self._length = dev.length self._sector_size = dev.sector_size self._dev = dev try: self._disk = parted.PedDisk.new(dev) except: label = archinfo[self._arch]["disklabel"] disk_type = parted.disk_type_get(label) self._disk = self._dev.disk_new_fresh(disk_type) self._disklabel = self._disk.type.name self._path = device_path self.update()
def clear(self): self.partedDisk.delete_all() # re-init the disk in case the partition table is corrupt self.partedDisk = self.partedDevice.disk_new_fresh( parted.disk_type_get("msdos")) self.partedDisk.commit() self.partitions = [] self.scanPartitionsOnDevice()
def blockPartitions(dev): pdev = parted.PedDevice.get(dev) try: disk = parted.PedDisk.new(pdev) except: # FIXME: replace with what exception could we get here, bare except sucks disk = pdev.disk_new_fresh(parted.disk_type_get("msdos")) part = disk.next_partition() while part: if part.fs_type and part.fs_type.name != "linux-swap(new)": yield dev + str(part.num), part.fs_type.name part = disk.next_partition(part)
def createPartitionsParted(self): import parted import ComParted if not self.exists(): raise ComException("Device %s not found" % self.getDeviceName()) phelper=ComParted.PartedHelper() #IDEA compare the partition configurations for update #1. delete all aprtitions dev=parted.PedDevice.get(self.getDeviceName()) try: disk=parted.PedDisk.new(dev) disk.delete_all() except parted.error: #FIXME use generic disk types disk=dev.disk_new_fresh(parted.disk_type_get("msdos")) # create new partitions for com_part in self.getAllPartitions(): type=com_part.getPartedType() if self.sameSize(): size=com_part.getPartedSize(dev) else: size=com_part.getPartedSizeOptimum(dev) flags=com_part.getPartedFlags() self.log.debug("creating partition: size: %i" % size ) phelper.add_partition(disk, type, size, flags) disk.commit() self.commit() #dev.sync() #dev.close() # run partx if the device is a multipath device self.log.debug("ComHostDisk: checking for multipath devices") if self.isDMMultipath(): self.log.debug("Device %s is a dm_multipath device, adding partitions" %self.getDeviceName()) __cmd=CMD_KPARTX + " -d " + self.getDeviceName() try: __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) __cmd=CMD_KPARTX + " -a " + self.getDeviceName() __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) #FIXME: crappy fix to give os some time to create devicefiles. time.sleep(10) except ComSystem.ExecLocalException, ele: ComLog.debugTraceLog(self.log) self.log.debug("Could not execute %s. Error %s" %(ele.cmd, ele))
def getDefaultDiskType(): """Get the default partition table type for this architecture.""" if iutil.getArch() == "i386": return parted.disk_type_get("msdos") elif iutil.getArch() == "ia64": return parted.disk_type_get("gpt") elif iutil.getArch() == "s390": return parted.disk_type_get("dasd") elif iutil.getArch() == "alpha": return parted.disk_type_get("bsd") elif iutil.getArch() == "sparc": return parted.disk_type_get("sun") elif iutil.getArch() == "ppc": return parted.disk_type_get("msdos") else: return parted.disk_type_get("msdos")
def __init__(self, device_path, arch="x86"): self._arch = arch self._path = "" self._device = None self._model = "" self._disk = None self._partitions = [] self._disklabel = "" self._length = 0 # total sectors self._sector_size = 0 self._parted_type = deviceType self._isRaid = False self._path = device_path self.checkRaid() try: self._dev = parted.PedDevice.get(device_path) except: print "parted PedDevice error on %s " % device_path return 0 if self._isRaid: self._model = "software raid" else: self._model = self._dev.model self._length = self._dev.length self._sector_size = self._dev.sector_size try: self._disk = parted.PedDisk.new(self._dev) except: print "parted PedDisk error on %s, assuming free space " % device_path label = archinfo[self._arch]["disklabel"] disk_type = parted.disk_type_get(label) self._disk = self._dev.disk_new_fresh(disk_type) self._disklabel = self._disk.type.name if self._isRaid: self.raidSanityCheck() #else: # self.update() self.update()
def single_partition_device_1_x(device, vars, log): lvm_flag = parted.partition_flag_get_by_name('lvm') try: log.write("Using pyparted 1.x\n") # wipe the old partition table utils.sysexec("dd if=/dev/zero of={} bs=512 count=1".format(device), log) # get the device dev = parted.PedDevice.get(device) # create a new partition table disk = dev.disk_new_fresh(parted.disk_type_get("msdos")) # create one big partition on each block device constraint = dev.constraint_any() new_part = disk.partition_new(parted.PARTITION_PRIMARY, parted.file_system_type_get("ext2"), 0, 1) # make it an lvm partition new_part.set_flag(lvm_flag, 1) # actually add the partition to the disk disk.add_partition(new_part, constraint) disk.maximize_partition(new_part, constraint) disk.commit() del disk except BootManagerException as e: log.write("BootManagerException while running: {}\n".format(str(e))) return 0 except parted.error as e: log.write("parted exception while running: {}\n".format(str(e))) return 0 return 1
def __init__(self, device, arch="x86", set_geometry=True, local_device=True): self._device = device self._partitions = [] self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} self._total_bytes = 0 self._cylinder_bytes = 0 self._arch = arch self._local_device = local_device if self._local_device: self._parted_dev = parted.PedDevice.get(self._device) try: self._parted_disk = parted.PedDisk.new(self._parted_dev) except: self._parted_disk = self._parted_dev.disk_new_fresh(parted.disk_type_get(archinfo[self._arch])) self._disklabel = self._parted_disk.type.name else: self._disklabel = archinfo[self._arch] self._labelinfo = labelinfo[self._disklabel] if set_geometry: self.set_disk_geometry_from_disk()
def getPartitions(device): """Returns all partitions and their filesystems of a given device but swap partition""" import parted partitions = [] pdev = parted.PedDevice.get(device) try: disk = parted.PedDisk.new(pdev) except: disk = pdev.disk_new_fresh(parted.disk_type_get("msdos")) part = disk.next_partition() while part: if part.fs_type and part.fs_type.name != "linux-swap(new)": partitions.append( device + str(part.num) + ":" + part.fs_type.name ) part = disk.next_partition(part) return partitions
def getDefaultDiskType(): return parted.disk_type_get("msdos")
def partition(self): """ TODO: before step 3, wipe drive and use the default disklabel for arch skip fixed partitions in all passes (in GLISD maybe?) """ parts_old = {} parts_new = self._install_profile.get_partition_tables() for device in GLIStorageDevice.detect_devices(): parts_old[device] = GLIStorageDevice.Device(device, arch=self._client_configuration.get_architecture_template()) parts_old[device].set_partitions_from_disk() self.notify_frontend("progress", (0, "Examining partitioning data")) total_steps = float(len(parts_new) * 4) # 4 for the number of passes over each device cur_progress = 0 for device in parts_new: # Skip this device in parts_new if device isn't detected on current system if not device in parts_old: self._logger.log("There is no physical device " + device + " detected to match the entry in the install profile...skipping") continue # This just makes things simpler in the code newparts = parts_new[device] oldparts = parts_old[device] # Check to see if the old and new partition table structures are the same...skip if they are if not self._check_table_changed(oldparts, newparts): self._logger.log("Partition table for " + device + " is unchanged...skipping") continue self._logger.log("partition(): Processing " + device + "...") # Commit ritual sepuku if there are any mounted filesystems on this device if GLIUtility.spawn("mount | grep '^" + device + "'", return_output=True)[1].strip(): raise GLIException("PartitionsMountedError", 'fatal', 'partition', "Cannot partition " + device + " due to filesystems being mounted") # We also can't handle "unknown" partitions for part in newparts: if newparts[part]['type'] == "unknown": raise GLIException("UnknownPartitionTypeError", 'fatal', 'partition', "Refusing to partition this drive due to the presence of an unknown type of partition") # Create pyparted objects for this device parted_dev = parted.PedDevice.get(device) try: parted_disk = parted.PedDisk.new(parted_dev) except: if self._debug: self._logger.log("partition(): could not load existing disklabel...creating new one") parted_disk = parted_dev.disk_new_fresh(parted.disk_type_get((newparts.get_disklabel() or GLIStorageDevice.archinfo[self._architecture_name]))) # Iterate through new partitions and check for 'origminor' and 'format' == False for part in newparts: tmppart_new = newparts[part] if not tmppart_new['origminor'] or tmppart_new['format']: continue if not tmppart_new['origminor'] in oldparts: raise GLIException("MissingPartitionsError", 'fatal', 'partition', "Cannot find the existing partition that a new one refers to. This is not a bug. This is in fact your (the user's) fault. You should not reuse the installprofile.xml from a previous install that started the partitioning step.") tmppart_old = oldparts[tmppart_new['origminor']] if parted_disk.type.check_feature(parted.DISK_TYPE_PARTITION_NAME): tmppart_new['name'] = tmppart_old['name'] tmppart_new['flags'] = tmppart_old['flags'] if tmppart_new['resized']: # Partition is being resized in the new layout self._logger.log(" Partition " + str(part) + " has origminor " + str(tmppart_new['origminor']) + " and it being resized...saving start sector " + str(tmppart_old['start'])) tmppart_new['start'] = tmppart_old['start'] tmppart_new['end'] = 0 else: # Partition is untouched in the new layout self._logger.log(" Partition " + str(part) + " has origminor " + str(tmppart_new['origminor']) + "...saving start sector " + str(tmppart_old['start']) + " and end sector " + str(tmppart_old['end'])) tmppart_new['start'] = tmppart_old['start'] tmppart_new['end'] = tmppart_old['end'] if self._check_table_layout_changed(parts_old[device], parts_new[device]): # First pass to delete old partitions that aren't resized self.notify_frontend("progress", (cur_progress / total_steps, "Deleting partitioning that aren't being resized for " + device)) cur_progress += 1 self._partition_delete_step(parted_disk, oldparts, newparts) # Second pass to resize old partitions that need to be resized self._logger.log("Partitioning: Second pass...") self.notify_frontend("progress", (cur_progress / total_steps, "Resizing remaining partitions for " + device)) cur_progress += 1 self._partition_resize_step(parted_disk, device, oldparts, newparts) # Wiping disk and creating blank disklabel try: parted_disk = parted_dev.disk_new_fresh(parted.disk_type_get(newparts.get_disklabel())) parted_disk.commit() except: raise GLIException("DiskLabelCreationError", 'fatal', 'partition', "Could not create a blank disklabel!") # Third pass to create new partition table self._logger.log("Partitioning: Third pass....creating partitions") self.notify_frontend("progress", (cur_progress / total_steps, "Recreating partition table for " + device)) cur_progress += 1 self._partition_recreate_step(parted_disk, newparts) else: cur_progress += 3 # Fourth pass to format partitions self._logger.log("Partitioning: formatting partitions") self.notify_frontend("progress", (cur_progress / total_steps, "Formatting partitions for " + device)) cur_progress += 1 self._partition_format_step(parted_disk, device, newparts) # All done for this device self.notify_frontend("progress", (cur_progress / total_steps, "Done with partitioning for " + device)) cur_progress += 1
def partition(self): """ TODO: before step 3, wipe drive and use the default disklabel for arch skip fixed partitions in all passes (in GLISD maybe?) """ parts_old = {} parts_new = self._install_profile.get_partition_tables() for device in GLIStorageDevice.detect_devices(): parts_old[device] = GLIStorageDevice.Device( device, arch=self._client_configuration.get_architecture_template()) parts_old[device].set_partitions_from_disk() self.notify_frontend("progress", (0, "Examining partitioning data")) total_steps = float(len(parts_new) * 4) # 4 for the number of passes over each device cur_progress = 0 for device in parts_new: # Skip this device in parts_new if device isn't detected on current system if not device in parts_old: self._logger.log( "There is no physical device " + device + " detected to match the entry in the install profile...skipping" ) continue # This just makes things simpler in the code newparts = parts_new[device] oldparts = parts_old[device] # Check to see if the old and new partition table structures are the same...skip if they are if not self._check_table_changed(oldparts, newparts): self._logger.log("Partition table for " + device + " is unchanged...skipping") continue self._logger.log("partition(): Processing " + device + "...") # Commit ritual sepuku if there are any mounted filesystems on this device if GLIUtility.spawn("mount | grep '^" + device + "'", return_output=True)[1].strip(): raise GLIException( "PartitionsMountedError", 'fatal', 'partition', "Cannot partition " + device + " due to filesystems being mounted") # We also can't handle "unknown" partitions for part in newparts: if newparts[part]['type'] == "unknown": raise GLIException( "UnknownPartitionTypeError", 'fatal', 'partition', "Refusing to partition this drive due to the presence of an unknown type of partition" ) # Create pyparted objects for this device parted_dev = parted.PedDevice.get(device) try: parted_disk = parted.PedDisk.new(parted_dev) except: if self._debug: self._logger.log( "partition(): could not load existing disklabel...creating new one" ) parted_disk = parted_dev.disk_new_fresh( parted.disk_type_get( (newparts.get_disklabel() or GLIStorageDevice.archinfo[self._architecture_name]))) # Iterate through new partitions and check for 'origminor' and 'format' == False for part in newparts: tmppart_new = newparts[part] if not tmppart_new['origminor'] or tmppart_new['format']: continue if not tmppart_new['origminor'] in oldparts: raise GLIException( "MissingPartitionsError", 'fatal', 'partition', "Cannot find the existing partition that a new one refers to. This is not a bug. This is in fact your (the user's) fault. You should not reuse the installprofile.xml from a previous install that started the partitioning step." ) tmppart_old = oldparts[tmppart_new['origminor']] if parted_disk.type.check_feature( parted.DISK_TYPE_PARTITION_NAME): tmppart_new['name'] = tmppart_old['name'] tmppart_new['flags'] = tmppart_old['flags'] if tmppart_new['resized']: # Partition is being resized in the new layout self._logger.log( " Partition " + str(part) + " has origminor " + str(tmppart_new['origminor']) + " and it being resized...saving start sector " + str(tmppart_old['start'])) tmppart_new['start'] = tmppart_old['start'] tmppart_new['end'] = 0 else: # Partition is untouched in the new layout self._logger.log(" Partition " + str(part) + " has origminor " + str(tmppart_new['origminor']) + "...saving start sector " + str(tmppart_old['start']) + " and end sector " + str(tmppart_old['end'])) tmppart_new['start'] = tmppart_old['start'] tmppart_new['end'] = tmppart_old['end'] if self._check_table_layout_changed(parts_old[device], parts_new[device]): # First pass to delete old partitions that aren't resized self.notify_frontend( "progress", (cur_progress / total_steps, "Deleting partitioning that aren't being resized for " + device)) cur_progress += 1 self._partition_delete_step(parted_disk, oldparts, newparts) # Second pass to resize old partitions that need to be resized self._logger.log("Partitioning: Second pass...") self.notify_frontend( "progress", (cur_progress / total_steps, "Resizing remaining partitions for " + device)) cur_progress += 1 self._partition_resize_step(parted_disk, device, oldparts, newparts) # Wiping disk and creating blank disklabel try: parted_disk = parted_dev.disk_new_fresh( parted.disk_type_get(newparts.get_disklabel())) parted_disk.commit() except: raise GLIException("DiskLabelCreationError", 'fatal', 'partition', "Could not create a blank disklabel!") # Third pass to create new partition table self._logger.log( "Partitioning: Third pass....creating partitions") self.notify_frontend( "progress", (cur_progress / total_steps, "Recreating partition table for " + device)) cur_progress += 1 self._partition_recreate_step(parted_disk, newparts) else: cur_progress += 3 # Fourth pass to format partitions self._logger.log("Partitioning: formatting partitions") self.notify_frontend("progress", (cur_progress / total_steps, "Formatting partitions for " + device)) cur_progress += 1 self._partition_format_step(parted_disk, device, newparts) # All done for this device self.notify_frontend("progress", (cur_progress / total_steps, "Done with partitioning for " + device)) cur_progress += 1