2 * fdiskP.h - private library header file
4 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
10 #ifndef _LIBFDISK_PRIVATE_H
11 #define _LIBFDISK_PRIVATE_H
17 #include <sys/types.h>
32 #define LIBFDISK_DEBUG_HELP (1 << 0)
33 #define LIBFDISK_DEBUG_INIT (1 << 1)
34 #define LIBFDISK_DEBUG_CXT (1 << 2)
35 #define LIBFDISK_DEBUG_LABEL (1 << 3)
36 #define LIBFDISK_DEBUG_ASK (1 << 4)
37 #define LIBFDISK_DEBUG_PART (1 << 6)
38 #define LIBFDISK_DEBUG_PARTTYPE (1 << 7)
39 #define LIBFDISK_DEBUG_TAB (1 << 8)
40 #define LIBFDISK_DEBUG_SCRIPT (1 << 9)
41 #define LIBFDISK_DEBUG_WIPE (1 << 10)
42 #define LIBFDISK_DEBUG_ITEM (1 << 11)
43 #define LIBFDISK_DEBUG_ALL 0xFFFF
45 UL_DEBUG_DECLARE_MASK(libfdisk
);
46 #define DBG(m, x) __UL_DBG(libfdisk, LIBFDISK_DEBUG_, m, x)
47 #define ON_DBG(m, x) __UL_DBG_CALL(libfdisk, LIBFDISK_DEBUG_, m, x)
48 #define DBG_FLUSH __UL_DBG_FLUSH(libfdisk, LIBFDISK_DEBUG_)
50 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(libfdisk)
54 * NLS -- the library has to be independent on main program, so define
55 * UL_TEXTDOMAIN_EXPLICIT before you include nls.h.
57 * Now we use util-linux.po (=PACKAGE), rather than maintain the texts
58 * in the separate libfdisk.po file.
60 #define LIBFDISK_TEXTDOMAIN PACKAGE
61 #define UL_TEXTDOMAIN_EXPLICIT LIBFDISK_TEXTDOMAIN
68 int (*body
)(struct fdisk_test
*ts
, int argc
, char *argv
[]);
73 extern int fdisk_run_test(struct fdisk_test
*tests
, int argc
, char *argv
[]);
76 #define FDISK_GPT_NPARTITIONS_DEFAULT 128
82 struct list_head
*p
; /* current position */
83 struct list_head
*head
; /* start position */
84 int direction
; /* FDISK_ITER_{FOR,BACK}WARD */
87 #define IS_ITER_FORWARD(_i) ((_i)->direction == FDISK_ITER_FORWARD)
88 #define IS_ITER_BACKWARD(_i) ((_i)->direction == FDISK_ITER_BACKWARD)
90 #define FDISK_ITER_INIT(itr, list) \
92 (itr)->p = IS_ITER_FORWARD(itr) ? \
93 (list)->next : (list)->prev; \
94 (itr)->head = (list); \
97 #define FDISK_ITER_ITERATE(itr, res, restype, member) \
99 res = list_entry((itr)->p, restype, member); \
100 (itr)->p = IS_ITER_FORWARD(itr) ? \
101 (itr)->p->next : (itr)->p->prev; \
107 struct fdisk_parttype
{
108 unsigned int code
; /* type as number or zero */
109 char *name
; /* description */
110 char *typestr
; /* type as string or NULL */
112 unsigned int flags
; /* FDISK_PARTTYPE_* flags */
113 int refcount
; /* reference counter for allocated types */
117 FDISK_PARTTYPE_UNKNOWN
= (1 << 1),
118 FDISK_PARTTYPE_INVISIBLE
= (1 << 2),
119 FDISK_PARTTYPE_ALLOCATED
= (1 << 3)
122 #define fdisk_parttype_is_invisible(_x) ((_x) && ((_x)->flags & FDISK_PARTTYPE_INVISIBLE))
123 #define fdisk_parttype_is_allocated(_x) ((_x) && ((_x)->flags & FDISK_PARTTYPE_ALLOCATED))
125 struct fdisk_partition
{
126 int refcount
; /* reference counter */
128 size_t partno
; /* partition number */
129 size_t parent_partno
; /* for logical partitions */
131 fdisk_sector_t start
; /* first sectors */
132 fdisk_sector_t size
; /* size in sectors */
134 int movestart
; /* FDISK_MOVE_* (scripts only) */
135 int resize
; /* FDISK_RESIZE_* (scripts only) */
137 char *name
; /* partition name */
138 char *uuid
; /* partition UUID */
139 char *attrs
; /* partition flags/attributes converted to string */
140 struct fdisk_parttype
*type
; /* partition type */
142 char *fstype
; /* filesystem type */
143 char *fsuuid
; /* filesystem uuid */
144 char *fslabel
; /* filesystem label */
146 struct list_head parts
; /* list of partitions */
148 /* extra fields for partition_to_string() */
149 char start_post
; /* start postfix (e.g. '+') */
150 char end_post
; /* end postfix */
151 char size_post
; /* size postfix */
153 uint64_t fsize
; /* bsd junk */
157 char *start_chs
; /* start C/H/S in string */
158 char *end_chs
; /* end C/H/S in string */
160 unsigned int boot
; /* MBR: bootable */
162 unsigned int container
: 1, /* container partition (e.g. extended partition) */
163 end_follow_default
: 1, /* use default end */
164 freespace
: 1, /* this is free space */
165 partno_follow_default
: 1, /* use default partno */
166 size_explicit
: 1, /* don't align the size */
167 start_follow_default
: 1, /* use default start */
168 fs_probed
: 1, /* already probed by blkid */
169 used
: 1, /* partition already used */
170 wholedisk
: 1; /* special system partition */
175 FDISK_MOVE_DOWN
= -1,
180 FDISK_RESIZE_NONE
= 0,
181 FDISK_RESIZE_REDUCE
= -1,
182 FDISK_RESIZE_ENLARGE
= 1
185 #define FDISK_INIT_UNDEF(_x) ((_x) = (__typeof__(_x)) -1)
186 #define FDISK_IS_UNDEF(_x) ((_x) == (__typeof__(_x)) -1)
189 struct list_head parts
; /* partitions */
191 size_t nents
; /* number of partitions */
195 * Legacy CHS based geometry
197 struct fdisk_geometry
{
199 fdisk_sector_t sectors
;
200 fdisk_sector_t cylinders
;
204 * Label specific operations
206 struct fdisk_label_operations
{
207 /* probe disk label */
208 int (*probe
)(struct fdisk_context
*cxt
);
209 /* write in-memory changes to disk */
210 int (*write
)(struct fdisk_context
*cxt
);
211 /* verify the partition table */
212 int (*verify
)(struct fdisk_context
*cxt
);
213 /* create new disk label */
214 int (*create
)(struct fdisk_context
*cxt
);
215 /* returns offset and size of the 'n' part of the PT */
216 int (*locate
)(struct fdisk_context
*cxt
, int n
, const char **name
,
217 uint64_t *offset
, size_t *size
);
218 /* reorder partitions */
219 int (*reorder
)(struct fdisk_context
*cxt
);
220 /* get details from label */
221 int (*get_item
)(struct fdisk_context
*cxt
, struct fdisk_labelitem
*item
);
222 /* set disk label ID */
223 int (*set_id
)(struct fdisk_context
*cxt
);
227 int (*add_part
)(struct fdisk_context
*cxt
, struct fdisk_partition
*pa
,
229 /* delete partition */
230 int (*del_part
)(struct fdisk_context
*cxt
, size_t partnum
);
232 /* fill in partition struct */
233 int (*get_part
)(struct fdisk_context
*cxt
, size_t n
,
234 struct fdisk_partition
*pa
);
235 /* modify partition */
236 int (*set_part
)(struct fdisk_context
*cxt
, size_t n
,
237 struct fdisk_partition
*pa
);
239 /* return state of the partition */
240 int (*part_is_used
)(struct fdisk_context
*cxt
, size_t partnum
);
242 int (*part_toggle_flag
)(struct fdisk_context
*cxt
, size_t i
, unsigned long flag
);
244 /* refresh alignment setting */
245 int (*reset_alignment
)(struct fdisk_context
*cxt
);
247 /* free in-memory label stuff */
248 void (*free
)(struct fdisk_label
*lb
);
250 /* deinit in-memory label stuff */
251 void (*deinit
)(struct fdisk_label
*lb
);
255 * The fields describes how to display libfdisk_partition
258 int id
; /* FDISK_FIELD_* */
259 const char *name
; /* field name */
260 double width
; /* field width (compatible with libsmartcols whint) */
261 int flags
; /* FDISK_FIELDFL_* */
264 /* note that the defaults is to display a column always */
266 FDISK_FIELDFL_DETAIL
= (1 << 1), /* only display if fdisk_is_details() */
267 FDISK_FIELDFL_EYECANDY
= (1 << 2), /* don't display if fdisk_is_details() */
268 FDISK_FIELDFL_NUMBER
= (1 << 3), /* column display numbers */
275 const char *name
; /* label name */
276 enum fdisk_labeltype id
; /* FDISK_DISKLABEL_* */
277 struct fdisk_parttype
*parttypes
; /* supported partitions types */
278 size_t nparttypes
; /* number of items in parttypes[] */
280 size_t nparts_max
; /* maximal number of partitions */
281 size_t nparts_cur
; /* number of currently used partitions */
283 int flags
; /* FDISK_LABEL_FL_* flags */
285 struct fdisk_geometry geom_min
; /* minimal geometry */
286 struct fdisk_geometry geom_max
; /* maximal geometry */
288 unsigned int changed
:1, /* label has been modified */
289 disabled
:1; /* this driver is disabled at all */
291 const struct fdisk_field
*fields
; /* all possible fields */
294 const struct fdisk_label_operations
*op
;
298 /* label driver flags */
300 FDISK_LABEL_FL_REQUIRE_GEOMETRY
= (1 << 2),
301 FDISK_LABEL_FL_INCHARS_PARTNO
= (1 << 3)
304 /* label allocators */
305 extern struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
);
306 extern struct fdisk_label
*fdisk_new_dos_label(struct fdisk_context
*cxt
);
307 extern struct fdisk_label
*fdisk_new_bsd_label(struct fdisk_context
*cxt
);
308 extern struct fdisk_label
*fdisk_new_sgi_label(struct fdisk_context
*cxt
);
309 extern struct fdisk_label
*fdisk_new_sun_label(struct fdisk_context
*cxt
);
312 struct ask_menuitem
{
317 struct ask_menuitem
*next
;
320 /* fdisk dialog -- note that nothing from this stuff will be directly exported,
321 * we will have get/set() function for everything.
324 int type
; /* FDISK_ASKTYPE_* */
330 /* FDISK_ASKTYPE_{NUMBER,OFFSET} */
332 uint64_t hig
; /* high limit */
333 uint64_t low
; /* low limit */
334 uint64_t dfl
; /* default */
336 uint64_t base
; /* for relative results */
337 uint64_t unit
; /* unit for offsets */
338 const char *range
; /* by library generated list */
339 unsigned int relative
:1,
343 /* FDISK_ASKTYPE_{WARN,WARNX,..} */
346 int errnum
; /* errno */
348 /* FDISK_ASKTYPE_YESNO */
350 int result
; /* TRUE or FALSE */
352 /* FDISK_ASKTYPE_STRING */
354 char *result
; /* allocated */
356 /* FDISK_ASKTYPE_MENU */
358 int dfl
; /* default menu item */
360 struct ask_menuitem
*first
;
365 struct fdisk_context
{
366 int dev_fd
; /* device descriptor */
367 char *dev_path
; /* device path */
368 char *dev_model
; /* on linux /sys/block/<name>/device/model or NULL */
369 struct stat dev_st
; /* stat(2) result */
373 unsigned char *firstsector
; /* buffer with master boot record */
374 unsigned long firstsector_bufsz
;
378 unsigned long io_size
; /* I/O size used by fdisk */
379 unsigned long optimal_io_size
; /* optional I/O returned by device */
380 unsigned long min_io_size
; /* minimal I/O size */
381 unsigned long phy_sector_size
; /* physical size */
382 unsigned long sector_size
; /* logical size */
383 unsigned long alignment_offset
;
385 unsigned int readonly
: 1, /* don't write to the device */
386 display_in_cyl_units
: 1, /* for obscure labels */
387 display_details
: 1, /* expert display mode */
388 protect_bootbits
: 1, /* don't zeroize first sector */
389 pt_collision
: 1, /* another PT detected by libblkid */
390 no_disalogs
: 1, /* disable dialog-driven partititoning */
391 dev_model_probed
: 1, /* tried to read from sys */
392 listonly
: 1; /* list partition, nothing else */
394 char *collision
; /* name of already existing FS/PT */
395 struct list_head wipes
; /* list of areas to wipe before write */
397 int sizeunit
; /* SIZE fields, FDISK_SIZEUNIT_* */
400 unsigned long grain
; /* alignment unit */
401 fdisk_sector_t first_lba
; /* recommended begin of the first partition */
402 fdisk_sector_t last_lba
; /* recommended end of last partition */
405 fdisk_sector_t total_sectors
; /* in logical sectors */
406 struct fdisk_geometry geom
;
408 /* user setting to overwrite device default */
409 struct fdisk_geometry user_geom
;
410 unsigned long user_pyh_sector
;
411 unsigned long user_log_sector
;
412 unsigned long user_grain
;
414 struct fdisk_label
*label
; /* current label, pointer to labels[] */
416 size_t nlabels
; /* number of initialized label drivers */
417 struct fdisk_label
*labels
[8]; /* all supported labels,
418 * FIXME: use any enum rather than hardcoded number */
420 int (*ask_cb
)(struct fdisk_context
*, struct fdisk_ask
*, void *); /* fdisk dialogs callback */
421 void *ask_data
; /* ask_cb() data */
423 struct fdisk_context
*parent
; /* for nested PT */
424 struct fdisk_script
*script
; /* what we want to follow */
429 FDISK_DIFF_UNCHANGED
= 0,
435 extern int fdisk_diff_tables(struct fdisk_table
*a
, struct fdisk_table
*b
,
436 struct fdisk_iter
*itr
,
437 struct fdisk_partition
**res
, int *change
);
438 extern void fdisk_debug_print_table(struct fdisk_table
*tb
);
442 extern int __fdisk_switch_label(struct fdisk_context
*cxt
,
443 struct fdisk_label
*lb
);
444 extern int fdisk_missing_geometry(struct fdisk_context
*cxt
);
447 fdisk_sector_t
fdisk_scround(struct fdisk_context
*cxt
, fdisk_sector_t num
);
448 fdisk_sector_t
fdisk_cround(struct fdisk_context
*cxt
, fdisk_sector_t num
);
450 extern int fdisk_discover_geometry(struct fdisk_context
*cxt
);
451 extern int fdisk_discover_topology(struct fdisk_context
*cxt
);
453 extern int fdisk_has_user_device_geometry(struct fdisk_context
*cxt
);
454 extern int fdisk_apply_user_device_properties(struct fdisk_context
*cxt
);
455 extern int fdisk_apply_label_device_properties(struct fdisk_context
*cxt
);
456 extern void fdisk_zeroize_device_properties(struct fdisk_context
*cxt
);
459 extern int fdisk_init_firstsector_buffer(struct fdisk_context
*cxt
,
460 unsigned int protect_off
, unsigned int protect_size
);
461 extern int fdisk_read_firstsector(struct fdisk_context
*cxt
);
464 extern int fdisk_probe_labels(struct fdisk_context
*cxt
);
465 extern void fdisk_deinit_label(struct fdisk_label
*lb
);
467 struct fdisk_labelitem
{
468 int refcount
; /* reference counter */
469 int id
; /* <label>_ITEM_* */
470 char type
; /* s = string, j = uint64 */
471 const char *name
; /* human readable name */
479 /* Use only internally for non-allocated items, never use
480 * refcouting for such items!
482 #define FDISK_LABELITEM_INIT { .type = 0, .refcount = 0 }
485 struct fdisk_ask
*fdisk_new_ask(void);
486 void fdisk_reset_ask(struct fdisk_ask
*ask
);
487 int fdisk_ask_set_query(struct fdisk_ask
*ask
, const char *str
);
488 int fdisk_ask_set_type(struct fdisk_ask
*ask
, int type
);
489 int fdisk_do_ask(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
);
490 int fdisk_ask_number_set_range(struct fdisk_ask
*ask
, const char *range
);
491 int fdisk_ask_number_set_default(struct fdisk_ask
*ask
, uint64_t dflt
);
492 int fdisk_ask_number_set_low(struct fdisk_ask
*ask
, uint64_t low
);
493 int fdisk_ask_number_set_high(struct fdisk_ask
*ask
, uint64_t high
);
494 int fdisk_ask_number_set_base(struct fdisk_ask
*ask
, uint64_t base
);
495 int fdisk_ask_number_set_unit(struct fdisk_ask
*ask
, uint64_t unit
);
496 int fdisk_ask_number_is_relative(struct fdisk_ask
*ask
);
497 int fdisk_ask_number_set_wrap_negative(struct fdisk_ask
*ask
, int wrap_negative
);
498 int fdisk_ask_menu_set_default(struct fdisk_ask
*ask
, int dfl
);
499 int fdisk_ask_menu_add_item(struct fdisk_ask
*ask
, int key
,
500 const char *name
, const char *desc
);
501 int fdisk_ask_print_set_errno(struct fdisk_ask
*ask
, int errnum
);
502 int fdisk_ask_print_set_mesg(struct fdisk_ask
*ask
, const char *mesg
);
503 int fdisk_info_new_partition(
504 struct fdisk_context
*cxt
,
505 int num
, fdisk_sector_t start
, fdisk_sector_t stop
,
506 struct fdisk_parttype
*t
);
509 extern struct dos_partition
*fdisk_dos_get_partition(
510 struct fdisk_context
*cxt
,
514 void fdisk_free_wipe_areas(struct fdisk_context
*cxt
);
515 int fdisk_set_wipe_area(struct fdisk_context
*cxt
, uint64_t start
, uint64_t size
, int enable
);
516 int fdisk_do_wipe(struct fdisk_context
*cxt
);
517 int fdisk_has_wipe_area(struct fdisk_context
*cxt
, uint64_t start
, uint64_t size
);
518 int fdisk_check_collisions(struct fdisk_context
*cxt
);
520 #endif /* _LIBFDISK_PRIVATE_H */