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