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