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