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