]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/sfdisk.c
sfdisk: create empty label on 'write' command
[thirdparty/util-linux.git] / disk-utils / sfdisk.c
1 /*
2 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
3 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
4 *
5 * This program is free software. You can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation: either Version 1
8 * or (at your option) any later version.
9 *
10 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
11 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
12 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
13 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
14 * This program had (head,sector,cylinder) as basic unit, and was
15 * (therefore) broken in several ways for the use on larger disks -
16 * for example, my last patch (from 2.0d to 2.0e) was required
17 * to allow a partition to cross cylinder 8064, and to write an
18 * extended partition past the 4GB mark.
19 *
20 * Karel Zak wrote new sfdisk based on libfdisk from util-linux
21 * in 2014.
22 */
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <getopt.h>
31 #include <sys/stat.h>
32 #include <assert.h>
33 #include <fcntl.h>
34 #include <libsmartcols.h>
35 #ifdef HAVE_LIBREADLINE
36 # define _FUNCTION_DEF
37 # include <readline/readline.h>
38 #endif
39 #include <libgen.h>
40
41 #include "c.h"
42 #include "xalloc.h"
43 #include "nls.h"
44 #include "debug.h"
45 #include "strutils.h"
46 #include "closestream.h"
47 #include "colors.h"
48 #include "blkdev.h"
49 #include "all-io.h"
50 #include "rpmatch.h"
51 #include "optutils.h"
52
53 #include "libfdisk.h"
54 #include "fdisk-list.h"
55
56 /*
57 * sfdisk debug stuff (see fdisk.h and include/debug.h)
58 */
59 static UL_DEBUG_DEFINE_MASK(sfdisk);
60 UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
61
62 #define SFDISKPROG_DEBUG_INIT (1 << 1)
63 #define SFDISKPROG_DEBUG_PARSE (1 << 2)
64 #define SFDISKPROG_DEBUG_MISC (1 << 3)
65 #define SFDISKPROG_DEBUG_ASK (1 << 4)
66 #define SFDISKPROG_DEBUG_ALL 0xFFFF
67
68 #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
69 #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
70
71 enum {
72 ACT_FDISK = 1,
73 ACT_ACTIVATE,
74 ACT_CHANGE_ID,
75 ACT_DUMP,
76 ACT_LIST,
77 ACT_LIST_FREE,
78 ACT_LIST_TYPES,
79 ACT_REORDER,
80 ACT_SHOW_SIZE,
81 ACT_SHOW_GEOM,
82 ACT_VERIFY,
83 ACT_PARTTYPE,
84 ACT_PARTUUID,
85 ACT_PARTLABEL,
86 ACT_PARTATTRS,
87 ACT_DELETE
88 };
89
90 struct sfdisk {
91 int act; /* ACT_* */
92 int partno; /* -N <partno>, default -1 */
93 int wipemode; /* remove foreign signatures from disk */
94 int pwipemode; /* remove foreign signatures from partitions */
95 const char *label; /* --label <label> */
96 const char *label_nested; /* --label-nested <label> */
97 const char *backup_file; /* -O <path> */
98 const char *move_typescript; /* --movedata <typescript> */
99 char *prompt;
100
101 struct fdisk_context *cxt; /* libfdisk context */
102 struct fdisk_partition *orig_pa; /* -N <partno> before the change */
103
104 unsigned int verify : 1, /* call fdisk_verify_disklabel() */
105 quiet : 1, /* suppress extra messages */
106 interactive : 1, /* running on tty */
107 noreread : 1, /* don't check device is in use */
108 force : 1, /* do also stupid things */
109 backup : 1, /* backup sectors before write PT */
110 container : 1, /* PT contains container (MBR extended) partitions */
111 append : 1, /* don't create new PT, append partitions only */
112 json : 1, /* JSON dump */
113 movedata: 1, /* move data after resize */
114 notell : 1, /* don't tell kernel aout new PT */
115 noact : 1; /* do not write to device */
116 };
117
118 #define SFDISK_PROMPT ">>> "
119
120 static void sfdiskprog_init_debug(void)
121 {
122 __UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
123 }
124
125
126 static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
127 {
128 char *p;
129 size_t sz;
130
131 #ifdef HAVE_LIBREADLINE
132 if (isatty(STDIN_FILENO)) {
133 p = readline(prompt);
134 if (!p)
135 return 1;
136 memcpy(buf, p, bufsz);
137 free(p);
138 } else
139 #endif
140 {
141 fputs(prompt, stdout);
142 fflush(stdout);
143
144 if (!fgets(buf, bufsz, stdin))
145 return 1;
146 }
147
148 for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
149
150 if (p > buf)
151 memmove(buf, p, p - buf); /* remove blank space */
152 sz = strlen(buf);
153 if (sz && *(buf + sz - 1) == '\n')
154 *(buf + sz - 1) = '\0';
155
156 DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
157 return 0;
158 }
159
160 static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
161 struct fdisk_ask *ask,
162 void *data)
163 {
164 struct sfdisk *sf = (struct sfdisk *) data;
165 int rc = 0;
166
167 assert(ask);
168
169 switch(fdisk_ask_get_type(ask)) {
170 case FDISK_ASKTYPE_INFO:
171 if (sf->quiet)
172 break;
173 fputs(fdisk_ask_print_get_mesg(ask), stdout);
174 fputc('\n', stdout);
175 break;
176 case FDISK_ASKTYPE_WARNX:
177 fflush(stdout);
178 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
179 fputs(fdisk_ask_print_get_mesg(ask), stderr);
180 color_fdisable(stderr);
181 fputc('\n', stderr);
182 break;
183 case FDISK_ASKTYPE_WARN:
184 fflush(stdout);
185 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
186 fputs(fdisk_ask_print_get_mesg(ask), stderr);
187 errno = fdisk_ask_print_get_errno(ask);
188 fprintf(stderr, ": %m\n");
189 color_fdisable(stderr);
190 break;
191 case FDISK_ASKTYPE_YESNO:
192 {
193 char buf[BUFSIZ];
194 fputc('\n', stdout);
195 do {
196 int x;
197 fputs(fdisk_ask_get_query(ask), stdout);
198 rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf));
199 if (rc)
200 break;
201 x = rpmatch(buf);
202 if (x == RPMATCH_YES || x == RPMATCH_NO) {
203 fdisk_ask_yesno_set_result(ask, x);
204 break;
205 }
206 } while(1);
207 DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
208 break;
209 }
210 default:
211 break;
212 }
213 return rc;
214 }
215
216 static void sfdisk_init(struct sfdisk *sf)
217 {
218 fdisk_init_debug(0);
219 scols_init_debug(0);
220 sfdiskprog_init_debug();
221
222 sf->cxt = fdisk_new_context();
223 if (!sf->cxt)
224 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
225 fdisk_set_ask(sf->cxt, ask_callback, (void *) sf);
226 fdisk_enable_bootbits_protection(sf->cxt, 1);
227
228 if (sf->label_nested) {
229 struct fdisk_context *x = fdisk_new_nested_context(sf->cxt,
230 sf->label_nested);
231 if (!x)
232 err(EXIT_FAILURE, _("failed to allocate nested libfdisk context"));
233 /* the original context is available by fdisk_get_parent() */
234 sf->cxt = x;
235 }
236 }
237
238 static int sfdisk_deinit(struct sfdisk *sf)
239 {
240 struct fdisk_context *parent;
241
242 assert(sf);
243 assert(sf->cxt);
244
245 parent = fdisk_get_parent(sf->cxt);
246 if (parent) {
247 fdisk_unref_context(sf->cxt);
248 sf->cxt = parent;
249 }
250
251 fdisk_unref_context(sf->cxt);
252 free(sf->prompt);
253
254 memset(sf, 0, sizeof(*sf));
255 return 0;
256 }
257
258 static struct fdisk_partition *get_partition(struct fdisk_context *cxt, size_t partno)
259 {
260 struct fdisk_table *tb = NULL;
261 struct fdisk_partition *pa;
262
263 if (fdisk_get_partitions(cxt, &tb) != 0)
264 return NULL;
265
266 pa = fdisk_table_get_partition_by_partno(tb, partno);
267 if (pa)
268 fdisk_ref_partition(pa);
269 fdisk_unref_table(tb);
270 return pa;
271 }
272
273 static void backup_sectors(struct sfdisk *sf,
274 const char *tpl,
275 const char *name,
276 const char *devname,
277 uint64_t offset, size_t size)
278 {
279 char *fname;
280 int fd, devfd;
281
282 devfd = fdisk_get_devfd(sf->cxt);
283 assert(devfd >= 0);
284
285 xasprintf(&fname, "%s0x%08"PRIx64".bak", tpl, offset);
286
287 fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
288 if (fd < 0)
289 goto fail;
290
291 if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) {
292 fdisk_warn(sf->cxt, _("cannot seek %s"), devname);
293 goto fail;
294 } else {
295 unsigned char *buf = xmalloc(size);
296
297 if (read_all(devfd, (char *) buf, size) != (ssize_t) size) {
298 fdisk_warn(sf->cxt, _("cannot read %s"), devname);
299 free(buf);
300 goto fail;
301 }
302 if (write_all(fd, buf, size) != 0) {
303 fdisk_warn(sf->cxt, _("cannot write %s"), fname);
304 free(buf);
305 goto fail;
306 }
307 free(buf);
308 }
309
310 fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"),
311 name, (uintmax_t) offset, (uintmax_t) size, fname);
312 close(fd);
313 free(fname);
314 return;
315 fail:
316 errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname);
317 }
318
319 static char *mk_backup_filename_tpl(const char *filename, const char *devname, const char *suffix)
320 {
321 char *tpl = NULL;
322 char *name, *buf = xstrdup(devname);
323
324 name = basename(buf);
325
326 if (!filename) {
327 const char *home = getenv ("HOME");
328 if (!home)
329 errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined"));
330 xasprintf(&tpl, "%s/sfdisk-%s%s", home, name, suffix);
331 } else
332 xasprintf(&tpl, "%s-%s%s", filename, name, suffix);
333
334 free(buf);
335 return tpl;
336 }
337
338
339 static void backup_partition_table(struct sfdisk *sf, const char *devname)
340 {
341 const char *name;
342 char *tpl;
343 uint64_t offset = 0;
344 size_t size = 0;
345 int i = 0;
346
347 assert(sf);
348
349 if (!fdisk_has_label(sf->cxt))
350 return;
351
352 tpl = mk_backup_filename_tpl(sf->backup_file, devname, "-");
353
354 color_scheme_enable("header", UL_COLOR_BOLD);
355 fdisk_info(sf->cxt, _("Backup files:"));
356 color_disable();
357
358 while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size)
359 backup_sectors(sf, tpl, name, devname, offset, size);
360
361 if (!sf->quiet)
362 fputc('\n', stdout);
363 free(tpl);
364 }
365
366 static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa)
367 {
368 struct fdisk_partition *pa = get_partition(sf->cxt, partno);
369 char *devname = NULL, *typescript = NULL, *buf = NULL;
370 FILE *f = NULL;
371 int ok = 0, fd, backward = 0;
372 fdisk_sector_t nsectors, from, to, step, i;
373 size_t ss, step_bytes, cc;
374 uintmax_t src, dst;
375 int errsv;
376
377 assert(sf->movedata);
378
379 if (!pa)
380 warnx(_("failed to read new partition from device; ignoring --move-data"));
381 else if (!fdisk_partition_has_size(pa))
382 warnx(_("failed to get size of the new partition; ignoring --move-data"));
383 else if (!fdisk_partition_has_start(pa))
384 warnx(_("failed to get start of the new partition; ignoring --move-data"));
385 else if (!fdisk_partition_has_size(orig_pa))
386 warnx(_("failed to get size of the old partition; ignoring --move-data"));
387 else if (!fdisk_partition_has_start(orig_pa))
388 warnx(_("failed to get start of the old partition; ignoring --move-data"));
389 else if (fdisk_partition_get_start(pa) == fdisk_partition_get_start(orig_pa))
390 warnx(_("start of the partition has not been moved; ignoring --move-data"));
391 else if (fdisk_partition_get_size(orig_pa) < fdisk_partition_get_size(pa))
392 warnx(_("new partition is smaller than original; ignoring --move-data"));
393 else
394 ok = 1;
395 if (!ok)
396 return -EINVAL;
397
398 DBG(MISC, ul_debug("moving data"));
399
400 fd = fdisk_get_devfd(sf->cxt);
401
402 ss = fdisk_get_sector_size(sf->cxt);
403 nsectors = fdisk_partition_get_size(orig_pa);
404 from = fdisk_partition_get_start(orig_pa);
405 to = fdisk_partition_get_start(pa);
406
407 if ((to >= from && from + nsectors >= to) ||
408 (from >= to && to + nsectors >= from)) {
409 /* source and target overlay, check if we need to copy
410 * backwardly from end of the source */
411 DBG(MISC, ul_debug("overlay between source and target"));
412 backward = from < to;
413 DBG(MISC, ul_debug(" copy order: %s", backward ? "backward" : "forward"));
414
415 step = from > to ? from - to : to - from;
416 if (step > nsectors)
417 step = nsectors;
418 } else
419 step = nsectors;
420
421 /* make step usable for malloc() */
422 if (step * ss > (getpagesize() * 256U))
423 step = (getpagesize() * 256) / ss;
424
425 /* align the step (note that nsectors does not have to be power of 2) */
426 while (nsectors % step)
427 step--;
428
429 step_bytes = step * ss;
430 DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes));
431
432 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
433 if (!backward)
434 posix_fadvise(fd, from * ss, nsectors * ss, POSIX_FADV_SEQUENTIAL);
435 #endif
436 devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1);
437 typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
438
439 if (!sf->quiet) {
440 fdisk_info(sf->cxt,"");
441 color_scheme_enable("header", UL_COLOR_BOLD);
442 fdisk_info(sf->cxt, _("Data move:"));
443 color_disable();
444 fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
445 printf(_(" old start: %ju, new start: %ju (move %ju sectors)\n"),
446 (uintmax_t) from, (uintmax_t) to, (uintmax_t) nsectors);
447 fflush(stdout);
448 }
449
450 if (sf->interactive) {
451 int yes = 0;
452 fdisk_ask_yesno(sf->cxt, _("Do you want to move partition data?"), &yes);
453 if (!yes) {
454 fdisk_info(sf->cxt, _("Leaving."));
455 return 0;
456 }
457 }
458
459 f = fopen(typescript, "w");
460 if (!f)
461 goto fail;
462
463 /* don't translate */
464 fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
465 fprintf(f, "# Disk: %s\n", devname);
466 fprintf(f, "# Partition: %zu\n", partno + 1);
467 fprintf(f, "# Operation: move data\n");
468 fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
469 (uintmax_t)from, (uintmax_t)from * ss);
470 fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
471 (uintmax_t)to, (uintmax_t)to * ss);
472 fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
473 (uintmax_t)nsectors, (uintmax_t)nsectors * ss);
474 fprintf(f, "# Sector size: %zu\n", ss);
475 fprintf(f, "# Step size (in bytes): %zu\n", step_bytes);
476 fprintf(f, "# Steps: %ju\n", (uintmax_t)(nsectors / step));
477 fprintf(f, "#\n");
478 fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
479
480 src = (backward ? from + nsectors : from) * ss;
481 dst = (backward ? to + nsectors : to) * ss;
482 buf = xmalloc(step_bytes);
483
484 DBG(MISC, ul_debug(" initial: src=%ju dst=%ju", src, dst));
485
486 for (cc = 1, i = 0; i < nsectors; i += step, cc++) {
487 ssize_t rc;
488
489 if (backward)
490 src -= step_bytes, dst -= step_bytes;
491
492 DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst));
493
494 /* read source */
495 if (lseek(fd, src, SEEK_SET) == (off_t) -1)
496 goto fail;
497 rc = read(fd, buf, step_bytes);
498 if (rc < 0 || rc != (ssize_t) step_bytes)
499 goto fail;
500
501 /* write target */
502 if (lseek(fd, dst, SEEK_SET) == (off_t) -1)
503 goto fail;
504 rc = write(fd, buf, step_bytes);
505 if (rc < 0 || rc != (ssize_t) step_bytes)
506 goto fail;
507 fsync(fd);
508
509 /* write log */
510 fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
511
512 #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
513 posix_fadvise(fd, src, step_bytes, POSIX_FADV_DONTNEED);
514 #endif
515 if (!backward)
516 src += step_bytes, dst += step_bytes;
517 }
518
519 fclose(f);
520 free(buf);
521 free(devname);
522 free(typescript);
523
524 return 0;
525 fail:
526 errsv = -errno;
527 warn(_("%s: failed to move data"), devname);
528 if (f)
529 fclose(f);
530 free(buf);
531 free(devname);
532 free(typescript);
533
534 return errsv;
535 }
536
537 static int write_changes(struct sfdisk *sf)
538 {
539 int rc = 0;
540
541 if (sf->noact)
542 fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act)."));
543 else {
544 rc = fdisk_write_disklabel(sf->cxt);
545 if (rc == 0 && sf->movedata && sf->orig_pa)
546 rc = move_partition_data(sf, sf->partno, sf->orig_pa);
547 if (!rc) {
548 fdisk_info(sf->cxt, _("\nThe partition table has been altered."));
549 if (!sf->notell)
550 fdisk_reread_partition_table(sf->cxt);
551 }
552 }
553 if (!rc)
554 rc = fdisk_deassign_device(sf->cxt,
555 sf->noact || sf->notell); /* no-sync */
556 return rc;
557 }
558
559 /*
560 * sfdisk --list [<device ..]
561 */
562 static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
563 {
564 int fail = 0;
565 fdisk_enable_listonly(sf->cxt, 1);
566
567 if (argc) {
568 int i, ct = 0;
569
570 for (i = 0; i < argc; i++) {
571 if (ct)
572 fputs("\n\n", stdout);
573 if (print_device_pt(sf->cxt, argv[i], 1, sf->verify) != 0)
574 fail++;
575 ct++;
576 }
577 } else
578 print_all_devices_pt(sf->cxt, sf->verify);
579
580 return fail;
581 }
582
583 /*
584 * sfdisk --list-free [<device ..]
585 */
586 static int command_list_freespace(struct sfdisk *sf, int argc, char **argv)
587 {
588 int fail = 0;
589 fdisk_enable_listonly(sf->cxt, 1);
590
591 if (argc) {
592 int i, ct = 0;
593
594 for (i = 0; i < argc; i++) {
595 if (ct)
596 fputs("\n\n", stdout);
597 if (print_device_freespace(sf->cxt, argv[i], 1) != 0)
598 fail++;
599 ct++;
600 }
601 } else
602 print_all_devices_freespace(sf->cxt);
603
604 return fail;
605 }
606
607 /*
608 * sfdisk --list-types
609 */
610 static int command_list_types(struct sfdisk *sf)
611 {
612 const struct fdisk_parttype *t;
613 struct fdisk_label *lb;
614 const char *name;
615 size_t i = 0;
616 int codes;
617
618 assert(sf);
619 assert(sf->cxt);
620
621 name = sf->label ? sf->label : "dos";
622 lb = fdisk_get_label(sf->cxt, name);
623 if (!lb)
624 errx(EXIT_FAILURE, _("unsupported label '%s'"), name);
625
626 codes = fdisk_label_has_code_parttypes(lb);
627 fputs(_("Id Name\n\n"), stdout);
628
629 while ((t = fdisk_label_get_parttype(lb, i++))) {
630 if (codes)
631 printf("%2x %s\n", fdisk_parttype_get_code(t),
632 fdisk_parttype_get_name(t));
633 else
634 printf("%s %s\n", fdisk_parttype_get_string(t),
635 fdisk_parttype_get_name(t));
636 }
637
638 return 0;
639 }
640
641 static int verify_device(struct sfdisk *sf, const char *devname)
642 {
643 int rc = 1;
644
645 fdisk_enable_listonly(sf->cxt, 1);
646
647 if (fdisk_assign_device(sf->cxt, devname, 1)) {
648 warn(_("cannot open %s"), devname);
649 return 1;
650 }
651
652 color_scheme_enable("header", UL_COLOR_BOLD);
653 fdisk_info(sf->cxt, "%s:", devname);
654 color_disable();
655
656 if (!fdisk_has_label(sf->cxt))
657 fdisk_info(sf->cxt, _("unrecognized partition table type"));
658 else
659 rc = fdisk_verify_disklabel(sf->cxt);
660
661 fdisk_deassign_device(sf->cxt, 1);
662 return rc;
663 }
664
665 /*
666 * sfdisk --verify [<device ..]
667 */
668 static int command_verify(struct sfdisk *sf, int argc, char **argv)
669 {
670 int nfails = 0, ct = 0;
671
672 if (argc) {
673 int i;
674 for (i = 0; i < argc; i++) {
675 if (i)
676 fdisk_info(sf->cxt, " ");
677 if (verify_device(sf, argv[i]) < 0)
678 nfails++;
679 }
680 } else {
681 FILE *f = NULL;
682 char *dev;
683
684 while ((dev = next_proc_partition(&f))) {
685 if (ct)
686 fdisk_info(sf->cxt, " ");
687 if (verify_device(sf, dev) < 0)
688 nfails++;
689 free(dev);
690 ct++;
691 }
692 }
693
694 return nfails;
695 }
696
697 static int get_size(const char *dev, int silent, uintmax_t *sz)
698 {
699 int fd, rc = 0;
700
701 fd = open(dev, O_RDONLY);
702 if (fd < 0) {
703 if (!silent)
704 warn(_("cannot open %s"), dev);
705 return -errno;
706 }
707
708 if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) {
709 if (!silent)
710 warn(_("Cannot get size of %s"), dev);
711 rc = -errno;
712 }
713
714 close(fd);
715 return rc;
716 }
717
718 /*
719 * sfdisk --show-size [<device ..]
720 *
721 * (silly, but just for backward compatibility)
722 */
723 static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
724 int argc, char **argv)
725 {
726 uintmax_t sz;
727
728 if (argc) {
729 int i;
730 for (i = 0; i < argc; i++) {
731 if (get_size(argv[i], 0, &sz) == 0)
732 printf("%ju\n", sz / 2);
733 }
734 } else {
735 FILE *f = NULL;
736 uintmax_t total = 0;
737 char *dev;
738
739 while ((dev = next_proc_partition(&f))) {
740 if (get_size(dev, 1, &sz) == 0) {
741 printf("%s: %9ju\n", dev, sz / 2);
742 total += sz / 2;
743 }
744 free(dev);
745 }
746 if (total)
747 printf(_("total: %ju blocks\n"), total);
748 }
749
750 return 0;
751 }
752
753 static int print_geom(struct sfdisk *sf, const char *devname)
754 {
755 fdisk_enable_listonly(sf->cxt, 1);
756
757 if (fdisk_assign_device(sf->cxt, devname, 1)) {
758 warn(_("cannot open %s"), devname);
759 return 1;
760 }
761
762 fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
763 devname,
764 (uintmax_t) fdisk_get_geom_cylinders(sf->cxt),
765 (uintmax_t) fdisk_get_geom_heads(sf->cxt),
766 (uintmax_t) fdisk_get_geom_sectors(sf->cxt));
767
768 fdisk_deassign_device(sf->cxt, 1);
769 return 0;
770 }
771
772 /*
773 * sfdisk --show-geometry [<device ..]
774 */
775 static int command_show_geometry(struct sfdisk *sf, int argc, char **argv)
776 {
777 int nfails = 0;
778
779 if (argc) {
780 int i;
781 for (i = 0; i < argc; i++) {
782 if (print_geom(sf, argv[i]) < 0)
783 nfails++;
784 }
785 } else {
786 FILE *f = NULL;
787 char *dev;
788
789 while ((dev = next_proc_partition(&f))) {
790 if (print_geom(sf, dev) < 0)
791 nfails++;
792 free(dev);
793 }
794 }
795
796 return nfails;
797 }
798
799 /*
800 * sfdisk --activate <device> [<partno> ...]
801 */
802 static int command_activate(struct sfdisk *sf, int argc, char **argv)
803 {
804 int rc, nparts, i, listonly;
805 struct fdisk_partition *pa = NULL;
806 const char *devname = NULL;
807
808 if (argc < 1)
809 errx(EXIT_FAILURE, _("no disk device specified"));
810 devname = argv[0];
811
812 /* --activate <device> */
813 listonly = argc == 1;
814
815 rc = fdisk_assign_device(sf->cxt, devname, listonly);
816 if (rc)
817 err(EXIT_FAILURE, _("cannot open %s"), devname);
818
819 if (fdisk_is_label(sf->cxt, GPT)) {
820 /* Switch from GPT to PMBR */
821 sf->cxt = fdisk_new_nested_context(sf->cxt, "dos");
822 if (!sf->cxt)
823 err(EXIT_FAILURE, _("cannot switch to PMBR"));
824 } else if (!fdisk_is_label(sf->cxt, DOS))
825 errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only"));
826
827 if (!listonly && sf->backup)
828 backup_partition_table(sf, devname);
829
830 nparts = fdisk_get_npartitions(sf->cxt);
831 for (i = 0; i < nparts; i++) {
832 char *data = NULL;
833
834 /* note that fdisk_get_partition() reuses the @pa pointer, you
835 * don't have to (re)allocate it */
836 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
837 continue;
838
839 /* sfdisk --activate list bootable partitions */
840 if (listonly) {
841 if (!fdisk_partition_is_bootable(pa))
842 continue;
843 if (fdisk_partition_to_string(pa, sf->cxt,
844 FDISK_FIELD_DEVICE, &data) == 0) {
845 printf("%s\n", data);
846 free(data);
847 }
848
849 /* deactivate all active partitions */
850 } else if (fdisk_partition_is_bootable(pa))
851 fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE);
852 }
853
854 /* sfdisk --activate <partno> [..] */
855 for (i = 1; i < argc; i++) {
856 int n;
857
858 if (i == 1 && strcmp(argv[1], "-") == 0)
859 break;
860 n = strtou32_or_err(argv[i], _("failed to parse partition number"));
861
862 rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE);
863 if (rc)
864 errx(EXIT_FAILURE,
865 _("%s: partition %d: failed to toggle bootable flag"),
866 devname, i + 1);
867 }
868
869 fdisk_unref_partition(pa);
870
871 if (listonly)
872 rc = fdisk_deassign_device(sf->cxt, 1);
873 else
874 rc = write_changes(sf);
875 return rc;
876 }
877
878 /*
879 * sfdisk --delete <device> [<partno> ...]
880 */
881 static int command_delete(struct sfdisk *sf, int argc, char **argv)
882 {
883 size_t i;
884 const char *devname = NULL;
885
886 if (argc < 1)
887 errx(EXIT_FAILURE, _("no disk device specified"));
888 devname = argv[0];
889
890 if (fdisk_assign_device(sf->cxt, devname, 0) != 0)
891 err(EXIT_FAILURE, _("cannot open %s"), devname);
892
893 if (sf->backup)
894 backup_partition_table(sf, devname);
895
896 /* delete all */
897 if (argc == 1) {
898 size_t nparts = fdisk_get_npartitions(sf->cxt);
899 for (i = 0; i < nparts; i++) {
900 if (fdisk_is_partition_used(sf->cxt, i) &&
901 fdisk_delete_partition(sf->cxt, i) != 0)
902 errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, i + 1);
903 }
904 /* delete specified */
905 } else {
906 for (i = 1; i < (size_t) argc; i++) {
907 size_t n = strtou32_or_err(argv[i], _("failed to parse partition number"));
908
909 if (fdisk_delete_partition(sf->cxt, n - 1) != 0)
910 errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, n);
911 }
912 }
913
914 return write_changes(sf);
915 }
916
917 /*
918 * sfdisk --reorder <device>
919 */
920 static int command_reorder(struct sfdisk *sf, int argc, char **argv)
921 {
922 const char *devname = NULL;
923 int rc;
924
925 if (argc)
926 devname = argv[0];
927 if (!devname)
928 errx(EXIT_FAILURE, _("no disk device specified"));
929
930 rc = fdisk_assign_device(sf->cxt, devname, 0); /* read-write */
931 if (rc)
932 err(EXIT_FAILURE, _("cannot open %s"), devname);
933
934 if (sf->backup)
935 backup_partition_table(sf, devname);
936
937 if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchanged */
938 rc = fdisk_deassign_device(sf->cxt, 1);
939 else
940 rc = write_changes(sf);
941
942 return rc;
943 }
944
945
946 /*
947 * sfdisk --dump <device>
948 */
949 static int command_dump(struct sfdisk *sf, int argc, char **argv)
950 {
951 const char *devname = NULL;
952 struct fdisk_script *dp;
953 int rc;
954
955 if (argc)
956 devname = argv[0];
957 if (!devname)
958 errx(EXIT_FAILURE, _("no disk device specified"));
959
960 rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */
961 if (rc)
962 err(EXIT_FAILURE, _("cannot open %s"), devname);
963
964 if (!fdisk_has_label(sf->cxt))
965 errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
966
967 dp = fdisk_new_script(sf->cxt);
968 if (!dp)
969 err(EXIT_FAILURE, _("failed to allocate dump struct"));
970
971 rc = fdisk_script_read_context(dp, NULL);
972 if (rc)
973 errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname);
974
975 if (sf->json)
976 fdisk_script_enable_json(dp, 1);
977 fdisk_script_write_file(dp, stdout);
978
979 fdisk_unref_script(dp);
980 fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
981 return 0;
982 }
983
984 static void assign_device_partition(struct sfdisk *sf,
985 const char *devname,
986 size_t partno,
987 int rdonly)
988 {
989 int rc;
990 size_t n;
991 struct fdisk_label *lb = NULL;
992
993 assert(sf);
994 assert(devname);
995
996 /* read-only when a new <type> undefined */
997 rc = fdisk_assign_device(sf->cxt, devname, rdonly);
998 if (rc)
999 err(EXIT_FAILURE, _("cannot open %s"), devname);
1000
1001 lb = fdisk_get_label(sf->cxt, NULL);
1002 if (!lb)
1003 errx(EXIT_FAILURE, _("%s: no partition table found"), devname);
1004
1005 n = fdisk_get_npartitions(sf->cxt);
1006 if (partno > n)
1007 errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains "
1008 "only %zu partitions"), devname, partno, n);
1009 if (!fdisk_is_partition_used(sf->cxt, partno - 1))
1010 errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"),
1011 devname, partno);
1012 }
1013
1014 /*
1015 * sfdisk --part-type <device> <partno> [<type>]
1016 */
1017 static int command_parttype(struct sfdisk *sf, int argc, char **argv)
1018 {
1019 size_t partno;
1020 struct fdisk_parttype *type = NULL;
1021 struct fdisk_label *lb;
1022 const char *devname = NULL, *typestr = NULL;
1023
1024 if (!argc)
1025 errx(EXIT_FAILURE, _("no disk device specified"));
1026 devname = argv[0];
1027
1028 if (argc < 2)
1029 errx(EXIT_FAILURE, _("no partition number specified"));
1030 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1031
1032 if (argc == 3)
1033 typestr = argv[2];
1034 else if (argc > 3)
1035 errx(EXIT_FAILURE, _("unexpected arguments"));
1036
1037 /* read-only when a new <type> undefined */
1038 assign_device_partition(sf, devname, partno, !typestr);
1039
1040 lb = fdisk_get_label(sf->cxt, NULL);
1041
1042 /* print partition type */
1043 if (!typestr) {
1044 const struct fdisk_parttype *t = NULL;
1045 struct fdisk_partition *pa = NULL;
1046
1047 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1048 t = fdisk_partition_get_type(pa);
1049 if (!t)
1050 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"),
1051 devname, partno);
1052
1053 if (fdisk_label_has_code_parttypes(lb))
1054 printf("%2x\n", fdisk_parttype_get_code(t));
1055 else
1056 printf("%s\n", fdisk_parttype_get_string(t));
1057
1058 fdisk_unref_partition(pa);
1059 fdisk_deassign_device(sf->cxt, 1);
1060 return 0;
1061 }
1062
1063 if (sf->backup)
1064 backup_partition_table(sf, devname);
1065
1066 /* parse <type> and apply to PT */
1067 type = fdisk_label_parse_parttype(lb, typestr);
1068 if (!type)
1069 errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"),
1070 fdisk_label_get_name(lb), typestr);
1071
1072 else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0)
1073 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"),
1074 devname, partno);
1075 fdisk_unref_parttype(type);
1076 return write_changes(sf);
1077 }
1078
1079 /*
1080 * sfdisk --part-uuid <device> <partno> [<uuid>]
1081 */
1082 static int command_partuuid(struct sfdisk *sf, int argc, char **argv)
1083 {
1084 size_t partno;
1085 struct fdisk_partition *pa = NULL;
1086 const char *devname = NULL, *uuid = NULL;
1087
1088 if (!argc)
1089 errx(EXIT_FAILURE, _("no disk device specified"));
1090 devname = argv[0];
1091
1092 if (argc < 2)
1093 errx(EXIT_FAILURE, _("no partition number specified"));
1094 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1095
1096 if (argc == 3)
1097 uuid = argv[2];
1098 else if (argc > 3)
1099 errx(EXIT_FAILURE, _("unexpected arguments"));
1100
1101 /* read-only if uuid not given */
1102 assign_device_partition(sf, devname, partno, !uuid);
1103
1104 /* print partition uuid */
1105 if (!uuid) {
1106 const char *str = NULL;
1107
1108 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1109 str = fdisk_partition_get_uuid(pa);
1110 if (!str)
1111 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"),
1112 devname, partno);
1113 printf("%s\n", str);
1114 fdisk_unref_partition(pa);
1115 fdisk_deassign_device(sf->cxt, 1);
1116 return 0;
1117 }
1118
1119 if (sf->backup)
1120 backup_partition_table(sf, devname);
1121
1122 pa = fdisk_new_partition();
1123 if (!pa)
1124 err(EXIT_FAILURE, _("failed to allocate partition object"));
1125
1126 if (fdisk_partition_set_uuid(pa, uuid) != 0 ||
1127 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1128 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"),
1129 devname, partno);
1130 fdisk_unref_partition(pa);
1131 return write_changes(sf);
1132 }
1133
1134 /*
1135 * sfdisk --part-label <device> <partno> [<label>]
1136 */
1137 static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
1138 {
1139 size_t partno;
1140 struct fdisk_partition *pa = NULL;
1141 const char *devname = NULL, *name = NULL;
1142
1143 if (!argc)
1144 errx(EXIT_FAILURE, _("no disk device specified"));
1145 devname = argv[0];
1146
1147 if (argc < 2)
1148 errx(EXIT_FAILURE, _("no partition number specified"));
1149 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1150
1151 if (argc == 3)
1152 name = argv[2];
1153 else if (argc > 3)
1154 errx(EXIT_FAILURE, _("unexpected arguments"));
1155
1156 /* read-only if name not given */
1157 assign_device_partition(sf, devname, partno, !name);
1158
1159 /* print partition name */
1160 if (!name) {
1161 const char *str = NULL;
1162
1163 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1164 str = fdisk_partition_get_name(pa);
1165 if (!str)
1166 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"),
1167 devname, partno);
1168 printf("%s\n", str);
1169 fdisk_unref_partition(pa);
1170 fdisk_deassign_device(sf->cxt, 1);
1171 return 0;
1172 }
1173
1174 if (sf->backup)
1175 backup_partition_table(sf, devname);
1176
1177 pa = fdisk_new_partition();
1178 if (!pa)
1179 err(EXIT_FAILURE, _("failed to allocate partition object"));
1180
1181 if (fdisk_partition_set_name(pa, name) != 0 ||
1182 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1183 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"),
1184 devname, partno);
1185
1186 fdisk_unref_partition(pa);
1187 return write_changes(sf);
1188 }
1189
1190 /*
1191 * sfdisk --part-attrs <device> <partno> [<attrs>]
1192 */
1193 static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
1194 {
1195 size_t partno;
1196 struct fdisk_partition *pa = NULL;
1197 const char *devname = NULL, *attrs = NULL;
1198
1199 if (!argc)
1200 errx(EXIT_FAILURE, _("no disk device specified"));
1201 devname = argv[0];
1202
1203 if (argc < 2)
1204 errx(EXIT_FAILURE, _("no partition number specified"));
1205 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1206
1207 if (argc == 3)
1208 attrs = argv[2];
1209 else if (argc > 3)
1210 errx(EXIT_FAILURE, _("unexpected arguments"));
1211
1212 /* read-only if name not given */
1213 assign_device_partition(sf, devname, partno, !attrs);
1214
1215 /* print partition name */
1216 if (!attrs) {
1217 const char *str = NULL;
1218
1219 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1220 str = fdisk_partition_get_attrs(pa);
1221 if (str)
1222 printf("%s\n", str);
1223 fdisk_unref_partition(pa);
1224 fdisk_deassign_device(sf->cxt, 1);
1225 return 0;
1226 }
1227
1228 if (sf->backup)
1229 backup_partition_table(sf, devname);
1230
1231 pa = fdisk_new_partition();
1232 if (!pa)
1233 err(EXIT_FAILURE, _("failed to allocate partition object"));
1234
1235 if (fdisk_partition_set_attrs(pa, attrs) != 0 ||
1236 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1237 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"),
1238 devname, partno);
1239
1240 fdisk_unref_partition(pa);
1241 return write_changes(sf);
1242 }
1243
1244 static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
1245 {
1246 struct fdisk_partition *pa = NULL;
1247 char *data;
1248
1249 assert(sf);
1250
1251 if (sf->quiet)
1252 return;
1253 if (fdisk_get_partition(sf->cxt, n, &pa) != 0)
1254 return;
1255
1256 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data);
1257 printf("%12s : ", data);
1258
1259 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data);
1260 printf("%12s ", data);
1261
1262 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data);
1263 printf("%12s ", data);
1264
1265 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data);
1266 printf("(%s) ", data);
1267
1268 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data);
1269 printf("%s\n", data);
1270
1271 fdisk_unref_partition(pa);
1272 }
1273
1274 static void command_fdisk_help(void)
1275 {
1276 fputs(_("\nHelp:\n"), stdout);
1277
1278 fputc('\n', stdout);
1279 color_scheme_enable("help-title", UL_COLOR_BOLD);
1280 fputs(_(" Commands:\n"), stdout);
1281 color_disable();
1282 fputs(_(" write write table to disk and exit\n"), stdout);
1283 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout);
1284 fputs(_(" abort exit sfdisk shell\n"), stdout);
1285 fputs(_(" print display the partition table\n"), stdout);
1286 fputs(_(" help show this help text\n"), stdout);
1287 fputc('\n', stdout);
1288 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout);
1289
1290 fputc('\n', stdout);
1291 color_scheme_enable("help-title", UL_COLOR_BOLD);
1292 fputs(_(" Input format:\n"), stdout);
1293 color_disable();
1294 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout);
1295
1296 fputc('\n', stdout);
1297 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1298 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1299 " The default is the first free space.\n"), stdout);
1300
1301 fputc('\n', stdout);
1302 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1303 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1304 " The default is all available space.\n"), stdout);
1305
1306 fputc('\n', stdout);
1307 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout);
1308 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout);
1309 fputs(_(" GPT: UUID or L,S,H shortcuts.\n"), stdout);
1310
1311 fputc('\n', stdout);
1312 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout);
1313
1314 fputc('\n', stdout);
1315 color_scheme_enable("help-title", UL_COLOR_BOLD);
1316 fputs(_(" Example:\n"), stdout);
1317 color_disable();
1318 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout);
1319 fputc('\n', stdout);
1320 }
1321
1322 enum {
1323 SFDISK_DONE_NONE = 0,
1324 SFDISK_DONE_EOF,
1325 SFDISK_DONE_ABORT,
1326 SFDISK_DONE_WRITE,
1327 SFDISK_DONE_ASK
1328 };
1329
1330 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1331 static int loop_control_commands(struct sfdisk *sf,
1332 struct fdisk_script *dp,
1333 char *buf)
1334 {
1335 const char *p = skip_blank(buf);
1336 int rc = SFDISK_DONE_NONE;
1337
1338 if (strcmp(p, "print") == 0)
1339 list_disklabel(sf->cxt);
1340 else if (strcmp(p, "help") == 0)
1341 command_fdisk_help();
1342 else if (strcmp(p, "quit") == 0)
1343 rc = SFDISK_DONE_ASK;
1344 else if (strcmp(p, "write") == 0)
1345 rc = SFDISK_DONE_WRITE;
1346 else if (strcmp(p, "abort") == 0)
1347 rc = SFDISK_DONE_ABORT;
1348 else {
1349 if (sf->interactive)
1350 fdisk_warnx(sf->cxt, _("unsupported command"));
1351 else {
1352 fdisk_warnx(sf->cxt, _("line %d: unsupported command"),
1353 fdisk_script_get_nlines(dp));
1354 rc = -EINVAL;
1355 }
1356 }
1357 return rc;
1358 }
1359
1360 static int has_container(struct sfdisk *sf)
1361 {
1362 size_t i, nparts;
1363 struct fdisk_partition *pa = NULL;
1364
1365 if (sf->container)
1366 return sf->container;
1367
1368 nparts = fdisk_get_npartitions(sf->cxt);
1369 for (i = 0; i < nparts; i++) {
1370 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
1371 continue;
1372 if (fdisk_partition_is_container(pa)) {
1373 sf->container = 1;
1374 break;
1375 }
1376 }
1377
1378 fdisk_unref_partition(pa);
1379 return sf->container;
1380 }
1381
1382 static size_t last_pt_partno(struct sfdisk *sf)
1383 {
1384 size_t i, nparts, partno = 0;
1385 struct fdisk_partition *pa = NULL;
1386
1387
1388 nparts = fdisk_get_npartitions(sf->cxt);
1389 for (i = 0; i < nparts; i++) {
1390 size_t x;
1391
1392 if (fdisk_get_partition(sf->cxt, i, &pa) != 0 ||
1393 !fdisk_partition_is_used(pa))
1394 continue;
1395 x = fdisk_partition_get_partno(pa);
1396 if (x > partno)
1397 partno = x;
1398 }
1399
1400 fdisk_unref_partition(pa);
1401 return partno;
1402 }
1403
1404 #ifdef HAVE_LIBREADLINE
1405 static char *sfdisk_fgets(struct fdisk_script *dp,
1406 char *buf, size_t bufsz, FILE *f)
1407 {
1408 struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp);
1409
1410 assert(dp);
1411 assert(buf);
1412 assert(bufsz > 2);
1413
1414 if (sf->interactive) {
1415 char *p = readline(sf->prompt);
1416 size_t len;
1417
1418 if (!p)
1419 return NULL;
1420 len = strlen(p);
1421 if (len > bufsz - 2)
1422 len = bufsz - 2;
1423
1424 memcpy(buf, p, len);
1425 buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
1426 buf[len + 1] = '\0';
1427 free(p);
1428 fflush(stdout);
1429 return buf;
1430 }
1431 return fgets(buf, bufsz, f);
1432 }
1433 #endif
1434
1435 static int ignore_partition(struct fdisk_partition *pa)
1436 {
1437 /* incomplete partition setting */
1438 if (!fdisk_partition_has_start(pa) && !fdisk_partition_start_is_default(pa))
1439 return 1;
1440 if (!fdisk_partition_has_size(pa) && !fdisk_partition_end_is_default(pa))
1441 return 1;
1442
1443 /* probably dump from old sfdisk with start=0 size=0 */
1444 if (fdisk_partition_has_start(pa) && fdisk_partition_get_start(pa) == 0 &&
1445 fdisk_partition_has_size(pa) && fdisk_partition_get_size(pa) == 0)
1446 return 1;
1447
1448 return 0;
1449 }
1450
1451 static void follow_wipe_mode(struct sfdisk *sf)
1452 {
1453 int dowipe = sf->wipemode == WIPEMODE_ALWAYS ? 1 : 0;
1454
1455 if (sf->interactive && sf->wipemode == WIPEMODE_AUTO)
1456 dowipe = 1; /* do it in interactive mode */
1457
1458 if (fdisk_is_ptcollision(sf->cxt) && sf->wipemode != WIPEMODE_NEVER)
1459 dowipe = 1; /* always wipe old PT */
1460
1461 fdisk_enable_wipe(sf->cxt, dowipe);
1462 if (sf->quiet)
1463 return;
1464
1465 if (dowipe) {
1466 if (!fdisk_is_ptcollision(sf->cxt)) {
1467 fdisk_info(sf->cxt, _("The old %s signature will be removed by a write command."),
1468 fdisk_get_collision(sf->cxt));
1469 fputc('\n', stderr);
1470 }
1471 } else {
1472 fdisk_warnx(sf->cxt, _(
1473 "The old %s signature may remain on the device. "
1474 "It is recommended to wipe the device with wipefs(8) or "
1475 "sfdisk --wipe, in order to avoid possible collisions."),
1476 fdisk_get_collision(sf->cxt));
1477 fputc('\n', stderr);
1478 }
1479 }
1480
1481 static int wipe_partition(struct sfdisk *sf, size_t partno)
1482 {
1483 int rc, yes = 0;
1484 char *fstype = NULL;
1485 struct fdisk_partition *tmp = NULL;
1486
1487 DBG(MISC, ul_debug("checking for signature"));
1488
1489 rc = fdisk_get_partition(sf->cxt, partno, &tmp);
1490 if (rc)
1491 goto done;
1492
1493 rc = fdisk_partition_to_string(tmp, sf->cxt, FDISK_FIELD_FSTYPE, &fstype);
1494 if (rc || fstype == NULL)
1495 goto done;
1496
1497 fdisk_warnx(sf->cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
1498
1499 if (sf->pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
1500 fdisk_ask_yesno(sf->cxt, _("Do you want to remove the signature?"), &yes);
1501 else if (sf->pwipemode == WIPEMODE_ALWAYS)
1502 yes = 1;
1503
1504 if (yes) {
1505 fdisk_info(sf->cxt, _("The signature will be removed by a write command."));
1506 rc = fdisk_wipe_partition(sf->cxt, partno, TRUE);
1507 }
1508 done:
1509 fdisk_unref_partition(tmp);
1510 free(fstype);
1511 DBG(MISC, ul_debug("partition wipe check end [rc=%d]", rc));
1512 return rc;
1513 }
1514
1515 static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname,
1516 size_t next_partno, int created)
1517 {
1518 if (created) {
1519 char *partname = fdisk_partname(devname, next_partno + 1);
1520 if (!partname)
1521 err(EXIT_FAILURE, _("failed to allocate partition name"));
1522
1523 if (!sf->prompt || !startswith(sf->prompt, partname)) {
1524 free(sf->prompt);
1525 xasprintf(&sf->prompt,"%s: ", partname);
1526 }
1527 free(partname);
1528 } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
1529 free(sf->prompt);
1530 sf->prompt = xstrdup(SFDISK_PROMPT);
1531 }
1532 }
1533
1534 /*
1535 * sfdisk <device> [[-N] <partno>]
1536 *
1537 * Note that the option -N is there for backward compatibility only.
1538 */
1539 static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
1540 {
1541 int rc = 0, partno = sf->partno, created = 0, unused = 0;
1542 struct fdisk_script *dp;
1543 struct fdisk_table *tb = NULL;
1544 const char *devname = NULL, *label;
1545 char buf[BUFSIZ];
1546 size_t next_partno = (size_t) -1;
1547
1548 if (argc)
1549 devname = argv[0];
1550 if (partno < 0 && argc > 1)
1551 partno = strtou32_or_err(argv[1],
1552 _("failed to parse partition number"));
1553 if (!devname)
1554 errx(EXIT_FAILURE, _("no disk device specified"));
1555
1556 rc = fdisk_assign_device(sf->cxt, devname, 0);
1557 if (rc)
1558 err(EXIT_FAILURE, _("cannot open %s"), devname);
1559
1560 dp = fdisk_new_script(sf->cxt);
1561 if (!dp)
1562 err(EXIT_FAILURE, _("failed to allocate script handler"));
1563 fdisk_set_script(sf->cxt, dp);
1564 #ifdef HAVE_LIBREADLINE
1565 fdisk_script_set_fgets(dp, sfdisk_fgets);
1566 #endif
1567 fdisk_script_set_userdata(dp, (void *) sf);
1568
1569 /*
1570 * Don't create a new disklabel when [-N] <partno> specified. In this
1571 * case reuse already specified disklabel. Let's check that the disk
1572 * really contains the partition.
1573 */
1574 if (partno >= 0) {
1575 size_t n;
1576
1577 if (!fdisk_has_label(sf->cxt))
1578 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1579 "no partition table was found"),
1580 devname, partno + 1);
1581 n = fdisk_get_npartitions(sf->cxt);
1582 if ((size_t) partno > n)
1583 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1584 "partition table contains only %zu "
1585 "partitions"),
1586 devname, partno + 1, n);
1587
1588 if (!fdisk_is_partition_used(sf->cxt, partno)) {
1589 fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"),
1590 devname, partno + 1);
1591 unused = 1;
1592 }
1593 created = 1;
1594 next_partno = partno;
1595
1596 if (sf->movedata)
1597 sf->orig_pa = get_partition(sf->cxt, partno);
1598 }
1599
1600 if (sf->append) {
1601 created = 1;
1602 next_partno = last_pt_partno(sf) + 1;
1603 }
1604
1605 if (!sf->quiet && sf->interactive) {
1606 color_scheme_enable("welcome", UL_COLOR_GREEN);
1607 fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING);
1608 color_disable();
1609 fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n"
1610 "Be careful before using the write command.\n"));
1611 }
1612
1613 if (!sf->noact && !sf->noreread) {
1614 if (!sf->quiet)
1615 fputs(_("Checking that no-one is using this disk right now ..."), stdout);
1616 if (fdisk_device_is_used(sf->cxt)) {
1617 if (!sf->quiet)
1618 fputs(_(" FAILED\n\n"), stdout);
1619
1620 fdisk_warnx(sf->cxt, _(
1621 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1622 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1623 "Use the --no-reread flag to suppress this check.\n"));
1624
1625 if (!sf->force)
1626 errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
1627 } else if (!sf->quiet)
1628 fputs(_(" OK\n\n"), stdout);
1629 }
1630
1631 if (fdisk_get_collision(sf->cxt))
1632 follow_wipe_mode(sf);
1633
1634 if (sf->backup)
1635 backup_partition_table(sf, devname);
1636
1637 if (!sf->quiet) {
1638 list_disk_geometry(sf->cxt);
1639 if (fdisk_has_label(sf->cxt)) {
1640 fdisk_info(sf->cxt, _("\nOld situation:"));
1641 list_disklabel(sf->cxt);
1642 }
1643 }
1644
1645 if (sf->label)
1646 label = sf->label;
1647 else if (fdisk_has_label(sf->cxt))
1648 label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
1649 else
1650 label = "dos"; /* just for backward compatibility */
1651
1652 fdisk_script_set_header(dp, "label", label);
1653
1654
1655 if (!sf->quiet && sf->interactive) {
1656 if (!fdisk_has_label(sf->cxt) && !sf->label)
1657 fdisk_info(sf->cxt,
1658 _("\nsfdisk is going to create a new '%s' disk label.\n"
1659 "Use 'label: <name>' before you define a first partition\n"
1660 "to override the default."), label);
1661 fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n"));
1662 } else if (!sf->quiet)
1663 fputc('\n', stdout);
1664
1665 tb = fdisk_script_get_table(dp);
1666 assert(tb);
1667
1668 do {
1669 size_t nparts;
1670
1671 DBG(PARSE, ul_debug("<---next-line--->"));
1672 if (next_partno == (size_t) -1)
1673 next_partno = fdisk_table_get_nents(tb);
1674
1675 if (created
1676 && partno < 0
1677 && next_partno == fdisk_get_npartitions(sf->cxt)
1678 && !has_container(sf)) {
1679 fdisk_info(sf->cxt, _("All partitions used."));
1680 rc = SFDISK_DONE_ASK;
1681 break;
1682 }
1683
1684 refresh_prompt_buffer(sf, devname, next_partno, created);
1685
1686
1687 if (sf->prompt && (sf->interactive || !sf->quiet)) {
1688 #ifndef HAVE_LIBREADLINE
1689 fputs(sf->prompt, stdout);
1690 #else
1691 if (!sf->interactive)
1692 fputs(sf->prompt, stdout);
1693 #endif
1694 }
1695
1696 rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
1697 if (rc < 0) {
1698 DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
1699 buf[sizeof(buf) - 1] = '\0';
1700 rc = loop_control_commands(sf, dp, buf);
1701 if (rc)
1702 break;
1703 continue;
1704 } else if (rc == 1) {
1705 rc = SFDISK_DONE_EOF;
1706 if (!sf->quiet)
1707 fputs(_("Done.\n"), stdout);
1708 break;
1709 }
1710
1711 nparts = fdisk_table_get_nents(tb);
1712 if (nparts) {
1713 size_t cur_partno;
1714 struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1);
1715
1716 assert(pa);
1717
1718 if (ignore_partition(pa)) {
1719 fdisk_info(sf->cxt, _("Ignoring partition."));
1720 next_partno++;
1721 continue;
1722 }
1723 if (!created) { /* create a new disklabel */
1724 rc = fdisk_apply_script_headers(sf->cxt, dp);
1725 created = !rc;
1726 if (rc)
1727 fdisk_warnx(sf->cxt, _(
1728 "Failed to apply script headers, "
1729 "disk label not created."));
1730
1731 if (rc == 0 && fdisk_get_collision(sf->cxt))
1732 follow_wipe_mode(sf);
1733 }
1734 if (!rc && partno >= 0) { /* -N <partno>, modify partition */
1735 rc = fdisk_set_partition(sf->cxt, partno, pa);
1736 rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT;
1737 break;
1738 } else if (!rc) { /* add partition */
1739 if (!sf->interactive && !sf->quiet &&
1740 (!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) {
1741 refresh_prompt_buffer(sf, devname, next_partno, created);
1742 fputs(sf->prompt, stdout);
1743 }
1744 rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
1745 if (rc) {
1746 errno = -rc;
1747 fdisk_warn(sf->cxt, _("Failed to add #%d partition"), next_partno + 1);
1748 }
1749 }
1750
1751 /* wipe partition on success
1752 *
1753 * Note that unused=1 means -N <partno> for unused,
1754 * otherwise we wipe only newly created partitions.
1755 */
1756 if (rc == 0 && (unused || partno < 0)) {
1757 rc = wipe_partition(sf, unused ? (size_t) partno : cur_partno);
1758 if (rc)
1759 errno = -rc;
1760 }
1761
1762 if (!rc) {
1763 /* success print result */
1764 if (sf->interactive)
1765 sfdisk_print_partition(sf, cur_partno);
1766 next_partno = cur_partno + 1;
1767 } else if (pa) /* error, drop partition from script */
1768 fdisk_table_remove_partition(tb, pa);
1769 } else
1770 fdisk_info(sf->cxt, _("Script header accepted."));
1771
1772 if (rc && !sf->interactive) {
1773 rc = SFDISK_DONE_ABORT;
1774 break;
1775 }
1776 } while (1);
1777
1778 /* create empty disk label if label, but no partition specified */
1779 if ((rc == SFDISK_DONE_EOF || rc == SFDISK_DONE_WRITE) && created == 0
1780 && fdisk_script_has_force_label(dp) == 1
1781 && fdisk_table_get_nents(tb) == 0
1782 && fdisk_script_get_header(dp, "label")) {
1783
1784 int xrc = fdisk_apply_script_headers(sf->cxt, dp);
1785 created = !xrc;
1786 if (xrc) {
1787 fdisk_warnx(sf->cxt, _(
1788 "Failed to apply script headers, "
1789 "disk label not created."));
1790 rc = SFDISK_DONE_ABORT;
1791 }
1792 }
1793
1794 if (!sf->quiet && rc != SFDISK_DONE_ABORT) {
1795 fdisk_info(sf->cxt, _("\nNew situation:"));
1796 list_disk_identifier(sf->cxt);
1797 list_disklabel(sf->cxt);
1798 }
1799
1800 switch (rc) {
1801 case SFDISK_DONE_ASK:
1802 case SFDISK_DONE_EOF:
1803 if (sf->interactive) {
1804 int yes = 0;
1805 fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes);
1806 if (!yes) {
1807 fdisk_info(sf->cxt, _("Leaving."));
1808 rc = 0;
1809 break;
1810 }
1811 }
1812 /* fallthrough */
1813 case SFDISK_DONE_WRITE:
1814 rc = write_changes(sf);
1815 break;
1816 case SFDISK_DONE_ABORT:
1817 default: /* rc < 0 on error */
1818 fdisk_info(sf->cxt, _("Leaving.\n"));
1819 break;
1820 }
1821
1822 fdisk_unref_script(dp);
1823 return rc;
1824 }
1825
1826 static void __attribute__((__noreturn__)) usage(void)
1827 {
1828 FILE *out = stdout;
1829 fputs(USAGE_HEADER, out);
1830
1831 fprintf(out,
1832 _(" %1$s [options] <dev> [[-N] <part>]\n"
1833 " %1$s [options] <command>\n"), program_invocation_short_name);
1834
1835 fputs(USAGE_SEPARATOR, out);
1836 fputs(_("Display or manipulate a disk partition table.\n"), out);
1837
1838 fputs(USAGE_COMMANDS, out);
1839 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out);
1840 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out);
1841 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out);
1842 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
1843 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
1844 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out);
1845 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out);
1846 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
1847 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
1848 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out);
1849 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out);
1850
1851 fputs(USAGE_SEPARATOR, out);
1852 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
1853 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
1854 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
1855 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
1856
1857 fputs(USAGE_SEPARATOR, out);
1858 fputs(_(" <dev> device (usually disk) path\n"), out);
1859 fputs(_(" <part> partition number\n"), out);
1860 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
1861
1862 fputs(USAGE_OPTIONS, out);
1863 fputs(_(" -a, --append append partitions to existing partition table\n"), out);
1864 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out);
1865 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out);
1866 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out);
1867 fputs(_(" -f, --force disable all consistency checking\n"), out);
1868 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out);
1869 fprintf(out,
1870 " %s\n", USAGE_COLORS_DEFAULT);
1871 fputs(_(" -N, --partno <num> specify partition number\n"), out);
1872 fputs(_(" -n, --no-act do everything except write to device\n"), out);
1873 fputs(_(" --no-reread do not check whether the device is in use\n"), out);
1874 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out);
1875 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out);
1876 fputs(_(" -o, --output <list> output columns\n"), out);
1877 fputs(_(" -q, --quiet suppress extra info messages\n"), out);
1878 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out);
1879 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out);
1880 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
1881 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out);
1882 fputs(USAGE_SEPARATOR, out);
1883 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out);
1884 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out);
1885 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
1886
1887 fputs(USAGE_SEPARATOR, out);
1888 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP);
1889 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION);
1890
1891 list_available_columns(out);
1892
1893 printf(USAGE_MAN_TAIL("sfdisk(8)"));
1894 exit(EXIT_SUCCESS);
1895 }
1896
1897
1898 int main(int argc, char *argv[])
1899 {
1900 const char *outarg = NULL;
1901 int rc = -EINVAL, c, longidx = -1, bytes = 0;
1902 int colormode = UL_COLORMODE_UNDEF;
1903 struct sfdisk _sf = {
1904 .partno = -1,
1905 .wipemode = WIPEMODE_AUTO,
1906 .pwipemode = WIPEMODE_AUTO,
1907 .interactive = isatty(STDIN_FILENO) ? 1 : 0,
1908 }, *sf = &_sf;
1909
1910 enum {
1911 OPT_CHANGE_ID = CHAR_MAX + 1,
1912 OPT_PRINT_ID,
1913 OPT_ID,
1914 OPT_NOREREAD,
1915 OPT_PARTUUID,
1916 OPT_PARTLABEL,
1917 OPT_PARTTYPE,
1918 OPT_PARTATTRS,
1919 OPT_BYTES,
1920 OPT_COLOR,
1921 OPT_MOVEDATA,
1922 OPT_DELETE,
1923 OPT_NOTELL
1924 };
1925
1926 static const struct option longopts[] = {
1927 { "activate",no_argument, NULL, 'A' },
1928 { "append", no_argument, NULL, 'a' },
1929 { "backup", no_argument, NULL, 'b' },
1930 { "backup-file", required_argument, NULL, 'O' },
1931 { "bytes", no_argument, NULL, OPT_BYTES },
1932 { "color", optional_argument, NULL, OPT_COLOR },
1933 { "delete", no_argument, NULL, OPT_DELETE },
1934 { "dump", no_argument, NULL, 'd' },
1935 { "help", no_argument, NULL, 'h' },
1936 { "force", no_argument, NULL, 'f' },
1937 { "json", no_argument, NULL, 'J' },
1938 { "label", required_argument, NULL, 'X' },
1939 { "label-nested", required_argument, NULL, 'Y' },
1940 { "list", no_argument, NULL, 'l' },
1941 { "list-free", no_argument, NULL, 'F' },
1942 { "list-types", no_argument, NULL, 'T' },
1943 { "no-act", no_argument, NULL, 'n' },
1944 { "no-reread", no_argument, NULL, OPT_NOREREAD },
1945 { "no-tell-kernel", no_argument, NULL, OPT_NOTELL },
1946 { "move-data", optional_argument, NULL, OPT_MOVEDATA },
1947 { "output", required_argument, NULL, 'o' },
1948 { "partno", required_argument, NULL, 'N' },
1949 { "reorder", no_argument, NULL, 'r' },
1950 { "show-geometry", no_argument, NULL, 'g' },
1951 { "quiet", no_argument, NULL, 'q' },
1952 { "verify", no_argument, NULL, 'V' },
1953 { "version", no_argument, NULL, 'v' },
1954 { "wipe", required_argument, NULL, 'w' },
1955 { "wipe-partitions", required_argument, NULL, 'W' },
1956
1957 { "part-uuid", no_argument, NULL, OPT_PARTUUID },
1958 { "part-label", no_argument, NULL, OPT_PARTLABEL },
1959 { "part-type", no_argument, NULL, OPT_PARTTYPE },
1960 { "part-attrs", no_argument, NULL, OPT_PARTATTRS },
1961
1962 { "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */
1963 { "unit", required_argument, NULL, 'u' }, /* deprecated */
1964 { "Linux", no_argument, NULL, 'L' }, /* deprecated */
1965 { "show-size", no_argument, NULL, 's' }, /* deprecated */
1966
1967 { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */
1968 { "id", no_argument, NULL, 'c' }, /* deprecated */
1969 { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */
1970
1971 { NULL, 0, NULL, 0 },
1972 };
1973 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1974 { 's','u'}, /* --show-size --unit */
1975 { 0 }
1976 };
1977 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1978
1979
1980 setlocale(LC_ALL, "");
1981 bindtextdomain(PACKAGE, LOCALEDIR);
1982 textdomain(PACKAGE);
1983 atexit(close_stdout);
1984
1985 while ((c = getopt_long(argc, argv, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1986 longopts, &longidx)) != -1) {
1987
1988 err_exclusive_options(c, longopts, excl, excl_st);
1989
1990 switch(c) {
1991 case 'A':
1992 sf->act = ACT_ACTIVATE;
1993 break;
1994 case 'a':
1995 sf->append = 1;
1996 break;
1997 case 'b':
1998 sf->backup = 1;
1999 break;
2000 case OPT_CHANGE_ID:
2001 case OPT_PRINT_ID:
2002 case OPT_ID:
2003 warnx(_("%s is deprecated in favour of --part-type"),
2004 longopts[longidx].name);
2005 sf->act = ACT_PARTTYPE;
2006 break;
2007 case 'c':
2008 warnx(_("--id is deprecated in favour of --part-type"));
2009 sf->act = ACT_PARTTYPE;
2010 break;
2011 case 'J':
2012 sf->json = 1;
2013 /* fallthrough */
2014 case 'd':
2015 sf->act = ACT_DUMP;
2016 break;
2017 case 'F':
2018 sf->act = ACT_LIST_FREE;
2019 break;
2020 case 'f':
2021 sf->force = 1;
2022 break;
2023 case 'G':
2024 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2025 /* fallthrough */
2026 case 'g':
2027 sf->act = ACT_SHOW_GEOM;
2028 break;
2029 case 'h':
2030 usage();
2031 break;
2032 case 'l':
2033 sf->act = ACT_LIST;
2034 break;
2035 case 'L':
2036 warnx(_("--Linux option is unnecessary and deprecated"));
2037 break;
2038 case 'o':
2039 outarg = optarg;
2040 break;
2041 case 'O':
2042 sf->backup = 1;
2043 sf->backup_file = optarg;
2044 break;
2045 case 'n':
2046 sf->noact = 1;
2047 break;
2048 case 'N':
2049 sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1;
2050 break;
2051 case 'q':
2052 sf->quiet = 1;
2053 break;
2054 case 'r':
2055 sf->act = ACT_REORDER;
2056 break;
2057 case 's':
2058 sf->act = ACT_SHOW_SIZE;
2059 break;
2060 case 'T':
2061 sf->act = ACT_LIST_TYPES;
2062 break;
2063 case 'u':
2064 if (*optarg != 'S')
2065 errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg);
2066 break;
2067 case 'v':
2068 printf(_("%s from %s\n"), program_invocation_short_name,
2069 PACKAGE_STRING);
2070 return EXIT_SUCCESS;
2071 case 'V':
2072 sf->verify = 1;
2073 break;
2074 case 'w':
2075 sf->wipemode = wipemode_from_string(optarg);
2076 if (sf->wipemode < 0)
2077 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2078 break;
2079 case 'W':
2080 sf->pwipemode = wipemode_from_string(optarg);
2081 if (sf->pwipemode < 0)
2082 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2083 break;
2084 case 'X':
2085 sf->label = optarg;
2086 break;
2087 case 'Y':
2088 sf->label_nested = optarg;
2089 break;
2090
2091 case OPT_PARTUUID:
2092 sf->act = ACT_PARTUUID;
2093 break;
2094 case OPT_PARTTYPE:
2095 sf->act = ACT_PARTTYPE;
2096 break;
2097 case OPT_PARTLABEL:
2098 sf->act = ACT_PARTLABEL;
2099 break;
2100 case OPT_PARTATTRS:
2101 sf->act = ACT_PARTATTRS;
2102 break;
2103 case OPT_NOREREAD:
2104 sf->noreread = 1;
2105 break;
2106 case OPT_BYTES:
2107 bytes = 1;
2108 break;
2109 case OPT_COLOR:
2110 colormode = UL_COLORMODE_AUTO;
2111 if (optarg)
2112 colormode = colormode_or_err(optarg,
2113 _("unsupported color mode"));
2114 break;
2115 case OPT_MOVEDATA:
2116 sf->movedata = 1;
2117 sf->move_typescript = optarg;
2118 break;
2119 case OPT_DELETE:
2120 sf->act = ACT_DELETE;
2121 break;
2122 case OPT_NOTELL:
2123 sf->notell = 1;
2124 break;
2125 default:
2126 errtryhelp(EXIT_FAILURE);
2127 }
2128 }
2129
2130 colors_init(colormode, "sfdisk");
2131
2132 sfdisk_init(sf);
2133 if (bytes)
2134 fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES);
2135
2136 if (outarg)
2137 init_fields(NULL, outarg, NULL);
2138
2139 if (sf->verify && !sf->act)
2140 sf->act = ACT_VERIFY; /* --verify make be used with --list too */
2141 else if (!sf->act)
2142 sf->act = ACT_FDISK; /* default */
2143
2144 if (sf->movedata && !(sf->act == ACT_FDISK && sf->partno >= 0))
2145 errx(EXIT_FAILURE, _("--movedata requires -N"));
2146
2147 switch (sf->act) {
2148 case ACT_ACTIVATE:
2149 rc = command_activate(sf, argc - optind, argv + optind);
2150 break;
2151
2152 case ACT_DELETE:
2153 rc = command_delete(sf, argc - optind, argv + optind);
2154 break;
2155
2156 case ACT_LIST:
2157 rc = command_list_partitions(sf, argc - optind, argv + optind);
2158 break;
2159
2160 case ACT_LIST_TYPES:
2161 rc = command_list_types(sf);
2162 break;
2163
2164 case ACT_LIST_FREE:
2165 rc = command_list_freespace(sf, argc - optind, argv + optind);
2166 break;
2167
2168 case ACT_FDISK:
2169 rc = command_fdisk(sf, argc - optind, argv + optind);
2170 break;
2171
2172 case ACT_DUMP:
2173 rc = command_dump(sf, argc - optind, argv + optind);
2174 break;
2175
2176 case ACT_SHOW_SIZE:
2177 rc = command_show_size(sf, argc - optind, argv + optind);
2178 break;
2179
2180 case ACT_SHOW_GEOM:
2181 rc = command_show_geometry(sf, argc - optind, argv + optind);
2182 break;
2183
2184 case ACT_VERIFY:
2185 rc = command_verify(sf, argc - optind, argv + optind);
2186 break;
2187
2188 case ACT_PARTTYPE:
2189 rc = command_parttype(sf, argc - optind, argv + optind);
2190 break;
2191
2192 case ACT_PARTUUID:
2193 rc = command_partuuid(sf, argc - optind, argv + optind);
2194 break;
2195
2196 case ACT_PARTLABEL:
2197 rc = command_partlabel(sf, argc - optind, argv + optind);
2198 break;
2199
2200 case ACT_PARTATTRS:
2201 rc = command_partattrs(sf, argc - optind, argv + optind);
2202 break;
2203
2204 case ACT_REORDER:
2205 rc = command_reorder(sf, argc - optind, argv + optind);
2206 break;
2207 }
2208
2209 sfdisk_deinit(sf);
2210
2211 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
2212 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2213 }
2214