bool MParted::MParted_Core::openDevice(const QString & devicePath) {
    QByteArray ba = devicePath.toLocal8Bit();

    pedDevice = ped_device_get(ba.constData());

    return pedDevice;
}
Пример #2
0
int
main (int argc, char **argv)
{
  atexit (close_stdout);
  set_program_name (argv[0]);

  if (argc != 2)
    return EXIT_FAILURE;

  char const *dev_name = argv[1];
  PedDevice *dev = ped_device_get (dev_name);
  if (dev == NULL)
    return EXIT_FAILURE;
  PedDisk *disk = ped_disk_new (dev);
  if (disk == NULL)
    return EXIT_FAILURE;

  PedSector max_length = ped_disk_max_partition_length (disk);
  PedSector max_start_sector = ped_disk_max_partition_start_sector (disk);

  printf ("max len: %llu\n", (unsigned long long) max_length);
  printf ("max start sector: %llu\n", (unsigned long long) max_start_sector);

  ped_disk_destroy (disk);
  ped_device_destroy (dev);
  return EXIT_SUCCESS;
}
Пример #3
0
int nwipe_device_get( nwipe_context_t*** c, char **devnamelist, int ndevnames )
{
	/**
	 * Gets information about devices
	 *
	 * @parameter device_names  A reference to a null array pointer.
	 * @parameter devnamelist   An array of string pointers to the device names
	 * @parameter ndevnames     Number of elements in devnamelist
	 * @modifies  device_names  Populates device_names with an array of nwipe_contect_t
	 * @returns                 The number of strings in the device_names array.
	 *
	 */

	PedDevice* dev = NULL;
	
	int i;
	int dcount = 0;

	for(i = 0; i < ndevnames; i++) {

		dev = ped_device_get(devnamelist[i]);
		if (!dev)
			break;

                if (check_device(c, dev, dcount))
                        dcount++;
	}

	/* Return the number of devices that were found. */
	return dcount;

} /* nwipe_device_get */
Пример #4
0
int main(int argc, char* argv[])
{
    PedDevice* device;
    PedDisk* disk;

    if(argc != 2)
        error(EXIT_FAILURE, 0, "wrong number of argument");
    
    device = ped_device_get(argv[1]);
    if(device == NULL)
        error(EXIT_FAILURE, 0, "error getting the device");

    disk = ped_disk_new(device);
    if(disk == NULL)
    {
        ped_device_destroy(device);
        error(EXIT_FAILURE, 0, "error getting the disk");
    }

    ped_disk_delete_all(disk);
    
    ped_disk_commit(disk);

    ped_disk_destroy(disk);
    ped_device_destroy(device);

        
    return 0;
}
Пример #5
0
PyObject *py_ped_device_get(PyObject *s, PyObject *args) {
    PedDevice *device = NULL;
    _ped_Device *ret = NULL;
    char *path = NULL;

    if (!PyArg_ParseTuple(args, "z", &path)) {
        return NULL;
    }

    if (path == NULL) {
        PyErr_Format(DeviceException, "Could not find device for empty path");
        return NULL;
    }

    device = ped_device_get(path);
    if (device) {
        ret = PedDevice2_ped_Device(device);
    }
    else {
        if (partedExnRaised) {
            partedExnRaised = 0;

            if (!PyErr_ExceptionMatches(PartedException) &&
                !PyErr_ExceptionMatches(PyExc_NotImplementedError))
                PyErr_SetString(IOException, partedExnMessage);
        }
        else
            PyErr_Format(DeviceException, "Could not find device for path %s", path);

        return NULL;
    }

    return (PyObject *) ret;
}
Пример #6
0
/**
 * Returns the size of a device
 *
 */
unsigned long long get_device_size(const char* dev) {
  PedDevice* device = ped_device_get(dev);
  if(device == NULL) {
    return 0;
  }
  return device->length * device->sector_size;
}
Пример #7
0
bool LibPartedDevice::createPartitionTable(Report& report, const PartitionTable& ptable)
{
    PedDiskType* pedDiskType = ped_disk_type_get(ptable.typeName().toLatin1().constData());

    if (pedDiskType == nullptr) {
        report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not retrieve partition table type \"%1\" for <filename>%2</filename>.", ptable.typeName(), deviceNode());
        return false;
    }

    PedDevice* dev = ped_device_get(deviceNode().toLatin1().constData());

    if (dev == nullptr) {
        report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not open backend device <filename>%1</filename>.", deviceNode());
        return false;
    }

    PedDisk* disk = ped_disk_new_fresh(dev, pedDiskType);

    if (disk == nullptr) {
        report.line() << xi18nc("@info:progress", "Creating partition table failed: Could not create a new partition table in the backend for device <filename>%1</filename>.", deviceNode());
        return false;
    }

    return LibPartedPartitionTable::commit(disk);
}
Пример #8
0
int main(int argc, char *argv[])
{
	PedDevice *dev;
	PedDisk *disk;
	PedPartition *part;
	PedConstraint *constraint;

	/*
	 * argv [1] device
	 *      [2] udv_name
	 *      [3] start
	 *      [4] end
	 */

	dev = ped_device_get(argv[1]);
	if (!dev)
	{
		perror("get_device");
		return -1;
	}

	//disk = _create_disk_label(dev, ped_disk_type_get("gpt"));
	disk = ped_disk_new(dev);
	if (!disk)
	{
		fprintf(stderr, "fail to create disk label gpt\n");
		return -2;
	}
	constraint = ped_constraint_any(dev);

	// part1: 17.4Kb ~ 15MB
	/*
	part = ped_partition_new(disk, PED_PARTITION_NORMAL,
				NULL,
				34, 29296);
	ped_disk_add_partition(disk, part, constraint);
	*/

	// part2: 15MB ~ 35MB
	part = ped_partition_new(disk, PED_PARTITION_NORMAL,
				NULL,
				(int)(atoll(argv[3])/512),
				(int)(atoll(argv[4])/512));
	ped_partition_set_name(part, argv[2]);
	ped_disk_add_partition(disk, part, constraint);

	ped_disk_commit(disk);

	ped_constraint_destroy(constraint);
	ped_disk_destroy(disk);
	ped_device_destroy(dev);

	return 0;
}
int main(int argc, char** argv)
{
	PedDevice* device = ped_device_get(argv[1]);
	if (!device) return 1;

	PedDisk* disk = ped_disk_new(device);

	printf("%d\n",device->bios_geom.sectors * device->bios_geom.heads * device->bios_geom.cylinders);
	
	return 0;
}
Пример #10
0
bool LibPartedDevice::open()
{
    Q_ASSERT(pedDevice() == nullptr);

    if (pedDevice())
        return false;

    m_PedDevice = ped_device_get(deviceNode().toLatin1().constData());

    return m_PedDevice != nullptr;
}
int main (int argc, char* argv[])
{
    PedDevice* device = NULL;
    PedDisk* disk = NULL;
    PedDiskType* type = NULL;
    PedPartition* part = NULL;
    
    int return_code = EXIT_FAILURE;
    
        if (argc != 2){
            printf("wrong number of arguments\n");
            return EXIT_FAILURE;
        }
    
    
    device = ped_device_get (argv[1]);
    printf("device:%s\n",argv[1]);
    if (device == NULL)
        goto error_exit;
    
    type = ped_disk_probe (device);
    if (type == NULL) 
        goto error_destroy_device;
        
    printf("Disk Type:%s\n",type->name);
        
    
    disk = ped_disk_new (device);
    if (disk == NULL)
        goto error_destroy_device;
    
    do
    {
        part = ped_disk_next_partition (disk, part);
        if ((part != NULL) && (part->num > -1)){
                printf("%d %s %s\n",part->num,ped_partition_type_get_name(part->type),ped_unit_format (device,part->geom.length));
        }
        
    }while( part );
    
    ped_disk_destroy (disk);
    return_code = EXIT_SUCCESS;
    
// Fall through to clean up 
error_destroy_device:
    ped_device_destroy (device);

error_exit:
    return return_code;
}
Пример #12
0
Файл: ui.c Проект: bcl/parted
int
command_line_get_device (const char* prompt, PedDevice** value)
{
        char *def_dev_name = *value ? (*value)->path : NULL;
        char *dev_name = command_line_get_word (prompt, def_dev_name, NULL, 1);
        if (!dev_name)
                return 0;

        PedDevice *dev = ped_device_get (dev_name);
        free (dev_name);
        if (!dev)
                return 0;

        *value = dev;
        return 1;
}
Пример #13
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;
}
Пример #14
0
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);
}
Пример #15
0
int main(int argc, char* argv[])
{
    PedDevice* device;
    PedDisk* disk_old;
    PedDisk* disk;
    PedDiskType* type;

    if(argc != 3)
        error(EXIT_FAILURE, 0, "wrong number of arguments");

    device = ped_device_get(argv[1]);
    if(device == NULL)
        return -1;

    type = ped_disk_type_get(argv[2]);
    if(type == NULL)
    {
        ped_device_destroy(device);
        error(EXIT_FAILURE, 0, "invalid type");
        return -1;
    }

    disk_old = ped_disk_new(device);
    printf("disk type: %s \n",disk_old->type->name);
    disk = ped_disk_new_fresh(device, disk_old->type);
    if(disk == NULL)
    {
        ped_device_destroy(device);
        error(EXIT_FAILURE, 0, "error formating disk");
        return -1;
    }

    if(!ped_disk_commit(disk))
    {
        ped_disk_destroy(disk);
        error(EXIT_FAILURE, 0, "error writing a new partition table");
        return -1;
    }

    ped_disk_destroy(disk);
    ped_device_destroy(device);
    return 0;
}
Пример #16
0
int main(int argc, char *argv[])
{
	PedDevice *device;
	PedDisk *disk;
	PedPartition *part;
	char *name;

	if (argc != 2)
		error(EXIT_FAILURE, 0, "wrong number!");

	device = ped_device_get(argv[1]);
	if (!device)
		goto error_out;

	disk = ped_disk_new(device);
	if (!disk)
		goto error;

	printf("%3s %s %s %s %s\n", "NO.", "Start", "Size", "FS", "NAME");

	for (part = ped_disk_next_partition(disk, NULL); part;
		part = ped_disk_next_partition(disk, part) )
	{
		if (part->num <  0)
			continue;
		name = ped_partition_get_name(part);

		printf("%lld %lld %lld %s %s\n", part->num,
			part->geom.start, part->geom.length,
			(part->fs_type) ? part->fs_type->name : "",
			name ? name : "");
	}

	ped_disk_destroy(disk);
	ped_device_destroy(device);
	return 0;

error:
	ped_device_destroy(device);
error_out:
	return 1;
}
int
main(int argc, char *argv[])
{
	PedDevice *dev;
	ped_exception_fetch_all();
	ped_device_probe_all();
	if (argc > 1) {
		int i;
		for (i = 1; i < argc; ++i) {
			dev = ped_device_get(argv[i]);
			if (dev) {
				process_device(dev);
				ped_device_destroy(dev);
			}
		}
	} else {
		for (dev = NULL; NULL != (dev = ped_device_get_next(dev));)
			process_device(dev);
	}
	return 0;
}
Пример #18
0
char* query_esp_path_by_disk(const char* path)
{
    g_message("[%s], path: %s\n", __func__, path);
    PedDevice* device = ped_device_get(path);
    PedDiskType *type = ped_disk_probe(device);
    if (type == 0) {
        return NULL;
    }
    if (strncmp(type->name, "loop", 5) == 0) {
        return NULL;
    }
    PedDisk* disk = ped_disk_new(device);
    if (disk == 0) {
        return NULL;
    }

    PedPartition* part = 0;
    for (part = ped_disk_next_partition(disk, NULL); part;
         part = ped_disk_next_partition(disk, part)) {
        if (part->num <  0) {
            continue;
        }

        if (part->fs_type == 0 ||
            strncmp(part->fs_type->name, "fat32", 5) != 0) {
            continue;
        }


        int is_esp = has_efi_directory(part);
        g_debug("[%s] %s is ESP ? : %d\n", __func__,
                ped_partition_get_path(part), is_esp);
        if (is_esp) {
            return ped_partition_get_path(part);
        }
    }

    return NULL;
}
Пример #19
0
/* _ped_Device -> PedDevice functions */
PedDevice *_ped_Device2PedDevice(PyObject *s) {
    _ped_Device *dev = (_ped_Device *) s;
    PedDevice *ret;

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

    ret = ped_device_get(dev->path);
    if (ret == NULL) {
        if (partedExnRaised) {
            partedExnRaised = 0;

            if (!PyErr_ExceptionMatches(PartedException) &&
                !PyErr_ExceptionMatches(PyExc_NotImplementedError))
                PyErr_SetString(DeviceException, partedExnMessage);
        }
        else
            PyErr_Format(DeviceException, "Could not find device for path %s", dev->path);
    }
    return ret;
}
Пример #20
0
int
main (int argc, char **argv)
{
	PedDevice *device;
	PedDisk *disk;

	if (argc != 2) {
		fprintf (stderr, "Usage: %s DEVICE\n", argv[0]);
		exit (1);
	}

	device = ped_device_get (argv[1]);
	if (!device)
		exit (1);

	disk = ped_disk_new (device);
	if (!disk)
		exit (1);

	printf ("%s\n", disk->type->name);

	return 0;
}
Пример #21
0
char* query_esp_path_by_disk_path(const char* path)
{
    PedDevice* device = ped_device_get(path);
    PedDiskType *type = ped_disk_probe(device);
    if (type == 0) {
        return NULL;
    }
    if (strncmp(type->name, "loop", 5) == 0) {
        return NULL;
    }
    PedDisk* disk = ped_disk_new(device);
    if (disk == 0) {
        return NULL;
    }

    PedPartition* esp = find_partition(disk,
        (PartitionFilter)filter_partition_by_esp, NULL, NULL);
    if (esp != NULL) {
        return ped_partition_get_path(esp);
    }

    return NULL;
}
Пример #22
0
int detect_raids()
{
	FILE *fp;
	char *line, *ptr;
	PedDevice *dev = NULL;
	PedDisk *disk = NULL;
	PedPartition *part = NULL;

	if ((fp = fopen("/proc/mdstat", "r"))== NULL)
	{
		//perror("Could not open output file for writing");
		return(-1);
	}
	MALLOC(line, PATH_MAX);
	while(!feof(fp))
	{
		if(fgets(line, PATH_MAX, fp) == NULL)
			break;
		if(strstr(line, "md")==line)
		{
			ptr = line;
			while(*ptr!=' ')
				ptr++;
			*ptr='\0';
			dev = ped_device_get(g_strdup_printf("/dev/%s", line));
			disk = ped_disk_new_fresh(dev, ped_disk_type_get ("loop"));
			if(disk)
			{
				part=ped_disk_next_partition(disk, NULL);
				partdetails(part, 0);
			}
		}
	}
	FREE(line);
	fclose(fp);
	return(0);
}
Пример #23
0
gboolean
part_del_partition (char *device_file, guint64 offset)
{
	gboolean ret;
	PedDevice *device;
	PedDisk *disk;
	PedPartition *part;
	PartitionTable *p;
	gboolean is_extended;
	int n;

	HAL_INFO (("In part_del_partition: device_file=%s, offset=%lld", device_file, offset));
	
	ret = FALSE;


	/* sigh.. one would think that if you passed the sector of where the
	 * the beginning of the extended partition starts, then _by_sector
	 * would return the same as _extended_partition. 
	 *
	 * Sadly it's not so..
	 *
	 * So, check if the passed offset actually corresponds to a nested
	 * partition table...
	 */
	is_extended = FALSE;
	p = part_table_load_from_disk (device_file);
	if (p == NULL) {
		HAL_INFO (("Cannot load partition table from %s", device_file));
		goto out;
	}
	for (n = 0; n < part_table_get_num_entries (p); n++) {
		PartitionTable *nested;
		nested = part_table_entry_get_nested (p, n);
		if (nested != NULL) {
			if (part_table_get_offset (nested) == offset) {
				HAL_INFO (("partition to delete is an extended partition"));
				is_extended = TRUE;
			}
		}
	}
	part_table_free (p);

	device = ped_device_get (device_file);
	if (device == NULL) {
		HAL_INFO (("ped_device_get() failed"));
		goto out;
	}
	HAL_INFO (("got it"));

	disk = ped_disk_new (device);
	if (disk == NULL) {
		HAL_INFO (("ped_disk_new() failed"));
		goto out_ped_device;
	}
	HAL_INFO (("got disk"));

	if (is_extended) {
		part = ped_disk_extended_partition (disk);
	} else {
		part = ped_disk_get_partition_by_sector (disk, offset / 512);
	}

	if (part == NULL) {
		HAL_INFO (("ped_disk_get_partition_by_sector() failed"));
		goto out_ped_disk;
	}
				  
	HAL_INFO (("got partition - part->type=%d", part->type));
	/* allow only to delete primary, logical and extended partitions */
	if (! ((part->type == PED_PARTITION_NORMAL) ||
	       (part->type == PED_PARTITION_LOGICAL) ||
	       (part->type == PED_PARTITION_EXTENDED))) {
		HAL_INFO (("no data partition at given offset %lld for device %s", offset, device_file));
		goto out_ped_disk;
	}

	if (ped_disk_delete_partition (disk, part) == 0) {
		HAL_INFO (("ped_disk_delete_partition() failed"));
		goto out_ped_disk;
	}

	/* use commit_to_dev rather than just commit to avoid
	 * libparted sending BLKRRPART to the kernel - we want to do
	 * this ourselves... 
	 */

	if (ped_disk_commit_to_dev (disk) == 0) {
		HAL_INFO (("ped_disk_commit_to_dev() failed"));
		goto out_ped_disk;
	}
	HAL_INFO (("committed to disk"));

	ret = TRUE;

	ped_disk_destroy (disk);
	ped_device_destroy (device);
	goto out;

out_ped_disk:
	ped_disk_destroy (disk);

out_ped_device:
	ped_device_destroy (device);

out:
	return ret;
}
Пример #24
0
gboolean disk_resize_grow(const gchar* disk_path, GChildWatchFunc async_func_watcher, gpointer data) {
	char command[LINE_MAX] = { 0 };
	int last_partition_num;
	const gchar* partition_path;
	PedDevice* dev = NULL;
	PedDisk* disk = NULL;
	gboolean result = false;
	PedPartition* partition;
	PedSector start;
	PedSector end;
	PedGeometry geometry_start;
	PedGeometry* geometry_end;
	PedConstraint* constraint;

	resize_fs = false;

	/* to handle exceptions, i.e Fix PMBR */
	ped_exception_set_handler(disk_exception_handler);

	if (!disk_path) {
		LOG(MOD "Disk path is empty\n");
		return false;
	}

	dev = ped_device_get(disk_path);

	if (!dev) {
		LOG(MOD "Cannot get device '%s'\n", disk_path);
		return false;
	}

	/*
	* verify disk, disk_exception_handler will called
	* if the disk has problems and it needs to be fixed
	* and resized
	*/
	disk = ped_disk_new(dev);

	if (!disk) {
		LOG(MOD "Cannot create a new disk '%s'\n", disk_path);
		return false;
	}

	if (!resize_fs) {
		/* do not resize filesystem, it is ok */
		LOG(MOD "Nothing to do with '%s' disk\n", disk_path);
		return false;
	}

	LOG(MOD "Resizing filesystem disk '%s'\n", disk_path);

	last_partition_num = ped_disk_get_last_partition_num(disk);
	partition = ped_disk_get_partition(disk, last_partition_num);

	if (!partition) {
		LOG(MOD "Cannot get partition '%d' disk '%s'\n", last_partition_num, disk_path);
		return false;
	}

	start = partition->geom.start;
	end = (-PED_MEGABYTE_SIZE) / dev->sector_size + dev->length;

	geometry_start.dev = dev;
	geometry_start.start = start;
	geometry_start.end = end;
	geometry_start.length = 1;

	geometry_end = ped_geometry_new(dev, end, 1);

	if (!geometry_end) {
		LOG(MOD "Cannot get partition '%d' disk '%s'\n", last_partition_num, disk_path);
		return false;
	}

	constraint = ped_constraint_new(ped_alignment_any, ped_alignment_any, &geometry_start, geometry_end, 1, dev->length);

	if (!constraint) {
		LOG(MOD "Cannot create a new constraint disk '%s'\n", disk_path);
		goto fail1;
	}

	if (!ped_disk_set_partition_geom(disk, partition, constraint, start, end)) {
		LOG(MOD "Cannot set partition geometry disk '%s'\n", disk_path);
		goto fail2;
	}

	if (!ped_disk_commit(disk)) {
		LOG(MOD "Cannot write the partition table to disk '%s'\n", disk_path);
		goto fail2;
	}

	partition_path = ped_partition_get_path(partition);

	if (!partition_path) {
		LOG(MOD "Cannot get partition path disk '%s'\n", disk_path);
		goto fail2;
	}

	snprintf(command, LINE_MAX, RESIZEFS_PATH " %s", partition_path);
	exec_task_async(command, async_func_watcher, data);

	result = true;
	LOG(MOD "Resizing filesystem done\n");

fail2:
	ped_constraint_destroy (constraint);
fail1:
	ped_geometry_destroy (geometry_end);
	return result;
}
Пример #25
0
/** Opens the Device
	@return true if the Device could be successfully opened
*/
bool CopySourceDevice::open()
{
	m_PedDevice = ped_device_get(device().deviceNode().toAscii());
	return m_PedDevice != NULL && ped_device_open(m_PedDevice);
}
Пример #26
0
Device::Device(QString devnode) : devnode(devnode) {
    dev = ped_device_get(qPrintable(devnode));
}
Пример #27
0
/**
 * @brief GetPartitions
 * @param deviceName : dev name of the disk eg. '/dev/sdc'
 * @param partitions : a vector of partitions that this function will write to. 
 * @return true if partitions on disk(deviceName) was successfully read.
 */
bool GetPartitions(std::string deviceName, cPartitions& partitions)
{
    PedDevice* device = NULL;
    PedDisk* disk = NULL;
    PedPartition* part = NULL;
    bool result = false;
    cPartition partition;
    ped_device_probe_all ();
    
    device = ped_device_get(deviceName.c_str());
    if (device == NULL) 
    {
        LOG_PARTED_ERROR("ped_device_get('%s') returned NULL", deviceName.c_str());
        
        result = false;
        goto cleanup;
    } // if
    
    disk = ped_disk_new(device);
    if (disk == NULL) 
    {
        LOG_PARTED_ERROR("ped_disk_new(%p) returned NULL for device %s",
                  disk, deviceName.c_str());
        
        result = false;
        goto cleanup;
    } // if
    
    
    result = true;
    partitions.clear();
    
    // iterate over found paritions
    for (part = ped_disk_next_partition (disk, NULL); 
         part;
         part = ped_disk_next_partition (disk, part))
    {
        if (part->num < 0) 
        {
            LOG_PARTED_WARN("got negative partition number := %d! in device %s", 
                     part->num,
                     deviceName.c_str());
            continue;
        }
        
        /* device name to which this partition belongs */
        partition.deviceName = deviceName;
        
        partition.number = part->num;

        /* convert from number of blocks to number of Megabytes MiB*/
        partition.size = (part->geom.length * device->sector_size) /(1024 * 1024);
        
        partition.filesystem = (part->fs_type) ? part->fs_type->name : "";
        
        /* partition type , avoid extended partitions and freespace ...*/
        partition.mountable = ((part->type&PED_PARTITION_LOGICAL)
                ||(part->type==PED_PARTITION_NORMAL));
        
        /* add to partition list */
        partitions.push_back (partition);
    } // for
    
    
cleanup:
    /* clean up */
    if (disk)
        ped_disk_destroy (disk);
    
    if (device)
        ped_device_destroy (device);

    return result;
} // GetPartitions()
Пример #28
0
int
main (int argc, char **argv)
{
	set_program_name (argv[0]);

	PedSector start = 0, len = 0;
	PedGeometry geom, new_geom;
	PedDevice *dev;
	PedFileSystem *fs;
	PedTimer *g_timer = NULL;

	if (argc != 2 && argc != 4) {
		fprintf(stderr, "usage: %s <device>\n"
				"       %s <device> <start> <length>\n",
				argv[0], argv[0]);
		return 1;
	}

	dev = ped_device_get(argv[1]);
	if (!dev) {
		fprintf(stderr, "cannot create device %s\n", argv[1]);
		return 1;
	}

	if (!ped_device_open(dev)) {
		fprintf(stderr, "cannot open device %s\n", argv[1]);
		return 1;
	}

	if (!ped_geometry_init(&geom, dev, 0, dev->length)) {
		fprintf(stderr, "cannot initialize geometry\n");
		return 1;
	}

	if (argc > 2) {
		start = strtoll(argv[2], NULL, 0);
		len = strtoll(argv[3], NULL, 0);
	} else {
		start = 0;
		len = dev->length;
	}

	if (!ped_geometry_init(&new_geom, dev, start, len)) {
		fprintf(stderr, "cannot initialize new geometry\n");
		return 1;
	}

	fs = ped_file_system_open(&geom);
	if (!fs) {
		fprintf(stderr, "cannot read fs\n");
		return 1;
	}

	if (!ped_file_system_resize(fs, &new_geom, g_timer)) {
		fprintf(stderr, "cannot resize filesystem\n");
		return 1;
	}

	ped_file_system_close(fs);
	return 0;
}
Пример #29
0
int main(int argc, char **argv)
{
    PedDevice *dev;
    PedDisk *disk;
    PedPartition *part;
    int cmd = DISK_LAYOUT;
    const char *path;
    const char *partsep;

    if (argc == 3 && STREQ(argv[2], "-g")) {
        cmd = DISK_GEOMETRY;
    } else if (argc != 2) {
        fprintf(stderr, "syntax: %s DEVICE [-g]\n", argv[0]);
        return 1;
    }

    path = argv[1];
    partsep = *path && c_isdigit(path[strlen(path)-1]) ? "p" : "";

    if ((dev = ped_device_get(path)) == NULL) {
        fprintf(stderr, "unable to access device %s\n", path);
        return 2;
    }

    /* return the geometry of the disk and then exit */
    if(cmd == DISK_GEOMETRY) {
        printf("%d%c%d%c%d%c",
               dev->hw_geom.cylinders, '\0',
               dev->hw_geom.heads, '\0',
               dev->hw_geom.sectors, '\0');
        return 0;
    }

    if ((disk = ped_disk_new(dev)) == NULL) {
        fprintf(stderr, "unable to access disk %s\n", argv[1]);
        return 2;
    }

    /* Get the first partition, and then iterate over all */
    part = ped_disk_next_partition(disk, NULL);
    while (part) {
        const char *type;
        const char *content;
        if (part->type & PED_PARTITION_LOGICAL) {
            type = "logical";
            if (part->type & PED_PARTITION_FREESPACE)
                content = "free";
            else if (part->type & PED_PARTITION_METADATA)
                content = "metadata";
            else if (part->type & PED_PARTITION_PROTECTED)
                content = "protected";
            else
                content = "data";
        } else if (part->type == PED_PARTITION_EXTENDED) {
            type = "extended";
            content = "metadata";
        } else {
            type = "normal";
            if (part->type & PED_PARTITION_FREESPACE)
                content = "free";
            else if (part->type & PED_PARTITION_METADATA)
                content = "metadata";
            else if (part->type & PED_PARTITION_PROTECTED)
                content = "protected";
            else
                content = "data";
        }

        /* We do +1 on geom.end, because we want end of the last sector
         * in bytes, not the last sector number
         */
        if (part->num != -1) {
            printf("%s%s%d%c%s%c%s%c%llu%c%llu%c%llu%c",
                   path, partsep,
                   part->num, '\0',
                   type, '\0',
                   content, '\0',
                   part->geom.start * 512llu, '\0',
                   (part->geom.end + 1 ) * 512llu, '\0',
                   part->geom.length * 512llu, '\0');
        } else {
            printf("%s%c%s%c%s%c%llu%c%llu%c%llu%c",
                   "-", '\0',
                   type, '\0',
                   content, '\0',
                   part->geom.start * 512llu, '\0',
                   (part->geom.end + 1 ) * 512llu, '\0',
                   part->geom.length * 512llu, '\0');
        }
        part = ped_disk_next_partition(disk, part);
    }

    return 0;
}
Пример #30
0
gboolean
part_create_partition_table (char *device_file, PartitionScheme scheme)
{
	PedDevice *device;
	PedDisk *disk;
	PedDiskType *disk_type;
	gboolean ret;

	ret = FALSE;

	HAL_INFO (("In part_create_partition_table: device_file=%s, scheme=%d", device_file, scheme));

	device = ped_device_get (device_file);
	if (device == NULL) {
		HAL_INFO (("ped_device_get() failed"));
		goto out;
	}
	HAL_INFO (("got it"));

	switch (scheme) {
	case PART_TYPE_MSDOS:
		disk_type = ped_disk_type_get ("msdos");
		break;
	case PART_TYPE_APPLE:
		disk_type = ped_disk_type_get ("mac");
		break;
	case PART_TYPE_GPT:
		disk_type = ped_disk_type_get ("gpt");
		break;
	default:
		disk_type = NULL;
		break;
	}

	if (disk_type == NULL) {
		HAL_INFO (("Unknown or unsupported partitioning scheme %d", scheme));
		goto out;
	}

        disk = ped_disk_new_fresh (device, disk_type);
	if (disk == NULL) {
		HAL_INFO (("ped_disk_new_fresh() failed"));
		goto out_ped_device;
	}
	HAL_INFO (("got disk"));

	if (ped_disk_commit_to_dev (disk) == 0) {
		HAL_INFO (("ped_disk_commit_to_dev() failed"));
		goto out_ped_disk;
	}
	HAL_INFO (("committed to disk"));

	ret = TRUE;

	ped_disk_destroy (disk);
	ped_device_destroy (device);
	goto out;

out_ped_disk:
	ped_disk_destroy (disk);

out_ped_device:
	ped_device_destroy (device);

out:
	return ret;
}