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;
}
Exemple #2
0
/*
 * Returns the _ped.DiskType for the specified _ped.Device.
 * Even though this function is part of pydisk.c, it's a method
 * on _ped.Device since it operates on _ped.Device objects and
 * not on _ped.Disk objects.
 */
PyObject *py_ped_disk_probe(PyObject *s, PyObject *args) {
    PedDevice *device = NULL;
    PedDiskType *type = NULL;
    _ped_DiskType *ret = NULL;

    device = _ped_Device2PedDevice(s);
    if (device) {
        type = ped_disk_probe(device);
        if (type == NULL) {
            PyErr_Format(IOException, "Could not probe device %s", device->path);
            return NULL;
        }

        ret = PedDiskType2_ped_DiskType(type);
        if (ret == NULL) {
            return NULL;
        }
    }

    return (PyObject *) ret;
}
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;
}
/* read the parttable.
 * if no parttable exist in device, disk_ and disk_type_ == NULL. 
 */
bool PartitionTable::read()
{
    if (disk_type_ && disk_ && part_list_) { // already loaded
        return true;
    }

    if ( disk_ )
        ped_disk_destroy( disk_ );
    if ( part_list_ )
        delete part_list_;

    disk_type_ = ped_disk_probe( pdev_ );
    if ( disk_type_ == NULL )
        disk_ = NULL;
    else
        disk_ = ped_disk_new( pdev_ );

    if ( disk_ != NULL ) {
        part_list_ = new PartitionList( this );
        return true;
    } else
        return false;
}
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;
}
Exemple #6
0
int
get_all_partitions(struct partition *parts[], const int max_parts, bool ignore_fs_type, PedPartitionFlag require_flag)
{
    struct partition *p;
    int part_count = 0;
    PedDevice *dev = NULL;
    PedDisk *disk;
    PedPartition *part;

    ped_device_probe_all();
    while ((dev = ped_device_get_next(dev)) != NULL) {
        if (dev->read_only)
            continue;
        if (strstr(dev->path, "/dev/mtd") == dev->path)
            continue;
        if (!ped_disk_probe(dev))
            continue;
        disk = ped_disk_new(dev);

        part = NULL;
        while ((part = ped_disk_next_partition(disk, part)) != NULL) {
            if (part->type & (PED_PARTITION_METADATA | PED_PARTITION_FREESPACE | PED_PARTITION_EXTENDED))
                continue;

            if (part_count >= max_parts)
                break;

#ifndef FIND_PARTS_MAIN
            /* allow other udebs to block partitions */
            if(block_partition(ped_partition_get_path(part)) != 0)
                continue;
#endif

            if (require_flag && !ped_partition_get_flag(part, require_flag))
                continue;

            p = malloc(sizeof(*p));
            p->path = ped_partition_get_path(part);
            if (strstr(p->path, "/dev/hd") == p->path) {
                static char *targets[] = { "master", "slave" };
                char drive;
                int part;

                if (sscanf(p->path, "/dev/hd%c%d", &drive, &part) == 2
                        && drive >= 'a' && drive <= 'z')
                    asprintf(&p->description, "IDE%d %s\\, part. %d",
                            (drive - 'a') / 2 + 1, targets[(drive - 'a') % 2],
                            part);
                else
                    p->description = strdup(p->path);
            } else if (strstr(p->path, "/dev/sd") == p->path) {
                char drive;
                int host, bus, target, lun, part;
                int done = 0;

                if (sscanf(p->path, "/dev/sd%c%d", &drive, &part) == 2
                        && drive >= 'a' && drive <= 'z') {
                    struct stat st;
                    char *disk, *disk_pos, *sys_device;
                    disk = strdup(p->path + 5);
                    for (disk_pos = disk + strlen(disk) - 1; disk_pos > disk;
                         --disk_pos) {
                        if (*disk_pos >= '0' && *disk_pos <= '9')
                            *disk_pos = 0;
                        else
                            break;
                    }
                    sys_device = malloc(strlen(disk) + 19);
                    sprintf(sys_device, "/sys/block/%s/device", disk);
                    /* TODO: device symlinks are allegedly slated to go
                     * away, but it's not entirely clear what their
                     * replacement will be yet ...
                     */
                    if (lstat(sys_device, &st) == 0 && S_ISLNK(st.st_mode)) {
                        char buf[512];
                        memset(buf, 0, 512);
                        if (readlink(sys_device, buf, 511) > 0) {
                            const char *bus_id = basename(buf);
                            if (sscanf(bus_id, "%d:%d:%d:%d",
                                        &host, &bus, &target, &lun) == 4) {
                                asprintf(&p->description, "SCSI%d (%d\\,%d\\,%d) part. %d",
                                        host + 1, bus, target, lun, part);
                                done = 1;
                            }
                        }
                    }
                }
                if (!done)
                    p->description = strdup(p->path);
            } else
                p->description = strdup(p->path);
            p->fstype = NULL;
            p->fsid = NULL;
            p->size = 0L;
            p->op.filesystem = NULL;
            p->op.mountpoint = NULL;
            p->op.done = 0;
            test_lvm(p);
            test_evms(p);
            test_raid(p);
            /* FIXME: Other tests? */

            get_partition_info(p, part, dev, ignore_fs_type);
            parts[part_count++] = p;
        }

        if (part_count >= max_parts)
            break;
    }

    return part_count;
}