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