示例#1
0
static PedFileSystemType* getPedFileSystemType(FileSystem::Type t)
{
    for (quint32 i = 0; i < sizeof(mapFileSystemTypeToLibPartedName) / sizeof(mapFileSystemTypeToLibPartedName[0]); i++)
        if (mapFileSystemTypeToLibPartedName[i].type == t)
            return ped_file_system_type_get(mapFileSystemTypeToLibPartedName[i].name.toLatin1().constData());

    // if we didn't find anything, go with ext2 as a safe fallback
    return ped_file_system_type_get("ext2");
}
示例#2
0
文件: ui.c 项目: bcl/parted
int
command_line_get_fs_type (const char* prompt, const PedFileSystemType*(* value))
{
        char*                 fs_type_name;
        PedFileSystemType*    fs_type;

        fs_type_name = command_line_get_word (prompt,
                                              *value ? (*value)->name : NULL,
                                                     fs_type_list, 1);
        if (!fs_type_name) {
                ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
                                     _("Expecting a file system type."));
                return 0;
        }

        fs_type = ped_file_system_type_get (fs_type_name);
        if (!fs_type) {
                ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
                                     _("Unknown file system type \"%s\"."),
                                     fs_type_name);
                free (fs_type_name);
                return 0;
        }

        free (fs_type_name);
        *value = fs_type;
        return 1;
}
bool MParted::MParted_Core::setPartitionType(MParted::Partition & partition) {
    if (!openDeviceAndDisk(partition.devicePath))
        return false;

    bool return_value = false;

    //Lookup libparted file system type using MParted's name, as most match
    PedFileSystemType * fs_type = ped_file_system_type_get(partition.getFilesystemString().toStdString().c_str());

    //If not found, and FS is linux-swap, then try linux-swap(v1)
    if (!fs_type && partition.getFilesystemString() == "linux-swap")
        fs_type = ped_file_system_type_get("linux-swap(v1)");

    //If not found, and FS is linux-swap, then try linux-swap(new)
    if (!fs_type && partition.getFilesystemString() == "linux-swap")
        fs_type = ped_file_system_type_get("linux-swap(new)");

    //default is Linux (83)
    if (!fs_type)
        fs_type = ped_file_system_type_get("ext2");

    if (fs_type && partition.filesystem != MParted::FS_LVM2_PV) {
        PedPartition *pedPartition = ped_disk_get_partition_by_sector(pedDisk, partition.getSector());

        //Also clear any libparted LVM flag so that it doesn't override the file system type
        if (pedPartition
                && ped_partition_set_flag(pedPartition, PED_PARTITION_LVM, 0)
                && ped_partition_set_system(pedPartition, fs_type)
                && commit())
        return_value = true;
    }
    else if (partition.filesystem == MParted::FS_LVM2_PV) {
        PedPartition *pedPartition = ped_disk_get_partition_by_sector(pedDisk, partition.getSector());

        if (pedPartition
                && ped_partition_set_flag(pedPartition, PED_PARTITION_LVM, 1)
                && commit())
        return_value = true;
    }


    closeDeviceAndDisk();

    return return_value;
}
示例#4
0
/* _ped_FileSystemType -> PedFileSystemType functions */
PedFileSystemType *_ped_FileSystemType2PedFileSystemType(PyObject *s) {
    PedFileSystemType *ret = NULL;
    _ped_FileSystemType *type = (_ped_FileSystemType *) s;

    if (type == NULL) {
        PyErr_SetString(PyExc_TypeError, "Empty _ped.FileSystemType");
        return NULL;
    }

    if ((ret = ped_file_system_type_get(type->name)) == NULL) {
        PyErr_SetString(UnknownTypeException, type->name);
        return NULL;
    }

    return ret;
}
示例#5
0
/**
 * Creates the boot- and the linux partition and formats the linux
 * partition with an ext2 filesystem
 *
 */
int create_partitions(const char* dev, unsigned long bootsector_size) {
  PedDisk* disk;
  PedDevice* device;
  PedPartition* boot_part;
  PedPartition* linux_part;
  PedFileSystemType* fs_type;
  PedTimer* timer;

  // get device from string e.g. "/dev/sdd"
  device = ped_device_get(dev);
  if(device == NULL) {
    return 0;
  }

  // create new partition table
  disk = ped_disk_new_fresh(device, ped_disk_type_get("msdos"));
  if(disk == NULL) {
    ped_device_destroy(device);
    return 0;
  }

  // get file system type (ext2)
  fs_type = ped_file_system_type_get(FILE_SYSTEM);

  // create partitions
  boot_part = ped_partition_new(disk, PED_PARTITION_NORMAL, fs_type, 0, bootsector_size / device->sector_size);
  linux_part = ped_partition_new(disk, PED_PARTITION_NORMAL, fs_type, bootsector_size / device->sector_size, device->length - 1);

  // add partitions to partition table
  PedConstraint* constraint = ped_constraint_any(device);
  ped_disk_add_partition(disk, linux_part, constraint);
  ped_disk_add_partition(disk, boot_part, constraint);
  ped_constraint_destroy(constraint);

  // create timer
  timer = ped_timer_new(create_ext2_timer, NULL);

  // create filesystem
  ped_file_system_create(&linux_part->geom, fs_type, timer);

  // commit to hardware
  ped_disk_commit_to_dev(disk);
  ped_disk_commit_to_os(disk);

  return 1;
}
void
gnome_format_create_partition(gchar *block_dev, gchar *fs, GError **error) {

        PedDevice *device;
        PedDisk *disk;
        PedFileSystemType *fs_type;
        PedPartition *part;
        
        ped_exception_set_handler(parted_exception_handler);
        
        try(device = ped_device_get(block_dev));
        try(disk = ped_disk_new(device));
        
        int last_part_num = ped_disk_get_last_partition_num(disk);
        if (last_part_num != -1) {
                // if partitions exist, delete them
                try(ped_disk_delete_all(disk));
        }

        long long end = device->length - 1;

        try(fs_type = ped_file_system_type_get(fs));
        
        // create new partition
        try(part = ped_partition_new(disk, PED_PARTITION_NORMAL, fs_type, 1, end));
        try(ped_disk_add_partition(disk, part, ped_constraint_any(device)));

        try(ped_file_system_create(&part->geom, fs_type, NULL));
                
        // commit changes
        try(ped_disk_commit_to_dev(disk));
        
        // this needs root priviliges
//        try(ped_disk_commit_to_os(disk));

#ifdef DEBUG
        printf("device.sector_size: %lld\n", device->sector_size);
        printf("device.length: %lld\n", device->length);
        printf("end: %lld\n", end);
#endif
        
        // free stuff
        ped_disk_destroy(disk);
        ped_device_destroy(device);
}
示例#7
0
/* 1:1 function mappings for filesys.h in libparted */
PyObject *py_ped_file_system_type_get(PyObject *s, PyObject *args) {
    PedFileSystemType *fstype = NULL;
    _ped_FileSystemType *ret = NULL;
    char *name = NULL;

    if (!PyArg_ParseTuple(args, "s", &name)) {
        return NULL;
    }

    fstype = ped_file_system_type_get(name);
    if (fstype) {
        ret = PedFileSystemType2_ped_FileSystemType(fstype);
        return (PyObject *) ret;
    }
    else {
        PyErr_SetString(UnknownTypeException, name);
        return NULL;
    }
}
示例#8
0
PartitionState do_test1(PedDevice *dev, label_type labelType) {
    PartitionState state;
    //PedGeometry geom;
    PedDisk *disk;
    PedPartition *part;
    PedPartition *grub_partition = 0, *boot_partition = 0, *root_partition = 0;
    PedDiskType *type = 0;
    PedFileSystemType *ext4 = ped_file_system_type_get("ext4");
    bool dirty = false;
    PedSector start = 0, end = 0;

    /*if (!ped_geometry_init(&geom,dev,0,dev->length)) {
        qDebug() << "unable to init geom";
        return;
    }*/

    disk = ped_disk_new(dev);
    /*type = ped_disk_probe(dev);
    if (type) {
        qDebug() << "current partition type:" << type->name;
        disk = type->ops->alloc(dev);
        if (!type->ops->read(disk)) {
            qDebug() << "failed to read gpt tables";
            return;
        }
    }*/
    if (!disk) {
        qDebug() << "no tables detected";
        if (labelType == label_type::gpt) {
            type = ped_disk_type_get("gpt");
        } else if (labelType == label_type::mbr) {
            type = ped_disk_type_get("msdos");
        }
        disk = ped_disk_new_fresh(dev,type);
        ped_disk_commit(disk);
    }
    if (disk) {
        for (part = ped_disk_next_partition(disk,NULL);
             part;
             part = ped_disk_next_partition(disk,part)) {
            if (!ped_partition_is_active(part)) continue;
            QString name(ped_partition_get_name(part));
            qDebug() << "partition" << part->num << name;
            if (name == "boot") boot_partition = part;
            if (name == "root") root_partition = part;
            if (ped_partition_get_flag(part,PED_PARTITION_BIOS_GRUB)) grub_partition = part;
            for (int f = PED_PARTITION_FIRST_FLAG; f < PED_PARTITION_LAST_FLAG; f++) {
                if (ped_partition_get_flag(part,(PedPartitionFlag)f)) {
                    QString flag_name(ped_partition_flag_get_name((PedPartitionFlag)f));
                    qDebug() << "flag" << flag_name << "is set";
                }
            }
        }

        PedConstraint *constraint = ped_constraint_any(dev);
        if (!grub_partition) {
            start = (1024*1024) / dev->sector_size;
            end = ((1024*1024) / dev->sector_size) + start;
            qDebug() << "creating" << start << end;
            grub_partition = ped_partition_new(disk,PED_PARTITION_NORMAL,ext4,start,end);
            if (labelType == label_type::gpt) {
                ped_partition_set_name(grub_partition,"bios boot");
                ped_partition_set_flag(grub_partition,PED_PARTITION_BIOS_GRUB,1);
            }
            if (!ped_disk_add_partition(disk,grub_partition,constraint)) {
                qDebug() << "error adding partition";
            }
            dirty = true;
        }

        if (!boot_partition) {
            start = (1024*1024*2) / dev->sector_size;
            end = ((1024*1024*128) / dev->sector_size) + start;
            qDebug() << "creating" << start << end;
            boot_partition = ped_partition_new(disk,PED_PARTITION_NORMAL,NULL,start,end);
            if (labelType == label_type::gpt) {
                ped_partition_set_name(boot_partition,"boot");
            }
            //ped_partition_set_flag(boot_partition,PED_PARTITION_BOOT,1);
            if (!ped_disk_add_partition(disk,boot_partition,constraint)) {
                qDebug() << "error adding partition";
            }
            dirty = true;
        }
        if (!root_partition) {
            start = (1024*1024*129) / dev->sector_size;
            end = dev->length;
            qDebug() << "creating" << start << end;
            root_partition = ped_partition_new(disk,PED_PARTITION_NORMAL,ext4,start,end);
            if (labelType == label_type::gpt) {
                ped_partition_set_name(root_partition,"root");
                //ped_partition_set_flag(root_partition,PED_PARTITION_ROOT,1);
            }
            if (!ped_disk_add_partition(disk,root_partition,constraint)) {
                qDebug() << "error adding partition";
            }
            dirty = true;
        }
        ped_constraint_destroy(constraint);
    }
    if (dirty) ped_disk_commit(disk);
    state.boot_path = ped_partition_get_path(boot_partition);
    state.root_path = ped_partition_get_path(root_partition);
    return state;
}
bool MParted::MParted_Core::createPartition(MParted::Partition & new_partition, MParted::Sector min_size) {
    new_partition.partitionNumber = 0;

    if (!openDeviceAndDisk(new_partition.devicePath))
        return false;

    PedPartitionType type;
    PedPartition *pedPartition = NULL;
    PedConstraint *constraint = NULL;
    PedFileSystemType* fs_type = NULL;

    //create new partition
    switch (new_partition.type) {
        case MParted::TYPE_PRIMARY:
            type = PED_PARTITION_NORMAL;
            break;
        case MParted::TYPE_LOGICAL:
            type = PED_PARTITION_LOGICAL;
            break;
        case MParted::TYPE_EXTENDED:
            type = PED_PARTITION_EXTENDED;
            break;

        default:
            type = PED_PARTITION_FREESPACE;
    }

    if (new_partition.type != MParted::TYPE_EXTENDED)
        fs_type = ped_file_system_type_get("ext2");

    pedPartition = ped_partition_new(pedDisk, type, fs_type,
                                     new_partition.sector_start,
                                     new_partition.sector_end);

    if (!pedPartition) {
        // Clean up
        closeDeviceAndDisk();
        return false;
    }

    if (new_partition.alignment == MParted::ALIGN_MEBIBYTE) {
        PedGeometry *geom = ped_geometry_new(pedDevice,
                                             new_partition.sector_start,
                                             new_partition.getSectorLength());
        if (geom)
            constraint = ped_constraint_exact(geom);
    }
    else {
        constraint = ped_constraint_any(pedDevice);
    }


    if (constraint) {
        if (min_size > 0
                && new_partition.filesystem != MParted::FS_XFS) // Permit copying to smaller xfs partition
            constraint->min_size = min_size;

        if (ped_disk_add_partition(pedDisk, pedPartition, constraint) && commit()) {
            // Get partition path
            new_partition.path = Utils::charToStringFree(ped_partition_get_path(pedPartition)); // we have to free the result of ped_partition_get_path()

            new_partition.partitionNumber = pedPartition->num;
            new_partition.sector_start = pedPartition->geom.start;
            new_partition.sector_end = pedPartition->geom.end;
        }

        ped_constraint_destroy(constraint);
    }

    // Clean up
    closeDeviceAndDisk();


    return (new_partition.partitionNumber > 0);
}
void SystemPartitioner::init()
{
#if NONDESTRUCTIVE
	emit done();
	return;
#endif
	// Check # of harddisks to see if RAID0-ing them makes any sense
	ped_device_probe_all();
	PedDevice *tmp=NULL;
	int disks=0;
	QString installsource(getenv("INSTALLSOURCE"));
	MSG("Install source is " + installsource);
	while((tmp=ped_device_get_next(tmp))) {
		// FIXME workaround for parted breakage
		// Skip CD-ROMs parted accidentally marks as harddisk
		QString p(tmp->path);
		if(!p.startsWith("/dev/sd") && (p.contains("/dev/scd") || p.contains("/dev/sr") || access(QFile::encodeName("/proc/ide/" + p.section('/', 2, 2) + "/cache"), R_OK)))
			continue;
		// It's not a good idea to install on a USB stick we're installing from...
		if(installsource.startsWith(p))
			continue;
		MSG(QString("Found disk: ") + QString(tmp->path) + ":" + "/proc/ide/" + p.section('/', 2, 2) + "/" + "cache" + ":" + QString::number(access(QFile::encodeName("/proc/ide/" + p.section('/', 2, 2) + "/cache"), R_OK)));
#if 0
		// Check if the drive is actually there -- some empty CF
		// readers misidentify themselves...
		int fd=open(tmp->path, O_RDONLY);
		if(fd < 0)
			continue;
		char test;
		if(read(fd, &test, 1) <= 0) {
			close(fd);
			continue;
		}
		close(fd);
#endif
		disks++;
	}
	ped_device_free_all();

	// Grab all disks
	ped_device_probe_all();

	PedFileSystemType const *ext3=ped_file_system_type_get("ext2"); // parted doesn't support ext3 creation yet, so we need this hack
	PedFileSystemType const *bootext3=ped_file_system_type_get("ext2");
	PedFileSystemType const *swap=ped_file_system_type_get("linux-swap");

	Meminfo m;
	unsigned long long swapsize=m.suggested_swap();
	QStringList raidPartitions;

	PedDevice *dev=NULL;
	PedPartition *fspart=NULL;
	PedGeometry *fsgeo=NULL;
	setHelp(tr("Removing other OSes..."));
	resizeEvent(0);

	uint32_t bootsize=0;
#ifndef NO_RAID0
	if(disks>1)
		bootsize=32*1024*2; /* size is in 512 kB blocks --> we use 32 MB */
#endif
	while((dev=ped_device_get_next(dev))) {
		// FIXME workaround for parted breakage
		QString p(dev->path);
		if(!p.startsWith("/dev/sd") && (p.contains("/dev/scd") || p.contains("/dev/sr") || access(QFile::encodeName("/proc/ide/" + p.section('/', 2, 2) + "/cache"), R_OK)))
			continue;
		// It's not a good idea to install on a USB stick we're installing from...
		if(installsource.startsWith(p))
			continue;
		//unsigned long long disksize=dev->length*dev->sector_size;
		PedDiskType *type=ped_disk_type_get("msdos");
		PedDisk *disk=ped_disk_new_fresh(dev, type);

		PedGeometry *part=ped_geometry_new(dev, 0, dev->length);
		if(swapsize && _swap.isEmpty() && ((unsigned long long)part->length > swapsize + bootsize)) {
			// Split disk in swap and fs partitions
			PedGeometry *swapgeo=ped_geometry_new(dev, 0, swapsize);
			PedGeometry *bootgeo=NULL;
			uint32_t fssize=part->end - swapsize;
			uint32_t fsstart=swapsize+1;
			if(bootsize) {
				bootgeo=ped_geometry_new(dev, swapsize+1, bootsize);
				fssize -= bootsize;
				fsstart += bootsize;
			}
			fsgeo=ped_geometry_new(dev, fsstart, fssize);

			PedPartition *swappart=ped_partition_new(disk, (PedPartitionType)0, swap, swapgeo->start, swapgeo->end);
			PedPartition *bootpart=NULL;
			if(bootsize)
				bootpart=ped_partition_new(disk, (PedPartitionType)0, bootext3, bootgeo->start, bootgeo->end);

			fspart=ped_partition_new(disk, (PedPartitionType)0, ext3, fsgeo->start, fsgeo->end);
			if(bootsize)
				ped_partition_set_flag(fspart, PED_PARTITION_RAID, 1);
			ped_disk_add_partition(disk, swappart, ped_constraint_any(dev));
			ped_disk_commit(disk);

			ped_geometry_destroy(swapgeo);
			
			setHelp(tr("Creating swap filesystem"));
			PedFileSystem *swapfs=ped_file_system_create(&(swappart->geom), swap, NULL /*timer->timer()*/);
			ped_file_system_close(swapfs);
			_swap = dev->path + QString::number(swappart->num);

			// Parted's swap creator is buggy
			QProcess::execute("/sbin/mkswap " + _swap);

			if(bootpart) {
				setHelp(tr("Creating boot filesystem"));
				ped_disk_add_partition(disk, bootpart, ped_constraint_any(dev));
				ped_disk_commit(disk);
				PedFileSystem *bootfs=ped_file_system_create(&(bootpart->geom), bootext3, timer->timer());
				ped_file_system_close(bootfs);
				ped_partition_set_flag(bootpart, PED_PARTITION_BOOT, 1);
				ped_geometry_destroy(bootgeo);

				QString devname=dev->path + QString::number(bootpart->num);
				_size.insert(devname, bootpart->geom.length);
				if(_rootfs == "ext3") {
					Ext3FS e3(devname);
					e3.addJournal(0);
					e3.setCheckInterval(0);
					e3.setCheckMountcount(-1);
					e3.setDirIndex();
				} else {
					QProcess::execute("/sbin/mkfs." + _rootfs + " " + _mkfsopts + " " + devname);
				}
				if(!_postmkfs.isEmpty())
					QProcess::execute(_postmkfs + " " + devname);

				_partitions.insert(devname, FileSystem("/boot", _rootfs));
			}
		} else {
			// Grab the whole disk for filesystem
			fsgeo=ped_constraint_solve_max(ped_constraint_any(dev));
			fspart=ped_partition_new(disk, (PedPartitionType)0, ext3, part->start, part->end);
			if(bootsize)
				ped_partition_set_flag(fspart, PED_PARTITION_RAID, 1);
		}
		ped_disk_add_partition(disk, fspart, ped_constraint_any(dev));
		if(!bootsize)
			ped_partition_set_flag(fspart, PED_PARTITION_BOOT, 1);
		ped_disk_commit(disk);
		if(!bootsize) {
			setHelp(tr("Creating Linux filesystem"));
			PedFileSystem *fs=ped_file_system_create(&(fspart->geom), ext3, timer->timer());
			_totalSize += fspart->geom.length;
			ped_file_system_close(fs);
		}
		ped_geometry_destroy(fsgeo);
		// Convert to ext3 and turn off checking
		QString devname=dev->path + QString::number(fspart->num);
		if(bootsize)
			raidPartitions.append(devname);
		else if(!_partitions.hasMountpoint("/"))
			_partitions.insert(devname, FileSystem("/", _rootfs));
		else {
			QString mp(devname);
			mp.replace("/dev", "/mnt");
			_partitions.insert(devname, FileSystem(mp, _rootfs));
		}
		_size.insert(devname, fspart->geom.length);
		if(!bootsize) {
			if(_rootfs == "ext3") {
				Ext3FS e3(devname);
				e3.addJournal(0);
				e3.setCheckInterval(0);
				e3.setCheckMountcount(-1);
				e3.setDirIndex();
			} else {
				QProcess::execute("/sbin/mkfs." + _rootfs + " " + _mkfsopts + " " + devname);
			}
			if(!_postmkfs.isEmpty())
				QProcess::execute(_postmkfs + " " + devname);
			ped_disk_destroy(disk);
		}
	}

	if(bootsize) {
		setHelp(tr("Combining disks..."));
		// Make sure we can read the array we're building first...
		Modules::instance()->loadWithDeps("md");
		Modules::instance()->loadWithDeps("raid0");
		// Now create it
		FILE *f=fopen("/tmp/mdadm.conf", "w");
		if(!f)
			QMessageBox::information(0, "debug", QString("Failed to create mdadm.conf: ") + strerror(errno));
		fprintf(f, "DEVICE partitions");
		fprintf(f, "ARRAY /dev/md0 name=ArkLinux devices=%s level=0 num-devices=%u\n", qPrintable(raidPartitions.join(",")), raidPartitions.count());
		fprintf(f, "MAILADDR root@localhost\n");
		fclose(f);

		QString command="/sbin/mdadm --create -e 1.2 --chunk=32 --level=0 --raid-devices=" + QString::number(raidPartitions.count()) + " --name=ArkLinux --force /dev/md0 " + raidPartitions.join(" ");
		QProcess::execute(command);
		
		if(_rootfs == "ext3") {
			QProcess::execute("/sbin/mke2fs -j /dev/md0");
			Ext3FS e3("/dev/md0");
			e3.setCheckInterval(0);
			e3.setCheckMountcount(-1);
			e3.setDirIndex();
		} else {
			QProcess::execute("/sbin/mkfs." + _rootfs + " " + _mkfsopts + " /dev/md0");
		}
		if(!_postmkfs.isEmpty())
			QProcess::execute(_postmkfs + " /dev/md0");

		_partitions.insert("/dev/md0", FileSystem("/", _rootfs));
		_size.clear();
		_size.insert("/dev/md0", _totalSize);
	}

	// We don't need a UI to take the whole system - we're done.
	emit done();
}