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