1/* 2 * libfdisk.h - libfdisk API 3 * 4 * Copyright (C) 2012-2014 Karel Zak <kzak@redhat.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 */ 20 21#ifndef _LIBFDISK_H 22#define _LIBFDISK_H 23 24#ifdef __cplusplus 25extern "C" { 26#endif 27 28#include <stdio.h> 29#include <stdarg.h> 30#include <stdint.h> 31#include <sys/types.h> 32 33/** 34 * LIBFDISK_VERSION: 35 * 36 * Library version string 37 */ 38#define LIBFDISK_VERSION "@LIBFDISK_VERSION@" 39 40#define LIBFDISK_MAJOR_VERSION @LIBFDISK_MAJOR_VERSION@ 41#define LIBFDISK_MINOR_VERSION @LIBFDISK_MINOR_VERSION@ 42#define LIBFDISK_PATCH_VERSION @LIBFDISK_PATCH_VERSION@ 43 44/** 45 * fdisk_context: 46 * 47 * Basic library handler. 48 */ 49struct fdisk_context; 50 51/** 52 * fdisk_label: 53 * 54 * Disk label specific driver and setting. 55 */ 56struct fdisk_label; 57 58/** 59 * fdisk_parttype: 60 * 61 * Partition type. 62 */ 63struct fdisk_parttype; 64 65/** 66 * fdisk_partition: 67 * 68 * Partition abstraction (and template). 69 */ 70struct fdisk_partition; 71 72/** 73 * fdisk_ask: 74 * 75 * Ask API handler for dialogs with users. 76 */ 77struct fdisk_ask; 78 79/** 80 * fdisk_iter: 81 * 82 * Unified iterator. 83 */ 84struct fdisk_iter; 85 86/** 87 * fdisk_table: 88 * 89 * Container for fdisk_partition objects 90 */ 91struct fdisk_table; 92 93/** 94 * fdisk_field 95 * 96 * Output field description. 97 */ 98struct fdisk_field; 99 100/** 101 * fdisk_script 102 * 103 * library handler for sfdisk compatible scripts and dumps 104 */ 105struct fdisk_script; 106 107/** 108 * fdisk_sector_t 109 * 110 * LBA addresses type 111 */ 112typedef uint64_t fdisk_sector_t; 113 114/** 115 * fdisk_labeltype: 116 * @FDISK_DISKLABEL_DOS: MBR label type 117 * @FDISK_DISKLABEL_SUN: SUN label type 118 * @FDISK_DISKLABEL_SGI: SGI label type 119 * @FDISK_DISKLABEL_BSD: BSD label type 120 * @FDISK_DISKLABEL_GPT: UEFI GPT type 121 * 122 * Supported partition table types (labels) 123 */ 124enum fdisk_labeltype { 125 FDISK_DISKLABEL_DOS = (1 << 1), /* MBR label type */ 126 FDISK_DISKLABEL_SUN = (1 << 2), /* SUN label type */ 127 FDISK_DISKLABEL_SGI = (1 << 3), /* SGI label type */ 128 FDISK_DISKLABEL_BSD = (1 << 4), /* BSD label t ype */ 129 FDISK_DISKLABEL_GPT = (1 << 5) /* UEFI GPT type */ 130}; 131 132/** 133 * fdisk_labelitem: 134 * 135 * library handler for label specific information. See 136 * generic FDISK_LABELITEM_* and label specific {GPT,MBR,..}_LABELITEM_*. 137 */ 138struct fdisk_labelitem; 139 140/** 141 * fdisk_asktype: 142 * @FDISK_ASKTYPE_NONE: undefined type 143 * @FDISK_ASKTYPE_NUMBER: ask for number 144 * @FDISK_ASKTYPE_OFFSET: ask for offset 145 * @FDISK_ASKTYPE_WARN: print warning message and errno 146 * @FDISK_ASKTYPE_WARNX: print warning message 147 * @FDISK_ASKTYPE_INFO: print info message 148 * @FDISK_ASKTYPE_YESNO: ask Yes/No question 149 * @FDISK_ASKTYPE_STRING: ask for string 150 * @FDISK_ASKTYPE_MENU: ask for menu item 151 * 152 * Ask API dialog types 153 */ 154enum fdisk_asktype { 155 FDISK_ASKTYPE_NONE = 0, 156 FDISK_ASKTYPE_NUMBER, 157 FDISK_ASKTYPE_OFFSET, 158 FDISK_ASKTYPE_WARN, 159 FDISK_ASKTYPE_WARNX, 160 FDISK_ASKTYPE_INFO, 161 FDISK_ASKTYPE_YESNO, 162 FDISK_ASKTYPE_STRING, 163 FDISK_ASKTYPE_MENU 164}; 165 166/* init.c */ 167extern void fdisk_init_debug(int mask); 168 169/* version.c */ 170extern int fdisk_parse_version_string(const char *ver_string); 171extern int fdisk_get_library_version(const char **ver_string); 172extern int fdisk_get_library_features(const char ***features); 173 174/* context.h */ 175 176#define FDISK_PLURAL 0 177#define FDISK_SINGULAR 1 178 179struct fdisk_context *fdisk_new_context(void); 180struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent, const char *name); 181void fdisk_unref_context(struct fdisk_context *cxt); 182void fdisk_ref_context(struct fdisk_context *cxt); 183 184struct fdisk_context *fdisk_get_parent(struct fdisk_context *cxt); 185size_t fdisk_get_npartitions(struct fdisk_context *cxt); 186 187struct fdisk_label *fdisk_get_label(struct fdisk_context *cxt, const char *name); 188int fdisk_next_label(struct fdisk_context *cxt, struct fdisk_label **lb); 189size_t fdisk_get_nlabels(struct fdisk_context *cxt); 190 191int fdisk_has_label(struct fdisk_context *cxt); 192int fdisk_is_labeltype(struct fdisk_context *cxt, enum fdisk_labeltype id); 193#define fdisk_is_label(c, x) fdisk_is_labeltype(c, FDISK_DISKLABEL_ ## x) 194 195 196int fdisk_assign_device(struct fdisk_context *cxt, 197 const char *fname, int readonly); 198int fdisk_assign_device_by_fd(struct fdisk_context *cxt, int fd, 199 const char *fname, int readonly); 200int fdisk_deassign_device(struct fdisk_context *cxt, int nosync); 201int fdisk_reassign_device(struct fdisk_context *cxt); 202 203int fdisk_is_readonly(struct fdisk_context *cxt); 204int fdisk_is_regfile(struct fdisk_context *cxt); 205int fdisk_device_is_used(struct fdisk_context *cxt); 206 207int fdisk_disable_dialogs(struct fdisk_context *cxt, int disable); 208int fdisk_has_dialogs(struct fdisk_context *cxt); 209 210int fdisk_enable_details(struct fdisk_context *cxt, int enable); 211int fdisk_is_details(struct fdisk_context *cxt); 212 213int fdisk_enable_listonly(struct fdisk_context *cxt, int enable); 214int fdisk_is_listonly(struct fdisk_context *cxt); 215 216int fdisk_enable_wipe(struct fdisk_context *cxt, int enable); 217int fdisk_has_wipe(struct fdisk_context *cxt); 218const char *fdisk_get_collision(struct fdisk_context *cxt); 219int fdisk_is_ptcollision(struct fdisk_context *cxt); 220 221int fdisk_set_unit(struct fdisk_context *cxt, const char *str); 222const char *fdisk_get_unit(struct fdisk_context *cxt, int n); 223int fdisk_use_cylinders(struct fdisk_context *cxt); 224unsigned int fdisk_get_units_per_sector(struct fdisk_context *cxt); 225 226unsigned long fdisk_get_optimal_iosize(struct fdisk_context *cxt); 227unsigned long fdisk_get_minimal_iosize(struct fdisk_context *cxt); 228unsigned long fdisk_get_physector_size(struct fdisk_context *cxt); 229unsigned long fdisk_get_sector_size(struct fdisk_context *cxt); 230unsigned long fdisk_get_alignment_offset(struct fdisk_context *cxt); 231unsigned long fdisk_get_grain_size(struct fdisk_context *cxt); 232fdisk_sector_t fdisk_get_first_lba(struct fdisk_context *cxt); 233fdisk_sector_t fdisk_set_first_lba(struct fdisk_context *cxt, fdisk_sector_t lba); 234fdisk_sector_t fdisk_get_last_lba(struct fdisk_context *cxt); 235fdisk_sector_t fdisk_set_last_lba(struct fdisk_context *cxt, fdisk_sector_t lba); 236fdisk_sector_t fdisk_get_nsectors(struct fdisk_context *cxt); 237 238const char *fdisk_get_devname(struct fdisk_context *cxt); 239int fdisk_get_devfd(struct fdisk_context *cxt); 240dev_t fdisk_get_devno(struct fdisk_context *cxt); 241const char *fdisk_get_devmodel(struct fdisk_context *cxt); 242 243 244unsigned int fdisk_get_geom_heads(struct fdisk_context *cxt); 245fdisk_sector_t fdisk_get_geom_sectors(struct fdisk_context *cxt); 246fdisk_sector_t fdisk_get_geom_cylinders(struct fdisk_context *cxt); 247 248enum { 249 FDISK_SIZEUNIT_HUMAN = 0, /* default, human readable {M,G,P,...} */ 250 FDISK_SIZEUNIT_BYTES /* bytes */ 251}; 252int fdisk_set_size_unit(struct fdisk_context *cxt, int unit); 253int fdisk_get_size_unit(struct fdisk_context *cxt); 254 255int fdisk_has_protected_bootbits(struct fdisk_context *cxt); 256int fdisk_enable_bootbits_protection(struct fdisk_context *cxt, int enable); 257 258/* parttype.c */ 259struct fdisk_parttype *fdisk_new_parttype(void); 260void fdisk_ref_parttype(struct fdisk_parttype *t); 261void fdisk_unref_parttype(struct fdisk_parttype *t); 262int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str); 263int fdisk_parttype_set_typestr(struct fdisk_parttype *t, const char *str); 264int fdisk_parttype_set_code(struct fdisk_parttype *t, int code); 265size_t fdisk_label_get_nparttypes(const struct fdisk_label *lb); 266struct fdisk_parttype *fdisk_label_get_parttype(const struct fdisk_label *lb, size_t n); 267int fdisk_label_get_parttype_shortcut( 268 const struct fdisk_label *lb, size_t n, 269 const char **typestr, 270 const char **shortcut, 271 const char **alias); 272int fdisk_label_has_code_parttypes(const struct fdisk_label *lb); 273int fdisk_label_has_parttypes_shortcuts(const struct fdisk_label *lb); 274struct fdisk_parttype *fdisk_label_get_parttype_from_code( 275 const struct fdisk_label *lb, 276 unsigned int code); 277struct fdisk_parttype *fdisk_label_get_parttype_from_string( 278 const struct fdisk_label *lb, 279 const char *str); 280struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code, 281 const char *typestr); 282struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type); 283struct fdisk_parttype *fdisk_label_parse_parttype( 284 const struct fdisk_label *lb, 285 const char *str); 286struct fdisk_parttype *fdisk_label_advparse_parttype( 287 const struct fdisk_label *lb, 288 const char *str, 289 int flags); 290 291/** 292 * fdisk_parttype_parser_flags: 293 * @FDISK_PARTTYPE_PARSE_DATA: parse hex or UUID from string 294 * @FDISK_PARTTYPE_PARSE_DATALAST: try hex or UUID as the last possibility (don't use!) 295 * @FDISK_PARTTYPE_PARSE_SHORTCUT: try input as type shortcut (e.g 'L' for linux partition) 296 * @FDISK_PARTTYPE_PARSE_ALIAS: try input as type alias (e.g. 'linux' for linux partition) 297 * @FDISK_PARTTYPE_PARSE_DEPRECATED: accept also deprecated aliases and shortcuts 298 * @FDISK_PARTTYPE_PARSE_DEFAULT: recommended flags for new code 299 * @FDISK_PARTTYPE_PARSE_NOUNKNOWN: ignore unknown types 300 * @FDISK_PARTTYPE_PARSE_SEQNUM: use input as sequntial number of type (e.g. list-types fdisk dialog) 301 */ 302enum fdisk_parttype_parser_flags { 303 FDISK_PARTTYPE_PARSE_DATA = (1 << 1), 304 FDISK_PARTTYPE_PARSE_DATALAST = (1 << 2), 305 FDISK_PARTTYPE_PARSE_SHORTCUT = (1 << 3), 306 FDISK_PARTTYPE_PARSE_ALIAS = (1 << 4), 307 FDISK_PARTTYPE_PARSE_DEPRECATED = (1 << 5), 308 FDISK_PARTTYPE_PARSE_NOUNKNOWN = (1 << 6), 309 FDISK_PARTTYPE_PARSE_SEQNUM = (1 << 7), 310 311 FDISK_PARTTYPE_PARSE_DEFAULT = (FDISK_PARTTYPE_PARSE_DATA | \ 312 FDISK_PARTTYPE_PARSE_SHORTCUT | \ 313 FDISK_PARTTYPE_PARSE_ALIAS | \ 314 FDISK_PARTTYPE_PARSE_SEQNUM ) 315}; 316 317const char *fdisk_parttype_get_string(const struct fdisk_parttype *t); 318unsigned int fdisk_parttype_get_code(const struct fdisk_parttype *t); 319const char *fdisk_parttype_get_name(const struct fdisk_parttype *t); 320int fdisk_parttype_is_unknown(const struct fdisk_parttype *t); 321 322 323/* field.c */ 324extern int fdisk_field_get_id(const struct fdisk_field *field); 325extern const char *fdisk_field_get_name(const struct fdisk_field *field); 326extern double fdisk_field_get_width(const struct fdisk_field *field); 327extern int fdisk_field_is_number(const struct fdisk_field *field); 328 329 330/* label.c */ 331 332/** 333 * fdisk_fieldtype: 334 * @FDISK_FIELD_NONE: unspecified item 335 * @FDISK_FIELD_DEVICE: partition device name 336 * @FDISK_FIELD_START: start offset of the partition 337 * @FDISK_FIELD_END: end offset of the partition 338 * @FDISK_FIELD_SECTORS: number of sectors 339 * @FDISK_FIELD_CYLINDERS: number of cylinders (deprecated) 340 * @FDISK_FIELD_SIZE: partition size 341 * @FDISK_FIELD_TYPE: partition type 342 * @FDISK_FIELD_TYPEID: partition type ID 343 * @FDISK_FIELD_ATTR: partition attribute (GPT) 344 * @FDISK_FIELD_BOOT: partition boot flag 345 * @FDISK_FIELD_BSIZE: size of the boot area (BSD) 346 * @FDISK_FIELD_CPG: BSD 347 * @FDISK_FIELD_EADDR: End-C/H/S (MBR) 348 * @FDISK_FIELD_FSIZE: BSD 349 * @FDISK_FIELD_NAME: partition label/name 350 * @FDISK_FIELD_SADDR: Start-C/H/S (MBR) 351 * @FDISK_FIELD_UUID: partition UUID (GPT) 352 * @FDISK_FIELD_FSUUID: Filesystem UUID 353 * @FDISK_FIELD_FSLABEL: Filesystem LABEL 354 * @FDISK_FIELD_FSTYPE: Filesystem type 355 * @FDISK_NFIELDS: Don't use, counter. 356 * 357 * Types of fdisk_field. The fields describe a partition. 358 */ 359enum fdisk_fieldtype { 360 FDISK_FIELD_NONE = 0, 361 362 /* generic */ 363 FDISK_FIELD_DEVICE, 364 FDISK_FIELD_START, 365 FDISK_FIELD_END, 366 FDISK_FIELD_SECTORS, 367 FDISK_FIELD_CYLINDERS, 368 FDISK_FIELD_SIZE, 369 FDISK_FIELD_TYPE, 370 FDISK_FIELD_TYPEID, 371 372 /* label specific */ 373 FDISK_FIELD_ATTR, 374 FDISK_FIELD_BOOT, 375 FDISK_FIELD_BSIZE, 376 FDISK_FIELD_CPG, 377 FDISK_FIELD_EADDR, 378 FDISK_FIELD_FSIZE, 379 FDISK_FIELD_NAME, 380 FDISK_FIELD_SADDR, 381 FDISK_FIELD_UUID, 382 383 FDISK_FIELD_FSUUID, 384 FDISK_FIELD_FSLABEL, 385 FDISK_FIELD_FSTYPE, 386 387 FDISK_NFIELDS /* must be last */ 388}; 389 390int fdisk_label_get_type(const struct fdisk_label *lb); 391const char *fdisk_label_get_name(const struct fdisk_label *lb); 392int fdisk_label_require_geometry(const struct fdisk_label *lb); 393 394 395extern int fdisk_write_disklabel(struct fdisk_context *cxt); 396extern int fdisk_verify_disklabel(struct fdisk_context *cxt); 397extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name); 398extern int fdisk_list_disklabel(struct fdisk_context *cxt); 399extern int fdisk_locate_disklabel(struct fdisk_context *cxt, int n, 400 const char **name, 401 uint64_t *offset, 402 size_t *size); 403 404extern int fdisk_label_get_geomrange_cylinders(const struct fdisk_label *lb, 405 fdisk_sector_t *mi, fdisk_sector_t *ma); 406extern int fdisk_label_get_geomrange_heads(const struct fdisk_label *lb, 407 unsigned int *mi, unsigned int *ma); 408extern int fdisk_label_get_geomrange_sectors(const struct fdisk_label *lb, 409 fdisk_sector_t *mi, fdisk_sector_t *ma); 410 411/** 412 * fdisk_labelitem_gen: 413 * @FDISK_LABELITEM_ID: Unique disk identifier 414 * @__FDISK_NLABELITEMS: Specifies reserved range for generic items (0..7) 415 * 416 * Generic disklabel items. 417 */ 418enum fdisk_labelitem_gen { 419 FDISK_LABELITEM_ID = 0, 420 __FDISK_NLABELITEMS = 8 421}; 422 423/* item.c */ 424extern struct fdisk_labelitem *fdisk_new_labelitem(void); 425extern void fdisk_ref_labelitem(struct fdisk_labelitem *li); 426extern int fdisk_reset_labelitem(struct fdisk_labelitem *li); 427extern void fdisk_unref_labelitem(struct fdisk_labelitem *li); 428extern const char *fdisk_labelitem_get_name(struct fdisk_labelitem *li); 429extern int fdisk_labelitem_get_id(struct fdisk_labelitem *li); 430extern int fdisk_labelitem_get_data_u64(struct fdisk_labelitem *li, uint64_t *data); 431extern int fdisk_labelitem_get_data_string(struct fdisk_labelitem *li, const char **data); 432extern int fdisk_labelitem_is_string(struct fdisk_labelitem *li); 433extern int fdisk_labelitem_is_number(struct fdisk_labelitem *li); 434 435extern int fdisk_get_disklabel_item(struct fdisk_context *cxt, int id, struct fdisk_labelitem *item); 436 437extern int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id); 438extern int fdisk_set_disklabel_id(struct fdisk_context *cxt); 439extern int fdisk_set_disklabel_id_from_string(struct fdisk_context *cxt, const char *str); 440 441extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition **pa); 442extern int fdisk_set_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition *pa); 443extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_partition *pa, size_t *partno); 444extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partno); 445extern int fdisk_delete_all_partitions(struct fdisk_context *cxt); 446 447extern int fdisk_wipe_partition(struct fdisk_context *cxt, size_t partno, int enable); 448 449extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum, 450 struct fdisk_parttype *t); 451 452 453extern int fdisk_label_get_fields_ids( 454 const struct fdisk_label *lb, 455 struct fdisk_context *cxt, 456 int **ids, size_t *nids); 457 458extern int fdisk_label_get_fields_ids_all( 459 const struct fdisk_label *lb, 460 struct fdisk_context *cxt, 461 int **ids, size_t *nids); 462 463extern const struct fdisk_field *fdisk_label_get_field(const struct fdisk_label *lb, int id); 464extern const struct fdisk_field *fdisk_label_get_field_by_name( 465 const struct fdisk_label *lb, 466 const char *name); 467 468extern void fdisk_label_set_changed(struct fdisk_label *lb, int changed); 469extern int fdisk_label_is_changed(const struct fdisk_label *lb); 470 471extern void fdisk_label_set_disabled(struct fdisk_label *lb, int disabled); 472extern int fdisk_label_is_disabled(const struct fdisk_label *lb); 473 474extern int fdisk_is_partition_used(struct fdisk_context *cxt, size_t n); 475 476extern int fdisk_toggle_partition_flag(struct fdisk_context *cxt, size_t partnum, unsigned long flag); 477 478extern struct fdisk_partition *fdisk_new_partition(void); 479extern void fdisk_reset_partition(struct fdisk_partition *pa); 480extern void fdisk_ref_partition(struct fdisk_partition *pa); 481extern void fdisk_unref_partition(struct fdisk_partition *pa); 482extern int fdisk_partition_is_freespace(struct fdisk_partition *pa); 483 484int fdisk_partition_set_start(struct fdisk_partition *pa, uint64_t off); 485int fdisk_partition_unset_start(struct fdisk_partition *pa); 486fdisk_sector_t fdisk_partition_get_start(struct fdisk_partition *pa); 487int fdisk_partition_has_start(struct fdisk_partition *pa); 488int fdisk_partition_cmp_start(struct fdisk_partition *a, 489 struct fdisk_partition *b); 490int fdisk_partition_start_follow_default(struct fdisk_partition *pa, int enable); 491int fdisk_partition_start_is_default(struct fdisk_partition *pa); 492 493int fdisk_partition_set_size(struct fdisk_partition *pa, uint64_t sz); 494int fdisk_partition_unset_size(struct fdisk_partition *pa); 495fdisk_sector_t fdisk_partition_get_size(struct fdisk_partition *pa); 496int fdisk_partition_has_size(struct fdisk_partition *pa); 497int fdisk_partition_size_explicit(struct fdisk_partition *pa, int enable); 498 499int fdisk_partition_has_end(struct fdisk_partition *pa); 500fdisk_sector_t fdisk_partition_get_end(struct fdisk_partition *pa); 501 502int fdisk_partition_set_partno(struct fdisk_partition *pa, size_t num); 503int fdisk_partition_unset_partno(struct fdisk_partition *pa); 504size_t fdisk_partition_get_partno(struct fdisk_partition *pa); 505int fdisk_partition_has_partno(struct fdisk_partition *pa); 506int fdisk_partition_cmp_partno(struct fdisk_partition *a, 507 struct fdisk_partition *b); 508 509int fdisk_partition_partno_follow_default(struct fdisk_partition *pa, int enable); 510 511extern int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *type); 512extern struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa); 513extern int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name); 514extern const char *fdisk_partition_get_name(struct fdisk_partition *pa); 515extern int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid); 516extern int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs); 517extern const char *fdisk_partition_get_uuid(struct fdisk_partition *pa); 518extern const char *fdisk_partition_get_attrs(struct fdisk_partition *pa); 519extern int fdisk_partition_is_nested(struct fdisk_partition *pa); 520extern int fdisk_partition_is_container(struct fdisk_partition *pa); 521extern int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent); 522extern int fdisk_partition_is_used(struct fdisk_partition *pa); 523extern int fdisk_partition_is_bootable(struct fdisk_partition *pa); 524extern int fdisk_partition_is_wholedisk(struct fdisk_partition *pa); 525extern int fdisk_partition_to_string(struct fdisk_partition *pa, 526 struct fdisk_context *cxt, 527 int id, char **data); 528 529int fdisk_partition_next_partno(struct fdisk_partition *pa, 530 struct fdisk_context *cxt, 531 size_t *n); 532 533extern int fdisk_partition_end_follow_default(struct fdisk_partition *pa, int enable); 534extern int fdisk_partition_end_is_default(struct fdisk_partition *pa); 535 536extern int fdisk_reorder_partitions(struct fdisk_context *cxt); 537 538extern int fdisk_partition_has_wipe(struct fdisk_context *cxt, struct fdisk_partition *pa); 539 540 541/* table.c */ 542extern struct fdisk_table *fdisk_new_table(void); 543extern int fdisk_reset_table(struct fdisk_table *tb); 544extern void fdisk_ref_table(struct fdisk_table *tb); 545extern void fdisk_unref_table(struct fdisk_table *tb); 546extern size_t fdisk_table_get_nents(struct fdisk_table *tb); 547extern int fdisk_table_is_empty(struct fdisk_table *tb); 548extern int fdisk_table_add_partition(struct fdisk_table *tb, struct fdisk_partition *pa); 549extern int fdisk_table_remove_partition(struct fdisk_table *tb, struct fdisk_partition *pa); 550 551extern int fdisk_get_partitions(struct fdisk_context *cxt, struct fdisk_table **tb); 552extern int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb); 553 554 555extern int fdisk_table_wrong_order(struct fdisk_table *tb); 556extern int fdisk_table_sort_partitions(struct fdisk_table *tb, 557 int (*cmp)(struct fdisk_partition *, 558 struct fdisk_partition *)); 559 560extern int fdisk_table_next_partition( 561 struct fdisk_table *tb, 562 struct fdisk_iter *itr, 563 struct fdisk_partition **pa); 564 565extern struct fdisk_partition *fdisk_table_get_partition( 566 struct fdisk_table *tb, 567 size_t n); 568extern struct fdisk_partition *fdisk_table_get_partition_by_partno( 569 struct fdisk_table *tb, 570 size_t partno); 571 572extern int fdisk_apply_table(struct fdisk_context *cxt, struct fdisk_table *tb); 573 574/* alignment.c */ 575#define FDISK_ALIGN_UP 1 576#define FDISK_ALIGN_DOWN 2 577#define FDISK_ALIGN_NEAREST 3 578 579fdisk_sector_t fdisk_align_lba(struct fdisk_context *cxt, fdisk_sector_t lba, int direction); 580fdisk_sector_t fdisk_align_lba_in_range(struct fdisk_context *cxt, 581 fdisk_sector_t lba, fdisk_sector_t start, fdisk_sector_t stop); 582int fdisk_lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba); 583 584int fdisk_override_geometry(struct fdisk_context *cxt, 585 unsigned int cylinders, 586 unsigned int heads, 587 unsigned int sectors); 588int fdisk_save_user_geometry(struct fdisk_context *cxt, 589 unsigned int cylinders, 590 unsigned int heads, 591 unsigned int sectors); 592int fdisk_save_user_sector_size(struct fdisk_context *cxt, 593 unsigned int phy, 594 unsigned int log); 595 596int fdisk_save_user_grain(struct fdisk_context *cxt, unsigned long grain); 597 598int fdisk_has_user_device_properties(struct fdisk_context *cxt); 599int fdisk_reset_alignment(struct fdisk_context *cxt); 600int fdisk_reset_device_properties(struct fdisk_context *cxt); 601int fdisk_reread_partition_table(struct fdisk_context *cxt); 602int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org); 603 604/* iter.c */ 605enum { 606 607 FDISK_ITER_FORWARD = 0, 608 FDISK_ITER_BACKWARD 609}; 610extern struct fdisk_iter *fdisk_new_iter(int direction); 611extern void fdisk_free_iter(struct fdisk_iter *itr); 612extern void fdisk_reset_iter(struct fdisk_iter *itr, int direction); 613extern int fdisk_iter_get_direction(struct fdisk_iter *itr); 614 615 616/* dos.c */ 617#define DOS_FLAG_ACTIVE 1 618 619extern int fdisk_dos_move_begin(struct fdisk_context *cxt, size_t i); 620extern int fdisk_dos_enable_compatible(struct fdisk_label *lb, int enable); 621extern int fdisk_dos_is_compatible(struct fdisk_label *lb); 622 623/* sun.h */ 624extern int fdisk_sun_set_alt_cyl(struct fdisk_context *cxt); 625extern int fdisk_sun_set_xcyl(struct fdisk_context *cxt); 626extern int fdisk_sun_set_ilfact(struct fdisk_context *cxt); 627extern int fdisk_sun_set_rspeed(struct fdisk_context *cxt); 628extern int fdisk_sun_set_pcylcount(struct fdisk_context *cxt); 629 630/** 631 * fdisk_labelitem_sun: 632 * @SUN_LABELITEM_LABELID: Label ID 633 * @SUN_LABELITEM_VTOCID: Volume ID 634 * @SUN_LABELITEM_RPM: Rpm 635 * @SUN_LABELITEM_ACYL: Alternate cylinders 636 * @SUN_LABELITEM_PCYL: Physical cylinders 637 * @SUN_LABELITEM_APC: Extra sects/cyl 638 * @SUN_LABELITEM_INTRLV: Interleave 639 * 640 * SUN specific label items. 641 */ 642enum fdisk_labelitem_sun { 643 SUN_LABELITEM_LABELID = __FDISK_NLABELITEMS, 644 SUN_LABELITEM_VTOCID, 645 SUN_LABELITEM_RPM, 646 SUN_LABELITEM_ACYL, 647 SUN_LABELITEM_PCYL, 648 SUN_LABELITEM_APC, 649 SUN_LABELITEM_INTRLV 650}; 651 652/* bsd.c */ 653extern int fdisk_bsd_edit_disklabel(struct fdisk_context *cxt); 654extern int fdisk_bsd_write_bootstrap(struct fdisk_context *cxt); 655extern int fdisk_bsd_link_partition(struct fdisk_context *cxt); 656 657/** 658 * fdisk_labelitem_bsd: 659 * @BSD_LABELITEM_TYPE: type 660 * @BSD_LABELITEM_DISK: disk 661 * @BSD_LABELITEM_PACKNAME: packname 662 * @BSD_LABELITEM_FLAGS: flags (removable, ecc, badsect) 663 * @BSD_LABELITEM_SECSIZE: Bytes/Sector 664 * @BSD_LABELITEM_NTRACKS: Tracks/Cylinder 665 * @BSD_LABELITEM_SECPERCYL: Sectors/Cylinder 666 * @BSD_LABELITEM_CYLINDERS: Cylinders 667 * @BSD_LABELITEM_RPM: rpm 668 * @BSD_LABELITEM_INTERLEAVE: interleave 669 * @BSD_LABELITEM_TRACKSKEW: trackskew 670 * @BSD_LABELITEM_CYLINDERSKEW: cylinderskew 671 * @BSD_LABELITEM_HEADSWITCH: headswitch 672 * @BSD_LABELITEM_TRKSEEK: track-to-track seek 673 * 674 * BSD specific label items. 675 */ 676enum fdisk_labelitem_bsd { 677 /* specific */ 678 BSD_LABELITEM_TYPE = __FDISK_NLABELITEMS, 679 BSD_LABELITEM_DISK, 680 BSD_LABELITEM_PACKNAME, 681 BSD_LABELITEM_FLAGS, 682 BSD_LABELITEM_SECSIZE, 683 BSD_LABELITEM_NTRACKS, 684 BSD_LABELITEM_SECPERCYL, 685 BSD_LABELITEM_CYLINDERS, 686 BSD_LABELITEM_RPM, 687 BSD_LABELITEM_INTERLEAVE, 688 BSD_LABELITEM_TRACKSKEW, 689 BSD_LABELITEM_CYLINDERSKEW, 690 BSD_LABELITEM_HEADSWITCH, 691 BSD_LABELITEM_TRKSEEK 692}; 693 694/* sgi.h */ 695#define SGI_FLAG_BOOT 1 696#define SGI_FLAG_SWAP 2 697extern int fdisk_sgi_set_bootfile(struct fdisk_context *cxt); 698extern int fdisk_sgi_create_info(struct fdisk_context *cxt); 699 700/** 701 * fdisk_labelitem_sgi: 702 * @SGI_LABELITEM_PCYLCOUNT: Physical cylinders 703 * @SGI_LABELITEM_SPARECYL: Extra sects/cyl 704 * @SGI_LABELITEM_ILFACT: nterleave 705 * @SGI_LABELITEM_BOOTFILE: Bootfile 706 * 707 * SGI specific label items. 708 */ 709enum fdisk_labelitem_sgi { 710 SGI_LABELITEM_PCYLCOUNT = __FDISK_NLABELITEMS, 711 SGI_LABELITEM_SPARECYL, 712 SGI_LABELITEM_ILFACT, 713 SGI_LABELITEM_BOOTFILE 714}; 715 716/* gpt */ 717 718/* 719 * GPT partition attributes 720 */ 721 722/** 723 * GPT_FLAG_REQUIRED: 724 * 725 * GPT attribute; marks a partition as system partition (disk 726 * partitioning utilities must preserve the partition as is) 727 */ 728#define GPT_FLAG_REQUIRED 1 729 730/** 731 * GPT_FLAG_NOBLOCK: 732 * 733 * GPT attribute; EFI firmware should ignore the content of the 734 * partition and not try to read from it 735 */ 736#define GPT_FLAG_NOBLOCK 2 737 738/** 739 * GPT_FLAG_LEGACYBOOT: 740 * 741 * GPT attribute; use the partition for legacy boot method 742 */ 743#define GPT_FLAG_LEGACYBOOT 3 744 745/** 746 * GPT_FLAG_GUIDSPECIFIC: 747 * 748 * GPT attribute; for bites 48-63, defined and used by the individual partition 749 * type. 750 * 751 * The flag GPT_FLAG_GUIDSPECIFIC forces libfdisk to ask (by ask API) 752 * for a bit number. If you want to toggle specific bit and avoid any 753 * dialog, then use the bit number (in range 48..63). For example: 754 * 755 * // start dialog to ask for bit number 756 * fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC); 757 * 758 * // toggle bit 60 759 * fdisk_toggle_partition_flag(cxt, n, 60); 760 */ 761#define GPT_FLAG_GUIDSPECIFIC 4 762 763extern int fdisk_gpt_is_hybrid(struct fdisk_context *cxt); 764extern int fdisk_gpt_set_npartitions(struct fdisk_context *cxt, uint32_t nents); 765extern int fdisk_gpt_get_partition_attrs(struct fdisk_context *cxt, size_t partnum, uint64_t *attrs); 766extern int fdisk_gpt_set_partition_attrs(struct fdisk_context *cxt, size_t partnum, uint64_t attrs); 767 768extern void fdisk_gpt_disable_relocation(struct fdisk_label *lb, int disable); 769extern void fdisk_gpt_enable_minimize(struct fdisk_label *lb, int enable); 770 771/** 772 * fdisk_labelitem_gpt: 773 * @GPT_LABELITEM_ID: GPT disklabel UUID (!= partition UUID) 774 * @GPT_LABELITEM_FIRSTLBA: First Usable LBA 775 * @GPT_LABELITEM_LASTLBA: Usable LBA 776 * @GPT_LABELITEM_ALTLBA: Alternative LBA (backup header LBA) 777 * @GPT_LABELITEM_ENTRIESLBA: Partitions entries array LBA 778 * @GPT_LABELITEM_ENTRIESALLOC: Number of allocated entries in entries array 779 * 780 * GPT specific label items. 781 */ 782enum fdisk_labelitem_gpt { 783 /* generic */ 784 GPT_LABELITEM_ID = FDISK_LABELITEM_ID, 785 /* specific */ 786 GPT_LABELITEM_FIRSTLBA = __FDISK_NLABELITEMS, 787 GPT_LABELITEM_LASTLBA, 788 GPT_LABELITEM_ALTLBA, 789 GPT_LABELITEM_ENTRIESLBA, 790 GPT_LABELITEM_ENTRIESALLOC 791}; 792 793/* script.c */ 794struct fdisk_script *fdisk_new_script(struct fdisk_context *cxt); 795struct fdisk_script *fdisk_new_script_from_file(struct fdisk_context *cxt, 796 const char *filename); 797void fdisk_ref_script(struct fdisk_script *dp); 798void fdisk_unref_script(struct fdisk_script *dp); 799 800const char *fdisk_script_get_header(struct fdisk_script *dp, const char *name); 801int fdisk_script_set_header(struct fdisk_script *dp, const char *name, const char *data); 802struct fdisk_table *fdisk_script_get_table(struct fdisk_script *dp); 803int fdisk_script_set_table(struct fdisk_script *dp, struct fdisk_table *tb); 804int fdisk_script_get_nlines(struct fdisk_script *dp); 805int fdisk_script_has_force_label(struct fdisk_script *dp); 806 807int fdisk_script_set_userdata(struct fdisk_script *dp, void *data); 808void *fdisk_script_get_userdata(struct fdisk_script *dp); 809 810int fdisk_script_set_fgets(struct fdisk_script *dp, 811 char *(*fn_fgets)(struct fdisk_script *, char *, size_t, FILE *)); 812int fdisk_script_read_context(struct fdisk_script *dp, struct fdisk_context *cxt); 813int fdisk_script_enable_json(struct fdisk_script *dp, int json); 814int fdisk_script_write_file(struct fdisk_script *dp, FILE *f); 815int fdisk_script_read_file(struct fdisk_script *dp, FILE *f); 816int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t bufsz); 817 818int fdisk_set_script(struct fdisk_context *cxt, struct fdisk_script *dp); 819struct fdisk_script *fdisk_get_script(struct fdisk_context *cxt); 820 821int fdisk_apply_script_headers(struct fdisk_context *cxt, struct fdisk_script *dp); 822int fdisk_apply_script(struct fdisk_context *cxt, struct fdisk_script *dp); 823 824 825/* ask.c */ 826#define fdisk_is_ask(a, x) (fdisk_ask_get_type(a) == FDISK_ASKTYPE_ ## x) 827 828int fdisk_set_ask(struct fdisk_context *cxt, 829 int (*ask_cb)(struct fdisk_context *, struct fdisk_ask *, void *), 830 void *data); 831 832 833void fdisk_ref_ask(struct fdisk_ask *ask); 834void fdisk_unref_ask(struct fdisk_ask *ask); 835const char *fdisk_ask_get_query(struct fdisk_ask *ask); 836int fdisk_ask_get_type(struct fdisk_ask *ask); 837const char *fdisk_ask_number_get_range(struct fdisk_ask *ask); 838uint64_t fdisk_ask_number_get_default(struct fdisk_ask *ask); 839uint64_t fdisk_ask_number_get_low(struct fdisk_ask *ask); 840uint64_t fdisk_ask_number_get_high(struct fdisk_ask *ask); 841uint64_t fdisk_ask_number_get_result(struct fdisk_ask *ask); 842int fdisk_ask_number_set_result(struct fdisk_ask *ask, uint64_t result); 843uint64_t fdisk_ask_number_get_base(struct fdisk_ask *ask); 844uint64_t fdisk_ask_number_get_unit(struct fdisk_ask *ask); 845int fdisk_ask_number_set_relative(struct fdisk_ask *ask, int relative); 846int fdisk_ask_number_is_wrap_negative(struct fdisk_ask *ask); 847int fdisk_ask_number_inchars(struct fdisk_ask *ask); 848int fdisk_ask_partnum(struct fdisk_context *cxt, size_t *partnum, int wantnew); 849 850int fdisk_ask_number(struct fdisk_context *cxt, 851 uintmax_t low, 852 uintmax_t dflt, 853 uintmax_t high, 854 const char *query, 855 uintmax_t *result); 856char *fdisk_ask_string_get_result(struct fdisk_ask *ask); 857int fdisk_ask_string_set_result(struct fdisk_ask *ask, char *result); 858int fdisk_ask_string(struct fdisk_context *cxt, 859 const char *query, 860 char **result); 861int fdisk_ask_yesno(struct fdisk_context *cxt, 862 const char *query, 863 int *result); 864int fdisk_ask_yesno_get_result(struct fdisk_ask *ask); 865int fdisk_ask_yesno_set_result(struct fdisk_ask *ask, int result); 866int fdisk_ask_menu_get_default(struct fdisk_ask *ask); 867int fdisk_ask_menu_set_result(struct fdisk_ask *ask, int key); 868int fdisk_ask_menu_get_result(struct fdisk_ask *ask, int *key); 869int fdisk_ask_menu_get_item(struct fdisk_ask *ask, size_t idx, int *key, 870 const char **name, const char **desc); 871size_t fdisk_ask_menu_get_nitems(struct fdisk_ask *ask); 872int fdisk_ask_print_get_errno(struct fdisk_ask *ask); 873const char *fdisk_ask_print_get_mesg(struct fdisk_ask *ask); 874 875int fdisk_info(struct fdisk_context *cxt, const char *fmt, ...); 876int fdisk_warn(struct fdisk_context *cxt, const char *fmt, ...); 877int fdisk_warnx(struct fdisk_context *cxt, const char *fmt, ...); 878 879/* utils.h */ 880extern char *fdisk_partname(const char *dev, size_t partno); 881 882#ifdef __cplusplus 883} 884#endif 885 886#endif /* _LIBFDISK_H */ 887