]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/sfdisk.c
Merge branch 'betterMinixChecks' of https://github.com/n3world/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 if (!fdisk_has_label(sf->cxt))
954 errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
955
956 dp = fdisk_new_script(sf->cxt);
957 if (!dp)
958 err(EXIT_FAILURE, _("failed to allocate dump struct"));
959
960 rc = fdisk_script_read_context(dp, NULL);
961 if (rc)
962 errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname);
963
964 if (sf->json)
965 fdisk_script_enable_json(dp, 1);
966 fdisk_script_write_file(dp, stdout);
967
968 fdisk_unref_script(dp);
969 fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
970 return 0;
971 }
972
973 static void assign_device_partition(struct sfdisk *sf,
974 const char *devname,
975 size_t partno,
976 int rdonly)
977 {
978 int rc;
979 size_t n;
980 struct fdisk_label *lb = NULL;
981
982 assert(sf);
983 assert(devname);
984
985 /* read-only when a new <type> undefined */
986 rc = fdisk_assign_device(sf->cxt, devname, rdonly);
987 if (rc)
988 err(EXIT_FAILURE, _("cannot open %s"), devname);
989
990 lb = fdisk_get_label(sf->cxt, NULL);
991 if (!lb)
992 errx(EXIT_FAILURE, _("%s: no partition table found"), devname);
993
994 n = fdisk_get_npartitions(sf->cxt);
995 if (partno > n)
996 errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains "
997 "only %zu partitions"), devname, partno, n);
998 if (!fdisk_is_partition_used(sf->cxt, partno - 1))
999 errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"),
1000 devname, partno);
1001 }
1002
1003 /*
1004 * sfdisk --part-type <device> <partno> [<type>]
1005 */
1006 static int command_parttype(struct sfdisk *sf, int argc, char **argv)
1007 {
1008 size_t partno;
1009 struct fdisk_parttype *type = NULL;
1010 struct fdisk_label *lb;
1011 const char *devname = NULL, *typestr = NULL;
1012
1013 if (!argc)
1014 errx(EXIT_FAILURE, _("no disk device specified"));
1015 devname = argv[0];
1016
1017 if (argc < 2)
1018 errx(EXIT_FAILURE, _("no partition number specified"));
1019 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1020
1021 if (argc == 3)
1022 typestr = argv[2];
1023 else if (argc > 3)
1024 errx(EXIT_FAILURE, _("unexpected arguments"));
1025
1026 /* read-only when a new <type> undefined */
1027 assign_device_partition(sf, devname, partno, !typestr);
1028
1029 lb = fdisk_get_label(sf->cxt, NULL);
1030
1031 /* print partition type */
1032 if (!typestr) {
1033 const struct fdisk_parttype *t = NULL;
1034 struct fdisk_partition *pa = NULL;
1035
1036 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1037 t = fdisk_partition_get_type(pa);
1038 if (!t)
1039 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"),
1040 devname, partno);
1041
1042 if (fdisk_label_has_code_parttypes(lb))
1043 printf("%2x\n", fdisk_parttype_get_code(t));
1044 else
1045 printf("%s\n", fdisk_parttype_get_string(t));
1046
1047 fdisk_unref_partition(pa);
1048 fdisk_deassign_device(sf->cxt, 1);
1049 return 0;
1050 }
1051
1052 if (sf->backup)
1053 backup_partition_table(sf, devname);
1054
1055 /* parse <type> and apply to PT */
1056 type = fdisk_label_parse_parttype(lb, typestr);
1057 if (!type)
1058 errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"),
1059 fdisk_label_get_name(lb), typestr);
1060
1061 else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0)
1062 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"),
1063 devname, partno);
1064 fdisk_unref_parttype(type);
1065 return write_changes(sf);
1066 }
1067
1068 /*
1069 * sfdisk --part-uuid <device> <partno> [<uuid>]
1070 */
1071 static int command_partuuid(struct sfdisk *sf, int argc, char **argv)
1072 {
1073 size_t partno;
1074 struct fdisk_partition *pa = NULL;
1075 const char *devname = NULL, *uuid = NULL;
1076
1077 if (!argc)
1078 errx(EXIT_FAILURE, _("no disk device specified"));
1079 devname = argv[0];
1080
1081 if (argc < 2)
1082 errx(EXIT_FAILURE, _("no partition number specified"));
1083 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1084
1085 if (argc == 3)
1086 uuid = argv[2];
1087 else if (argc > 3)
1088 errx(EXIT_FAILURE, _("unexpected arguments"));
1089
1090 /* read-only if uuid not given */
1091 assign_device_partition(sf, devname, partno, !uuid);
1092
1093 /* print partition uuid */
1094 if (!uuid) {
1095 const char *str = NULL;
1096
1097 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1098 str = fdisk_partition_get_uuid(pa);
1099 if (!str)
1100 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"),
1101 devname, partno);
1102 printf("%s\n", str);
1103 fdisk_unref_partition(pa);
1104 fdisk_deassign_device(sf->cxt, 1);
1105 return 0;
1106 }
1107
1108 if (sf->backup)
1109 backup_partition_table(sf, devname);
1110
1111 pa = fdisk_new_partition();
1112 if (!pa)
1113 err(EXIT_FAILURE, _("failed to allocate partition object"));
1114
1115 if (fdisk_partition_set_uuid(pa, uuid) != 0 ||
1116 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1117 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"),
1118 devname, partno);
1119 fdisk_unref_partition(pa);
1120 return write_changes(sf);
1121 }
1122
1123 /*
1124 * sfdisk --part-label <device> <partno> [<label>]
1125 */
1126 static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
1127 {
1128 size_t partno;
1129 struct fdisk_partition *pa = NULL;
1130 const char *devname = NULL, *name = NULL;
1131
1132 if (!argc)
1133 errx(EXIT_FAILURE, _("no disk device specified"));
1134 devname = argv[0];
1135
1136 if (argc < 2)
1137 errx(EXIT_FAILURE, _("no partition number specified"));
1138 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1139
1140 if (argc == 3)
1141 name = argv[2];
1142 else if (argc > 3)
1143 errx(EXIT_FAILURE, _("unexpected arguments"));
1144
1145 /* read-only if name not given */
1146 assign_device_partition(sf, devname, partno, !name);
1147
1148 /* print partition name */
1149 if (!name) {
1150 const char *str = NULL;
1151
1152 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1153 str = fdisk_partition_get_name(pa);
1154 if (!str)
1155 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"),
1156 devname, partno);
1157 printf("%s\n", str);
1158 fdisk_unref_partition(pa);
1159 fdisk_deassign_device(sf->cxt, 1);
1160 return 0;
1161 }
1162
1163 if (sf->backup)
1164 backup_partition_table(sf, devname);
1165
1166 pa = fdisk_new_partition();
1167 if (!pa)
1168 err(EXIT_FAILURE, _("failed to allocate partition object"));
1169
1170 if (fdisk_partition_set_name(pa, name) != 0 ||
1171 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1172 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"),
1173 devname, partno);
1174
1175 fdisk_unref_partition(pa);
1176 return write_changes(sf);
1177 }
1178
1179 /*
1180 * sfdisk --part-attrs <device> <partno> [<attrs>]
1181 */
1182 static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
1183 {
1184 size_t partno;
1185 struct fdisk_partition *pa = NULL;
1186 const char *devname = NULL, *attrs = NULL;
1187
1188 if (!argc)
1189 errx(EXIT_FAILURE, _("no disk device specified"));
1190 devname = argv[0];
1191
1192 if (argc < 2)
1193 errx(EXIT_FAILURE, _("no partition number specified"));
1194 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1195
1196 if (argc == 3)
1197 attrs = argv[2];
1198 else if (argc > 3)
1199 errx(EXIT_FAILURE, _("unexpected arguments"));
1200
1201 /* read-only if name not given */
1202 assign_device_partition(sf, devname, partno, !attrs);
1203
1204 /* print partition name */
1205 if (!attrs) {
1206 const char *str = NULL;
1207
1208 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1209 str = fdisk_partition_get_attrs(pa);
1210 if (str)
1211 printf("%s\n", str);
1212 fdisk_unref_partition(pa);
1213 fdisk_deassign_device(sf->cxt, 1);
1214 return 0;
1215 }
1216
1217 if (sf->backup)
1218 backup_partition_table(sf, devname);
1219
1220 pa = fdisk_new_partition();
1221 if (!pa)
1222 err(EXIT_FAILURE, _("failed to allocate partition object"));
1223
1224 if (fdisk_partition_set_attrs(pa, attrs) != 0 ||
1225 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1226 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"),
1227 devname, partno);
1228
1229 fdisk_unref_partition(pa);
1230 return write_changes(sf);
1231 }
1232
1233 static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
1234 {
1235 struct fdisk_partition *pa = NULL;
1236 char *data;
1237
1238 assert(sf);
1239
1240 if (sf->quiet)
1241 return;
1242 if (fdisk_get_partition(sf->cxt, n, &pa) != 0)
1243 return;
1244
1245 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data);
1246 printf("%12s : ", data);
1247
1248 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data);
1249 printf("%12s ", data);
1250
1251 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data);
1252 printf("%12s ", data);
1253
1254 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data);
1255 printf("(%s) ", data);
1256
1257 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data);
1258 printf("%s\n", data);
1259
1260 fdisk_unref_partition(pa);
1261 }
1262
1263 static void command_fdisk_help(void)
1264 {
1265 fputs(_("\nHelp:\n"), stdout);
1266
1267 fputc('\n', stdout);
1268 color_scheme_enable("help-title", UL_COLOR_BOLD);
1269 fputs(_(" Commands:\n"), stdout);
1270 color_disable();
1271 fputs(_(" write write table to disk and exit\n"), stdout);
1272 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout);
1273 fputs(_(" abort exit sfdisk shell\n"), stdout);
1274 fputs(_(" print display the partition table\n"), stdout);
1275 fputs(_(" help show this help text\n"), stdout);
1276 fputc('\n', stdout);
1277 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout);
1278
1279 fputc('\n', stdout);
1280 color_scheme_enable("help-title", UL_COLOR_BOLD);
1281 fputs(_(" Input format:\n"), stdout);
1282 color_disable();
1283 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout);
1284
1285 fputc('\n', stdout);
1286 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1287 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1288 " The default is the first free space.\n"), stdout);
1289
1290 fputc('\n', stdout);
1291 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1292 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1293 " The default is all available space.\n"), stdout);
1294
1295 fputc('\n', stdout);
1296 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout);
1297 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout);
1298 fputs(_(" GPT: UUID or L,S,H shortcuts.\n"), stdout);
1299
1300 fputc('\n', stdout);
1301 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout);
1302
1303 fputc('\n', stdout);
1304 color_scheme_enable("help-title", UL_COLOR_BOLD);
1305 fputs(_(" Example:\n"), stdout);
1306 color_disable();
1307 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout);
1308 fputc('\n', stdout);
1309 }
1310
1311 enum {
1312 SFDISK_DONE_NONE = 0,
1313 SFDISK_DONE_EOF,
1314 SFDISK_DONE_ABORT,
1315 SFDISK_DONE_WRITE,
1316 SFDISK_DONE_ASK
1317 };
1318
1319 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1320 static int loop_control_commands(struct sfdisk *sf,
1321 struct fdisk_script *dp,
1322 char *buf)
1323 {
1324 const char *p = skip_blank(buf);
1325 int rc = SFDISK_DONE_NONE;
1326
1327 if (strcmp(p, "print") == 0)
1328 list_disklabel(sf->cxt);
1329 else if (strcmp(p, "help") == 0)
1330 command_fdisk_help();
1331 else if (strcmp(p, "quit") == 0)
1332 rc = SFDISK_DONE_ASK;
1333 else if (strcmp(p, "write") == 0)
1334 rc = SFDISK_DONE_WRITE;
1335 else if (strcmp(p, "abort") == 0)
1336 rc = SFDISK_DONE_ABORT;
1337 else {
1338 if (sf->interactive)
1339 fdisk_warnx(sf->cxt, _("unsupported command"));
1340 else {
1341 fdisk_warnx(sf->cxt, _("line %d: unsupported command"),
1342 fdisk_script_get_nlines(dp));
1343 rc = -EINVAL;
1344 }
1345 }
1346 return rc;
1347 }
1348
1349 static int has_container(struct sfdisk *sf)
1350 {
1351 size_t i, nparts;
1352 struct fdisk_partition *pa = NULL;
1353
1354 if (sf->container)
1355 return sf->container;
1356
1357 nparts = fdisk_get_npartitions(sf->cxt);
1358 for (i = 0; i < nparts; i++) {
1359 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
1360 continue;
1361 if (fdisk_partition_is_container(pa)) {
1362 sf->container = 1;
1363 break;
1364 }
1365 }
1366
1367 fdisk_unref_partition(pa);
1368 return sf->container;
1369 }
1370
1371 static size_t last_pt_partno(struct sfdisk *sf)
1372 {
1373 size_t i, nparts, partno = 0;
1374 struct fdisk_partition *pa = NULL;
1375
1376
1377 nparts = fdisk_get_npartitions(sf->cxt);
1378 for (i = 0; i < nparts; i++) {
1379 size_t x;
1380
1381 if (fdisk_get_partition(sf->cxt, i, &pa) != 0 ||
1382 !fdisk_partition_is_used(pa))
1383 continue;
1384 x = fdisk_partition_get_partno(pa);
1385 if (x > partno)
1386 partno = x;
1387 }
1388
1389 fdisk_unref_partition(pa);
1390 return partno;
1391 }
1392
1393 #ifdef BLKRRPART
1394 static int is_device_used(struct sfdisk *sf)
1395 {
1396 struct stat st;
1397 int fd;
1398
1399 assert(sf);
1400 assert(sf->cxt);
1401
1402 fd = fdisk_get_devfd(sf->cxt);
1403 if (fd < 0)
1404 return 0;
1405
1406 if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)
1407 && major(st.st_rdev) != LOOPDEV_MAJOR)
1408 return ioctl(fd, BLKRRPART) != 0;
1409 return 0;
1410 }
1411 #else
1412 static int is_device_used(struct sfdisk *sf __attribute__((__unused__)))
1413 {
1414 return 0;
1415 }
1416 #endif
1417
1418 #ifdef HAVE_LIBREADLINE
1419 static char *sfdisk_fgets(struct fdisk_script *dp,
1420 char *buf, size_t bufsz, FILE *f)
1421 {
1422 struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp);
1423
1424 assert(dp);
1425 assert(buf);
1426 assert(bufsz > 2);
1427
1428 if (sf->interactive) {
1429 char *p = readline(sf->prompt);
1430 size_t len;
1431
1432 if (!p)
1433 return NULL;
1434 len = strlen(p);
1435 if (len > bufsz - 2)
1436 len = bufsz - 2;
1437
1438 memcpy(buf, p, len);
1439 buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
1440 buf[len + 1] = '\0';
1441 free(p);
1442 fflush(stdout);
1443 return buf;
1444 }
1445 return fgets(buf, bufsz, f);
1446 }
1447 #endif
1448
1449 static int ignore_partition(struct fdisk_partition *pa)
1450 {
1451 /* incomplete partition setting */
1452 if (!fdisk_partition_has_start(pa) && !fdisk_partition_start_is_default(pa))
1453 return 1;
1454 if (!fdisk_partition_has_size(pa) && !fdisk_partition_end_is_default(pa))
1455 return 1;
1456
1457 /* probably dump from old sfdisk with start=0 size=0 */
1458 if (fdisk_partition_has_start(pa) && fdisk_partition_get_start(pa) == 0 &&
1459 fdisk_partition_has_size(pa) && fdisk_partition_get_size(pa) == 0)
1460 return 1;
1461
1462 return 0;
1463 }
1464
1465 static int wipe_partition(struct sfdisk *sf, size_t partno)
1466 {
1467 int rc, yes = 0;
1468 char *fstype = NULL;;
1469 struct fdisk_partition *tmp = NULL;
1470
1471 DBG(MISC, ul_debug("checking for signature"));
1472
1473 rc = fdisk_get_partition(sf->cxt, partno, &tmp);
1474 if (rc)
1475 goto done;
1476
1477 rc = fdisk_partition_to_string(tmp, sf->cxt, FDISK_FIELD_FSTYPE, &fstype);
1478 if (rc || fstype == NULL)
1479 goto done;
1480
1481 fdisk_warnx(sf->cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
1482
1483 if (sf->pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
1484 fdisk_ask_yesno(sf->cxt, _("Do you want to remove the signature?"), &yes);
1485 else if (sf->pwipemode == WIPEMODE_ALWAYS)
1486 yes = 1;
1487
1488 if (yes) {
1489 fdisk_info(sf->cxt, _("The signature will be removed by a write command."));
1490 rc = fdisk_wipe_partition(sf->cxt, partno, TRUE);
1491 }
1492 done:
1493 fdisk_unref_partition(tmp);
1494 free(fstype);
1495 DBG(MISC, ul_debug("partition wipe check end [rc=%d]", rc));
1496 return rc;
1497 }
1498
1499 static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname,
1500 size_t next_partno, int created)
1501 {
1502 if (created) {
1503 char *partname = fdisk_partname(devname, next_partno + 1);
1504 if (!partname)
1505 err(EXIT_FAILURE, _("failed to allocate partition name"));
1506
1507 if (!sf->prompt || !startswith(sf->prompt, partname)) {
1508 free(sf->prompt);
1509 xasprintf(&sf->prompt,"%s: ", partname);
1510 }
1511 free(partname);
1512 } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
1513 free(sf->prompt);
1514 sf->prompt = xstrdup(SFDISK_PROMPT);
1515 }
1516 }
1517
1518 /*
1519 * sfdisk <device> [[-N] <partno>]
1520 *
1521 * Note that the option -N is there for backward compatibility only.
1522 */
1523 static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
1524 {
1525 int rc = 0, partno = sf->partno, created = 0, unused = 0;
1526 struct fdisk_script *dp;
1527 struct fdisk_table *tb = NULL;
1528 const char *devname = NULL, *label;
1529 char buf[BUFSIZ];
1530 size_t next_partno = (size_t) -1;
1531
1532 if (argc)
1533 devname = argv[0];
1534 if (partno < 0 && argc > 1)
1535 partno = strtou32_or_err(argv[1],
1536 _("failed to parse partition number"));
1537 if (!devname)
1538 errx(EXIT_FAILURE, _("no disk device specified"));
1539
1540 rc = fdisk_assign_device(sf->cxt, devname, 0);
1541 if (rc)
1542 err(EXIT_FAILURE, _("cannot open %s"), devname);
1543
1544 dp = fdisk_new_script(sf->cxt);
1545 if (!dp)
1546 err(EXIT_FAILURE, _("failed to allocate script handler"));
1547 fdisk_set_script(sf->cxt, dp);
1548 #ifdef HAVE_LIBREADLINE
1549 fdisk_script_set_fgets(dp, sfdisk_fgets);
1550 #endif
1551 fdisk_script_set_userdata(dp, (void *) sf);
1552
1553 /*
1554 * Don't create a new disklabel when [-N] <partno> specified. In this
1555 * case reuse already specified disklabel. Let's check that the disk
1556 * really contains the partition.
1557 */
1558 if (partno >= 0) {
1559 size_t n;
1560
1561 if (!fdisk_has_label(sf->cxt))
1562 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1563 "no partition table was found"),
1564 devname, partno + 1);
1565 n = fdisk_get_npartitions(sf->cxt);
1566 if ((size_t) partno > n)
1567 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1568 "partition table contains only %zu "
1569 "partitions"),
1570 devname, partno + 1, n);
1571
1572 if (!fdisk_is_partition_used(sf->cxt, partno)) {
1573 fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"),
1574 devname, partno + 1);
1575 unused = 1;
1576 }
1577 created = 1;
1578 next_partno = partno;
1579
1580 if (sf->movedata)
1581 sf->orig_pa = get_partition(sf->cxt, partno);
1582 }
1583
1584 if (sf->append) {
1585 created = 1;
1586 next_partno = last_pt_partno(sf) + 1;
1587 }
1588
1589 if (!sf->quiet && sf->interactive) {
1590 color_scheme_enable("welcome", UL_COLOR_GREEN);
1591 fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING);
1592 color_disable();
1593 fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n"
1594 "Be careful before using the write command.\n"));
1595 }
1596
1597 if (!sf->noact && !sf->noreread) {
1598 if (!sf->quiet)
1599 fputs(_("Checking that no-one is using this disk right now ..."), stdout);
1600 if (is_device_used(sf)) {
1601 if (!sf->quiet)
1602 fputs(_(" FAILED\n\n"), stdout);
1603
1604 fdisk_warnx(sf->cxt, _(
1605 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1606 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1607 "Use the --no-reread flag to suppress this check.\n"));
1608
1609 if (!sf->force)
1610 errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
1611 } else if (!sf->quiet)
1612 fputs(_(" OK\n\n"), stdout);
1613 }
1614
1615 if (fdisk_get_collision(sf->cxt)) {
1616 int dowipe = sf->wipemode == WIPEMODE_ALWAYS ? 1 : 0;
1617
1618 fdisk_warnx(sf->cxt, _("Device %s already contains a %s signature."),
1619 devname, fdisk_get_collision(sf->cxt));
1620
1621 if (sf->interactive && sf->wipemode == WIPEMODE_AUTO)
1622 dowipe = 1; /* do it in interactive mode */
1623
1624 fdisk_enable_wipe(sf->cxt, dowipe);
1625 if (dowipe)
1626 fdisk_warnx(sf->cxt, _(
1627 "The signature will be removed by a write command."));
1628 else
1629 fdisk_warnx(sf->cxt, _(
1630 "It is strongly recommended to wipe the device with "
1631 "wipefs(8), in order to avoid possible collisions."));
1632 fputc('\n', stderr);
1633 }
1634
1635 if (sf->backup)
1636 backup_partition_table(sf, devname);
1637
1638 if (!sf->quiet) {
1639 list_disk_geometry(sf->cxt);
1640 if (fdisk_has_label(sf->cxt)) {
1641 fdisk_info(sf->cxt, _("\nOld situation:"));
1642 list_disklabel(sf->cxt);
1643 }
1644 }
1645
1646 if (sf->label)
1647 label = sf->label;
1648 else if (fdisk_has_label(sf->cxt))
1649 label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
1650 else
1651 label = "dos"; /* just for backward compatibility */
1652
1653 fdisk_script_set_header(dp, "label", label);
1654
1655
1656 if (!sf->quiet && sf->interactive) {
1657 if (!fdisk_has_label(sf->cxt) && !sf->label)
1658 fdisk_info(sf->cxt,
1659 _("\nsfdisk is going to create a new '%s' disk label.\n"
1660 "Use 'label: <name>' before you define a first partition\n"
1661 "to override the default."), label);
1662 fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n"));
1663 } else if (!sf->quiet)
1664 fputc('\n', stdout);
1665
1666 tb = fdisk_script_get_table(dp);
1667 assert(tb);
1668
1669 do {
1670 size_t nparts;
1671
1672 DBG(PARSE, ul_debug("<---next-line--->"));
1673 if (next_partno == (size_t) -1)
1674 next_partno = fdisk_table_get_nents(tb);
1675
1676 if (created
1677 && partno < 0
1678 && next_partno == fdisk_get_npartitions(sf->cxt)
1679 && !has_container(sf)) {
1680 fdisk_info(sf->cxt, _("All partitions used."));
1681 rc = SFDISK_DONE_ASK;
1682 break;
1683 }
1684
1685 refresh_prompt_buffer(sf, devname, next_partno, created);
1686 if (sf->prompt && (sf->interactive || !sf->quiet)) {
1687 #ifndef HAVE_LIBREADLINE
1688 fputs(sf->prompt, stdout);
1689 #else
1690 if (!sf->interactive)
1691 fputs(sf->prompt, stdout);
1692 #endif
1693 }
1694
1695 rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
1696 if (rc < 0) {
1697 DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
1698 buf[sizeof(buf) - 1] = '\0';
1699 rc = loop_control_commands(sf, dp, buf);
1700 if (rc)
1701 break;
1702 continue;
1703 } else if (rc == 1) {
1704 rc = SFDISK_DONE_EOF;
1705 fputs(_("Done.\n"), stdout);
1706 break;
1707 }
1708
1709 nparts = fdisk_table_get_nents(tb);
1710 if (nparts) {
1711 size_t cur_partno;
1712 struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1);
1713
1714 assert(pa);
1715
1716 if (ignore_partition(pa)) {
1717 fdisk_info(sf->cxt, _("Ignoring partition."));
1718 next_partno++;
1719 continue;
1720 }
1721 if (!created) { /* create a new disklabel */
1722 rc = fdisk_apply_script_headers(sf->cxt, dp);
1723 created = !rc;
1724 if (rc)
1725 fdisk_warnx(sf->cxt, _(
1726 "Failed to apply script headers, "
1727 "disk label not created."));
1728 }
1729 if (!rc && partno >= 0) { /* -N <partno>, modify partition */
1730 rc = fdisk_set_partition(sf->cxt, partno, pa);
1731 rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT;
1732 break;
1733 } else if (!rc) { /* add partition */
1734 if (!sf->interactive &&
1735 (!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) {
1736 refresh_prompt_buffer(sf, devname, next_partno, created);
1737 fputs(sf->prompt, stdout);
1738 }
1739 rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
1740 if (rc) {
1741 errno = -rc;
1742 fdisk_warn(sf->cxt, _("Failed to add partition"));
1743
1744 }
1745 }
1746
1747 /* wipe partition on success
1748 *
1749 * Note that unused=1 means -N <partno> for unused,
1750 * otherwise we wipe only newly created partitions.
1751 */
1752 if (rc == 0 && (unused || partno < 0)) {
1753 rc = wipe_partition(sf, unused ? (size_t) partno : cur_partno);
1754 if (rc)
1755 errno = -rc;
1756 }
1757
1758 if (!rc) {
1759 /* success print result */
1760 if (sf->interactive)
1761 sfdisk_print_partition(sf, cur_partno);
1762 next_partno = cur_partno + 1;
1763 } else if (pa) /* error, drop partition from script */
1764 fdisk_table_remove_partition(tb, pa);
1765 } else
1766 fdisk_info(sf->cxt, _("Script header accepted."));
1767
1768 if (rc && !sf->interactive) {
1769 rc = SFDISK_DONE_ABORT;
1770 break;
1771 }
1772 } while (1);
1773
1774 /* create empty disk label if label, but no partition specified */
1775 if (rc == SFDISK_DONE_EOF && created == 0
1776 && fdisk_script_has_force_label(dp) == 1
1777 && fdisk_table_get_nents(tb) == 0
1778 && fdisk_script_get_header(dp, "label")) {
1779
1780 int xrc = fdisk_apply_script_headers(sf->cxt, dp);
1781 created = !xrc;
1782 if (xrc) {
1783 fdisk_warnx(sf->cxt, _(
1784 "Failed to apply script headers, "
1785 "disk label not created."));
1786 rc = SFDISK_DONE_ABORT;
1787 }
1788 }
1789
1790 if (!sf->quiet && rc != SFDISK_DONE_ABORT) {
1791 fdisk_info(sf->cxt, _("\nNew situation:"));
1792 list_disk_identifier(sf->cxt);
1793 list_disklabel(sf->cxt);
1794 }
1795
1796 switch (rc) {
1797 case SFDISK_DONE_ASK:
1798 case SFDISK_DONE_EOF:
1799 if (sf->interactive) {
1800 int yes = 0;
1801 fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes);
1802 if (!yes) {
1803 fdisk_info(sf->cxt, _("Leaving."));
1804 rc = 0;
1805 break;
1806 }
1807 }
1808 case SFDISK_DONE_WRITE:
1809 rc = write_changes(sf);
1810 break;
1811 case SFDISK_DONE_ABORT:
1812 default: /* rc < 0 on error */
1813 fdisk_info(sf->cxt, _("Leaving.\n"));
1814 break;
1815 }
1816
1817 fdisk_unref_script(dp);
1818 return rc;
1819 }
1820
1821 static void __attribute__ ((__noreturn__)) usage(FILE *out)
1822 {
1823 fputs(USAGE_HEADER, out);
1824
1825 fprintf(out,
1826 _(" %1$s [options] <dev> [[-N] <part>]\n"
1827 " %1$s [options] <command>\n"), program_invocation_short_name);
1828
1829 fputs(USAGE_SEPARATOR, out);
1830 fputs(_("Display or manipulate a disk partition table.\n"), out);
1831
1832 fputs(_("\nCommands:\n"), out);
1833 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out);
1834 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out);
1835 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out);
1836 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
1837 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
1838 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out);
1839 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out);
1840 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
1841 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
1842 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out);
1843 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out);
1844
1845 fputs(USAGE_SEPARATOR, out);
1846 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
1847 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
1848 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
1849 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
1850
1851 fputs(USAGE_SEPARATOR, out);
1852 fputs(_(" <dev> device (usually disk) path\n"), out);
1853 fputs(_(" <part> partition number\n"), out);
1854 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
1855
1856 fputs(USAGE_OPTIONS, out);
1857 fputs(_(" -a, --append append partitions to existing partition table\n"), out);
1858 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out);
1859 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out);
1860 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out);
1861 fputs(_(" -f, --force disable all consistency checking\n"), out);
1862 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out);
1863 fprintf(out,
1864 " %s\n", USAGE_COLORS_DEFAULT);
1865 fputs(_(" -N, --partno <num> specify partition number\n"), out);
1866 fputs(_(" -n, --no-act do everything except write to device\n"), out);
1867 fputs(_(" --no-reread do not check whether the device is in use\n"), out);
1868 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out);
1869 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out);
1870 fputs(_(" -o, --output <list> output columns\n"), out);
1871 fputs(_(" -q, --quiet suppress extra info messages\n"), out);
1872 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out);
1873 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out);
1874 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
1875 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out);
1876 fputs(USAGE_SEPARATOR, out);
1877 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out);
1878 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out);
1879 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
1880
1881 fputs(USAGE_SEPARATOR, out);
1882 fputs(USAGE_HELP, out);
1883 fputs(_(" -v, --version output version information and exit\n"), out);
1884
1885 list_available_columns(out);
1886
1887 fprintf(out, USAGE_MAN_TAIL("sfdisk(8)"));
1888 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1889 }
1890
1891
1892 int main(int argc, char *argv[])
1893 {
1894 const char *outarg = NULL;
1895 int rc = -EINVAL, c, longidx = -1, bytes = 0;
1896 int colormode = UL_COLORMODE_UNDEF;
1897 struct sfdisk _sf = {
1898 .partno = -1,
1899 .wipemode = WIPEMODE_AUTO,
1900 .pwipemode = WIPEMODE_AUTO,
1901 .interactive = isatty(STDIN_FILENO) ? 1 : 0,
1902 }, *sf = &_sf;
1903
1904 enum {
1905 OPT_CHANGE_ID = CHAR_MAX + 1,
1906 OPT_PRINT_ID,
1907 OPT_ID,
1908 OPT_NOREREAD,
1909 OPT_PARTUUID,
1910 OPT_PARTLABEL,
1911 OPT_PARTTYPE,
1912 OPT_PARTATTRS,
1913 OPT_BYTES,
1914 OPT_COLOR,
1915 OPT_MOVEDATA,
1916 OPT_DELETE,
1917 OPT_NOTELL
1918 };
1919
1920 static const struct option longopts[] = {
1921 { "activate",no_argument, NULL, 'A' },
1922 { "append", no_argument, NULL, 'a' },
1923 { "backup", no_argument, NULL, 'b' },
1924 { "backup-file", required_argument, NULL, 'O' },
1925 { "bytes", no_argument, NULL, OPT_BYTES },
1926 { "color", optional_argument, NULL, OPT_COLOR },
1927 { "delete", no_argument, NULL, OPT_DELETE },
1928 { "dump", no_argument, NULL, 'd' },
1929 { "help", no_argument, NULL, 'h' },
1930 { "force", no_argument, NULL, 'f' },
1931 { "json", no_argument, NULL, 'J' },
1932 { "label", required_argument, NULL, 'X' },
1933 { "label-nested", required_argument, NULL, 'Y' },
1934 { "list", no_argument, NULL, 'l' },
1935 { "list-free", no_argument, NULL, 'F' },
1936 { "list-types", no_argument, NULL, 'T' },
1937 { "no-act", no_argument, NULL, 'n' },
1938 { "no-reread", no_argument, NULL, OPT_NOREREAD },
1939 { "no-tell-kernel", no_argument, NULL, OPT_NOTELL },
1940 { "move-data", optional_argument, NULL, OPT_MOVEDATA },
1941 { "output", required_argument, NULL, 'o' },
1942 { "partno", required_argument, NULL, 'N' },
1943 { "reorder", no_argument, NULL, 'r' },
1944 { "show-size", no_argument, NULL, 's' },
1945 { "show-geometry", no_argument, NULL, 'g' },
1946 { "quiet", no_argument, NULL, 'q' },
1947 { "verify", no_argument, NULL, 'V' },
1948 { "version", no_argument, NULL, 'v' },
1949 { "wipe", required_argument, NULL, 'w' },
1950 { "wipe-partitions", required_argument, NULL, 'W' },
1951
1952 { "part-uuid", no_argument, NULL, OPT_PARTUUID },
1953 { "part-label", no_argument, NULL, OPT_PARTLABEL },
1954 { "part-type", no_argument, NULL, OPT_PARTTYPE },
1955 { "part-attrs", no_argument, NULL, OPT_PARTATTRS },
1956
1957 { "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */
1958 { "unit", required_argument, NULL, 'u' },
1959 { "Linux", no_argument, NULL, 'L' }, /* deprecated */
1960
1961 { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */
1962 { "id", no_argument, NULL, 'c' }, /* deprecated */
1963 { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */
1964
1965 { NULL, 0, 0, 0 },
1966 };
1967
1968 setlocale(LC_ALL, "");
1969 bindtextdomain(PACKAGE, LOCALEDIR);
1970 textdomain(PACKAGE);
1971 atexit(close_stdout);
1972
1973 while ((c = getopt_long(argc, argv, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1974 longopts, &longidx)) != -1) {
1975 switch(c) {
1976 case 'A':
1977 sf->act = ACT_ACTIVATE;
1978 break;
1979 case 'a':
1980 sf->append = 1;
1981 break;
1982 case 'b':
1983 sf->backup = 1;
1984 break;
1985 case OPT_CHANGE_ID:
1986 case OPT_PRINT_ID:
1987 case OPT_ID:
1988 warnx(_("%s is deprecated in favour of --part-type"),
1989 longopts[longidx].name);
1990 sf->act = ACT_PARTTYPE;
1991 break;
1992 case 'c':
1993 warnx(_("--id is deprecated in favour of --part-type"));
1994 sf->act = ACT_PARTTYPE;
1995 break;
1996 case 'J':
1997 sf->json = 1;
1998 /* fallthrough */
1999 case 'd':
2000 sf->act = ACT_DUMP;
2001 break;
2002 case 'F':
2003 sf->act = ACT_LIST_FREE;
2004 break;
2005 case 'f':
2006 sf->force = 1;
2007 break;
2008 case 'G':
2009 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2010 case 'g':
2011 sf->act = ACT_SHOW_GEOM;
2012 break;
2013 case 'h':
2014 usage(stdout);
2015 break;
2016 case 'l':
2017 sf->act = ACT_LIST;
2018 break;
2019 case 'L':
2020 warnx(_("--Linux option is unnecessary and deprecated"));
2021 break;
2022 case 'o':
2023 outarg = optarg;
2024 break;
2025 case 'O':
2026 sf->backup = 1;
2027 sf->backup_file = optarg;
2028 break;
2029 case 'n':
2030 sf->noact = 1;
2031 break;
2032 case 'N':
2033 sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1;
2034 break;
2035 case 'q':
2036 sf->quiet = 1;
2037 break;
2038 case 'r':
2039 sf->act = ACT_REORDER;
2040 break;
2041 case 's':
2042 sf->act = ACT_SHOW_SIZE;
2043 break;
2044 case 'T':
2045 sf->act = ACT_LIST_TYPES;
2046 break;
2047 case 'u':
2048 if (*optarg != 'S')
2049 errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg);
2050 break;
2051 case 'v':
2052 printf(_("%s from %s\n"), program_invocation_short_name,
2053 PACKAGE_STRING);
2054 return EXIT_SUCCESS;
2055 case 'V':
2056 sf->verify = 1;
2057 break;
2058 case 'w':
2059 sf->wipemode = wipemode_from_string(optarg);
2060 if (sf->wipemode < 0)
2061 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2062 break;
2063 case 'W':
2064 sf->pwipemode = wipemode_from_string(optarg);
2065 if (sf->pwipemode < 0)
2066 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2067 break;
2068 case 'X':
2069 sf->label = optarg;
2070 break;
2071 case 'Y':
2072 sf->label_nested = optarg;
2073 break;
2074
2075 case OPT_PARTUUID:
2076 sf->act = ACT_PARTUUID;
2077 break;
2078 case OPT_PARTTYPE:
2079 sf->act = ACT_PARTTYPE;
2080 break;
2081 case OPT_PARTLABEL:
2082 sf->act = ACT_PARTLABEL;
2083 break;
2084 case OPT_PARTATTRS:
2085 sf->act = ACT_PARTATTRS;
2086 break;
2087 case OPT_NOREREAD:
2088 sf->noreread = 1;
2089 break;
2090 case OPT_BYTES:
2091 bytes = 1;
2092 break;
2093 case OPT_COLOR:
2094 colormode = UL_COLORMODE_AUTO;
2095 if (optarg)
2096 colormode = colormode_or_err(optarg,
2097 _("unsupported color mode"));
2098 break;
2099 case OPT_MOVEDATA:
2100 sf->movedata = 1;
2101 sf->move_typescript = optarg;
2102 break;
2103 case OPT_DELETE:
2104 sf->act = ACT_DELETE;
2105 break;
2106 case OPT_NOTELL:
2107 sf->notell = 1;
2108 break;
2109 default:
2110 errtryhelp(EXIT_FAILURE);
2111 }
2112 }
2113
2114 colors_init(colormode, "sfdisk");
2115
2116 sfdisk_init(sf);
2117 if (bytes)
2118 fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES);
2119
2120 if (outarg)
2121 init_fields(NULL, outarg, NULL);
2122
2123 if (sf->verify && !sf->act)
2124 sf->act = ACT_VERIFY; /* --verify make be used with --list too */
2125 else if (!sf->act)
2126 sf->act = ACT_FDISK; /* default */
2127
2128 if (sf->movedata && !(sf->act == ACT_FDISK && sf->partno >= 0))
2129 errx(EXIT_FAILURE, _("--movedata requires -N"));
2130
2131 switch (sf->act) {
2132 case ACT_ACTIVATE:
2133 rc = command_activate(sf, argc - optind, argv + optind);
2134 break;
2135
2136 case ACT_DELETE:
2137 rc = command_delete(sf, argc - optind, argv + optind);
2138 break;
2139
2140 case ACT_LIST:
2141 rc = command_list_partitions(sf, argc - optind, argv + optind);
2142 break;
2143
2144 case ACT_LIST_TYPES:
2145 rc = command_list_types(sf);
2146 break;
2147
2148 case ACT_LIST_FREE:
2149 rc = command_list_freespace(sf, argc - optind, argv + optind);
2150 break;
2151
2152 case ACT_FDISK:
2153 rc = command_fdisk(sf, argc - optind, argv + optind);
2154 break;
2155
2156 case ACT_DUMP:
2157 rc = command_dump(sf, argc - optind, argv + optind);
2158 break;
2159
2160 case ACT_SHOW_SIZE:
2161 rc = command_show_size(sf, argc - optind, argv + optind);
2162 break;
2163
2164 case ACT_SHOW_GEOM:
2165 rc = command_show_geometry(sf, argc - optind, argv + optind);
2166 break;
2167
2168 case ACT_VERIFY:
2169 rc = command_verify(sf, argc - optind, argv + optind);
2170 break;
2171
2172 case ACT_PARTTYPE:
2173 rc = command_parttype(sf, argc - optind, argv + optind);
2174 break;
2175
2176 case ACT_PARTUUID:
2177 rc = command_partuuid(sf, argc - optind, argv + optind);
2178 break;
2179
2180 case ACT_PARTLABEL:
2181 rc = command_partlabel(sf, argc - optind, argv + optind);
2182 break;
2183
2184 case ACT_PARTATTRS:
2185 rc = command_partattrs(sf, argc - optind, argv + optind);
2186 break;
2187
2188 case ACT_REORDER:
2189 rc = command_reorder(sf, argc - optind, argv + optind);
2190 break;
2191 }
2192
2193 sfdisk_deinit(sf);
2194
2195 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
2196 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2197 }
2198