10 * Returns: new instance.
12 struct fdisk_partition
*fdisk_new_partition(void)
14 struct fdisk_partition
*pa
= calloc(1, sizeof(*pa
));
17 INIT_LIST_HEAD(&pa
->parts
);
18 pa
->partno
= FDISK_EMPTY_PARTNO
;
19 pa
->parent_partno
= FDISK_EMPTY_PARTNO
;
20 DBG(PART
, ul_debugobj(pa
, "alloc"));
25 * fdisk_reset_partition:
28 * Resets partition content.
30 void fdisk_reset_partition(struct fdisk_partition
*pa
)
37 DBG(PART
, ul_debugobj(pa
, "reset"));
39 fdisk_free_parttype(pa
->type
);
43 memset(pa
, 0, sizeof(*pa
));
44 pa
->partno
= FDISK_EMPTY_PARTNO
;
45 pa
->parent_partno
= FDISK_EMPTY_PARTNO
;
47 INIT_LIST_HEAD(&pa
->parts
);
51 * fdisk_ref_partition:
52 * @tb: partition pointer
54 * Incremparts reference counter.
56 void fdisk_ref_partition(struct fdisk_partition
*pa
)
63 * fdisk_unref_partition:
64 * @tb: partition pointer
66 * De-incremparts reference counter, on zero the @tb is automatically
69 void fdisk_unref_partition(struct fdisk_partition
*pa
)
75 if (pa
->refcount
<= 0) {
76 fdisk_reset_partition(pa
);
78 DBG(PART
, ul_debugobj(pa
, "free"));
84 * fdisk_partition_set_start:
86 * @off: offset in sectors
88 * Returns: 0 on success, <0 on error.
90 int fdisk_partition_set_start(struct fdisk_partition
*pa
, uint64_t off
)
99 * fdisk_partition_get_start:
102 * Returns: start offset in sectors
104 uint64_t fdisk_partition_get_start(struct fdisk_partition
*pa
)
106 return pa
? pa
->start
: 0;
110 * fdisk_partition_cmp_start:
113 * See fdisk_sort_table().
115 int fdisk_partition_cmp_start(struct fdisk_partition
*a
,
116 struct fdisk_partition
*b
)
118 return a
->start
- b
->start
;
122 * fdisk_partition_set_end:
124 * @off: offset in sectors
126 * Sets end offset, and zeroize size.
128 * The usual way is to address end of the partition by fdisk_partition_set_size().
130 * Returns: 0 on success, <0 on error.
132 int fdisk_partition_set_end(struct fdisk_partition
*pa
, uint64_t off
)
142 * fdisk_partition_get_start:
145 * Returns: start offset in sectors
147 uint64_t fdisk_partition_get_end(struct fdisk_partition
*pa
)
149 return pa
? pa
->end
: 0;
153 * fdisk_partition_set_size
157 * Sets size, zeroize end offset. See also fdisk_partition_set_end().
159 * Returns: 0 on success, <0 on error.
161 int fdisk_partition_set_size(struct fdisk_partition
*pa
, uint64_t size
)
171 * fdisk_partition_get_start:
174 * Returns: size in sectors
176 uint64_t fdisk_partition_get_size(struct fdisk_partition
*pa
)
178 return pa
? pa
->size
: 0;
182 * fdisk_partition_set_partno
184 * @n: partitiion number
186 * When @pa used as a tempalate for fdisk_add_partition() when infor label driver
187 * about wanted partition position.
189 * Returns: 0 on success, <0 on error.
191 int fdisk_partition_set_partno(struct fdisk_partition
*pa
, size_t n
)
199 size_t fdisk_partition_get_partno(struct fdisk_partition
*pa
)
201 return pa
? pa
->partno
: (size_t) -1;
204 int fdisk_partition_cmp_partno(struct fdisk_partition
*a
,
205 struct fdisk_partition
*b
)
207 return a
->partno
- b
->partno
;
210 int fdisk_partition_set_type(struct fdisk_partition
*pa
,
211 const struct fdisk_parttype
*type
)
215 fdisk_free_parttype(pa
->type
);
216 pa
->type
= fdisk_copy_parttype(type
);
220 const struct fdisk_parttype
*fdisk_partition_get_type(struct fdisk_partition
*pa
)
222 return pa
? pa
->type
: NULL
;
225 int fdisk_partition_set_name(struct fdisk_partition
*pa
, const char *name
)
241 const char *fdisk_partition_get_name(struct fdisk_partition
*pa
)
243 return pa
? pa
->name
: NULL
;
246 int fdisk_partition_set_uuid(struct fdisk_partition
*pa
, const char *uuid
)
263 * fdisk_partition_partno_follow_default
267 * When @pa used as a tempalate for fdisk_add_partition() when force label driver
268 * to add a new partition to the default (next) position.
270 * Returns: 0 on success, <0 on error.
272 int fdisk_partition_partno_follow_default(struct fdisk_partition
*pa
, int enable
)
276 pa
->partno_follow_default
= enable
? 1 : 0;
281 * fdisk_partition_start_follow_default
285 * When @pa used as a tempalate for fdisk_add_partition() when force label driver
286 * to use the first possible space for the new partition.
288 * Returns: 0 on success, <0 on error.
290 int fdisk_partition_start_follow_default(struct fdisk_partition
*pa
, int enable
)
294 pa
->start_follow_default
= enable
? 1 : 0;
299 * fdisk_partition_start_is_default:
302 * Returns: 1 if the partition follows default
304 int fdisk_partition_start_is_default(struct fdisk_partition
*pa
)
307 return pa
->start_follow_default
;
311 * fdisk_partition_start_follow_default
315 * When @pa used as a tempalate for fdisk_add_partition() when force label driver
316 * to use all the possible space for the new partition.
318 * Returns: 0 on success, <0 on error.
320 int fdisk_partition_end_follow_default(struct fdisk_partition
*pa
, int enable
)
324 pa
->end_follow_default
= enable
? 1 : 0;
329 * fdisk_partition_end_is_default:
332 * Returns: 1 if the partition follows default
334 int fdisk_partition_end_is_default(struct fdisk_partition
*pa
)
337 return pa
->end_follow_default
;
341 * fdisk_partition_size_explicit:
345 * By default libfdisk aligns the size when add the new partition (by
346 * fdisk_add_partrition()). If you want to disable this functionality use
349 * Returns: 0 on success, <0 on error.
351 int fdisk_partition_size_explicit(struct fdisk_partition
*pa
, int enable
)
355 pa
->size_explicit
= enable
? 1 : 0;
359 const char *fdisk_partition_get_uuid(struct fdisk_partition
*pa
)
361 return pa
? pa
->uuid
: NULL
;
364 const char *fdisk_partition_get_attrs(struct fdisk_partition
*pa
)
366 return pa
? pa
->attrs
: NULL
;
369 int fdisk_partition_set_attrs(struct fdisk_partition
*pa
, const char *attrs
)
385 int fdisk_partition_is_nested(struct fdisk_partition
*pa
)
387 return pa
&& pa
->parent_partno
!= FDISK_EMPTY_PARTNO
;
390 int fdisk_partition_is_container(struct fdisk_partition
*pa
)
392 return pa
&& pa
->container
;
395 int fdisk_partition_get_parent(struct fdisk_partition
*pa
, size_t *parent
)
398 *parent
= pa
->parent_partno
;
404 int fdisk_partition_is_used(struct fdisk_partition
*pa
)
406 return pa
&& pa
->used
;
409 int fdisk_partition_is_bootable(struct fdisk_partition
*pa
)
411 return pa
&& pa
->boot
;
414 int fdisk_partition_is_freespace(struct fdisk_partition
*pa
)
416 return pa
&& pa
->freespace
;
419 int fdisk_partition_next_partno(
420 struct fdisk_partition
*pa
,
421 struct fdisk_context
*cxt
,
427 if (pa
&& pa
->partno_follow_default
) {
430 DBG(PART
, ul_debugobj(pa
, "next partno (follow default)"));
432 for (i
= 0; i
< cxt
->label
->nparts_max
; i
++) {
433 if (!fdisk_is_partition_used(cxt
, i
)) {
440 } else if (pa
&& pa
->partno
!= FDISK_EMPTY_PARTNO
) {
442 DBG(PART
, ul_debugobj(pa
, "next partno (specified=%zu)", pa
->partno
));
444 if (pa
->partno
>= cxt
->label
->nparts_max
)
448 return fdisk_ask_partnum(cxt
, n
, 1);
454 * fdisk_partition_to_string:
456 * @id: field (FDISK_FIELD_*)
457 * @data: returns string with allocated data
459 * Returns info about partition converted to printable string.
463 * struct fdisk_parition *pa;
465 * fdisk_get_partition(cxt, 0, &pa);
466 * fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
467 * printf("first partition uuid: %s\n", data);
469 * fdisk_unref_partition(pa);
471 * returns UUID for the first partition.
473 * Returns 0 on success, otherwise, a corresponding error.
476 int fdisk_partition_to_string(struct fdisk_partition
*pa
,
477 struct fdisk_context
*cxt
,
489 case FDISK_FIELD_DEVICE
:
491 p
= strdup(_("Free space"));
492 else if (cxt
->label
->flags
& FDISK_LABEL_FL_INCHARS_PARTNO
)
493 rc
= asprintf(&p
, "%c", (int) pa
->partno
+ 'a');
495 p
= fdisk_partname(cxt
->dev_path
, pa
->partno
+ 1);
497 case FDISK_FIELD_BOOT
:
498 rc
= asprintf(&p
, "%c", pa
->boot
? '*' : ' ');
500 case FDISK_FIELD_START
:
501 x
= fdisk_cround(cxt
, pa
->start
);
502 rc
= pa
->start_post
?
503 asprintf(&p
, "%ju%c", x
, pa
->start_post
) :
504 asprintf(&p
, "%ju", x
);
506 case FDISK_FIELD_END
:
507 x
= fdisk_cround(cxt
, pa
->end
);
509 asprintf(&p
, "%ju%c", x
, pa
->end_post
) :
510 asprintf(&p
, "%ju", x
);
512 case FDISK_FIELD_SIZE
:
514 uint64_t sz
= pa
->size
* cxt
->sector_size
;
516 if (fdisk_is_details(cxt
)) {
518 asprintf(&p
, "%ju%c", sz
, pa
->size_post
) :
519 asprintf(&p
, "%ju", sz
);
521 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, sz
);
527 case FDISK_FIELD_CYLINDERS
:
528 rc
= asprintf(&p
, "%ju", (uintmax_t)
529 fdisk_cround(cxt
, pa
->size
));
531 case FDISK_FIELD_SECTORS
:
532 rc
= asprintf(&p
, "%ju", pa
->size
);
534 case FDISK_FIELD_BSIZE
:
535 rc
= asprintf(&p
, "%ju", pa
->bsize
);
537 case FDISK_FIELD_FSIZE
:
538 rc
= asprintf(&p
, "%ju", pa
->fsize
);
540 case FDISK_FIELD_CPG
:
541 rc
= asprintf(&p
, "%ju", pa
->cpg
);
543 case FDISK_FIELD_TYPE
:
544 p
= pa
->type
&& pa
->type
->name
? strdup(pa
->type
->name
) : NULL
;
546 case FDISK_FIELD_TYPEID
:
547 if (pa
->type
&& fdisk_parttype_get_string(pa
->type
))
548 rc
= asprintf(&p
, "%s", fdisk_parttype_get_string(pa
->type
));
550 rc
= asprintf(&p
, "%x", fdisk_parttype_get_code(pa
->type
));
552 case FDISK_FIELD_UUID
:
553 p
= pa
->uuid
? strdup(pa
->uuid
) : NULL
;
555 case FDISK_FIELD_NAME
:
556 p
= pa
->name
? strdup(pa
->name
) : NULL
;
558 case FDISK_FIELD_ATTR
:
559 p
= pa
->attrs
? strdup(pa
->attrs
) : NULL
;
561 case FDISK_FIELD_SADDR
:
562 p
= pa
->start_addr
? strdup(pa
->start_addr
) : NULL
;
564 case FDISK_FIELD_EADDR
:
565 p
= pa
->end_addr
? strdup(pa
->end_addr
) : NULL
;
582 * fdisk_get_partition:
584 * @partno: partition nuymber
585 * @pa: returns data about partition
587 * Fills in @pa with data about partition @n. Note that partno may address
588 * unused partition and then this function does not fill anything to @pa.
589 * See fdisk_is_partition_used(). If @pa points to NULL then the function
590 * allocates a newly allocated fdisk_partition struct.
592 * Returns: 0 on success, otherwise, a corresponding error.
594 int fdisk_get_partition(struct fdisk_context
*cxt
, size_t partno
,
595 struct fdisk_partition
**pa
)
598 struct fdisk_partition
*np
= NULL
;
600 if (!cxt
|| !cxt
->label
|| !pa
)
602 if (!cxt
->label
->op
->get_part
)
604 if (!fdisk_is_partition_used(cxt
, partno
))
608 np
= *pa
= fdisk_new_partition();
612 fdisk_reset_partition(*pa
);
614 (*pa
)->partno
= partno
;
615 rc
= cxt
->label
->op
->get_part(cxt
, partno
, *pa
);
619 fdisk_unref_partition(np
);
622 fdisk_reset_partition(*pa
);
624 (*pa
)->size_explicit
= 1;
629 * fdisk_set_partition:
631 * @partno: partition nuymber
632 * @pa: new partition setting
634 * Returns: 0 on success, <0 on error.
636 int fdisk_set_partition(struct fdisk_context
*cxt
, size_t partno
,
637 struct fdisk_partition
*pa
)
639 if (!cxt
|| !cxt
->label
|| !pa
)
641 if (!cxt
->label
->op
->set_part
)
644 DBG(CXT
, ul_debugobj(cxt
, "setting partition %zu %p (start=%ju, end=%ju, size=%ju, "
645 "defaults(start=%s, end=%s, partno=%s)",
650 pa
->start_follow_default
? "yes" : "no",
651 pa
->end_follow_default
? "yes" : "no",
652 pa
->partno_follow_default
? "yes" : "no"));
654 return cxt
->label
->op
->set_part(cxt
, partno
, pa
);
658 * fdisk_add_partition:
659 * @cxt: fdisk context
660 * @pa: template for the partition (or NULL)
661 * @partno: NULL or returns new partition number
663 * If @pa is not specified or any @pa item is missiong the libfdisk will ask by
666 * Creates a new partition.
668 * Returns: 0 on success, <0 on error.
670 int fdisk_add_partition(struct fdisk_context
*cxt
,
671 struct fdisk_partition
*pa
,
679 if (!cxt
|| !cxt
->label
)
681 if (!cxt
->label
->op
->add_part
)
683 if (fdisk_missing_geometry(cxt
))
687 DBG(CXT
, ul_debugobj(cxt
, "adding new partition %p (start=%ju, end=%ju, size=%ju, "
688 "defaults(start=%s, end=%s, partno=%s)",
693 pa
->start_follow_default
? "yes" : "no",
694 pa
->end_follow_default
? "yes" : "no",
695 pa
->partno_follow_default
? "yes" : "no"));
697 DBG(CXT
, ul_debugobj(cxt
, "adding partition"));
699 rc
= cxt
->label
->op
->add_part(cxt
, pa
, partno
);
701 DBG(CXT
, ul_debugobj(cxt
, "add partition done (rc=%d)", rc
));
706 * fdisk_delete_partition:
707 * @cxt: fdisk context
708 * @partno: partition number to delete
710 * Deletes a @partno partition.
712 * Returns: 0 on success, <0 on error
714 int fdisk_delete_partition(struct fdisk_context
*cxt
, size_t partno
)
716 if (!cxt
|| !cxt
->label
)
718 if (!cxt
->label
->op
->del_part
)
721 DBG(CXT
, ul_debugobj(cxt
, "deleting %s partition number %zd",
722 cxt
->label
->name
, partno
));
723 return cxt
->label
->op
->del_part(cxt
, partno
);
727 * fdisk_delete_all_partitions:
728 * @cxt: fdisk context
730 * Delete all used partitions.
732 * Returns: 0 on success, otherwise, a corresponding error.
734 int fdisk_delete_all_partitions(struct fdisk_context
*cxt
)
739 if (!cxt
|| !cxt
->label
)
742 for (i
= 0; i
< cxt
->label
->nparts_max
; i
++) {
744 if (!fdisk_is_partition_used(cxt
, i
))
746 rc
= fdisk_delete_partition(cxt
, i
);
755 * This is faster than fdisk_get_partition() + fdisk_partition_is_used()
757 int fdisk_is_partition_used(struct fdisk_context
*cxt
, size_t n
)
759 if (!cxt
|| !cxt
->label
)
761 if (!cxt
->label
->op
->part_is_used
)
764 return cxt
->label
->op
->part_is_used(cxt
, n
);