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