15 * @short_description: generic label independent partition abstraction
17 * The fdisk_partition provides label independent abstraction. The partitions
18 * are not directly connected with partition table (label) data. Any change to
19 * fdisk_partition does not affects in-memory or on-disk label data.
21 * The fdisk_partition is possible to use as a template for
22 * fdisk_add_partition() or fdisk_set_partition() operations.
25 static void init_partition(struct fdisk_partition
*pa
)
27 FDISK_INIT_UNDEF(pa
->size
);
28 FDISK_INIT_UNDEF(pa
->start
);
29 FDISK_INIT_UNDEF(pa
->partno
);
30 FDISK_INIT_UNDEF(pa
->parent_partno
);
31 FDISK_INIT_UNDEF(pa
->boot
);
33 INIT_LIST_HEAD(&pa
->parts
);
37 * fdisk_new_partition:
39 * Returns: new instance.
41 struct fdisk_partition
*fdisk_new_partition(void)
43 struct fdisk_partition
*pa
= calloc(1, sizeof(*pa
));
47 DBG(PART
, ul_debugobj(pa
, "alloc"));
52 * fdisk_reset_partition:
55 * Resets partition content.
57 void fdisk_reset_partition(struct fdisk_partition
*pa
)
64 DBG(PART
, ul_debugobj(pa
, "reset"));
67 fdisk_unref_parttype(pa
->type
);
77 memset(pa
, 0, sizeof(*pa
));
83 static struct fdisk_partition
*__copy_partition(struct fdisk_partition
*o
)
85 struct fdisk_partition
*n
= fdisk_new_partition();
91 memcpy(n
, o
, sizeof(*n
));
93 /* do not copy reference to lists, etc.*/
95 INIT_LIST_HEAD(&n
->parts
);
98 fdisk_ref_parttype(n
->type
);
100 rc
= strdup_between_structs(n
, o
, name
);
102 rc
= strdup_between_structs(n
, o
, uuid
);
104 rc
= strdup_between_structs(n
, o
, attrs
);
106 rc
= strdup_between_structs(n
, o
, fstype
);
108 rc
= strdup_between_structs(n
, o
, fsuuid
);
110 rc
= strdup_between_structs(n
, o
, fslabel
);
112 rc
= strdup_between_structs(n
, o
, start_chs
);
114 rc
= strdup_between_structs(n
, o
, end_chs
);
117 fdisk_unref_partition(n
);
124 * fdisk_ref_partition:
125 * @pa: partition pointer
127 * Increments reference counter.
129 void fdisk_ref_partition(struct fdisk_partition
*pa
)
136 * fdisk_unref_partition:
137 * @pa: partition pointer
139 * Decrements reference counter, on zero the @pa is automatically
142 void fdisk_unref_partition(struct fdisk_partition
*pa
)
148 if (pa
->refcount
<= 0) {
149 list_del(&pa
->parts
);
150 fdisk_reset_partition(pa
);
151 DBG(PART
, ul_debugobj(pa
, "free"));
157 * fdisk_partition_set_start:
159 * @off: offset in sectors, maximal is UINT64_MAX-1
161 * Note that zero is valid offset too. Use fdisk_partition_unset_start() to
162 * undefine the offset.
164 * Returns: 0 on success, <0 on error.
166 int fdisk_partition_set_start(struct fdisk_partition
*pa
, fdisk_sector_t off
)
170 if (FDISK_IS_UNDEF(off
))
178 * fdisk_partition_unset_start:
181 * Sets the size as undefined. See fdisk_partition_has_start().
183 * Returns: 0 on success, <0 on error.
185 int fdisk_partition_unset_start(struct fdisk_partition
*pa
)
189 FDISK_INIT_UNDEF(pa
->start
);
195 * fdisk_partition_get_start:
198 * The zero is also valid offset. The function may return random undefined
199 * value when start offset is undefined (for example after
200 * fdisk_partition_unset_start()). Always use fdisk_partition_has_start() to be
201 * sure that you work with valid numbers.
203 * Returns: start offset in sectors
205 fdisk_sector_t
fdisk_partition_get_start(struct fdisk_partition
*pa
)
211 * fdisk_partition_has_start:
216 int fdisk_partition_has_start(struct fdisk_partition
*pa
)
218 return pa
&& !FDISK_IS_UNDEF(pa
->start
);
223 * fdisk_partition_cmp_start:
227 * Compares partitions according to start offset, See fdisk_table_sort_partitions().
229 * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
231 int fdisk_partition_cmp_start(struct fdisk_partition
*a
,
232 struct fdisk_partition
*b
)
234 int no_a
= FDISK_IS_UNDEF(a
->start
),
235 no_b
= FDISK_IS_UNDEF(b
->start
);
244 return cmp_numbers(a
->start
, b
->start
);
248 * fdisk_partition_start_follow_default
252 * When @pa used as a template for fdisk_add_partition() when force label driver
253 * to use the first possible space for the new partition.
255 * Returns: 0 on success, <0 on error.
257 int fdisk_partition_start_follow_default(struct fdisk_partition
*pa
, int enable
)
261 pa
->start_follow_default
= enable
? 1 : 0;
266 * fdisk_partition_start_is_default:
269 * See fdisk_partition_start_follow_default().
271 * Returns: 1 if the partition follows default
273 int fdisk_partition_start_is_default(struct fdisk_partition
*pa
)
276 return pa
->start_follow_default
;
280 * fdisk_partition_set_size:
282 * @sz: size in sectors, maximal is UIN64_MAX-1
284 * Note that zero is valid size too. Use fdisk_partition_unset_size() to
287 * Returns: 0 on success, <0 on error.
289 int fdisk_partition_set_size(struct fdisk_partition
*pa
, fdisk_sector_t sz
)
293 if (FDISK_IS_UNDEF(sz
))
301 * fdisk_partition_unset_size:
304 * Sets the size as undefined. See fdisk_partition_has_size().
306 * Returns: 0 on success, <0 on error.
308 int fdisk_partition_unset_size(struct fdisk_partition
*pa
)
312 FDISK_INIT_UNDEF(pa
->size
);
318 * fdisk_partition_get_size:
321 * The zero is also valid size. The function may return random undefined
322 * value when size is undefined (for example after fdisk_partition_unset_size()).
323 * Always use fdisk_partition_has_size() to be sure that you work with valid
326 * Returns: size offset in sectors
328 fdisk_sector_t
fdisk_partition_get_size(struct fdisk_partition
*pa
)
334 * fdisk_partition_has_size:
339 int fdisk_partition_has_size(struct fdisk_partition
*pa
)
341 return pa
&& !FDISK_IS_UNDEF(pa
->size
);
345 * fdisk_partition_size_explicit:
349 * By default libfdisk aligns the size when add the new partition (by
350 * fdisk_add_partition()). If you want to disable this functionality use
353 * Returns: 0 on success, <0 on error.
355 int fdisk_partition_size_explicit(struct fdisk_partition
*pa
, int enable
)
359 pa
->size_explicit
= enable
? 1 : 0;
364 * fdisk_partition_set_partno:
366 * @num: partition number (0 is the first partition, maximal is SIZE_MAX-1)
368 * Note that zero is valid partno too. Use fdisk_partition_unset_partno() to
369 * undefine the partno.
371 * Returns: 0 on success, <0 on error.
373 int fdisk_partition_set_partno(struct fdisk_partition
*pa
, size_t num
)
377 if (FDISK_IS_UNDEF(num
))
384 * fdisk_partition_unset_partno:
387 * Sets the partno as undefined. See fdisk_partition_has_partno().
389 * Returns: 0 on success, <0 on error.
391 int fdisk_partition_unset_partno(struct fdisk_partition
*pa
)
395 FDISK_INIT_UNDEF(pa
->partno
);
400 * fdisk_partition_get_partno:
403 * The zero is also valid partition number. The function may return random
404 * value when partno is undefined (for example after fdisk_partition_unset_partno()).
405 * Always use fdisk_partition_has_partno() to be sure that you work with valid
408 * Returns: partition number (0 is the first partition)
410 size_t fdisk_partition_get_partno(struct fdisk_partition
*pa
)
416 * fdisk_partition_has_partno:
421 int fdisk_partition_has_partno(struct fdisk_partition
*pa
)
423 return pa
&& !FDISK_IS_UNDEF(pa
->partno
);
428 * fdisk_partition_cmp_partno:
432 * Compares partitions according to partition number See fdisk_table_sort_partitions().
434 * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
436 int fdisk_partition_cmp_partno(struct fdisk_partition
*a
,
437 struct fdisk_partition
*b
)
439 return a
->partno
- b
->partno
;
443 * fdisk_partition_partno_follow_default
447 * When @pa used as a template for fdisk_add_partition() when force label driver
448 * to add a new partition to the default (next) position.
450 * Returns: 0 on success, <0 on error.
452 int fdisk_partition_partno_follow_default(struct fdisk_partition
*pa
, int enable
)
456 pa
->partno_follow_default
= enable
? 1 : 0;
461 * fdisk_partition_set_type:
463 * @type: partition type
465 * Sets partition type.
467 * Returns: 0 on success, <0 on error.
469 int fdisk_partition_set_type(struct fdisk_partition
*pa
,
470 struct fdisk_parttype
*type
)
475 fdisk_ref_parttype(type
);
476 fdisk_unref_parttype(pa
->type
);
483 * fdisk_partition_get_type:
486 * Returns: pointer to partition type.
488 struct fdisk_parttype
*fdisk_partition_get_type(struct fdisk_partition
*pa
)
490 return pa
? pa
->type
: NULL
;
493 int fdisk_partition_set_name(struct fdisk_partition
*pa
, const char *name
)
497 return strdup_to_struct_member(pa
, name
, name
);
500 const char *fdisk_partition_get_name(struct fdisk_partition
*pa
)
502 return pa
? pa
->name
: NULL
;
505 int fdisk_partition_set_uuid(struct fdisk_partition
*pa
, const char *uuid
)
509 return strdup_to_struct_member(pa
, uuid
, uuid
);
513 * fdisk_partition_has_end:
516 * Returns: 1 if the partition has defined last sector
518 int fdisk_partition_has_end(struct fdisk_partition
*pa
)
520 return pa
&& !FDISK_IS_UNDEF(pa
->start
) && !FDISK_IS_UNDEF(pa
->size
);
524 * fdisk_partition_get_end:
527 * This function may returns absolute non-sense, always check
528 * fdisk_partition_has_end().
530 * Note that partition end is defined by fdisk_partition_set_start() and
531 * fdisk_partition_set_size().
533 * Returns: last partition sector LBA.
535 fdisk_sector_t
fdisk_partition_get_end(struct fdisk_partition
*pa
)
537 return pa
->start
+ pa
->size
- (pa
->size
== 0 ? 0 : 1);
541 * fdisk_partition_end_follow_default
545 * When @pa used as a template for fdisk_add_partition() when force label
546 * driver to use all the possible space for the new partition.
548 * Returns: 0 on success, <0 on error.
550 int fdisk_partition_end_follow_default(struct fdisk_partition
*pa
, int enable
)
554 pa
->end_follow_default
= enable
? 1 : 0;
559 * fdisk_partition_end_is_default:
562 * Returns: 1 if the partition follows default
564 int fdisk_partition_end_is_default(struct fdisk_partition
*pa
)
567 return pa
->end_follow_default
;
571 * fdisk_partition_get_uuid:
574 * Returns: partition UUID as string
576 const char *fdisk_partition_get_uuid(struct fdisk_partition
*pa
)
578 return pa
? pa
->uuid
: NULL
;
582 * fdisk_partition_get_attrs:
585 * Returns: partition attributes in string format
587 const char *fdisk_partition_get_attrs(struct fdisk_partition
*pa
)
589 return pa
? pa
->attrs
: NULL
;
593 * fdisk_partition_set_attrs:
597 * Sets @attrs to @pa.
599 * Return: 0 on success, <0 on error.
601 int fdisk_partition_set_attrs(struct fdisk_partition
*pa
, const char *attrs
)
605 return strdup_to_struct_member(pa
, attrs
, attrs
);
609 * fdisk_partition_is_nested:
612 * Returns: 1 if the partition is nested (e.g. MBR logical partition)
614 int fdisk_partition_is_nested(struct fdisk_partition
*pa
)
616 return pa
&& !FDISK_IS_UNDEF(pa
->parent_partno
);
620 * fdisk_partition_is_container:
623 * Returns: 1 if the partition is container (e.g. MBR extended partition)
625 int fdisk_partition_is_container(struct fdisk_partition
*pa
)
627 return pa
&& pa
->container
;
631 * fdisk_partition_get_parent:
633 * @parent: parent parno
635 * Returns: returns devno of the parent
637 int fdisk_partition_get_parent(struct fdisk_partition
*pa
, size_t *parent
)
640 *parent
= pa
->parent_partno
;
647 * fdisk_partition_is_used:
650 * Returns: 1 if the partition points to some area
652 int fdisk_partition_is_used(struct fdisk_partition
*pa
)
654 return pa
&& pa
->used
;
658 * fdisk_partition_is_bootable:
661 * Returns: 1 if the partition has enabled boot flag
663 int fdisk_partition_is_bootable(struct fdisk_partition
*pa
)
665 return pa
&& pa
->boot
== 1;
669 * fdisk_partition_is_freespace:
672 * Returns: 1 if @pa points to freespace
674 int fdisk_partition_is_freespace(struct fdisk_partition
*pa
)
676 return pa
&& pa
->freespace
;
680 * fdisk_partition_is_wholedisk:
683 * Returns: 1 if the partition is special whole-disk (e.g. SUN) partition
685 int fdisk_partition_is_wholedisk(struct fdisk_partition
*pa
)
687 return pa
&& pa
->wholedisk
;
691 * fdisk_partition_next_partno:
694 * @n: returns partition number
696 * If @pa specified and partno-follow-default (see fdisk_partition_partno_follow_default())
697 * enabled then returns next expected partno or -ERANGE on error.
699 * If @pa is NULL, or @pa does not specify any semantic for the next partno
700 * then use Ask API to ask user for the next partno. In this case returns 1 if
701 * no free partition available. If fdisk dialogs are disabled then returns -EINVAL.
703 * Returns: 0 on success, <0 on error, or 1 for non-free partno by Ask API.
705 int fdisk_partition_next_partno(
706 struct fdisk_partition
*pa
,
707 struct fdisk_context
*cxt
,
713 if (pa
&& pa
->partno_follow_default
) {
716 DBG(PART
, ul_debugobj(pa
, "next partno (follow default)"));
718 for (i
= 0; i
< cxt
->label
->nparts_max
; i
++) {
719 if (!fdisk_is_partition_used(cxt
, i
)) {
726 } else if (pa
&& fdisk_partition_has_partno(pa
)) {
728 DBG(PART
, ul_debugobj(pa
, "next partno (specified=%zu)", pa
->partno
));
730 if (pa
->partno
>= cxt
->label
->nparts_max
||
731 fdisk_is_partition_used(cxt
, pa
->partno
))
736 } else if (fdisk_has_dialogs(cxt
))
737 return fdisk_ask_partnum(cxt
, n
, 1);
742 static int probe_partition_content(struct fdisk_context
*cxt
, struct fdisk_partition
*pa
)
744 int rc
= 1; /* nothing */
746 DBG(PART
, ul_debugobj(pa
, "start probe #%zu partition [cxt %p] >>>", pa
->partno
, cxt
));
748 /* zeroize the current setting */
749 strdup_to_struct_member(pa
, fstype
, NULL
);
750 strdup_to_struct_member(pa
, fsuuid
, NULL
);
751 strdup_to_struct_member(pa
, fslabel
, NULL
);
753 if (!fdisk_partition_has_start(pa
) ||
754 !fdisk_partition_has_size(pa
))
759 uintmax_t start
, size
;
761 blkid_probe pr
= blkid_new_probe();
765 DBG(PART
, ul_debugobj(pa
, "blkid prober: %p", pr
));
767 start
= fdisk_partition_get_start(pa
) * fdisk_get_sector_size(cxt
);
768 size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cxt
);
770 if (blkid_probe_set_device(pr
, cxt
->dev_fd
, start
, size
) == 0
771 && blkid_do_fullprobe(pr
) == 0) {
776 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
777 rc
= strdup_to_struct_member(pa
, fstype
, data
);
779 if (!rc
&& !blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
780 rc
= strdup_to_struct_member(pa
, fslabel
, data
);
782 if (!rc
&& !blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
783 rc
= strdup_to_struct_member(pa
, fsuuid
, data
);
786 blkid_free_probe(pr
);
789 #endif /* HAVE_LIBBLKID */
792 DBG(PART
, ul_debugobj(pa
, "<<< end probe #%zu partition[cxt %p, rc=%d]", pa
->partno
, cxt
, rc
));
797 * fdisk_partition_to_string:
800 * @id: field (FDISK_FIELD_*)
801 * @data: returns string with allocated data
803 * Returns info about partition converted to printable string.
808 * struct fdisk_partition *pa;
810 * fdisk_get_partition(cxt, 0, &pa);
811 * fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
812 * printf("first partition uuid: %s\n", data);
814 * fdisk_unref_partition(pa);
818 * returns UUID for the first partition.
820 * Returns: 0 on success, otherwise, a corresponding error.
822 int fdisk_partition_to_string(struct fdisk_partition
*pa
,
823 struct fdisk_context
*cxt
,
831 if (!pa
|| !cxt
|| !data
)
835 case FDISK_FIELD_DEVICE
:
837 p
= strdup(_("Free space"));
838 else if (fdisk_partition_has_partno(pa
) && cxt
->dev_path
) {
839 if (cxt
->label
->flags
& FDISK_LABEL_FL_INCHARS_PARTNO
)
840 rc
= asprintf(&p
, "%c", (int) pa
->partno
+ 'a');
842 p
= fdisk_partname(cxt
->dev_path
, pa
->partno
+ 1);
845 case FDISK_FIELD_BOOT
:
846 p
= fdisk_partition_is_bootable(pa
) ? strdup("*") : NULL
;
848 case FDISK_FIELD_START
:
849 if (fdisk_partition_has_start(pa
)) {
850 x
= fdisk_cround(cxt
, pa
->start
);
851 rc
= pa
->start_post
?
852 asprintf(&p
, "%"PRIu64
"%c", x
, pa
->start_post
) :
853 asprintf(&p
, "%"PRIu64
, x
);
856 case FDISK_FIELD_END
:
857 if (fdisk_partition_has_end(pa
)) {
858 x
= fdisk_cround(cxt
, fdisk_partition_get_end(pa
));
860 asprintf(&p
, "%"PRIu64
"%c", x
, pa
->end_post
) :
861 asprintf(&p
, "%"PRIu64
, x
);
864 case FDISK_FIELD_SIZE
:
865 if (fdisk_partition_has_size(pa
)) {
866 uint64_t sz
= pa
->size
* cxt
->sector_size
;
868 switch (cxt
->sizeunit
) {
869 case FDISK_SIZEUNIT_BYTES
:
870 rc
= asprintf(&p
, "%"PRIu64
"", sz
);
872 case FDISK_SIZEUNIT_HUMAN
:
873 if (fdisk_is_details(cxt
))
875 asprintf(&p
, "%"PRIu64
"%c", sz
, pa
->size_post
) :
876 asprintf(&p
, "%"PRIu64
, sz
);
878 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, sz
);
886 case FDISK_FIELD_CYLINDERS
:
888 uintmax_t sz
= fdisk_partition_has_size(pa
) ? pa
->size
: 0;
890 /* Why we need to cast that to uintmax_t? */
891 rc
= asprintf(&p
, "%ju", (uintmax_t)(sz
/ (cxt
->geom
.heads
* cxt
->geom
.sectors
)) + 1);
894 case FDISK_FIELD_SECTORS
:
895 rc
= asprintf(&p
, "%ju",
896 fdisk_partition_has_size(pa
) ? (uintmax_t) pa
->size
: 0);
898 case FDISK_FIELD_BSIZE
:
899 rc
= asprintf(&p
, "%"PRIu64
, pa
->bsize
);
901 case FDISK_FIELD_FSIZE
:
902 rc
= asprintf(&p
, "%"PRIu64
, pa
->fsize
);
904 case FDISK_FIELD_CPG
:
905 rc
= asprintf(&p
, "%"PRIu64
, pa
->cpg
);
907 case FDISK_FIELD_TYPE
:
908 p
= pa
->type
&& pa
->type
->name
? strdup(_(pa
->type
->name
)) : NULL
;
910 case FDISK_FIELD_TYPEID
:
911 if (pa
->type
&& fdisk_parttype_get_string(pa
->type
))
912 rc
= asprintf(&p
, "%s", fdisk_parttype_get_string(pa
->type
));
914 rc
= asprintf(&p
, "%x", fdisk_parttype_get_code(pa
->type
));
916 case FDISK_FIELD_UUID
:
917 p
= pa
->uuid
&& *pa
->uuid
? strdup(pa
->uuid
) : NULL
;
919 case FDISK_FIELD_NAME
:
920 p
= pa
->name
&& *pa
->name
? strdup(pa
->name
) : NULL
;
922 case FDISK_FIELD_ATTR
:
923 p
= pa
->attrs
&& *pa
->attrs
? strdup(pa
->attrs
) : NULL
;
925 case FDISK_FIELD_SADDR
:
926 p
= pa
->start_chs
&& *pa
->start_chs
? strdup(pa
->start_chs
) : NULL
;
928 case FDISK_FIELD_EADDR
:
929 p
= pa
->end_chs
&& *pa
->end_chs
? strdup(pa
->end_chs
) : NULL
;
931 case FDISK_FIELD_FSUUID
:
932 if (pa
->fs_probed
|| probe_partition_content(cxt
, pa
) == 0)
933 p
= pa
->fsuuid
&& *pa
->fsuuid
? strdup(pa
->fsuuid
) : NULL
;
935 case FDISK_FIELD_FSLABEL
:
936 if (pa
->fs_probed
|| probe_partition_content(cxt
, pa
) == 0)
937 p
= pa
->fslabel
&& *pa
->fslabel
? strdup(pa
->fslabel
) : NULL
;
939 case FDISK_FIELD_FSTYPE
:
940 if (pa
->fs_probed
|| probe_partition_content(cxt
, pa
) == 0)
941 p
= pa
->fstype
&& *pa
->fstype
? strdup(pa
->fstype
) : NULL
;
962 * fdisk_get_partition:
964 * @partno: partition number (0 is the first partition)
965 * @pa: returns data about partition
967 * Reads disklabel and fills in @pa with data about partition @n.
969 * Note that partno may address unused partition and then this function does
970 * not fill anything to @pa. See fdisk_is_partition_used(). If @pa points to
971 * NULL then the function allocates a newly allocated fdisk_partition struct,
972 * use fdisk_unref_partition() to deallocate.
974 * Returns: 0 on success, otherwise, a corresponding error.
976 int fdisk_get_partition(struct fdisk_context
*cxt
, size_t partno
,
977 struct fdisk_partition
**pa
)
980 struct fdisk_partition
*np
= NULL
;
982 if (!cxt
|| !cxt
->label
|| !pa
)
984 if (!cxt
->label
->op
->get_part
)
986 if (!fdisk_is_partition_used(cxt
, partno
))
990 np
= *pa
= fdisk_new_partition();
994 fdisk_reset_partition(*pa
);
996 (*pa
)->partno
= partno
;
997 rc
= cxt
->label
->op
->get_part(cxt
, partno
, *pa
);
1001 fdisk_unref_partition(np
);
1004 fdisk_reset_partition(*pa
);
1006 (*pa
)->size_explicit
= 1;
1010 static struct fdisk_partition
*resize_get_by_offset(
1011 struct fdisk_table
*tb
,
1012 struct fdisk_partition
*cur
,
1015 struct fdisk_partition
*pa
= NULL
;
1016 struct fdisk_iter itr
;
1018 fdisk_reset_iter(&itr
, FDISK_ITER_FORWARD
);
1020 while (fdisk_table_next_partition(tb
, &itr
, &pa
) == 0) {
1021 if (!fdisk_partition_has_start(pa
) || !fdisk_partition_has_size(pa
))
1023 if (fdisk_partition_is_nested(cur
) &&
1024 pa
->parent_partno
!= cur
->parent_partno
)
1026 if (off
>= pa
->start
&& off
< pa
->start
+ pa
->size
)
1034 * Verify that area addressed by @start is freespace or the @cur[rent]
1035 * partition and continue to the next table entries until it's freespace, and
1036 * counts size of all this space.
1038 * This is core of the partition start offset move operation. We can move the
1039 * start within the current partition of to the another free space. It's
1040 * forbidden to move start of the partition to another already defined
1043 static int resize_get_last_possible(
1044 struct fdisk_table
*tb
,
1045 struct fdisk_partition
*cur
,
1046 fdisk_sector_t start
,
1047 fdisk_sector_t
*maxsz
)
1049 struct fdisk_partition
*pa
= NULL
, *last
= NULL
;
1050 struct fdisk_iter itr
;
1052 fdisk_reset_iter(&itr
, FDISK_ITER_FORWARD
);
1055 DBG(TAB
, ul_debugobj(tb
, "checking last possible for start=%ju", (uintmax_t) start
));
1058 while (fdisk_table_next_partition(tb
, &itr
, &pa
) == 0) {
1060 DBG(TAB
, ul_debugobj(tb
, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
1062 fdisk_partition_get_partno(pa
),
1063 (uintmax_t) fdisk_partition_get_start(pa
),
1064 (uintmax_t) fdisk_partition_get_end(pa
),
1065 (uintmax_t) fdisk_partition_get_size(pa
),
1066 fdisk_partition_is_freespace(pa
) ? " freespace" : "",
1067 fdisk_partition_is_nested(pa
) ? " nested" : "",
1068 fdisk_partition_is_container(pa
) ? " container" : ""));
1070 if (!fdisk_partition_has_start(pa
) ||
1071 !fdisk_partition_has_size(pa
) ||
1072 (fdisk_partition_is_container(pa
) && pa
!= cur
)) {
1073 DBG(TAB
, ul_debugobj(tb
, " ignored (no start/size or container)"));
1077 if (fdisk_partition_is_nested(pa
)
1078 && fdisk_partition_is_container(cur
)
1079 && pa
->parent_partno
== cur
->partno
) {
1080 DBG(TAB
, ul_debugobj(tb
, " ignore (nested child of the current partition)"));
1084 /* The current is nested, free space has to be nested within the same parent */
1085 if (fdisk_partition_is_nested(cur
)
1086 && pa
->parent_partno
!= cur
->parent_partno
) {
1087 DBG(TAB
, ul_debugobj(tb
, " ignore (nested required)"));
1092 if (start
>= pa
->start
&& start
< pa
->start
+ pa
->size
) {
1093 if (fdisk_partition_is_freespace(pa
) || pa
== cur
) {
1094 DBG(TAB
, ul_debugobj(tb
, " accepted as last"));
1097 DBG(TAB
, ul_debugobj(tb
, " failed to set last"));
1102 *maxsz
= pa
->size
- (start
- pa
->start
);
1103 DBG(TAB
, ul_debugobj(tb
, " new max=%ju", (uintmax_t) *maxsz
));
1105 } else if (!fdisk_partition_is_freespace(pa
) && pa
!= cur
) {
1106 DBG(TAB
, ul_debugobj(tb
, " no free space behind current"));
1110 *maxsz
= pa
->size
- (start
- pa
->start
);
1111 DBG(TAB
, ul_debugobj(tb
, " new max=%ju (last updated)", (uintmax_t) *maxsz
));
1116 DBG(PART
, ul_debugobj(cur
, "resize: max size=%ju", (uintmax_t) *maxsz
));
1118 DBG(PART
, ul_debugobj(cur
, "resize: nothing usable after %ju", (uintmax_t) start
));
1120 return last
? 0 : -1;
1124 * Uses template @tpl to recount start and size change of the partition @res. The
1125 * @tpl->size and @tpl->start are interpreted as relative to the current setting.
1127 static int recount_resize(
1128 struct fdisk_context
*cxt
, size_t partno
,
1129 struct fdisk_partition
*res
, struct fdisk_partition
*tpl
)
1131 fdisk_sector_t start
, size
, xsize
;
1132 struct fdisk_partition
*cur
= NULL
;
1133 struct fdisk_table
*tb
= NULL
;
1136 DBG(PART
, ul_debugobj(tpl
, ">>> resize requested"));
1138 FDISK_INIT_UNDEF(start
);
1139 FDISK_INIT_UNDEF(size
);
1141 rc
= fdisk_get_partitions(cxt
, &tb
);
1143 /* For resize we do not follow grain to detect free-space, but
1144 * we allow to resize with very small granulation. */
1145 unsigned long org
= cxt
->grain
;
1147 cxt
->grain
= cxt
->sector_size
;
1148 rc
= fdisk_get_freespaces(cxt
, &tb
);
1152 fdisk_unref_table(tb
);
1156 fdisk_table_sort_partitions(tb
, fdisk_partition_cmp_start
);
1158 DBG(PART
, ul_debugobj(tpl
, "resize partition partno=%zu in table:", partno
));
1159 ON_DBG(PART
, fdisk_debug_print_table(tb
));
1161 cur
= fdisk_table_get_partition_by_partno(tb
, partno
);
1163 fdisk_unref_table(tb
);
1167 /* 1a) set new start - change relative to the current on-disk setting */
1168 if (tpl
->movestart
&& fdisk_partition_has_start(tpl
)) {
1169 start
= fdisk_partition_get_start(cur
);
1170 if (tpl
->movestart
== FDISK_MOVE_DOWN
) {
1171 if (fdisk_partition_get_start(tpl
) > start
)
1173 start
-= fdisk_partition_get_start(tpl
);
1175 start
+= fdisk_partition_get_start(tpl
);
1177 DBG(PART
, ul_debugobj(tpl
, "resize: moving start %s relative, new start: %ju",
1178 tpl
->movestart
== FDISK_MOVE_DOWN
? "DOWN" : "UP", (uintmax_t)start
));
1180 /* 1b) set new start - absolute number */
1181 } else if (fdisk_partition_has_start(tpl
)) {
1182 start
= fdisk_partition_get_start(tpl
);
1183 DBG(PART
, ul_debugobj(tpl
, "resize: moving start to absolute offset: %ju",
1187 /* 2) verify that start is within the current partition or any freespace area */
1188 if (!FDISK_IS_UNDEF(start
)) {
1189 struct fdisk_partition
*area
= resize_get_by_offset(tb
, cur
, start
);
1192 DBG(PART
, ul_debugobj(tpl
, "resize: start points to the current partition"));
1193 else if (area
&& fdisk_partition_is_freespace(area
))
1194 DBG(PART
, ul_debugobj(tpl
, "resize: start points to freespace"));
1195 else if (!area
&& start
>= cxt
->first_lba
&& start
< cxt
->first_lba
+ (cxt
->grain
/ cxt
->sector_size
))
1196 DBG(PART
, ul_debugobj(tpl
, "resize: start points before first partition"));
1198 DBG(PART
, ul_debugobj(tpl
, "resize: start verification failed"));
1202 /* no change, start points to the current partition */
1203 DBG(PART
, ul_debugobj(tpl
, "resize: start unchanged"));
1204 start
= fdisk_partition_get_start(cur
);
1207 /* 3a) set new size -- reduce */
1208 if (tpl
->resize
== FDISK_RESIZE_REDUCE
&& fdisk_partition_has_size(tpl
)) {
1209 DBG(PART
, ul_debugobj(tpl
, "resize: reduce"));
1210 size
= fdisk_partition_get_size(cur
);
1211 if (fdisk_partition_get_size(tpl
) > size
)
1213 size
-= fdisk_partition_get_size(tpl
);
1215 /* 3b) set new size -- enlarge */
1216 } else if (tpl
->resize
== FDISK_RESIZE_ENLARGE
&& fdisk_partition_has_size(tpl
)) {
1217 DBG(PART
, ul_debugobj(tpl
, "resize: enlarge"));
1218 size
= fdisk_partition_get_size(cur
);
1219 size
+= fdisk_partition_get_size(tpl
);
1221 /* 3c) set new size -- no size specified, enlarge to all freespace */
1222 } else if (tpl
->resize
== FDISK_RESIZE_ENLARGE
) {
1223 DBG(PART
, ul_debugobj(tpl
, "resize: enlarge to all possible"));
1224 if (resize_get_last_possible(tb
, cur
, start
, &size
))
1227 /* 3d) set new size -- absolute number */
1228 } else if (fdisk_partition_has_size(tpl
)) {
1229 DBG(PART
, ul_debugobj(tpl
, "resize: new absolute size"));
1230 size
= fdisk_partition_get_size(tpl
);
1233 /* 4) verify that size is within the current partition or next free space */
1234 xsize
= !FDISK_IS_UNDEF(size
) ? size
: fdisk_partition_get_size(cur
);
1236 if (fdisk_partition_has_size(cur
)) {
1237 fdisk_sector_t maxsz
;
1239 if (resize_get_last_possible(tb
, cur
, start
, &maxsz
))
1241 DBG(PART
, ul_debugobj(tpl
, "resize: size=%ju, max=%ju",
1242 (uintmax_t) xsize
, (uintmax_t) maxsz
));
1247 if (FDISK_IS_UNDEF(size
)) {
1248 DBG(PART
, ul_debugobj(tpl
, "resize: size unchanged (undefined)"));
1252 DBG(PART
, ul_debugobj(tpl
, "<<< resize: SUCCESS: start %ju->%ju; size %ju->%ju",
1253 (uintmax_t) fdisk_partition_get_start(cur
), (uintmax_t) start
,
1254 (uintmax_t) fdisk_partition_get_size(cur
), (uintmax_t) size
));
1257 fdisk_unref_table(tb
);
1260 DBG(PART
, ul_debugobj(tpl
, "<<< resize: FAILED"));
1261 fdisk_warnx(cxt
, _("Failed to resize partition #%zu."), partno
+ 1);
1262 fdisk_unref_table(tb
);
1268 * fdisk_set_partition:
1270 * @partno: partition number (0 is the first partition)
1271 * @pa: new partition setting
1273 * Modifies disklabel according to setting with in @pa.
1275 * Returns: 0 on success, <0 on error.
1277 int fdisk_set_partition(struct fdisk_context
*cxt
, size_t partno
,
1278 struct fdisk_partition
*pa
)
1280 struct fdisk_partition
*xpa
= pa
, *tmp
= NULL
;
1283 if (!cxt
|| !cxt
->label
|| !pa
)
1285 if (!cxt
->label
->op
->set_part
)
1290 if (!fdisk_is_partition_used(cxt
, partno
)) {
1291 pa
->partno
= partno
;
1292 return fdisk_add_partition(cxt
, pa
, NULL
);
1295 if (pa
->resize
|| fdisk_partition_has_start(pa
) || fdisk_partition_has_size(pa
)) {
1296 xpa
= __copy_partition(pa
);
1303 FDISK_INIT_UNDEF(xpa
->size
);
1304 FDISK_INIT_UNDEF(xpa
->start
);
1306 rc
= recount_resize(cxt
, partno
, xpa
, pa
);
1311 DBG(CXT
, ul_debugobj(cxt
, "setting partition %zu %p (start=%ju, end=%ju, size=%ju)",
1313 (uintmax_t) fdisk_partition_get_start(xpa
),
1314 (uintmax_t) fdisk_partition_get_end(xpa
),
1315 (uintmax_t) fdisk_partition_get_size(xpa
)));
1317 /* disable wipe for old offset/size setting */
1318 if (fdisk_get_partition(cxt
, partno
, &tmp
) == 0 && tmp
) {
1319 wipe
= fdisk_set_wipe_area(cxt
, fdisk_partition_get_start(tmp
),
1320 fdisk_partition_get_size(tmp
), FALSE
);
1321 fdisk_unref_partition(tmp
);
1324 /* call label driver */
1325 rc
= cxt
->label
->op
->set_part(cxt
, partno
, xpa
);
1327 /* enable wipe for new offset/size */
1329 fdisk_wipe_partition(cxt
, partno
, TRUE
);
1331 DBG(CXT
, ul_debugobj(cxt
, "set_partition() rc=%d", rc
));
1333 fdisk_unref_partition(xpa
);
1338 * fdisk_wipe_partition:
1339 * @cxt: fdisk context
1340 * @partno: partition number
1343 * Enable/disable filesystems/RAIDs wiping in area defined by partition start and size.
1345 * Returns: <0 in case of error, 0 on success
1348 int fdisk_wipe_partition(struct fdisk_context
*cxt
, size_t partno
, int enable
)
1350 struct fdisk_partition
*pa
= NULL
;
1353 rc
= fdisk_get_partition(cxt
, partno
, &pa
);
1357 rc
= fdisk_set_wipe_area(cxt
, fdisk_partition_get_start(pa
),
1358 fdisk_partition_get_size(pa
), enable
);
1359 fdisk_unref_partition(pa
);
1360 return rc
< 0 ? rc
: 0;
1364 * fdisk_partition_has_wipe:
1365 * @cxt: fdisk context
1370 * Returns: 1 if the area specified by @pa will be wiped by write command, or 0.
1372 int fdisk_partition_has_wipe(struct fdisk_context
*cxt
, struct fdisk_partition
*pa
)
1374 return fdisk_has_wipe_area(cxt
, fdisk_partition_get_start(pa
),
1375 fdisk_partition_get_size(pa
));
1380 * fdisk_add_partition:
1381 * @cxt: fdisk context
1382 * @pa: template for the partition (or NULL)
1383 * @partno: NULL or returns new partition number
1385 * If @pa is not specified or any @pa item is missing the libfdisk will ask by
1388 * The @pa template is is important for non-interactive partitioning,
1389 * especially for MBR where is necessary to differentiate between
1390 * primary/logical; this is done by start offset or/and partno.
1391 * The rules for MBR:
1393 * A) template specifies start within extended partition: add logical
1394 * B) template specifies start out of extended partition: add primary
1395 * C) template specifies start (or default), partno < 4: add primary
1396 * D) template specifies default start, partno >= 4: add logical
1398 * otherwise MBR driver uses Ask-API to get missing information.
1400 * Adds a new partition to disklabel.
1402 * Returns: 0 on success, <0 on error.
1404 int fdisk_add_partition(struct fdisk_context
*cxt
,
1405 struct fdisk_partition
*pa
,
1410 if (!cxt
|| !cxt
->label
)
1412 if (!cxt
->label
->op
->add_part
)
1414 if (fdisk_missing_geometry(cxt
))
1419 DBG(CXT
, ul_debugobj(cxt
, "adding new partition %p", pa
));
1420 if (fdisk_partition_has_start(pa
))
1421 DBG(CXT
, ul_debugobj(cxt
, " start: %ju", (uintmax_t) fdisk_partition_get_start(pa
)));
1422 if (fdisk_partition_has_end(pa
))
1423 DBG(CXT
, ul_debugobj(cxt
, " end: %ju", (uintmax_t) fdisk_partition_get_end(pa
)));
1424 if (fdisk_partition_has_size(pa
))
1425 DBG(CXT
, ul_debugobj(cxt
, " size: %ju", (uintmax_t) fdisk_partition_get_size(pa
)));
1427 DBG(CXT
, ul_debugobj(cxt
, " defaults: start=%s, end=%s, partno=%s",
1428 pa
->start_follow_default
? "yes" : "no",
1429 pa
->end_follow_default
? "yes" : "no",
1430 pa
->partno_follow_default
? "yes" : "no"));
1432 DBG(CXT
, ul_debugobj(cxt
, "adding partition"));
1434 rc
= cxt
->label
->op
->add_part(cxt
, pa
, partno
);
1436 DBG(CXT
, ul_debugobj(cxt
, "add partition done (rc=%d)", rc
));
1441 * fdisk_delete_partition:
1442 * @cxt: fdisk context
1443 * @partno: partition number to delete (0 is the first partition)
1445 * Deletes a @partno partition from disklabel.
1447 * Returns: 0 on success, <0 on error
1449 int fdisk_delete_partition(struct fdisk_context
*cxt
, size_t partno
)
1451 if (!cxt
|| !cxt
->label
)
1453 if (!cxt
->label
->op
->del_part
)
1456 fdisk_wipe_partition(cxt
, partno
, 0);
1458 DBG(CXT
, ul_debugobj(cxt
, "deleting %s partition number %zd",
1459 cxt
->label
->name
, partno
));
1460 return cxt
->label
->op
->del_part(cxt
, partno
);
1464 * fdisk_delete_all_partitions:
1465 * @cxt: fdisk context
1467 * Delete all used partitions from disklabel.
1469 * Returns: 0 on success, otherwise, a corresponding error.
1471 int fdisk_delete_all_partitions(struct fdisk_context
*cxt
)
1476 if (!cxt
|| !cxt
->label
)
1479 for (i
= 0; i
< cxt
->label
->nparts_max
; i
++) {
1481 if (!fdisk_is_partition_used(cxt
, i
))
1483 rc
= fdisk_delete_partition(cxt
, i
);
1492 * fdisk_is_partition_used:
1494 * @n: partition number (0 is the first partition)
1496 * Check if the partition number @n is used by partition table. This function
1497 * does not check if the device is used (e.g. mounted) by system!
1499 * This is faster than fdisk_get_partition() + fdisk_partition_is_used().
1503 int fdisk_is_partition_used(struct fdisk_context
*cxt
, size_t n
)
1505 if (!cxt
|| !cxt
->label
)
1507 if (!cxt
->label
->op
->part_is_used
)
1510 return cxt
->label
->op
->part_is_used(cxt
, n
);