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; }
static int pc98_read (PedDisk* disk) { PED_ASSERT (disk != NULL); PED_ASSERT (disk->dev != NULL); ped_disk_delete_all (disk); return read_table (disk); }
static int aix_read (PedDisk* disk) { ped_disk_delete_all (disk); ped_exception_throw (PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_CANCEL, _("Support for reading AIX disk labels is " "is not implemented yet.")); return 0; }
static int loop_read (PedDisk* disk) { PedDevice* dev = NULL; PedGeometry* geom; PedFileSystemType* fs_type; PedPartition* part; PedConstraint* constraint_any; PED_ASSERT (disk != NULL); dev = disk->dev; constraint_any = ped_constraint_any (dev); ped_disk_delete_all (disk); void *buf; if (!ptt_read_sector (dev, 0, &buf)) goto error; int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE)); free (buf); if (found_sig) { ped_constraint_destroy (constraint_any); return 1; } geom = ped_geometry_new (dev, 0, dev->length); if (!geom) goto error; fs_type = ped_file_system_probe (geom); if (!fs_type) goto error_free_geom; part = ped_partition_new (disk, PED_PARTITION_NORMAL, fs_type, geom->start, geom->end); ped_geometry_destroy (geom); if (!part) goto error; part->fs_type = fs_type; if (!ped_disk_add_partition (disk, part, constraint_any)) goto error; ped_constraint_destroy (constraint_any); return 1; error_free_geom: ped_geometry_destroy (geom); error: ped_constraint_destroy (constraint_any); return 0; }
static int bsd_read (PedDisk* disk) { BSDDiskData* bsd_specific = (BSDDiskData*) disk->disk_specific; BSDRawLabel* label; int i; ped_disk_delete_all (disk); void *s0; if (!ptt_read_sector (disk->dev, 0, &s0)) return 0; memcpy (bsd_specific->boot_code, s0, sizeof (bsd_specific->boot_code)); free (s0); label = (BSDRawLabel *) (bsd_specific->boot_code + BSD_LABEL_OFFSET); for (i = 1; i <= BSD_MAXPARTITIONS; i++) { PedPartition* part; BSDPartitionData* bsd_part_data; PedSector start; PedSector end; PedConstraint* constraint_exact; if (!label->d_partitions[i - 1].p_size || !label->d_partitions[i - 1].p_fstype) continue; start = PED_LE32_TO_CPU(label->d_partitions[i - 1].p_offset); end = PED_LE32_TO_CPU(label->d_partitions[i - 1].p_offset) + PED_LE32_TO_CPU(label->d_partitions[i - 1].p_size) - 1; part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, start, end); if (!part) goto error; bsd_part_data = part->disk_specific; bsd_part_data->type = label->d_partitions[i - 1].p_fstype; part->num = i; part->fs_type = ped_file_system_probe (&part->geom); constraint_exact = ped_constraint_exact (&part->geom); if (!ped_disk_add_partition (disk, part, constraint_exact)) goto error; ped_constraint_destroy (constraint_exact); } return 1; error: return 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); }
static int dasd_read (PedDisk* disk) { int i; char str[20]; PedDevice* dev; PedPartition* part; PedFileSystemType *fs; PedSector start, end; PedConstraint* constraint_exact; partition_info_t *p; LinuxSpecific* arch_specific; DasdDiskSpecific* disk_specific; struct fdasd_anchor anchor; PDEBUG; PED_ASSERT (disk != NULL); PDEBUG; PED_ASSERT (disk->dev != NULL); PDEBUG; dev = disk->dev; arch_specific = LINUX_SPECIFIC(dev); disk_specific = disk->disk_specific; PDEBUG; fdasd_initialize_anchor(&anchor); if (fdasd_get_geometry(disk->dev, &anchor, arch_specific->fd) == 0) goto error_close_dev; disk_specific->label_block = anchor.label_block; if ((anchor.geo.cylinders * anchor.geo.heads) > BIG_DISK_SIZE) anchor.big_disk++; /* check dasd for labels and vtoc */ if (fdasd_check_volume(&anchor, arch_specific->fd)) { DasdPartitionData* dasd_data; /* Kernel partitioning code will report 'implicit' partitions * for non-CDL format DASDs even when there is no * label/VTOC. */ if (anchor.FBA_layout == 0) goto error_close_dev; disk_specific->format_type = 1; /* Register implicit partition */ ped_disk_delete_all (disk); start = (PedSector) arch_specific->real_sector_size / (PedSector) disk->dev->sector_size * (PedSector) (anchor.label_block + 1); end = disk->dev->length - 1; part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, start, end); if (!part) goto error_close_dev; part->num = 1; part->fs_type = ped_file_system_probe (&part->geom); dasd_data = part->disk_specific; dasd_data->raid = 0; dasd_data->lvm = 0; dasd_data->type = 0; if (!ped_disk_add_partition (disk, part, NULL)) goto error_close_dev; fdasd_cleanup(&anchor); return 1; } /* Save volume label (read by fdasd_check_volume) for writing */ memcpy(&disk_specific->vlabel, anchor.vlabel, sizeof(volume_label_t)); ped_disk_delete_all (disk); bool is_ldl = strncmp(anchor.vlabel->volkey, vtoc_ebcdic_enc("LNX1", str, 4), 4) == 0; bool is_cms = strncmp(anchor.vlabel->volkey, vtoc_ebcdic_enc("CMS1", str, 4), 4) == 0; if (is_ldl || is_cms) { DasdPartitionData* dasd_data; union vollabel { volume_label_t ldl; cms_volume_label_t cms; }; union vollabel *cms_ptr1 = (union vollabel *) anchor.vlabel; cms_volume_label_t *cms_ptr = &cms_ptr1->cms; volume_label_t *ldl_ptr = &cms_ptr1->ldl; int partition_start_block; disk_specific->format_type = 1; if (is_cms && cms_ptr->usable_count >= cms_ptr->block_count) partition_start_block = 2; /* FBA DASD */ else partition_start_block = 3; /* CKD DASD */ if (is_ldl) start = (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size * (long long) partition_start_block; else if (cms_ptr->disk_offset == 0) start = (long long) cms_ptr->block_size / (long long) disk->dev->sector_size * (long long) partition_start_block; else start = (long long) cms_ptr->block_size / (long long) disk->dev->sector_size * (long long) cms_ptr->disk_offset; if (is_ldl) if (ldl_ptr->ldl_version >= 0xf2) end = (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size * (long long) ldl_ptr->formatted_blocks - 1; else end = disk->dev->length - 1; else if (cms_ptr->disk_offset == 0) end = (long long) cms_ptr->block_size / (long long) disk->dev->sector_size * (long long) cms_ptr->block_count - 1; else /* Frankly, I do not understand why the last block of the CMS reserved file is not included in the partition; but this is the algorithm used by the Linux kernel. See fs/partitions/ibm.c in the Linux kernel source code. */ end = (long long) cms_ptr->block_size / (long long) disk->dev->sector_size * (long long) (cms_ptr->block_count - 1) - 1; part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, start, end); if (!part) goto error_close_dev; part->num = 1; part->fs_type = ped_file_system_probe (&part->geom); dasd_data = part->disk_specific; dasd_data->raid = 0; dasd_data->lvm = 0; dasd_data->type = 0; if (!ped_disk_add_partition (disk, part, NULL)) goto error_close_dev; fdasd_cleanup(&anchor); return 1; } /* CDL format, newer */ disk_specific->format_type = 2; p = anchor.first; PDEBUG; for (i = 1 ; i <= USABLE_PARTITIONS; i++) { char *ch = p->f1->DS1DSNAM; DasdPartitionData* dasd_data; if (p->used != 0x01) continue; PDEBUG; start = (long long)(long long) p->start_trk * (long long) disk->dev->hw_geom.sectors * (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size; end = (long long)((long long) p->end_trk + 1) * (long long) disk->dev->hw_geom.sectors * (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size - 1; part = ped_partition_new(disk, PED_PARTITION_NORMAL, NULL, start, end); PDEBUG; if (!part) goto error_close_dev; PDEBUG; part->num = i; part->fs_type = ped_file_system_probe(&part->geom); vtoc_ebcdic_dec(p->f1->DS1DSNAM, p->f1->DS1DSNAM, 44); ch = strstr(p->f1->DS1DSNAM, "PART"); if (ch != NULL) { strncpy(str, ch+9, 6); str[6] = '\0'; } dasd_data = part->disk_specific; if ((strncmp(PART_TYPE_RAID, str, 6) == 0) && (ped_file_system_probe(&part->geom) == NULL)) ped_partition_set_flag(part, PED_PARTITION_RAID, 1); else ped_partition_set_flag(part, PED_PARTITION_RAID, 0); if ((strncmp(PART_TYPE_LVM, str, 6) == 0) && (ped_file_system_probe(&part->geom) == NULL)) ped_partition_set_flag(part, PED_PARTITION_LVM, 1); else ped_partition_set_flag(part, PED_PARTITION_LVM, 0); if (strncmp(PART_TYPE_SWAP, str, 6) == 0) { fs = ped_file_system_probe(&part->geom); if (fs && is_linux_swap(fs->name)) { dasd_data->system = PARTITION_LINUX_SWAP; PDEBUG; } } vtoc_ebcdic_enc(p->f1->DS1DSNAM, p->f1->DS1DSNAM, 44); dasd_data->type = 0; constraint_exact = ped_constraint_exact (&part->geom); if (!constraint_exact) goto error_close_dev; if (!ped_disk_add_partition(disk, part, constraint_exact)) { ped_constraint_destroy(constraint_exact); goto error_close_dev; } ped_constraint_destroy(constraint_exact); if (p->fspace_trk > 0) { start = (long long)((long long) p->end_trk + 1) * (long long) disk->dev->hw_geom.sectors * (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size; end = (long long)((long long) p->end_trk + 1 + p->fspace_trk) * (long long) disk->dev->hw_geom.sectors * (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size - 1; part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, start, end); if (!part) goto error_close_dev; part->type = PED_PARTITION_FREESPACE; constraint_exact = ped_constraint_exact(&part->geom); if (!constraint_exact) goto error_close_dev; if (!ped_disk_add_partition(disk, part, constraint_exact)) { ped_constraint_destroy(constraint_exact); goto error_close_dev; } ped_constraint_destroy (constraint_exact); } p = p->next; } PDEBUG; fdasd_cleanup(&anchor); return 1; error_close_dev: PDEBUG; fdasd_cleanup(&anchor); return 0; }
static int read_table (PedDisk* disk) { int i; PC98RawTable table; PedConstraint* constraint_any; PED_ASSERT (disk != NULL); PED_ASSERT (disk->dev != NULL); constraint_any = ped_constraint_any (disk->dev); if (!ped_device_read (disk->dev, (void*) &table, 0, 2)) goto error; if (!pc98_check_magic(&table)) { if (ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL, _("Invalid partition table on %s."), disk->dev->path)) goto error; } for (i = 0; i < MAX_PART_COUNT; i++) { PC98RawPartition* raw_part; PedPartition* part; PC98PartitionData* pc98_data; PedSector part_start; PedSector part_end; raw_part = &table.partitions [i]; if (is_unused_partition(raw_part)) continue; part_start = legacy_start (disk, raw_part); part_end = legacy_end (disk, raw_part); part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, part_start, part_end); if (!part) goto error; pc98_data = part->disk_specific; PED_ASSERT (pc98_data != NULL); pc98_data->system = (raw_part->mid << 8) | raw_part->sid; pc98_data->boot = GET_BIT(raw_part->mid, 7); pc98_data->hidden = !GET_BIT(raw_part->sid, 7); ped_partition_set_name (part, raw_part->name); pc98_data->ipl_sector = chs_to_sector ( disk->dev, PED_LE16_TO_CPU(raw_part->ipl_cyl), raw_part->ipl_head, raw_part->ipl_sect); /* hack */ if (pc98_data->ipl_sector == part->geom.start) pc98_data->ipl_sector = 0; part->num = i + 1; if (!ped_disk_add_partition (disk, part, constraint_any)) goto error; if (part->geom.start != part_start || part->geom.end != part_end) { ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_CANCEL, _("Partition %d isn't aligned to cylinder " "boundaries. This is still unsupported."), part->num); goto error; } part->fs_type = ped_file_system_probe (&part->geom); } ped_constraint_destroy (constraint_any); return 1; error: ped_disk_delete_all (disk); ped_constraint_destroy (constraint_any); return 0; }
static int dvh_read (PedDisk* disk) { DVHDiskData* dvh_disk_data = disk->disk_specific; int i; struct volume_header vh; char boot_name [BFNAMESIZE + 1]; #ifndef DISCOVER_ONLY int write_back = 0; #endif PED_ASSERT (dvh_disk_data != NULL); ped_disk_delete_all (disk); void *s0; if (!ptt_read_sector (disk->dev, 0, &s0)) return 0; memcpy (&vh, s0, sizeof vh); free (s0); if (_checksum ((uint32_t*) &vh, sizeof (struct volume_header))) { if (ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL, _("Checksum is wrong, indicating the partition " "table is corrupt.")) == PED_EXCEPTION_CANCEL) return 0; } PED_ASSERT (PED_BE32_TO_CPU (vh.vh_magic) == VHMAGIC); dvh_disk_data->dev_params = vh.vh_dp; strncpy (boot_name, vh.vh_bootfile, BFNAMESIZE); boot_name[BFNAMESIZE] = 0; /* normal partitions */ for (i = 0; i < NPARTAB; i++) { PedPartition* part; if (!vh.vh_pt[i].pt_nblks) continue; /* Skip the whole-disk partition, parted disklikes overlap */ if (PED_BE32_TO_CPU (vh.vh_pt[i].pt_type) == PTYPE_VOLUME) continue; part = _parse_partition (disk, &vh.vh_pt[i]); if (!part) goto error_delete_all; part->fs_type = ped_file_system_probe (&part->geom); part->num = i + 1; if (PED_BE16_TO_CPU (vh.vh_rootpt) == i) ped_partition_set_flag (part, PED_PARTITION_ROOT, 1); if (PED_BE16_TO_CPU (vh.vh_swappt) == i) ped_partition_set_flag (part, PED_PARTITION_SWAP, 1); PedConstraint *constraint_exact = ped_constraint_exact (&part->geom); bool ok = ped_disk_add_partition (disk, part, constraint_exact); ped_constraint_destroy (constraint_exact); if (!ok) { ped_partition_destroy (part); goto error_delete_all; } } if (!ped_disk_extended_partition (disk)) { #ifdef DISCOVER_ONLY return 1; #else switch (_handle_no_volume_header (disk)) { case PED_EXCEPTION_CANCEL: return 0; case PED_EXCEPTION_IGNORE: return 1; case PED_EXCEPTION_FIX: write_back = 1; break; default: break; } #endif } /* boot partitions */ for (i = 0; i < NVDIR; i++) { PedPartition* part; if (!vh.vh_vd[i].vd_nbytes) continue; part = _parse_boot_file (disk, &vh.vh_vd[i]); if (!part) goto error_delete_all; part->fs_type = ped_file_system_probe (&part->geom); part->num = NPARTAB + i + 1; if (!strcmp (boot_name, ped_partition_get_name (part))) ped_partition_set_flag (part, PED_PARTITION_BOOT, 1); PedConstraint *constraint_exact = ped_constraint_exact (&part->geom); bool ok = ped_disk_add_partition (disk, part, constraint_exact); ped_constraint_destroy (constraint_exact); if (!ok) { ped_partition_destroy (part); goto error_delete_all; } } #ifndef DISCOVER_ONLY if (write_back) dvh_write (disk); #endif return 1; error_delete_all: ped_disk_delete_all (disk); return 0; }
/* We have already allocated a rdb, we are now reading it from the disk */ static int amiga_read (PedDisk* disk) { struct RigidDiskBlock *rdb; struct PartitionBlock *partition; uint32_t partblock; uint32_t partlist[AMIGA_MAX_PARTITIONS]; PedSector cylblocks; int i; PED_ASSERT(disk != NULL); PED_ASSERT(disk->dev != NULL); PED_ASSERT(disk->dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0); PED_ASSERT(disk->disk_specific != NULL); rdb = RDSK(disk->disk_specific); if (_amiga_find_rdb (disk->dev, rdb) == AMIGA_RDB_NOT_FOUND) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("%s : Didn't find rdb block, should never happen."), __func__); return 0; } /* Let's copy the rdb read geometry to the dev */ /* FIXME: should this go into disk->dev->bios_geom instead? */ disk->dev->hw_geom.cylinders = PED_BE32_TO_CPU (rdb->rdb_Cylinders); disk->dev->hw_geom.heads = PED_BE32_TO_CPU (rdb->rdb_Heads); disk->dev->hw_geom.sectors = PED_BE32_TO_CPU (rdb->rdb_Sectors); cylblocks = (PedSector) PED_BE32_TO_CPU (rdb->rdb_Heads) * (PedSector) PED_BE32_TO_CPU (rdb->rdb_Sectors); /* Remove all partitions in the former in memory table */ ped_disk_delete_all (disk); /* Let's allocate a partition block */ if (!(partition = ped_malloc (disk->dev->sector_size))) return 0; /* We initialize the hardblock free list to detect loops */ for (i = 0; i < AMIGA_MAX_PARTITIONS; i++) partlist[i] = LINK_END; for (i = 1, partblock = PED_BE32_TO_CPU(rdb->rdb_PartitionList); i < AMIGA_MAX_PARTITIONS && partblock != LINK_END; i++, partblock = PED_BE32_TO_CPU(partition->pb_Next)) { PedPartition *part; PedSector start, end; /* Let's look for loops in the partition table */ if (_amiga_loop_check(partblock, partlist, i)) { break; } /* Let's allocate and read a partition block to get its geometry*/ if (!_amiga_read_block (disk->dev, AMIGA(partition), (PedSector)partblock, NULL)) { free(partition); return 0; } start = ((PedSector) PED_BE32_TO_CPU (partition->de_LowCyl)) * cylblocks; end = (((PedSector) PED_BE32_TO_CPU (partition->de_HighCyl)) + 1) * cylblocks - 1; /* We can now construct a new partition */ if (!(part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL, start, end))) { free(partition); return 0; } /* And copy over the partition block */ memcpy(part->disk_specific, partition, 256); part->num = i; part->type = 0; /* Let's probe what file system is present on the disk */ part->fs_type = ped_file_system_probe (&part->geom); PedConstraint *constraint_exact = ped_constraint_exact (&part->geom); if (constraint_exact == NULL) return 0; bool ok = ped_disk_add_partition (disk, part, constraint_exact); ped_constraint_destroy (constraint_exact); if (!ok) { ped_partition_destroy(part); free(partition); return 0; } } free(partition); return 1; }