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