/
parted.c
2699 lines (2281 loc) · 87.1 KB
/
parted.c
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
parted - a frontend to libparted
Copyright (C) 1999-2003, 2005-2014, 2019-2023 Free Software Foundation,
Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdbool.h>
#include <uuid/uuid.h>
#include "argmatch.h"
#include "closeout.h"
#include "configmake.h"
#include "version-etc.h"
#include "command.h"
#include "ui.h"
#include "progname.h"
#include "table.h"
#include "version.h"
#define AUTHORS \
"<http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "parted"
#define N_(String) String
#if ENABLE_NLS
# include <libintl.h>
# include <locale.h>
# define _(String) dgettext (PACKAGE, String)
#else
# define _(String) (String)
#endif /* ENABLE_NLS */
#include <parted/parted.h>
#include <parted/debug.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "c-ctype.h"
#include "c-strcase.h"
#include "xalloc.h"
#include "jsonwrt.h"
#ifdef ENABLE_MTRACE
#include <mcheck.h>
#endif
#include <getopt.h>
/* minimum amount of free space to leave, or maximum amount to gobble up */
#define MIN_FREESPACE (1000 * 2) /* 1000k */
static int MEGABYTE_SECTORS (PedDevice* dev)
{
return PED_MEGABYTE_SIZE / dev->sector_size;
}
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
PRETEND_INPUT_TTY = CHAR_MAX + 1,
};
/* Output modes */
enum
{
HUMAN,
MACHINE,
JSON
};
enum
{
ALIGNMENT_NONE = 2,
ALIGNMENT_CYLINDER,
ALIGNMENT_MINIMAL,
ALIGNMENT_OPTIMAL
};
static char const *const align_args[] =
{
"none",
"cylinder",
"minimal",
"optimal",
NULL
};
static int const align_types[] =
{
ALIGNMENT_NONE,
ALIGNMENT_CYLINDER,
ALIGNMENT_MINIMAL,
ALIGNMENT_OPTIMAL
};
ARGMATCH_VERIFY (align_args, align_types);
typedef struct {
time_t last_update;
time_t predicted_time_left;
} TimerContext;
static struct option const options[] = {
/* name, has-arg, string-return-val, char-return-val */
{"help", 0, NULL, 'h'},
{"list", 0, NULL, 'l'},
{"machine", 0, NULL, 'm'},
{"json", 0, NULL, 'j'},
{"script", 0, NULL, 's'},
{"fix", 0, NULL, 'f'},
{"version", 0, NULL, 'v'},
{"align", required_argument, NULL, 'a'},
{"-pretend-input-tty", 0, NULL, PRETEND_INPUT_TTY},
{NULL, 0, NULL, 0}
};
static const char *const options_help [][2] = {
{"help", N_("displays this help message")},
{"list", N_("lists partition layout on all block devices")},
{"machine", N_("displays machine parseable output")},
{"json", N_("displays JSON output")},
{"script", N_("never prompts for user intervention")},
{"fix", N_("in script mode, fix instead of abort when asked")},
{"version", N_("displays the version")},
{"align=[none|cyl|min|opt]", N_("alignment for new partitions")},
{NULL, NULL}
};
int opt_script_mode = 0;
int opt_fix_mode = 0;
int pretend_input_tty = 0;
int opt_output_mode = HUMAN;
int disk_is_modified = 0;
int is_toggle_mode = 0;
int alignment = ALIGNMENT_OPTIMAL;
static const char* number_msg = N_(
"NUMBER is the partition number used by Linux. On MS-DOS disk labels, the "
"primary partitions number from 1 to 4, logical partitions from 5 onwards.\n");
static const char* label_type_msg_start = N_("LABEL-TYPE is one of: ");
static const char* flag_msg_start = N_("FLAG is one of: ");
static const char* disk_flag_msg_start = N_("FLAG is one of: ");
static const char* unit_msg_start = N_("UNIT is one of: ");
static const char* min_or_opt_msg = N_("desired alignment: minimum or optimal");
static const char* part_type_msg = N_("PART-TYPE is one of: primary, logical, "
"extended\n");
static const char* fs_type_msg_start = N_("FS-TYPE is one of: ");
static const char* start_end_msg = N_("START and END are disk locations, such as "
"4GB or 10%. Negative values count from the end of the disk. "
"For example, -1s specifies exactly the last sector.\n");
static const char* end_msg = N_("END is disk location, such as "
"4GB or 10%. Negative value counts from the end of the disk. "
"For example, -1s specifies exactly the last sector.\n");
static const char* state_msg = N_("STATE is one of: on, off\n");
static const char* device_msg = N_("DEVICE is usually /dev/hda or /dev/sda\n");
static const char* name_msg = N_("NAME is any word you want\n");
static const char* type_msg = N_("TYPE_ID is a value between 0x01 and 0xff, "
"TYPE_UUID is a UUID\n");
static const char* copyright_msg = N_(
"Copyright (C) 1998 - 2006 Free Software Foundation, Inc.\n"
"This program is free software, covered by the GNU General Public License.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n\n");
static char* label_type_msg;
static char* flag_msg;
static char* disk_flag_msg;
static char* unit_msg;
static char* mkpart_fs_type_msg;
static Command* commands [256] = {NULL};
static PedTimer* g_timer;
static TimerContext timer_context;
static struct ul_jsonwrt json;
static int _print_list ();
static void _done (PedDevice* dev, PedDisk *diskp);
static bool partition_align_check (PedDisk const *disk,
PedPartition const *part, enum AlignmentType a_type,
char **align_err);
static void
_timer_handler (PedTimer* timer, void* context)
{
TimerContext* tcontext = (TimerContext*) context;
int draw_this_time;
if (opt_script_mode || !isatty(fileno(stdout)))
return;
if (tcontext->last_update != timer->now && timer->now > timer->start) {
tcontext->predicted_time_left
= timer->predicted_end - timer->now;
tcontext->last_update = timer->now;
draw_this_time = 1;
} else {
draw_this_time = 0;
}
if (draw_this_time) {
wipe_line ();
if (timer->state_name)
printf ("%s... ", timer->state_name);
printf (_("%0.f%%\t(time left %.2d:%.2d)"),
(double) (100.0f * timer->frac),
(int) (tcontext->predicted_time_left / 60),
(int) (tcontext->predicted_time_left % 60));
fflush (stdout);
}
}
static int
_partition_warn_busy (PedPartition* part)
{
char* path;
if (ped_partition_is_busy (part)) {
path = ped_partition_get_path (part);
if (ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_YES_NO,
_("Partition %s is being used. Are you sure you " \
"want to continue?"),
path) != PED_EXCEPTION_YES)
{
free (path);
return 0;
}
free (path);
}
return 1;
}
static int
_disk_warn_busy (PedDisk* disk)
{
if (ped_device_is_busy (disk->dev))
return ped_exception_throw (
(opt_script_mode
? PED_EXCEPTION_ERROR
: PED_EXCEPTION_WARNING),
PED_EXCEPTION_IGNORE_CANCEL,
_("Partition(s) on %s are being used."),
disk->dev->path) == PED_EXCEPTION_IGNORE;
return 1;
}
static int
_disk_warn_loss (PedDisk* disk)
{
return ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_YES_NO,
_("The existing disk label on %s will be destroyed "
"and all data on this disk will be lost. Do you "
"want to continue?"),
disk->dev->path) == PED_EXCEPTION_YES;
}
/* This function changes "sector" to "new_sector" if the new value lies
* within the required range.
*/
static int
snap (PedSector* sector, PedSector new_sector, PedGeometry* range)
{
PED_ASSERT (ped_geometry_test_sector_inside (range, *sector));
if (!ped_geometry_test_sector_inside (range, new_sector))
return 0;
*sector = new_sector;
return 1;
}
typedef enum {
MOVE_NO = 0,
MOVE_STILL = 1,
MOVE_UP = 2,
MOVE_DOWN = 4
} EMoves;
enum { /* Don't change these values */
SECT_START = 0,
SECT_END = -1
};
/* Find the preferred way to adjust the sector s inside range.
* If a move isn't allowed or is out of range it can't be selected.
* what contains SECT_START if the sector to adjust is a start sector
* or SECT_END if it's an end one.
* The preferred move is to the nearest allowed boundary of the part
* partition (if at equal distance: to start if SECT_START or to end
* if SECT_END).
* The distance is returned in dist.
*/
static EMoves
prefer_snap (PedSector s, int what, PedGeometry* range, EMoves* allow,
PedPartition* part, PedSector* dist)
{
PedSector up_dist = -1, down_dist = -1;
PedSector new_sect;
EMoves move;
PED_ASSERT (what == SECT_START || what == SECT_END);
if (!(*allow & (MOVE_UP | MOVE_DOWN))) {
*dist = 0;
return MOVE_STILL;
}
if (*allow & MOVE_UP) {
new_sect = part->geom.end + 1 + what;
if (ped_geometry_test_sector_inside (range, new_sect))
up_dist = new_sect - s;
else
*allow &= ~MOVE_UP;
}
if (*allow & MOVE_DOWN) {
new_sect = part->geom.start + what;
if (ped_geometry_test_sector_inside (range, new_sect))
down_dist = s - new_sect;
else
*allow &= ~MOVE_DOWN;
}
move = MOVE_STILL;
if ((*allow & MOVE_UP) && (*allow & MOVE_DOWN)) {
if (down_dist < up_dist || (down_dist == up_dist
&& what == SECT_START) )
move = MOVE_DOWN;
else if (up_dist < down_dist || (down_dist == up_dist
&& what == SECT_END) )
move = MOVE_UP;
else
PED_ASSERT (0);
} else if (*allow & MOVE_UP)
move = MOVE_UP;
else if (*allow & MOVE_DOWN)
move = MOVE_DOWN;
*dist = ( move == MOVE_DOWN ? down_dist :
( move == MOVE_UP ? up_dist :
0 ) );
return move;
}
/* Snaps a partition to nearby partition boundaries. This is useful for
* gobbling up small amounts of free space, and also for reinterpreting small
* changes to a partition as non-changes (eg: perhaps the user only wanted to
* resize the end of a partition).
* Note that this isn't the end of the story... this function is
* always called before the constraint solver kicks in. So you don't need to
* worry too much about inadvertantly creating overlapping partitions, etc.
*/
static void
snap_to_boundaries (PedGeometry* new_geom, PedGeometry* old_geom,
PedDisk* disk,
PedGeometry* start_range, PedGeometry* end_range)
{
PedPartition* start_part;
PedPartition* end_part;
PedSector start = new_geom->start;
PedSector end = new_geom->end;
PedSector start_dist = -1, end_dist = -1;
EMoves start_allow, end_allow, start_want, end_want;
int adjacent;
start_allow = end_allow = MOVE_STILL | MOVE_UP | MOVE_DOWN;
start_part = ped_disk_get_partition_by_sector (disk, start);
end_part = ped_disk_get_partition_by_sector (disk, end);
adjacent = (start_part->geom.end + 1 == end_part->geom.start);
/* If we can snap to old_geom, then we will... */
/* and this will enforce the snapped positions */
if (old_geom) {
if (snap (&start, old_geom->start, start_range))
start_allow = MOVE_STILL;
if (snap (&end, old_geom->end, end_range))
end_allow = MOVE_STILL;
}
/* If start and end are on the same partition, we */
/* don't allow them to cross. */
if (start_part == end_part) {
start_allow &= ~MOVE_UP;
end_allow &= ~MOVE_DOWN;
}
/* Let's find our way */
start_want = prefer_snap (start, SECT_START, start_range, &start_allow,
start_part, &start_dist );
end_want = prefer_snap (end, SECT_END, end_range, &end_allow,
end_part, &end_dist );
PED_ASSERT (start_dist >= 0 && end_dist >= 0);
/* If start and end are on adjacent partitions, */
/* and if they would prefer crossing, then refrain */
/* the farthest to do so. */
if (adjacent && start_want == MOVE_UP && end_want == MOVE_DOWN) {
if (end_dist < start_dist) {
start_allow &= ~MOVE_UP;
start_want = prefer_snap (start, SECT_START,
start_range, &start_allow,
start_part, &start_dist );
PED_ASSERT (start_dist >= 0);
} else {
end_allow &= ~MOVE_DOWN;
end_want = prefer_snap (end, SECT_END,
end_range, &end_allow,
end_part, &end_dist );
PED_ASSERT (end_dist >= 0);
}
}
/* New positions */
start = ( start_want == MOVE_DOWN ? start_part->geom.start :
( start_want == MOVE_UP ? start_part->geom.end + 1 :
start ) );
end = ( end_want == MOVE_DOWN ? end_part->geom.start - 1 :
( end_want == MOVE_UP ? end_part->geom.end :
end ) );
PED_ASSERT (ped_geometry_test_sector_inside(start_range,start));
PED_ASSERT (ped_geometry_test_sector_inside (end_range, end));
PED_ASSERT (start <= end);
ped_geometry_set (new_geom, start, end - start + 1);
}
static PedConstraint*
constraint_from_start_end (PedDevice* dev, PedGeometry* range_start,
PedGeometry* range_end)
{
return ped_constraint_new (ped_alignment_any, ped_alignment_any,
range_start, range_end, 1, dev->length);
}
static PedConstraint*
constraint_from_start_end_fixed_start (PedDevice* dev, PedSector start_sector,
PedGeometry* range_end)
{
PedGeometry range_start;
range_start.dev = dev;
range_start.start = start_sector;
range_start.end = start_sector;
range_start.length = 1;
return ped_constraint_new (ped_alignment_any, ped_alignment_any,
&range_start, range_end, 1, dev->length);
}
void
help_on (char* topic)
{
Command* cmd;
cmd = command_get (commands, topic);
if (!cmd) return;
command_print_help (cmd);
}
void
print_commands_help ()
{
int i;
for (i=0; commands [i]; i++)
command_print_summary (commands [i]);
}
void
print_options_help ()
{
int i;
for (i=0; options_help [i][0]; i++) {
printf (" -%c, --%-25.25s %s\n",
options_help [i][0][0],
options_help [i][0],
_(options_help [i][1]));
}
}
int
do_help (PedDevice** dev, PedDisk** diskp)
{
if (command_line_get_word_count ()) {
char* word = command_line_pop_word ();
if (word) {
help_on (word);
free (word);
}
} else {
print_commands_help();
}
return 1;
}
static int
do_mklabel (PedDevice** dev, PedDisk** diskp)
{
PedDisk* disk;
const PedDiskType* type = NULL;
if (*diskp)
disk = *diskp;
else {
ped_exception_fetch_all ();
disk = ped_disk_new (*dev);
if (!disk) ped_exception_catch ();
ped_exception_leave_all ();
}
if (!command_line_get_disk_type (_("New disk label type?"), &type))
goto error;
if (disk) {
if (!_disk_warn_busy (disk))
goto error;
if (!opt_script_mode && !_disk_warn_loss (disk))
goto error;
}
disk = ped_disk_new_fresh (*dev, type);
if (!disk)
goto error;
if (!ped_disk_commit (disk))
goto error_destroy_disk;
if ((*dev)->type != PED_DEVICE_FILE)
disk_is_modified = 1;
if (*diskp)
ped_disk_destroy (*diskp);
*diskp = disk;
return 1;
error_destroy_disk:
ped_disk_destroy (disk);
*diskp = 0;
error:
return 0;
}
/* Strip blanks from the end of string STR, in place. */
void _strip_trailing_spaces(char *str)
{
if (!str)
return;
size_t i = strlen(str);
while (i && c_isblank(str[--i]))
str[i]='\0';
}
/* Return true if the unit is one of the supported IEC unit values */
static bool
_is_unit_IEC(const PedUnit unit) {
return (unit == PED_UNIT_KIBIBYTE) || (unit == PED_UNIT_MEBIBYTE) ||
(unit == PED_UNIT_GIBIBYTE) || (unit == PED_UNIT_TEBIBYTE);
}
/* Return true, if str ends with IEC units. */
static bool
_string_ends_with_iec_unit(const char *str)
{
/* 3 characters for the IEC unit and at least 1 digit */
if (!str || strlen(str) < 4)
return false;
char const *p = str + strlen(str) - 3;
PedUnit unit = ped_unit_get_by_name(p);
if (unit == -1) {
return false;
}
return _is_unit_IEC(unit);
}
/* Return true if str ends with explicit unit identifier.
* Do not parse the unit, just check if the unit is specified.
* This function expects trailing spaces to be already stripped.
*/
static bool
_string_has_unit_suffix(const char *str)
{
/* At least 1 digit and 1 character to meet the condition */
if (!str || strlen(str) < 2)
return false;
if (!isdigit(str[strlen(str) - 1]))
return true;
return false;
}
/* If the selected unit is one of KiB, MiB, GiB or TiB and the partition is not
* only 1 sector long, then adjust the end so that it is one sector before the
* given position. Also adjust range_end accordingly. Thus next partition can
* start immediately after this one.
*
* To be called after end sector is read from the user.
*
* https://lists.gnu.org/archive/html/bug-parted/2011-10/msg00009.html
*/
static void
_adjust_end_if_iec (PedSector* start, PedSector* end,
PedGeometry* range_end, char* end_input)
{
PED_ASSERT(start);
PED_ASSERT(end);
PED_ASSERT(range_end);
/* 1s partition - do not move the end */
if (*start == *end)
return;
_strip_trailing_spaces(end_input);
PedUnit unit = ped_unit_get_default();
if (_string_ends_with_iec_unit(end_input) ||
(!_string_has_unit_suffix(end_input) && _is_unit_IEC(unit))) {
*end -= 1;
range_end->start -= 1;
range_end->end -= 1;
}
}
static int
do_mkpart (PedDevice** dev, PedDisk** diskp)
{
PedDisk* disk;
PedPartition* part;
PedPartitionType part_type;
const PedFileSystemType* fs_type = ped_file_system_type_get ("ext2");
PedSector start = 0, end = 0;
PedGeometry *range_start = NULL, *range_end = NULL;
PedConstraint* user_constraint;
PedConstraint* dev_constraint;
PedConstraint* final_constraint;
char* peek_word;
char* part_name = NULL;
char *start_usr = NULL, *end_usr = NULL;
char *start_sol = NULL, *end_sol = NULL;
char *end_input = NULL;
if (*diskp)
disk = *diskp;
else {
disk = ped_disk_new (*dev);
*diskp = disk;
}
if (!disk)
goto error;
if (ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
alignment == ALIGNMENT_CYLINDER))
goto error;
if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) {
part_type = PED_PARTITION_NORMAL;
} else {
if (!command_line_get_part_type (_("Partition type?"),
disk, &part_type))
goto error;
}
/* The undocumented feature that mkpart sometimes takes a
partition name is next to useless, at least with a dvh
partition table, since it makes the "mkpart" command
fail unconditionally for a primary partition. E.g.,
mkpart primary any-name xfs 4096s 5000s
requires the name, yet always fails, saying that only
logical partitions may have names.
If you want a name, use parted's separate "name" command. */
if (ped_disk_type_check_feature (disk->type,
PED_DISK_TYPE_PARTITION_NAME)
&& ! (strcmp (disk->type->name, "dvh") == 0
&& part_type != PED_PARTITION_LOGICAL))
part_name = command_line_get_word (_("Partition name?"),
"", NULL, 1);
peek_word = command_line_peek_word ();
if (part_type == PED_PARTITION_EXTENDED
|| (peek_word && (isdigit (peek_word[0]) || peek_word[0] == '-'))) {
fs_type = NULL;
free (peek_word);
} else {
free (peek_word);
if (!command_line_get_fs_type (_("File system type?"),
&fs_type))
goto error;
}
if (!command_line_get_sector (_("Start?"), *dev, &start, &range_start, NULL))
goto error;
if (!command_line_get_sector (_("End?"), *dev, &end, &range_end, &end_input))
goto error;
_adjust_end_if_iec(&start, &end, range_end, end_input);
/* processing starts here */
part = ped_partition_new (disk, part_type, fs_type, start, end);
if (!part)
goto error;
snap_to_boundaries (&part->geom, NULL, disk, range_start, range_end);
/* create constraints */
user_constraint = constraint_from_start_end (*dev, range_start,
range_end);
PED_ASSERT (user_constraint != NULL);
if (alignment == ALIGNMENT_OPTIMAL)
dev_constraint =
ped_device_get_optimal_aligned_constraint(*dev);
else if (alignment == ALIGNMENT_MINIMAL)
dev_constraint =
ped_device_get_minimal_aligned_constraint(*dev);
else
dev_constraint = ped_device_get_constraint(*dev);
PED_ASSERT (dev_constraint != NULL);
final_constraint = ped_constraint_intersect (user_constraint,
dev_constraint);
ped_constraint_destroy (user_constraint);
ped_constraint_destroy (dev_constraint);
if (!final_constraint)
goto error_destroy_simple_constraints;
/* subject to partition constraint */
ped_exception_fetch_all();
bool added_ok = ped_disk_add_partition (disk, part, final_constraint);
ped_constraint_destroy (final_constraint);
if (!added_ok) {
ped_exception_leave_all();
PedConstraint *constraint_any = ped_constraint_any (*dev);
added_ok = ped_disk_add_partition (disk, part,
constraint_any);
ped_constraint_destroy (constraint_any);
if (!added_ok)
goto error_destroy_simple_constraints;
if (!ped_geometry_test_sector_inside(range_start, part->geom.start) ||
!ped_geometry_test_sector_inside(range_end, part->geom.end)) {
start_usr = ped_unit_format (*dev, start);
end_usr = ped_unit_format (*dev, end);
start_sol = ped_unit_format (*dev, part->geom.start);
end_sol = ped_unit_format (*dev, part->geom.end);
/* In script mode failure to use specified values is fatal.
* However, in interactive mode, it merely elicits a warning
* and a prompt for whether to proceed.
*/
switch (ped_exception_throw (
(opt_script_mode
? PED_EXCEPTION_ERROR
: PED_EXCEPTION_WARNING),
(opt_script_mode
? PED_EXCEPTION_CANCEL
: PED_EXCEPTION_YES_NO),
_("You requested a partition from %s to %s "
"(sectors %llu..%llu).\n"
"The closest location we can manage is "
"%s to %s (sectors %llu..%llu).%s"),
start_usr, end_usr,
start, end,
start_sol, end_sol,
part->geom.start, part->geom.end,
(opt_script_mode ? ""
: _("\nIs this still acceptable to you?"))))
{
case PED_EXCEPTION_YES:
/* all is well in this state */
break;
case PED_EXCEPTION_NO:
case PED_EXCEPTION_UNHANDLED:
default:
/* undo partition addition */
goto error_remove_part;
}
}
char *align_err = NULL;
if ((alignment == ALIGNMENT_OPTIMAL &&
!partition_align_check(disk, part, PA_OPTIMUM, &align_err)) ||
(alignment == ALIGNMENT_MINIMAL &&
!partition_align_check(disk, part, PA_MINIMUM, &align_err))) {
if (ped_exception_throw(
PED_EXCEPTION_WARNING,
(opt_script_mode
? PED_EXCEPTION_OK
: PED_EXCEPTION_IGNORE_CANCEL),
_("The resulting partition is not properly "
"aligned for best performance: %s"),
align_err ? align_err : _("unknown (malloc failed)")) ==
PED_EXCEPTION_CANCEL) {
free(align_err);
/* undo partition addition */
goto error_remove_part;
}
free(align_err);
}
} else {
ped_exception_leave_all();
}
ped_exception_catch();
/* set minor attributes */
if (part_name)
if (!ped_partition_set_name (part, part_name))
goto error_remove_part;
free (part_name); /* avoid double-free upon failure */
part_name = NULL;
if (!ped_partition_set_system (part, fs_type))
goto error_remove_part;
if (ped_partition_is_flag_available (part, PED_PARTITION_LBA))
ped_partition_set_flag (part, PED_PARTITION_LBA, 1);
if (!ped_disk_commit (disk))
goto error_remove_part;
/* clean up */
if (range_start != NULL)
ped_geometry_destroy (range_start);
if (range_end != NULL)
ped_geometry_destroy (range_end);
free (start_usr);
free (end_usr);
free (start_sol);
free (end_sol);
free(end_input);
if ((*dev)->type != PED_DEVICE_FILE)
disk_is_modified = 1;
return 1;
error_remove_part:
ped_disk_remove_partition (disk, part);
error_destroy_simple_constraints:
ped_partition_destroy (part);
error:
free (part_name);
if (range_start != NULL)
ped_geometry_destroy (range_start);
if (range_end != NULL)
ped_geometry_destroy (range_end);
free (start_usr);
free (end_usr);
free (start_sol);
free (end_sol);
free(end_input);
return 0;
}
static int
do_name (PedDevice** dev, PedDisk** diskp)
{
PedPartition* part = NULL;
char* name;
if (!*diskp)
*diskp = ped_disk_new (*dev);
if (!*diskp)
goto error;
if (!ped_disk_type_check_feature((*diskp)->type, PED_DISK_TYPE_PARTITION_NAME)) {
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
_("%s disk labels do not support partition name."),
(*diskp)->type->name);
goto error;
}
if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
goto error;
name = command_line_get_word (_("Partition name?"),
ped_partition_get_name (part), NULL, 0);
if (!name)
goto error;
if (!ped_partition_set_name (part, name))
goto error_free_name;
free (name);
if (!ped_disk_commit (*diskp))
goto error;
return 1;
error_free_name:
free (name);
error:
return 0;
}
static int
do_type (PedDevice** dev, PedDisk** diskp)
{
if (!*diskp)
*diskp = ped_disk_new (*dev);
if (!*diskp)
goto error;
bool has_type_id = ped_disk_type_check_feature ((*diskp)->type,
PED_DISK_TYPE_PARTITION_TYPE_ID);
bool has_type_uuid = ped_disk_type_check_feature ((*diskp)->type,
PED_DISK_TYPE_PARTITION_TYPE_UUID);
PED_ASSERT (!(has_type_id && has_type_uuid));
if (!has_type_id && !has_type_uuid) {
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
_("%s disk labels do not support partition type."),
(*diskp)->type->name);
goto error;
}
PedPartition* part = NULL;
if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
goto error;
char* input = NULL;
if (has_type_id) {
uint8_t type_id = ped_partition_get_type_id (part);
static char buf[8];
snprintf(buf, 8, "0x%02x", type_id);
input = command_line_get_word (_("Partition type-id?"), buf, NULL, 0);
if (!input)
goto error;
unsigned int tmp = strtol (input, (char**) NULL, 16);
if (tmp < 0x01 || tmp > 0xff) {
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
_("Invalid type-id."));
goto error_free_input;
}
if (!ped_partition_set_type_id (part, tmp))
goto error_free_input;
}
if (has_type_uuid) {
uint8_t* type_uuid = ped_partition_get_type_uuid (part);
static char buf[UUID_STR_LEN];
uuid_unparse_lower (type_uuid, buf);
free (type_uuid);
input = command_line_get_word (_("Partition type-uuid?"), buf, NULL, 0);
if (!input)
goto error;
uuid_t tmp;
if (uuid_parse (input, tmp) != 0 || uuid_is_null (tmp)) {
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
_("Invalid type-uuid."));
goto error_free_input;
}
if (!ped_partition_set_type_uuid (part, tmp))
goto error_free_input;
}