1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
#include "fdiskP.h"
/**
* SECTION: label
* @title: Label
* @short_description: disk label (PT) specific data and functions
*
* The fdisk_new_context() initializes all label drivers, and allocate
* per-label specific data struct. This concept can be used to store label specific
* settings to the label driver independently on the currently active label
* driver. Note that label struct cannot be deallocated, so there is no
* reference counting for fdisk_label objects. All is destroyed by
* fdisk_unref_context() only.
*
* Anyway, all label drives share in-memory first sector. The function
* fdisk_create_disklabel() overwrites this in-memory sector. But it's possible that
* label driver also uses another buffers, for example GPT reads more sectors
* from the device.
*
* All label operations are in-memory only, except fdisk_write_disklabel().
*
* All functions that use "struct fdisk_context" rather than "struct
* fdisk_label" use the currently active label driver.
*/
int fdisk_probe_labels(struct fdisk_context *cxt)
{
size_t i;
cxt->label = NULL;
for (i = 0; i < cxt->nlabels; i++) {
struct fdisk_label *lb = cxt->labels[i];
struct fdisk_label *org = fdisk_get_label(cxt, NULL);
int rc;
if (!lb->op->probe)
continue;
if (lb->disabled) {
DBG(CXT, ul_debugobj(cxt, "%s: disabled -- ignore", lb->name));
continue;
}
DBG(CXT, ul_debugobj(cxt, "probing for %s", lb->name));
cxt->label = lb;
rc = lb->op->probe(cxt);
cxt->label = org;
if (rc != 1) {
if (lb->op->deinit)
lb->op->deinit(lb); /* for sure */
continue;
}
__fdisk_switch_label(cxt, lb);
return 0;
}
DBG(CXT, ul_debugobj(cxt, "no label found"));
return 1; /* not found */
}
/**
* fdisk_label_get_name:
* @lb: label
*
* Returns: label name
*/
const char *fdisk_label_get_name(const struct fdisk_label *lb)
{
return lb ? lb->name : NULL;
}
/**
* fdisk_label_is_labeltype:
* @lb: label
*
* Returns: FDISK_DISKLABEL_*.
*/
int fdisk_label_get_type(const struct fdisk_label *lb)
{
return lb->id;
}
/**
* fdisk_label_require_geometry:
* @lb: label
*
* Returns: 1 if label requires CHS geometry
*/
int fdisk_label_require_geometry(const struct fdisk_label *lb)
{
assert(lb);
return lb->flags & FDISK_LABEL_FL_REQUIRE_GEOMETRY ? 1 : 0;
}
/**
* fdisk_label_get_fields_ids
* @lb: label (or NULL for the current label)
* @cxt: context
* @ids: returns allocated array with FDISK_FIELD_* IDs
* @nids: returns number of items in fields
*
* This function returns the default fields for the label.
*
* Note that the set of the default fields depends on fdisk_enable_details()
* function. If the details are enabled then this function usually returns more
* fields.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_label_get_fields_ids(
const struct fdisk_label *lb,
struct fdisk_context *cxt,
int **ids, size_t *nids)
{
size_t i, n;
int *c;
if (!cxt || (!lb && !cxt->label))
return -EINVAL;
lb = cxt->label;
if (!lb->fields || !lb->nfields)
return -ENOSYS;
c = calloc(lb->nfields, sizeof(int));
if (!c)
return -ENOMEM;
for (n = 0, i = 0; i < lb->nfields; i++) {
int id = lb->fields[i].id;
if ((fdisk_is_details(cxt) &&
(lb->fields[i].flags & FDISK_FIELDFL_EYECANDY))
|| (!fdisk_is_details(cxt) &&
(lb->fields[i].flags & FDISK_FIELDFL_DETAIL))
|| (id == FDISK_FIELD_SECTORS &&
fdisk_use_cylinders(cxt))
|| (id == FDISK_FIELD_CYLINDERS &&
!fdisk_use_cylinders(cxt)))
continue;
c[n++] = id;
}
if (ids)
*ids = c;
else
free(c);
if (nids)
*nids = n;
return 0;
}
/**
* fdisk_label_get_fields_ids_all
* @lb: label (or NULL for the current label)
* @cxt: context
* @ids: returns allocated array with FDISK_FIELD_* IDs
* @nids: returns number of items in fields
*
* This function returns all fields for the label.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_label_get_fields_ids_all(
const struct fdisk_label *lb,
struct fdisk_context *cxt,
int **ids, size_t *nids)
{
size_t i, n;
int *c;
if (!cxt || (!lb && !cxt->label))
return -EINVAL;
lb = cxt->label;
if (!lb->fields || !lb->nfields)
return -ENOSYS;
c = calloc(lb->nfields, sizeof(int));
if (!c)
return -ENOMEM;
for (n = 0, i = 0; i < lb->nfields; i++)
c[n++] = lb->fields[i].id;
if (ids)
*ids = c;
else
free(c);
if (nids)
*nids = n;
return 0;
}
/**
* fdisk_label_get_field:
* @lb: label
* @id: FDISK_FIELD_*
*
* The field struct describes data stored in struct fdisk_partition. The info
* about data is usable for example to generate human readable output (e.g.
* fdisk 'p'rint command). See fdisk_partition_to_string() and fdisk code.
*
* Returns: pointer to static instance of the field.
*/
const struct fdisk_field *fdisk_label_get_field(const struct fdisk_label *lb, int id)
{
size_t i;
assert(lb);
assert(id > 0);
for (i = 0; i < lb->nfields; i++) {
if (lb->fields[i].id == id)
return &lb->fields[i];
}
return NULL;
}
/**
* fdisk_label_get_field_by_name
* @lb: label
* @name: field name
*
* Returns: pointer to static instance of the field.
*/
const struct fdisk_field *fdisk_label_get_field_by_name(
const struct fdisk_label *lb,
const char *name)
{
size_t i;
assert(lb);
assert(name);
for (i = 0; i < lb->nfields; i++) {
if (lb->fields[i].name && strcasecmp(lb->fields[i].name, name) == 0)
return &lb->fields[i];
}
return NULL;
}
/**
* fdisk_write_disklabel:
* @cxt: fdisk context
*
* This function wipes the device (if enabled by fdisk_enable_wipe()) and then
* it writes in-memory changes to disk. Be careful!
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_write_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label || cxt->readonly)
return -EINVAL;
if (!cxt->label->op->write)
return -ENOSYS;
fdisk_do_wipe(cxt);
return cxt->label->op->write(cxt);
}
/**
* fdisk_verify_disklabel:
* @cxt: fdisk context
*
* Verifies the partition table.
*
* Returns: 0 on success, <1 runtime or option errors, >0 number of detected issues
*/
int fdisk_verify_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->verify)
return -ENOSYS;
if (fdisk_missing_geometry(cxt))
return -EINVAL;
return cxt->label->op->verify(cxt);
}
/**
* fdisk_list_disklabel:
* @cxt: fdisk context
*
* Lists details about disklabel, but no partitions.
*
* This function is based on fdisk_get_disklabel_item() and prints all label
* specific information by ASK interface (FDISK_ASKTYPE_INFO, aka fdisk_info()).
* The function requires enabled "details" by fdisk_enable_details().
*
* It's recommended to use fdisk_get_disklabel_item() if you need better
* control on output and formatting.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_list_disklabel(struct fdisk_context *cxt)
{
int id = 0, rc = 0;
struct fdisk_labelitem item = { .id = id };
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->display_details)
return 0;
/* List all label items */
do {
/* rc: < 0 error, 0 success, 1 unknown item, 2 out of range */
rc = fdisk_get_disklabel_item(cxt, id++, &item);
if (rc != 0)
continue;
switch (item.type) {
case 'j':
fdisk_info(cxt, "%s: %ju", item.name, item.data.num64);
break;
case 's':
if (item.data.str && item.name)
fdisk_info(cxt, "%s: %s", item.name, item.data.str);
break;
}
fdisk_reset_labelitem(&item);
} while (rc == 0 || rc == 1);
return rc < 0 ? rc : 0;
}
/**
* fdisk_create_disklabel:
* @cxt: fdisk context
* @name: label name
*
* Creates a new disk label of type @name. If @name is NULL, then it will
* create a default system label type, either SUN or DOS. The function
* automatically switches the current label driver to @name. The function
* fdisk_get_label() returns the current label driver.
*
* The function modifies in-memory data only.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
{
int haslabel = 0;
struct fdisk_label *lb;
if (!cxt)
return -EINVAL;
if (!name) { /* use default label creation */
#ifdef __sparc__
name = "sun";
#else
name = "dos";
#endif
}
if (cxt->label) {
fdisk_deinit_label(cxt->label);
haslabel = 1;
}
lb = fdisk_get_label(cxt, name);
if (!lb || lb->disabled)
return -EINVAL;
if (!haslabel || (lb && cxt->label != lb))
fdisk_check_collisions(cxt);
if (!lb->op->create)
return -ENOSYS;
__fdisk_switch_label(cxt, lb);
assert(cxt->label == lb);
if (haslabel && !cxt->parent)
fdisk_reset_device_properties(cxt);
DBG(CXT, ul_debugobj(cxt, "create a new %s label", lb->name));
return lb->op->create(cxt);
}
/**
* fdisk_locate_disklabel:
* @cxt: context
* @n: N item
* @name: return item name
* @offset: return offset where is item
* @size: of the item
*
* Locate disklabel and returns info about @n item of the label.
*
* For example GPT is composed from three items, PMBR and GPT, n=0 return
* offset to PMBR and n=1 return offset to GPT Header and n=2 returns offset to
* GPT array of partitions, n=3 and n=4 returns location of the backup GPT
* label at the end of the disk.
*
* The function returns the current in-memory situation. It's possible that a
* header location is modified by write operation, for example when enabled
* minimization (see fdisk_gpt_enable_minimize()). In this case it's better to
* call this function after fdisk_write_disklabel().
*
* For more details see 'D' expert fdisk command.
*
* Returns: 0 on success, <0 on error, 1 no more items.
*/
int fdisk_locate_disklabel(struct fdisk_context *cxt, int n, const char **name,
uint64_t *offset, size_t *size)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->locate)
return -ENOSYS;
DBG(CXT, ul_debugobj(cxt, "locating %d chunk of %s.", n, cxt->label->name));
return cxt->label->op->locate(cxt, n, name, offset, size);
}
/**
* fdisk_get_disklabel_id:
* @cxt: fdisk context
* @id: returns pointer to allocated string (MBR Id or GPT dirk UUID)
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id)
{
struct fdisk_labelitem item = FDISK_LABELITEM_INIT;
int rc;
if (!cxt || !cxt->label || !id)
return -EINVAL;
DBG(CXT, ul_debugobj(cxt, "asking for disk %s ID", cxt->label->name));
rc = fdisk_get_disklabel_item(cxt, FDISK_LABELITEM_ID, &item);
if (rc == 0) {
*id = item.data.str;
item.data.str = NULL;
}
fdisk_reset_labelitem(&item);
if (rc > 0)
rc = 0;
return rc;
}
/**
* fdisk_get_disklabel_item:
* @cxt: fdisk context
* @id: item ID (FDISK_LABELITEM_* or *_LABELITEM_*)
* @item: specifies and returns the item
*
* Note that @id is always in range 0..N. It's fine to use the function in loop
* until it returns error or 2, the result in @item should be ignored when
* function returns 1. Don't forget to use fdisk_reset_labelitem() or fdisk_unref_labelitem().
*
* Returns: 0 on success, < 0 on error, 1 on unsupported item, 2 id out of range
*/
int fdisk_get_disklabel_item(struct fdisk_context *cxt, int id, struct fdisk_labelitem *item)
{
if (!cxt || !cxt->label || !item)
return -EINVAL;
fdisk_reset_labelitem(item);
item->id = id;
DBG(CXT, ul_debugobj(cxt, "asking for disk %s item %d", cxt->label->name, item->id));
if (!cxt->label->op->get_item)
return -ENOSYS;
return cxt->label->op->get_item(cxt, item);
}
/**
* fdisk_set_disklabel_id:
* @cxt: fdisk context
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_set_disklabel_id(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->set_id)
return -ENOSYS;
DBG(CXT, ul_debugobj(cxt, "setting %s disk ID", cxt->label->name));
return cxt->label->op->set_id(cxt, NULL);
}
/**
* fdisk_set_disklabel_id_from_string
* @cxt: fdisk context
* @str: new Id
*
* Returns: 0 on success, otherwise, a corresponding error.
*
* Since: 2.36
*/
int fdisk_set_disklabel_id_from_string(struct fdisk_context *cxt, const char *str)
{
if (!cxt || !cxt->label || !str)
return -EINVAL;
if (!cxt->label->op->set_id)
return -ENOSYS;
DBG(CXT, ul_debugobj(cxt, "setting %s disk ID from '%s'", cxt->label->name, str));
return cxt->label->op->set_id(cxt, str);
}
/**
* fdisk_set_partition_type:
* @cxt: fdisk context
* @partnum: partition number
* @t: new type
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_set_partition_type(struct fdisk_context *cxt,
size_t partnum,
struct fdisk_parttype *t)
{
if (!cxt || !cxt->label || !t)
return -EINVAL;
if (cxt->label->op->set_part) {
struct fdisk_partition *pa = fdisk_new_partition();
int rc;
if (!pa)
return -ENOMEM;
fdisk_partition_set_type(pa, t);
DBG(CXT, ul_debugobj(cxt, "partition: %zd: set type", partnum));
rc = cxt->label->op->set_part(cxt, partnum, pa);
fdisk_unref_partition(pa);
return rc;
}
return -ENOSYS;
}
/**
* fdisk_toggle_partition_flag:
* @cxt: fdisk context
* @partnum: partition number
* @flag: flag ID
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_toggle_partition_flag(struct fdisk_context *cxt,
size_t partnum,
unsigned long flag)
{
int rc;
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->part_toggle_flag)
return -ENOSYS;
rc = cxt->label->op->part_toggle_flag(cxt, partnum, flag);
DBG(CXT, ul_debugobj(cxt, "partition: %zd: toggle: 0x%04lx [rc=%d]", partnum, flag, rc));
return rc;
}
/**
* fdisk_reorder_partitions
* @cxt: fdisk context
*
* Sort partitions according to the partition start sector.
*
* Returns: 0 on success, 1 reorder unnecessary, otherwise a corresponding error.
*/
int fdisk_reorder_partitions(struct fdisk_context *cxt)
{
int rc;
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->reorder)
return -ENOSYS;
rc = cxt->label->op->reorder(cxt);
switch (rc) {
case 0:
fdisk_info(cxt, _("Partitions order fixed."));
break;
case 1:
fdisk_info(cxt, _("Nothing to do. Ordering is correct already."));
break;
default:
fdisk_warnx(cxt, _("Failed to fix partitions order."));
break;
}
return rc;
}
/*
* Resets the current used label driver to initial state
*/
void fdisk_deinit_label(struct fdisk_label *lb)
{
assert(lb);
/* private label information */
if (lb->op->deinit)
lb->op->deinit(lb);
}
/**
* fdisk_label_set_changed:
* @lb: label
* @changed: 0/1
*
* Marks in-memory data as changed, to force fdisk_write_disklabel() to write
* to device. This should be unnecessary by default, the library keeps track
* about changes.
*/
void fdisk_label_set_changed(struct fdisk_label *lb, int changed)
{
assert(lb);
lb->changed = changed ? 1 : 0;
}
/**
* fdisk_label_is_changed:
* @lb: label
*
* Returns: 1 if in-memory data has been changed.
*/
int fdisk_label_is_changed(const struct fdisk_label *lb)
{
return lb ? lb->changed : 0;
}
/**
* fdisk_label_set_disabled:
* @lb: label
* @disabled: 0 or 1
*
* Mark label as disabled, then libfdisk is going to ignore the label when
* probe device for labels.
*/
void fdisk_label_set_disabled(struct fdisk_label *lb, int disabled)
{
assert(lb);
DBG(LABEL, ul_debug("%s label %s",
lb->name,
disabled ? "DISABLED" : "ENABLED"));
lb->disabled = disabled ? 1 : 0;
}
/**
* fdisk_label_is_disabled:
* @lb: label
*
* Returns: 1 if label driver disabled.
*/
int fdisk_label_is_disabled(const struct fdisk_label *lb)
{
assert(lb);
return lb ? lb->disabled : 0;
}
/**
* fdisk_label_get_geomrange_sectors:
* @lb: label
* @mi: minimal number
* @ma: maximal number
*
* The function provides minimal and maximal geometry supported for the label,
* if no range defined by library then returns -ENOSYS.
*
* Since: 2.32
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_label_get_geomrange_sectors(const struct fdisk_label *lb,
fdisk_sector_t *mi, fdisk_sector_t *ma)
{
if (!lb || lb->geom_min.sectors == 0)
return -ENOSYS;
if (mi)
*mi = lb->geom_min.sectors;
if (ma)
*ma = lb->geom_max.sectors;
return 0;
}
/**
* fdisk_label_get_geomrange_heads:
* @lb: label
* @mi: minimal number
* @ma: maximal number
*
* The function provides minimal and maximal geometry supported for the label,
* if no range defined by library then returns -ENOSYS.
*
* Since: 2.32
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_label_get_geomrange_heads(const struct fdisk_label *lb,
unsigned int *mi, unsigned int *ma)
{
if (!lb || lb->geom_min.heads == 0)
return -ENOSYS;
if (mi)
*mi = lb->geom_min.heads;
if (ma)
*ma = lb->geom_max.heads;
return 0;
}
/**
* fdisk_label_get_geomrange_cylinders:
* @lb: label
* @mi: minimal number
* @ma: maximal number
*
* The function provides minimal and maximal geometry supported for the label,
* if no range defined by library then returns -ENOSYS.
*
* Since: 2.32
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_label_get_geomrange_cylinders(const struct fdisk_label *lb,
fdisk_sector_t *mi, fdisk_sector_t *ma)
{
if (!lb || lb->geom_min.cylinders == 0)
return -ENOSYS;
if (mi)
*mi = lb->geom_min.cylinders;
if (ma)
*ma = lb->geom_max.cylinders;
return 0;
}
|