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