]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/sfdisk.c
blkid: remove extra trailing spaces in output
[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>
11ef0c35
KZ
33
34#include "c.h"
109dbc4f 35#include "xalloc.h"
1881390d
KZ
36#include "nls.h"
37#include "debug.h"
9912f01b 38#include "strutils.h"
1881390d 39#include "closestream.h"
9c1f9dd3 40#include "colors.h"
d2eb1457 41#include "blkdev.h"
ab02d87e 42#include "all-io.h"
fd6b7a7f 43
1881390d 44#include "libfdisk.h"
9c1f9dd3 45#include "fdisk-list.h"
fd6b7a7f
KZ
46
47/*
1881390d 48 * sfdisk debug stuff (see fdisk.h and include/debug.h)
fd6b7a7f 49 */
1881390d
KZ
50UL_DEBUG_DEFINE_MASK(sfdisk);
51UL_DEBUG_DEFINE_MASKANEMS(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
fd6b7a7f 52
1881390d
KZ
53#define SFDISKPROG_DEBUG_INIT (1 << 1)
54#define SFDISKPROG_DEBUG_PARSE (1 << 2)
55#define SFDISKPROG_DEBUG_MISC (1 << 3)
e8813494 56#define SFDISKPROG_DEBUG_ASK (1 << 4)
1881390d 57#define SFDISKPROG_DEBUG_ALL 0xFFFF
fd6b7a7f 58
1881390d
KZ
59#define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
60#define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
fd6b7a7f 61
9c1f9dd3 62enum {
d2c47697 63 ACT_FDISK = 1,
9c1f9dd3
KZ
64 ACT_ACTIVATE,
65 ACT_CHANGE_ID,
66 ACT_DUMP,
67 ACT_LIST,
9c1f9dd3 68 ACT_LIST_TYPES,
d2eb1457 69 ACT_SHOW_SIZE,
d420a7f9 70 ACT_SHOW_GEOM,
8eab3194
KZ
71 ACT_VERIFY,
72 ACT_PARTTYPE,
351fad50 73 ACT_PARTUUID,
e36fb07a 74 ACT_PARTLABEL,
bc9e8547 75 ACT_PARTATTRS,
9c1f9dd3
KZ
76};
77
78struct sfdisk {
3692c28d 79 int act; /* ACT_* */
7324f1bf
KZ
80 int partno; /* -N <partno>, default -1 */
81 const char *label; /* --label <label> */
e1422de3 82 const char *label_nested; /* --label-nested <label> */
ab02d87e 83 const char *backup_file; /* -O <path> */
9c1f9dd3
KZ
84
85 struct fdisk_context *cxt; /* libfdisk context */
d2c47697 86
f0edb076 87 unsigned int verify : 1, /* call fdisk_verify_disklabel() */
f01f2528 88 quiet : 1, /* suppres extra messages */
8be199ea 89 interactive : 1, /* running on tty */
db48b6a1
KZ
90 noreread : 1, /* don't check device is in use */
91 force : 1, /* do also stupid things */
ab02d87e 92 backup : 1, /* backup sectors before write PT */
e54b1c6f 93 container : 1, /* PT contains container (MBR extended) partitions */
347a7f77 94 append : 1, /* don't create new PT, append partitions only */
f01f2528 95 noact : 1; /* do not write to device */
9c1f9dd3
KZ
96};
97
fd6b7a7f 98
1881390d
KZ
99static void sfdiskprog_init_debug(void)
100{
101 __UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
fd6b7a7f
KZ
102}
103
e8813494
KZ
104
105static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
106{
107 char *p;
108 size_t sz;
109
110 fputs(prompt, stdout);
111 fflush(stdout);
112
113 if (!fgets(buf, bufsz, stdin))
114 return 1;
115
116 for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
117
118 if (p > buf)
119 memmove(buf, p, p - buf); /* remove blank space */
120 sz = strlen(buf);
121 if (sz && *(buf + sz - 1) == '\n')
122 *(buf + sz - 1) = '\0';
123
124 DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
125 return 0;
126}
127
f0edb076
KZ
128static int ask_callback(struct fdisk_context *cxt,
129 struct fdisk_ask *ask,
130 void *data)
9c1f9dd3 131{
f0edb076 132 struct sfdisk *sf = (struct sfdisk *) data;
e8813494
KZ
133 int rc = 0;
134
9c1f9dd3
KZ
135 assert(cxt);
136 assert(ask);
137
138 switch(fdisk_ask_get_type(ask)) {
139 case FDISK_ASKTYPE_INFO:
f0edb076
KZ
140 if (sf->quiet)
141 break;
9c1f9dd3
KZ
142 fputs(fdisk_ask_print_get_mesg(ask), stdout);
143 fputc('\n', stdout);
144 break;
145 case FDISK_ASKTYPE_WARNX:
146 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
147 fputs(fdisk_ask_print_get_mesg(ask), stderr);
148 color_fdisable(stderr);
149 fputc('\n', stderr);
150 break;
151 case FDISK_ASKTYPE_WARN:
152 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
153 fputs(fdisk_ask_print_get_mesg(ask), stderr);
154 errno = fdisk_ask_print_get_errno(ask);
155 fprintf(stderr, ": %m\n");
156 color_fdisable(stderr);
157 break;
e8813494
KZ
158 case FDISK_ASKTYPE_YESNO:
159 {
160 char buf[BUFSIZ];
161 fputc('\n', stdout);
162 do {
163 int x;
164 fputs(fdisk_ask_get_query(ask), stdout);
165 rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf));
166 if (rc)
167 break;
168 x = rpmatch(buf);
169 if (x == 1 || x == 0) {
170 fdisk_ask_yesno_set_result(ask, x);
171 break;
172 }
173 } while(1);
174 DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
175 break;
176 }
9c1f9dd3
KZ
177 default:
178 break;
179 }
e8813494 180 return rc;
9c1f9dd3
KZ
181}
182
183static void sfdisk_init(struct sfdisk *sf)
184{
185 fdisk_init_debug(0);
186 sfdiskprog_init_debug();
187
188 colors_init(UL_COLORMODE_UNDEF, "sfdisk");
189
190 sf->cxt = fdisk_new_context();
191 if (!sf->cxt)
192 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
f0edb076 193 fdisk_set_ask(sf->cxt, ask_callback, (void *) sf);
e1422de3
KZ
194
195 if (sf->label_nested) {
196 struct fdisk_context *x = fdisk_new_nested_context(sf->cxt,
197 sf->label_nested);
198 if (!x)
199 err(EXIT_FAILURE, _("failed to allocate nested libfdisk context"));
200 /* the original context is available by fdisk_get_parent() */
201 sf->cxt = x;
202 }
9c1f9dd3
KZ
203}
204
205static int sfdisk_deinit(struct sfdisk *sf)
206{
e1422de3 207 struct fdisk_context *parent;
9c1f9dd3
KZ
208 int rc;
209
210 assert(sf);
211 assert(sf->cxt);
212
e1422de3
KZ
213 parent = fdisk_get_parent(sf->cxt);
214 if (parent) {
215 fdisk_unref_context(sf->cxt);
216 sf->cxt = parent;
217 }
218
9c1f9dd3
KZ
219 fdisk_unref_context(sf->cxt);
220 memset(sf, 0, sizeof(*sf));
221
222 return rc;
223}
224
ab02d87e
KZ
225static void backup_sectors(struct sfdisk *sf,
226 const char *tpl,
227 const char *name,
228 const char *devname,
229 off_t offset, size_t size)
230{
231 char *fname;
232 int fd, devfd;
233
234 devfd = fdisk_get_devfd(sf->cxt);
235 assert(devfd >= 0);
236
237 xasprintf(&fname, "%s0x%08jx.bak", tpl, offset);
238
239 fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
240 if (fd < 0)
241 goto fail;
242
243 if (lseek(devfd, offset, SEEK_SET) == (off_t) -1) {
244 fdisk_warn(sf->cxt, _("cannot seek %s"), devname);
245 goto fail;
246 } else {
247 unsigned char *buf = xmalloc(size);
248
249 if (read_all(devfd, (char *) buf, size) != (ssize_t) size) {
250 fdisk_warn(sf->cxt, _("cannot read %s"), devname);
251 goto fail;
252 }
253 if (write_all(fd, buf, size) != 0) {
254 fdisk_warn(sf->cxt, _("cannot write %s"), fname);
255 goto fail;
256 }
257 free(buf);
258 }
259
260 fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"),
261 name, (uintmax_t) offset, (uintmax_t) size, fname);
262 close(fd);
263 free(fname);
264 return;
265fail:
266 errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname);
267}
268
269static void backup_partition_table(struct sfdisk *sf, const char *devname)
270{
271 const char *name;
272 char *tpl;
273 off_t offset = 0;
274 size_t size = 0;
275 int i = 0;
276
277 assert(sf);
278
279 if (!fdisk_has_label(sf->cxt))
280 return;
281
282 if (!sf->backup_file) {
283 /* initialize default backup filename */
284 const char *home = getenv ("HOME");
285 if (!home)
286 errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined"));
287 xasprintf(&tpl, "%s/sfdisk-%s-", home, basename(devname));
288 } else
289 xasprintf(&tpl, "%s-%s-", sf->backup_file, basename(devname));
290
291 color_scheme_enable("header", UL_COLOR_BOLD);
292 fdisk_info(sf->cxt, _("Backup files:"));
293 color_disable();
294
295 while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size)
296 backup_sectors(sf, tpl, name, devname, offset, size);
297
298 if (!sf->quiet)
299 fputc('\n', stdout);
300 free(tpl);
301}
302
351fad50
KZ
303static int write_changes(struct sfdisk *sf)
304{
305 int rc = 0;
306
307 if (sf->noact)
308 fdisk_info(sf->cxt, _("The partition table unchanged (--no-act)."));
309 else {
310 rc = fdisk_write_disklabel(sf->cxt);
311 if (!rc) {
312 fdisk_info(sf->cxt, _("\nThe partition table has been altered."));
313 fdisk_reread_partition_table(sf->cxt);
314 }
315 }
316 if (!rc)
317 rc = fdisk_deassign_device(sf->cxt, sf->noact); /* no-sync when no-act */
318 return rc;
319}
ab02d87e 320
148f6e6d
KZ
321/*
322 * sfdisk --list [<device ..]
323 */
9c1f9dd3
KZ
324static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
325{
9c1f9dd3
KZ
326 fdisk_enable_listonly(sf->cxt, 1);
327
207de32a 328 if (argc) {
d2eb1457 329 int i, ct = 0;
8a8d204c
KZ
330
331 for (i = 0; i < argc; i++) {
332 if (ct)
333 fputs("\n\n", stdout);
d2c47697 334 if (print_device_pt(sf->cxt, argv[i], 0, sf->verify) == 0)
8a8d204c
KZ
335 ct++;
336 }
9c1f9dd3 337 } else
d2c47697 338 print_all_devices_pt(sf->cxt, sf->verify);
9c1f9dd3
KZ
339
340 return 0;
341}
342
058dd97a
KZ
343/*
344 * sfdisk --list-types
345 */
346static int command_list_types(struct sfdisk *sf)
347{
348 const struct fdisk_parttype *t;
349 struct fdisk_label *lb;
350 const char *name;
351 size_t i = 0;
352 int codes;
353
354 assert(sf);
355 assert(sf->cxt);
356
357 name = sf->label ? sf->label : "dos";
358 lb = fdisk_get_label(sf->cxt, name);
359 if (!lb)
360 errx(EXIT_FAILURE, _("unsupported label '%s'"), name);
361
362 codes = fdisk_label_has_code_parttypes(lb);
363 fputs(_("Id Name\n\n"), stdout);
364
365 while ((t = fdisk_label_get_parttype(lb, i++))) {
366 if (codes)
367 printf("%2x %s\n", fdisk_parttype_get_code(t),
368 fdisk_parttype_get_name(t));
369 else
370 printf("%s %s\n", fdisk_parttype_get_string(t),
371 fdisk_parttype_get_name(t));
372 }
373
374 return 0;
375}
376
d2c47697
KZ
377static int verify_device(struct sfdisk *sf, const char *devname)
378{
379 int rc = 1;
380
381 fdisk_enable_listonly(sf->cxt, 1);
382
383 if (fdisk_assign_device(sf->cxt, devname, 1)) {
384 warn(_("cannot open: %s"), devname);
385 return 1;
386 }
387
388 color_scheme_enable("header", UL_COLOR_BOLD);
389 fdisk_info(sf->cxt, "%s:", devname);
390 color_disable();
391
392 if (!fdisk_has_label(sf->cxt))
393 fdisk_info(sf->cxt, _("unrecognized partition table type"));
394 else
395 rc = fdisk_verify_disklabel(sf->cxt);
396
397 fdisk_deassign_device(sf->cxt, 1);
398 return rc;
399}
400
401/*
402 * sfdisk --verify [<device ..]
403 */
404static int command_verify(struct sfdisk *sf, int argc, char **argv)
405{
406 int nfails = 0, ct = 0;
407
408 if (argc) {
409 int i;
410 for (i = 0; i < argc; i++) {
411 if (i)
412 fdisk_info(sf->cxt, " ");
413 if (verify_device(sf, argv[i]) < 0)
414 nfails++;
415 }
416 } else {
417 FILE *f = NULL;
418 char *dev;
419
420 while ((dev = next_proc_partition(&f))) {
421 if (ct)
422 fdisk_info(sf->cxt, " ");
423 if (verify_device(sf, dev) < 0)
424 nfails++;
425 free(dev);
426 ct++;
427 }
428 }
429
430 return nfails;
431}
432
148f6e6d 433static int get_size(const char *dev, int silent, uintmax_t *sz)
d2eb1457 434{
148f6e6d
KZ
435 int fd, rc = 0;
436
437 fd = open(dev, O_RDONLY);
d2eb1457
KZ
438 if (fd < 0) {
439 if (!silent)
440 warn(_("cannot open: %s"), dev);
441 return -errno;
442 }
d2eb1457
KZ
443
444 if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) {
445 if (!silent)
446 warn(_("Cannot get size of %s"), dev);
447 rc = -errno;
448 }
449
450 close(fd);
451 return rc;
452}
453
148f6e6d
KZ
454/*
455 * sfdisk --show-size [<device ..]
456 *
457 * (silly, but just for backward compatibility)
458 */
d2eb1457
KZ
459static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
460 int argc, char **argv)
461{
462 uintmax_t sz;
463
464 if (argc) {
465 int i;
466 for (i = 0; i < argc; i++) {
467 if (get_size(argv[i], 0, &sz) == 0)
468 printf("%ju\n", sz / 2);
469 }
470 } else {
471 FILE *f = NULL;
472 uintmax_t total = 0;
473 char *dev;
474
475 while ((dev = next_proc_partition(&f))) {
476 if (get_size(dev, 1, &sz) == 0) {
477 printf("%s: %9ju\n", dev, sz / 2);
478 total += sz / 2;
479 }
480 free(dev);
481 }
482 if (total)
483 printf(_("total: %ju blocks\n"), total);
484 }
485
486 return 0;
487}
488
d420a7f9
KZ
489static int print_geom(struct sfdisk *sf, const char *devname)
490{
491 fdisk_enable_listonly(sf->cxt, 1);
492
493 if (fdisk_assign_device(sf->cxt, devname, 1)) {
494 warn(_("cannot open: %s"), devname);
495 return 1;
496 }
497
498 fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
499 devname,
500 (uintmax_t) fdisk_get_geom_cylinders(sf->cxt),
501 (uintmax_t) fdisk_get_geom_heads(sf->cxt),
502 (uintmax_t) fdisk_get_geom_sectors(sf->cxt));
503
504 fdisk_deassign_device(sf->cxt, 1);
505 return 0;
506}
507
508/*
509 * sfdisk --show-geometry [<device ..]
510 */
511static int command_show_geometry(struct sfdisk *sf, int argc, char **argv)
512{
513 int nfails = 0;
514
515 if (argc) {
516 int i;
517 for (i = 0; i < argc; i++) {
518 if (print_geom(sf, argv[i]) < 0)
519 nfails++;
520 }
521 } else {
522 FILE *f = NULL;
523 char *dev;
524
525 while ((dev = next_proc_partition(&f))) {
526 if (print_geom(sf, dev) < 0)
527 nfails++;
528 free(dev);
529 }
530 }
531
532 return nfails;
533}
534
254b1dfc
KZ
535/*
536 * sfdisk --activate <device> [<partno> ...]
537 */
54b13b0c
KZ
538static int command_activate(struct sfdisk *sf, int argc, char **argv)
539{
21ca986d 540 int rc, nparts, i, listonly;
54b13b0c
KZ
541 struct fdisk_partition *pa = NULL;
542 const char *devname = NULL;
543
21ca986d 544 if (argc < 1)
54b13b0c 545 errx(EXIT_FAILURE, _("no disk device specified"));
8eab3194
KZ
546 devname = argv[0];
547
21ca986d
KZ
548 /* --activate <device> */
549 listonly = argc == 1;
54b13b0c 550
21ca986d 551 rc = fdisk_assign_device(sf->cxt, devname, listonly);
54b13b0c
KZ
552 if (rc)
553 err(EXIT_FAILURE, _("cannot open %s"), devname);
554
555 if (!fdisk_is_label(sf->cxt, DOS))
556 errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR only"));
557
ab02d87e
KZ
558 if (!listonly && sf->backup)
559 backup_partition_table(sf, devname);
21ca986d 560
54b13b0c
KZ
561 nparts = fdisk_get_npartitions(sf->cxt);
562 for (i = 0; i < nparts; i++) {
563 char *data = NULL;
564
565 /* note that fdisk_get_partition() reuses the @pa pointer, you
566 * don't have to (re)allocate it */
567 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
568 continue;
569
570 /* sfdisk --activate list bootable partitions */
21ca986d 571 if (listonly) {
54b13b0c
KZ
572 if (!fdisk_partition_is_bootable(pa))
573 continue;
574 if (fdisk_partition_to_string(pa, sf->cxt,
575 FDISK_FIELD_DEVICE, &data) == 0) {
576 printf("%s\n", data);
577 free(data);
578 }
579
580 /* deactivate all active partitions */
581 } else if (fdisk_partition_is_bootable(pa))
582 fdisk_partition_toggle_flag(sf->cxt, i, DOS_FLAG_ACTIVE);
583 }
584
585 /* sfdisk --activate <partno> [..] */
586 for (i = 1; i < argc; i++) {
587 int n = strtou32_or_err(argv[i], _("failed to parse partition number"));
588
254b1dfc
KZ
589 rc = fdisk_partition_toggle_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE);
590 if (rc)
591 errx(EXIT_FAILURE,
43a1ccec 592 _("%s: partition %d: failed to toggle bootable flag"),
254b1dfc 593 devname, i + 1);
54b13b0c
KZ
594 }
595
596 fdisk_unref_partition(pa);
351fad50
KZ
597 if (listonly)
598 rc = fdisk_deassign_device(sf->cxt, 1);
599 else
600 rc = write_changes(sf);
54b13b0c
KZ
601 return rc;
602}
603
148f6e6d
KZ
604/*
605 * sfdisk --dump <device>
606 */
8a8d204c
KZ
607static int command_dump(struct sfdisk *sf, int argc, char **argv)
608{
609 const char *devname = NULL;
610 struct fdisk_script *dp;
611 int rc;
612
613 if (argc)
614 devname = argv[0];
615 if (!devname)
616 errx(EXIT_FAILURE, _("no disk device specified"));
617
f01f2528 618 rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */
8a8d204c
KZ
619 if (rc)
620 err(EXIT_FAILURE, _("cannot open %s"), devname);
621
622 dp = fdisk_new_script(sf->cxt);
623 if (!dp)
624 err(EXIT_FAILURE, _("failed to allocate dump struct"));
625
626 rc = fdisk_script_read_context(dp, NULL);
627 if (rc)
628 err(EXIT_FAILURE, _("failed to dump partition table"));
629
630 fdisk_script_write_file(dp, stdout);
631
632 fdisk_unref_script(dp);
f01f2528 633 fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
8a8d204c
KZ
634 return 0;
635}
636
3a5bdedf
KZ
637static void assign_device_partition(struct sfdisk *sf,
638 const char *devname,
639 size_t partno,
640 int rdonly)
641{
642 int rc;
643 size_t n;
644 struct fdisk_label *lb = NULL;
645
646 assert(sf);
647 assert(devname);
648
649 /* read-only when a new <type> undefined */
650 rc = fdisk_assign_device(sf->cxt, devname, rdonly);
651 if (rc)
652 err(EXIT_FAILURE, _("cannot open %s"), devname);
653
654 lb = fdisk_get_label(sf->cxt, NULL);
655 if (!lb)
656 errx(EXIT_FAILURE, _("%s: not found partition table."), devname);
657
658 n = fdisk_get_npartitions(sf->cxt);
659 if (partno > n)
660 errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains %zu "
661 "partitions only."), devname, partno, n);
662 if (!fdisk_is_partition_used(sf->cxt, partno - 1))
663 errx(EXIT_FAILURE, _("%s: partition %zu: partition unnused"),
664 devname, partno);
665}
666
8eab3194 667/*
e36fb07a 668 * sfdisk --part-type <device> <partno> [<type>]
8eab3194
KZ
669 */
670static int command_parttype(struct sfdisk *sf, int argc, char **argv)
671{
3a5bdedf 672 size_t partno;
8eab3194 673 struct fdisk_parttype *type = NULL;
3a5bdedf 674 struct fdisk_label *lb;
8eab3194
KZ
675 const char *devname = NULL, *typestr = NULL;
676
677 if (!argc)
678 errx(EXIT_FAILURE, _("no disk device specified"));
679 devname = argv[0];
680
681 if (argc < 2)
682 errx(EXIT_FAILURE, _("no partition number specified"));
683 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
684
685 if (argc == 3)
686 typestr = argv[2];
687 else if (argc > 3)
688 errx(EXIT_FAILURE, _("uneexpected arguments"));
689
690 /* read-only when a new <type> undefined */
3a5bdedf 691 assign_device_partition(sf, devname, partno, !typestr);
8eab3194
KZ
692
693 lb = fdisk_get_label(sf->cxt, NULL);
8eab3194
KZ
694
695 /* print partition type */
696 if (!typestr) {
697 const struct fdisk_parttype *t = NULL;
2e73a998 698 struct fdisk_partition *pa = NULL;
8eab3194
KZ
699
700 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
701 t = fdisk_partition_get_type(pa);
702 if (!t)
703 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"),
704 devname, partno);
705
706 if (fdisk_label_has_code_parttypes(lb))
707 printf("%2x\n", fdisk_parttype_get_code(t));
708 else
709 printf("%s\n", fdisk_parttype_get_string(t));
710
711 fdisk_unref_partition(pa);
2e73a998 712 fdisk_deassign_device(sf->cxt, 1);
8eab3194
KZ
713 return 0;
714 }
715
ab02d87e
KZ
716 if (sf->backup)
717 backup_partition_table(sf, devname);
718
8eab3194
KZ
719 /* parse <type> and apply yo PT */
720 type = fdisk_label_parse_parttype(lb, typestr);
2e73a998
KZ
721 if (!type || fdisk_parttype_is_unknown(type))
722 errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"),
8eab3194 723 fdisk_label_get_name(lb), typestr);
8eab3194 724
2e73a998
KZ
725 else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0)
726 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"),
727 devname, partno);
dfc6db2a 728 fdisk_unref_parttype(type);
351fad50 729 return write_changes(sf);
3a5bdedf
KZ
730}
731
732/*
e36fb07a 733 * sfdisk --part-uuid <device> <partno> [<uuid>]
3a5bdedf
KZ
734 */
735static int command_partuuid(struct sfdisk *sf, int argc, char **argv)
736{
3a5bdedf
KZ
737 size_t partno;
738 struct fdisk_partition *pa = NULL;
739 const char *devname = NULL, *uuid = NULL;
740
741 if (!argc)
742 errx(EXIT_FAILURE, _("no disk device specified"));
743 devname = argv[0];
744
745 if (argc < 2)
746 errx(EXIT_FAILURE, _("no partition number specified"));
747 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
748
749 if (argc == 3)
750 uuid = argv[2];
751 else if (argc > 3)
752 errx(EXIT_FAILURE, _("uneexpected arguments"));
753
754 /* read-only if uuid not given */
755 assign_device_partition(sf, devname, partno, !uuid);
756
757 /* print partition uuid */
758 if (!uuid) {
f0f2be20 759 const char *str = NULL;
3a5bdedf
KZ
760
761 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
762 str = fdisk_partition_get_uuid(pa);
763 if (!str)
764 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"),
765 devname, partno);
766 printf("%s\n", str);
767 fdisk_unref_partition(pa);
768 fdisk_deassign_device(sf->cxt, 1);
769 return 0;
770 }
771
772 if (sf->backup)
773 backup_partition_table(sf, devname);
774
775 pa = fdisk_new_partition();
776 if (!pa)
777 err(EXIT_FAILURE, _("failed to allocate partition object"));
778
779 if (fdisk_partition_set_uuid(pa, uuid) != 0 ||
780 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
781 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"),
782 devname, partno);
351fad50
KZ
783 fdisk_unref_partition(pa);
784 return write_changes(sf);
785}
786
787/*
e36fb07a 788 * sfdisk --part-label <device> <partno> [<label>]
351fad50 789 */
e36fb07a 790static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
351fad50
KZ
791{
792 size_t partno;
793 struct fdisk_partition *pa = NULL;
794 const char *devname = NULL, *name = NULL;
795
796 if (!argc)
797 errx(EXIT_FAILURE, _("no disk device specified"));
798 devname = argv[0];
799
800 if (argc < 2)
801 errx(EXIT_FAILURE, _("no partition number specified"));
802 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
803
804 if (argc == 3)
805 name = argv[2];
806 else if (argc > 3)
807 errx(EXIT_FAILURE, _("uneexpected arguments"));
808
809 /* read-only if name not given */
810 assign_device_partition(sf, devname, partno, !name);
811
812 /* print partition name */
813 if (!name) {
f0f2be20 814 const char *str = NULL;
351fad50
KZ
815
816 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
817 str = fdisk_partition_get_name(pa);
818 if (!str)
819 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"),
820 devname, partno);
821 printf("%s\n", str);
822 fdisk_unref_partition(pa);
823 fdisk_deassign_device(sf->cxt, 1);
824 return 0;
825 }
826
827 if (sf->backup)
828 backup_partition_table(sf, devname);
829
830 pa = fdisk_new_partition();
831 if (!pa)
832 err(EXIT_FAILURE, _("failed to allocate partition object"));
833
834 if (fdisk_partition_set_name(pa, name) != 0 ||
835 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
836 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"),
837 devname, partno);
838
839 fdisk_unref_partition(pa);
840 return write_changes(sf);
8eab3194
KZ
841}
842
bc9e8547
KZ
843/*
844 * sfdisk --part-attrs <device> <partno> [<attrs>]
845 */
846static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
847{
848 size_t partno;
849 struct fdisk_partition *pa = NULL;
850 const char *devname = NULL, *attrs = NULL;
851
852 if (!argc)
853 errx(EXIT_FAILURE, _("no disk device specified"));
854 devname = argv[0];
855
856 if (argc < 2)
857 errx(EXIT_FAILURE, _("no partition number specified"));
858 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
859
860 if (argc == 3)
861 attrs = argv[2];
862 else if (argc > 3)
863 errx(EXIT_FAILURE, _("uneexpected arguments"));
864
865 /* read-only if name not given */
866 assign_device_partition(sf, devname, partno, !attrs);
867
868 /* print partition name */
869 if (!attrs) {
f0f2be20 870 const char *str = NULL;
bc9e8547
KZ
871
872 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
873 str = fdisk_partition_get_attrs(pa);
874 if (str)
875 printf("%s\n", str);
876 fdisk_unref_partition(pa);
877 fdisk_deassign_device(sf->cxt, 1);
878 return 0;
879 }
880
881 if (sf->backup)
882 backup_partition_table(sf, devname);
883
884 pa = fdisk_new_partition();
885 if (!pa)
886 err(EXIT_FAILURE, _("failed to allocate partition object"));
887
888 if (fdisk_partition_set_attrs(pa, attrs) != 0 ||
889 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
890 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"),
891 devname, partno);
892
893 fdisk_unref_partition(pa);
894 return write_changes(sf);
895}
896
b4df449f 897static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
7324f1bf 898{
c3bc7483 899 struct fdisk_partition *pa = NULL;
7324f1bf
KZ
900 char *data;
901
902 assert(sf);
903
f0edb076
KZ
904 if (sf->quiet)
905 return;
7324f1bf
KZ
906 if (fdisk_get_partition(sf->cxt, n, &pa) != 0)
907 return;
908
909 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data);
910 printf("%12s : ", data);
911
912 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data);
913 printf("%12s ", data);
914
915 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data);
916 printf("%12s ", data);
917
918 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data);
919 printf("(%s) ", data);
920
921 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data);
922 printf("%s\n", data);
923
924 fdisk_unref_partition(pa);
925}
926
3186f4a9
KZ
927static void command_fdisk_help(void)
928{
929 fputs(_("\nHelp:\n"), stdout);
930
931 fputc('\n', stdout);
932 color_scheme_enable("help-title", UL_COLOR_BOLD);
933 fputs(_(" Commands:\n"), stdout);
934 color_disable();
e8813494
KZ
935 fputs(_(" write write table to disk and exit\n"), stdout);
936 fputs(_(" quit show new situation and wait for user's feedbadk before write\n"), stdout);
937 fputs(_(" abort exit sfdisk shell\n"), stdout);
3186f4a9
KZ
938 fputs(_(" print print partition table.\n"), stdout);
939 fputs(_(" help this help.\n"), stdout);
940
941 fputc('\n', stdout);
942 color_scheme_enable("help-title", UL_COLOR_BOLD);
943 fputs(_(" Input format:\n"), stdout);
944 color_disable();
e54b1c6f 945 fputs(_(" <start>, <size>, <typy>, <bootable>\n"), stdout);
3186f4a9
KZ
946
947 fputc('\n', stdout);
948 fputs(_(" <start> begin of the partition in sectors. The default is the first\n"
949 " free space.\n"), stdout);
950
951 fputc('\n', stdout);
952 fputs(_(" <size> size of the partition in sectors if specified in format\n"
953 " <number>{K,M,G,T,P,E,Z,Y} then it's interpreted as size\n"
954 " in bytes. The default is all available space.\n"), stdout);
955
956 fputc('\n', stdout);
957 fputs(_(" <type> partition type. The default is Linux data partition.\n"), stdout);
958 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout);
959 fputs(_(" GPT: uuid or L,S,H shortcuts.\n"), stdout);
960
961 fputc('\n', stdout);
962 fputs(_(" <bootable> '*' to mark MBR partition as bootable. \n"), stdout);
963
964 fputc('\n', stdout);
965 color_scheme_enable("help-title", UL_COLOR_BOLD);
966 fputs(_(" Example:\n"), stdout);
967 color_disable();
968 fputs(_(" , 4G creates 4GiB partition on default start offset.\n"), stdout);
969 fputc('\n', stdout);
970}
971
e8813494
KZ
972enum {
973 SFDISK_DONE_NONE = 0,
974 SFDISK_DONE_EOF,
975 SFDISK_DONE_ABORT,
976 SFDISK_DONE_WRITE,
977 SFDISK_DONE_ASK
978};
979
980/* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
981static int loop_control_commands(struct sfdisk *sf,
982 struct fdisk_script *dp,
983 char *buf)
984{
985 const char *p = skip_blank(buf);
986 int rc = SFDISK_DONE_NONE;
987
988 if (strcmp(p, "print") == 0)
989 list_disklabel(sf->cxt);
990 else if (strcmp(p, "help") == 0)
991 command_fdisk_help();
992 else if (strcmp(p, "quit") == 0)
993 rc = SFDISK_DONE_ASK;
994 else if (strcmp(p, "write") == 0)
995 rc = SFDISK_DONE_WRITE;
996 else if (strcmp(p, "abort") == 0)
997 rc = SFDISK_DONE_ABORT;
998 else {
8be199ea 999 if (sf->interactive)
e8813494
KZ
1000 fdisk_warnx(sf->cxt, _("unsupported command"));
1001 else {
1002 fdisk_warnx(sf->cxt, _("line %d: unsupported command"),
1003 fdisk_script_get_nlines(dp));
1004 rc = -EINVAL;
1005 }
1006 }
1007 return rc;
1008}
1009
e54b1c6f
KZ
1010static int has_container(struct sfdisk *sf)
1011{
1012 size_t i, nparts;
1013 struct fdisk_partition *pa = NULL;
1014
1015 if (sf->container)
1016 return sf->container;
1017
1018 nparts = fdisk_get_npartitions(sf->cxt);
e54b1c6f
KZ
1019 for (i = 0; i < nparts; i++) {
1020 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
1021 continue;
1022 if (fdisk_partition_is_container(pa)) {
1023 sf->container = 1;
1024 break;
1025 }
1026 }
1027
1028 fdisk_unref_partition(pa);
1029 return sf->container;
1030}
1031
347a7f77
KZ
1032static size_t last_pt_partno(struct sfdisk *sf)
1033{
1034 size_t i, nparts, partno = 0;
1035 struct fdisk_partition *pa = NULL;
1036
1037
1038 nparts = fdisk_get_npartitions(sf->cxt);
1039 for (i = 0; i < nparts; i++) {
1040 size_t x;
1041
1042 if (fdisk_get_partition(sf->cxt, i, &pa) != 0 ||
1043 !fdisk_partition_is_used(pa))
1044 continue;
1045 x = fdisk_partition_get_partno(pa);
1046 if (x > partno)
1047 partno = x;
1048 }
1049
1050 fdisk_unref_partition(pa);
1051 return partno;
1052}
1053
db48b6a1
KZ
1054static int is_device_used(struct sfdisk *sf)
1055{
1056#ifdef BLKRRPART
1057 struct stat st;
1058 int fd;
1059
1060 assert(sf);
1061 assert(sf->cxt);
1062
1063 fd = fdisk_get_devfd(sf->cxt);
1064 if (fd < 0)
1065 return 0;
1066
1067 if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode))
1068 return ioctl(fd, BLKRRPART) != 0;
1069#endif
1070 return 0;
1071}
3186f4a9 1072
254b1dfc
KZ
1073/*
1074 * sfdisk <device> [[-N] <partno>]
1075 *
1076 * Note that the option -N is there for backward compatibility only.
1077 */
7324f1bf 1078static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
9c1f9dd3 1079{
7324f1bf
KZ
1080 int rc = 0, partno = sf->partno, created = 0;
1081 struct fdisk_script *dp;
1082 struct fdisk_table *tb = NULL;
1083 const char *devname = NULL, *label;
1084 char buf[BUFSIZ];
c3bc7483 1085 size_t next_partno = (size_t) -1;
9c1f9dd3 1086
8a8d204c
KZ
1087 if (argc)
1088 devname = argv[0];
254b1dfc 1089 if (partno < 0 && argc > 1)
54b13b0c 1090 partno = strtou32_or_err(argv[1],
9c1f9dd3 1091 _("failed to parse partition number"));
8a8d204c 1092 if (!devname)
9c1f9dd3
KZ
1093 errx(EXIT_FAILURE, _("no disk device specified"));
1094
347a7f77
KZ
1095 rc = fdisk_assign_device(sf->cxt, devname, 0);
1096 if (rc)
1097 err(EXIT_FAILURE, _("cannot open %s"), devname);
b4df449f 1098
7324f1bf
KZ
1099 dp = fdisk_new_script(sf->cxt);
1100 if (!dp)
1101 err(EXIT_FAILURE, _("failed to allocate script handler"));
347a7f77 1102 fdisk_set_script(sf->cxt, dp);
9c1f9dd3 1103
b4df449f
KZ
1104 /*
1105 * Don't create a new disklabel when [-N] <partno> specified. In this
1106 * case reuse already specified disklabel. Let's check that the disk
1107 * really contains the partition.
1108 */
1109 if (partno >= 0) {
1110 size_t n;
1111 if (!fdisk_has_label(sf->cxt))
8eab3194
KZ
1112 errx(EXIT_FAILURE, _("%s: cannot modify partition %d, "
1113 "not found partition table."),
1114 devname, partno);
b4df449f
KZ
1115 n = fdisk_get_npartitions(sf->cxt);
1116 if ((size_t) partno > n)
8eab3194 1117 errx(EXIT_FAILURE, _("%s: cannot modify partition %d, "
b4df449f 1118 "partition table contains %zu "
8eab3194
KZ
1119 "partitions only."),
1120 devname, partno, n);
b4df449f
KZ
1121 created = 1;
1122 next_partno = partno;
1123 }
1124
347a7f77
KZ
1125 if (sf->append) {
1126 created = 1;
1127 next_partno = last_pt_partno(sf) + 1;
1128 }
1129
8be199ea 1130 if (!sf->quiet && sf->interactive) {
3186f4a9
KZ
1131 color_scheme_enable("welcome", UL_COLOR_GREEN);
1132 fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING);
1133 color_disable();
1134 fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n"
1135 "Be careful before using the write command.\n"));
1136 }
1137
db48b6a1
KZ
1138 if (!sf->noact && !sf->noreread) {
1139 if (!sf->quiet)
1140 fputs(_("Checking that no-one is using this disk right now ..."), stdout);
1141 if (is_device_used(sf)) {
1142 fputs(_(" FAILED\n\n"), stdout);
1143
1144 fdisk_warnx(sf->cxt, _(
1145 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1146 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1147 "Use the --no-reread flag to suppress this check.\n"));
1148
1149 if (!sf->force)
1150 errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
1151 } else
1152 fputs(_(" OK\n\n"), stdout);
1153 }
1154
ab02d87e
KZ
1155 if (sf->backup)
1156 backup_partition_table(sf, devname);
1157
f0edb076
KZ
1158 if (!sf->quiet) {
1159 list_disk_geometry(sf->cxt);
1160 if (fdisk_has_label(sf->cxt)) {
1161 fdisk_info(sf->cxt, _("\nOld situation:"));
1162 list_disklabel(sf->cxt);
1163 }
3186f4a9 1164 }
7324f1bf
KZ
1165
1166 if (sf->label)
1167 label = sf->label;
1168 else if (fdisk_has_label(sf->cxt))
1169 label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
1170 else
1171 label = "dos"; /* just for backward compatibility */
1172
1173 fdisk_script_set_header(dp, "label", label);
1174
8be199ea 1175 if (!sf->quiet && sf->interactive) {
3186f4a9
KZ
1176 if (!fdisk_has_label(sf->cxt) && !sf->label)
1177 fdisk_info(sf->cxt,
1178 _("\nsfdisk is going to create a new '%s' disk label.\n"
1179 "Use 'label: <name>' before you define a first partition\n"
1180 "to override the default."), label);
1181 fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n"));
f0edb076 1182 } else if (!sf->quiet)
3186f4a9 1183 fputc('\n', stdout);
7324f1bf
KZ
1184
1185 tb = fdisk_script_get_table(dp);
c3bc7483 1186 assert(tb);
7324f1bf
KZ
1187
1188 do {
c3bc7483 1189 size_t nparts;
7324f1bf 1190
c3bc7483
KZ
1191 DBG(PARSE, ul_debug("<---next-line--->"));
1192 if (next_partno == (size_t) -1)
1193 next_partno = fdisk_table_get_nents(tb);
1194
e54b1c6f
KZ
1195 if (created
1196 && partno < 0
347a7f77 1197 && next_partno == fdisk_get_npartitions(sf->cxt)
e54b1c6f
KZ
1198 && !has_container(sf)) {
1199 fdisk_info(sf->cxt, _("All partitions used."));
1200 rc = SFDISK_DONE_ASK;
1201 break;
1202 }
1203
3186f4a9
KZ
1204 if (created) {
1205 char *partname = fdisk_partname(devname, next_partno + 1);
1206 if (!partname)
1207 err(EXIT_FAILURE, _("failed to allocate partition name"));
1208 printf("%s: ", partname);
1209 free(partname);
1210 } else
1211 printf(">>> ");
7324f1bf
KZ
1212
1213 rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
e8813494 1214 if (rc < 0) {
c3bc7483 1215 DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
7324f1bf 1216 buf[sizeof(buf) - 1] = '\0';
e8813494
KZ
1217 rc = loop_control_commands(sf, dp, buf);
1218 if (rc)
7324f1bf 1219 break;
7324f1bf 1220 continue;
e8813494
KZ
1221 } else if (rc == 1) {
1222 rc = SFDISK_DONE_EOF;
1223 break;
7324f1bf
KZ
1224 }
1225
1226 nparts = fdisk_table_get_nents(tb);
1227 if (nparts) {
c3bc7483 1228 size_t cur_partno;
7324f1bf
KZ
1229 struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1);
1230
c3bc7483 1231 assert(pa);
3186f4a9 1232
ecf40cda 1233 if (!fdisk_partition_has_start(pa) &&
3186f4a9
KZ
1234 !fdisk_partition_start_is_default(pa)) {
1235 fdisk_info(sf->cxt, _("Ignore partition %zu"), next_partno + 1);
1236 continue;
1237 }
b4df449f 1238 if (!created) { /* create a new disklabel */
7324f1bf
KZ
1239 rc = fdisk_apply_script_headers(sf->cxt, dp);
1240 created = !rc;
90dc69e4
KZ
1241 if (rc)
1242 fdisk_warnx(sf->cxt, _(
1243 "Failed to apply script headers, "
1244 "disk label not created."));
7324f1bf 1245 }
b4df449f 1246 if (!rc && partno >= 0) { /* -N <partno>, modify partition */
0ecf3ab5 1247 rc = fdisk_set_partition(sf->cxt, partno, pa);
b4df449f
KZ
1248 if (rc == 0)
1249 rc = SFDISK_DONE_ASK;
1250 break;
a8a4887b 1251 } else if (!rc) { /* add partition */
c3bc7483 1252 rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
a8a4887b
KZ
1253 if (rc) {
1254 errno = -rc;
90dc69e4 1255 fdisk_warn(sf->cxt, _("Failed to add partition"));
a8a4887b
KZ
1256 }
1257 }
7324f1bf 1258
c3bc7483 1259 if (!rc) { /* success, print reult */
8be199ea
KZ
1260 if (sf->interactive)
1261 sfdisk_print_partition(sf, cur_partno);
c3bc7483 1262 next_partno = cur_partno + 1;
90dc69e4 1263 } else if (pa) /* error, drop partition from script */
7324f1bf
KZ
1264 fdisk_table_remove_partition(tb, pa);
1265 } else
3186f4a9 1266 fdisk_info(sf->cxt, _("Script header accepted."));
e54b1c6f 1267
90dc69e4
KZ
1268 if (rc && !sf->interactive) {
1269 rc = SFDISK_DONE_ABORT;
1270 break;
1271 }
7324f1bf 1272 } while (1);
254b1dfc 1273
f0edb076 1274 if (!sf->quiet && rc != SFDISK_DONE_ABORT) {
3186f4a9 1275 fdisk_info(sf->cxt, _("\nNew situation:"));
7324f1bf
KZ
1276 list_disklabel(sf->cxt);
1277 }
e8813494
KZ
1278
1279 switch (rc) {
1280 case SFDISK_DONE_ASK:
05af8bd4 1281 case SFDISK_DONE_EOF:
8be199ea 1282 if (sf->interactive) {
e8813494
KZ
1283 int yes = 0;
1284 fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes);
1285 if (!yes) {
f01f2528 1286 fdisk_info(sf->cxt, _("Leaving."));
e8813494
KZ
1287 rc = 0;
1288 break;
1289 }
1290 }
e8813494 1291 case SFDISK_DONE_WRITE:
351fad50 1292 rc = write_changes(sf);
e8813494
KZ
1293 break;
1294 case SFDISK_DONE_ABORT:
b4df449f 1295 default: /* rc < 0 on error */
f0edb076 1296 fdisk_info(sf->cxt, _("Leaving.\n"));
e8813494 1297 break;
3186f4a9 1298 }
e8813494 1299
7324f1bf 1300 fdisk_unref_script(dp);
9c1f9dd3
KZ
1301 return rc;
1302}
1303
1881390d
KZ
1304static void __attribute__ ((__noreturn__)) usage(FILE *out)
1305{
1306 fputs(USAGE_HEADER, out);
fd6b7a7f 1307
1881390d 1308 fprintf(out,
43a1ccec
KZ
1309 _(" %1$s [options] <dev> [[-N] <part>]\n"
1310 " %1$s [options] <command>\n"), program_invocation_short_name);
1311
1312 fputs(_("\nCommands:\n"), out);
43a1ccec 1313 fputs(_(" -a, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out);
d420a7f9
KZ
1314 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out);
1315 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
1316 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
43a1ccec
KZ
1317 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
1318 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
1319 fputs(_(" -V, --verify test whether partitions seem correct\n"), out);
1320
e36fb07a
KZ
1321 fputs(USAGE_SEPARATOR, out);
1322 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
1323 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
1324 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
bc9e8547 1325 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
e36fb07a 1326
43a1ccec 1327 fputs(USAGE_SEPARATOR, out);
ab02d87e
KZ
1328 fputs(_(" <dev> device (usually disk) path\n"), out);
1329 fputs(_(" <part> partition number\n"), out);
1330 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
fd6b7a7f 1331
1881390d 1332 fputs(USAGE_OPTIONS, out);
347a7f77 1333 fputs(_(" -A, --append append partitions to existing partition table\n"), out);
ab02d87e
KZ
1334 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out);
1335 fputs(_(" -f, --force disable all consistency checking\n"), out);
01f9286c 1336 fputs(_(" -o, --output <list> output columns\n"), out);
ab02d87e
KZ
1337 fputs(_(" -O, --backup-file <path> override default backout file name\n"), out);
1338 fputs(_(" -N, --partno <num> specify partition number\n"), out);
1339 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
e1422de3 1340 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out);
ab02d87e
KZ
1341 fputs(_(" -q, --quiet suppress extra info messages\n"), out);
1342 fputs(_(" -n, --no-act do everything except write to device\n"), out);
1343 fputs(_(" --no-reread do not check whether the device is in use\n"), out);
35ce145f 1344 fputs(USAGE_SEPARATOR, out);
ab02d87e
KZ
1345 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
1346 fputs(_(" -L, --Linux deprecated, only for backward copatibility\n"), out);
058dd97a 1347
1881390d
KZ
1348 fputs(USAGE_SEPARATOR, out);
1349 fputs(USAGE_HELP, out);
1350 fputs(USAGE_VERSION, out);
fd6b7a7f 1351
01f9286c
KZ
1352 list_available_columns(out);
1353
1881390d
KZ
1354 fprintf(out, USAGE_MAN_TAIL("sfdisk(8)"));
1355 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
fd6b7a7f
KZ
1356}
1357
9c1f9dd3 1358
1881390d
KZ
1359int main(int argc, char *argv[])
1360{
01f9286c 1361 const char *outarg = NULL;
8eab3194 1362 int rc = -EINVAL, c, longidx = -1;
7324f1bf 1363 struct sfdisk _sf = {
8be199ea
KZ
1364 .partno = -1,
1365 .interactive = isatty(STDIN_FILENO) ? 1 : 0,
7324f1bf 1366 }, *sf = &_sf;
1881390d 1367
8eab3194
KZ
1368 enum {
1369 OPT_CHANGE_ID = CHAR_MAX + 1,
1370 OPT_PRINT_ID,
db48b6a1 1371 OPT_ID,
351fad50 1372 OPT_NOREREAD,
e36fb07a
KZ
1373 OPT_PARTUUID,
1374 OPT_PARTLABEL,
1375 OPT_PARTTYPE,
bc9e8547 1376 OPT_PARTATTRS,
8eab3194
KZ
1377 };
1378
1881390d 1379 static const struct option longopts[] = {
54b13b0c 1380 { "activate",no_argument, NULL, 'a' },
347a7f77 1381 { "append", no_argument, NULL, 'A' },
ab02d87e
KZ
1382 { "backup", no_argument, NULL, 'b' },
1383 { "backup-file", required_argument, NULL, 'O' },
8a8d204c 1384 { "dump", no_argument, NULL, 'd' },
1881390d 1385 { "help", no_argument, NULL, 'h' },
db48b6a1 1386 { "force", no_argument, NULL, 'f' },
7324f1bf 1387 { "label", required_argument, NULL, 'X' },
e1422de3 1388 { "label-nested", required_argument, NULL, 'Y' },
d2c47697 1389 { "list", no_argument, NULL, 'l' },
058dd97a 1390 { "list-types", no_argument, NULL, 'T' },
f01f2528 1391 { "no-act", no_argument, NULL, 'n' },
db48b6a1 1392 { "no-reread", no_argument, NULL, OPT_NOREREAD },
01f9286c 1393 { "output", required_argument, NULL, 'o' },
d2c47697
KZ
1394 { "partno", required_argument, NULL, 'N' },
1395 { "show-size", no_argument, NULL, 's' },
d420a7f9 1396 { "show-geometry", no_argument, NULL, 'g' },
f0edb076 1397 { "quiet", no_argument, NULL, 'q' },
d2c47697
KZ
1398 { "verify", no_argument, NULL, 'V' },
1399 { "version", no_argument, NULL, 'v' },
e36fb07a
KZ
1400
1401 { "part-uuid", no_argument, NULL, OPT_PARTUUID },
1402 { "part-label", no_argument, NULL, OPT_PARTLABEL },
1403 { "part-type", no_argument, NULL, OPT_PARTTYPE },
bc9e8547 1404 { "part-attrs", no_argument, NULL, OPT_PARTATTRS },
35ce145f 1405
8be199ea 1406 { "unit", required_argument, NULL, 'u' },
35ce145f 1407 { "Linux", no_argument, NULL, 'L' }, /* deprecated */
8eab3194 1408
8eab3194 1409 { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */
e36fb07a 1410 { "id", no_argument, NULL, 'c' }, /* deprecated */
8eab3194
KZ
1411 { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */
1412
1881390d
KZ
1413 { NULL, 0, 0, 0 },
1414 };
1415
1416 setlocale(LC_ALL, "");
1417 bindtextdomain(PACKAGE, LOCALEDIR);
1418 textdomain(PACKAGE);
1419 atexit(close_stdout);
1420
e1422de3 1421 while ((c = getopt_long(argc, argv, "aAbcdfghlLo:O:nN:qsTu:vVX:Y:",
8eab3194 1422 longopts, &longidx)) != -1) {
1881390d 1423 switch(c) {
54b13b0c
KZ
1424 case 'a':
1425 sf->act = ACT_ACTIVATE;
1426 break;
347a7f77
KZ
1427 case 'A':
1428 sf->append = 1;
1429 break;
ab02d87e
KZ
1430 case 'b':
1431 sf->backup = 1;
1432 break;
8eab3194
KZ
1433 case OPT_CHANGE_ID:
1434 case OPT_PRINT_ID:
1435 case OPT_ID:
e36fb07a 1436 warnx(_("%s is deprecated in favour of ---part-type"),
8eab3194 1437 longopts[longidx].name);
e36fb07a 1438 sf->act = ACT_PARTTYPE;
8eab3194 1439 case 'c':
e36fb07a 1440 warnx(_("--id s deprecated in favour of ---part-type"));
8eab3194
KZ
1441 sf->act = ACT_PARTTYPE;
1442 break;
e36fb07a
KZ
1443 case 'd':
1444 sf->act = ACT_DUMP;
1445 break;
db48b6a1
KZ
1446 case 'f':
1447 sf->force = 1;
1448 break;
e36fb07a
KZ
1449 case 'g':
1450 sf->act = ACT_SHOW_GEOM;
1451 break;
1881390d
KZ
1452 case 'h':
1453 usage(stdout);
1454 break;
9c1f9dd3
KZ
1455 case 'l':
1456 sf->act = ACT_LIST;
1457 break;
35ce145f 1458 case 'L':
e36fb07a 1459 warnx(_("--Linux option is unnecessary and deprecated"));
35ce145f 1460 break;
01f9286c
KZ
1461 case 'o':
1462 outarg = optarg;
1463 break;
ab02d87e
KZ
1464 case 'O':
1465 sf->backup = 1;
1466 sf->backup_file = optarg;
1467 break;
f01f2528
KZ
1468 case 'n':
1469 sf->noact = 1;
1470 break;
254b1dfc 1471 case 'N':
b4df449f 1472 sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1;
7324f1bf 1473 break;
f0edb076
KZ
1474 case 'q':
1475 sf->quiet = 1;
1476 break;
d2eb1457
KZ
1477 case 's':
1478 sf->act = ACT_SHOW_SIZE;
1479 break;
058dd97a
KZ
1480 case 'T':
1481 sf->act = ACT_LIST_TYPES;
1482 break;
d420a7f9 1483 case 'u':
d420a7f9
KZ
1484 if (*optarg != 'S')
1485 errx(EXIT_FAILURE, _("unssupported unit '%c'"), *optarg);
1486 break;
1881390d
KZ
1487 case 'v':
1488 printf(_("%s from %s\n"), program_invocation_short_name,
1489 PACKAGE_STRING);
1490 return EXIT_SUCCESS;
d2c47697
KZ
1491 case 'V':
1492 sf->verify = 1;
1493 break;
e36fb07a
KZ
1494 case 'X':
1495 sf->label = optarg;
1496 break;
e1422de3
KZ
1497 case 'Y':
1498 sf->label_nested = optarg;
1499 break;
e36fb07a
KZ
1500
1501 case OPT_PARTUUID:
1502 sf->act = ACT_PARTUUID;
1503 break;
1504 case OPT_PARTTYPE:
1505 sf->act = ACT_PARTTYPE;
1506 break;
1507 case OPT_PARTLABEL:
1508 sf->act = ACT_PARTLABEL;
351fad50 1509 break;
bc9e8547
KZ
1510 case OPT_PARTATTRS:
1511 sf->act = ACT_PARTATTRS;
1512 break;
db48b6a1
KZ
1513 case OPT_NOREREAD:
1514 sf->noreread = 1;
1515 break;
e36fb07a 1516
7324f1bf
KZ
1517 default:
1518 usage(stderr);
37b94458 1519 }
37b94458 1520 }
fd6b7a7f 1521
9c1f9dd3 1522 sfdisk_init(sf);
01f9286c
KZ
1523 if (outarg)
1524 init_fields(NULL, outarg, NULL);
22853e4a 1525
d2c47697
KZ
1526 if (sf->verify && !sf->act)
1527 sf->act = ACT_VERIFY; /* --verify make be used with --list too */
1528 else if (!sf->act)
1529 sf->act = ACT_FDISK; /* default */
1530
9c1f9dd3 1531 switch (sf->act) {
54b13b0c
KZ
1532 case ACT_ACTIVATE:
1533 rc = command_activate(sf, argc - optind, argv + optind);
1534 break;
1535
9c1f9dd3
KZ
1536 case ACT_LIST:
1537 rc = command_list_partitions(sf, argc - optind, argv + optind);
1538 break;
fd6b7a7f 1539
058dd97a
KZ
1540 case ACT_LIST_TYPES:
1541 rc = command_list_types(sf);
1542 break;
1543
9c1f9dd3 1544 case ACT_FDISK:
7324f1bf 1545 rc = command_fdisk(sf, argc - optind, argv + optind);
9c1f9dd3 1546 break;
8a8d204c
KZ
1547
1548 case ACT_DUMP:
1549 rc = command_dump(sf, argc - optind, argv + optind);
1550 break;
d2eb1457
KZ
1551
1552 case ACT_SHOW_SIZE:
1553 rc = command_show_size(sf, argc - optind, argv + optind);
1554 break;
d2c47697 1555
d420a7f9
KZ
1556 case ACT_SHOW_GEOM:
1557 rc = command_show_geometry(sf, argc - optind, argv + optind);
1558 break;
1559
d2c47697
KZ
1560 case ACT_VERIFY:
1561 rc = command_verify(sf, argc - optind, argv + optind);
1562 break;
8eab3194
KZ
1563
1564 case ACT_PARTTYPE:
1565 rc = command_parttype(sf, argc - optind, argv + optind);
1566 break;
3a5bdedf
KZ
1567
1568 case ACT_PARTUUID:
1569 rc = command_partuuid(sf, argc - optind, argv + optind);
1570 break;
1571
e36fb07a
KZ
1572 case ACT_PARTLABEL:
1573 rc = command_partlabel(sf, argc - optind, argv + optind);
351fad50
KZ
1574 break;
1575
bc9e8547
KZ
1576 case ACT_PARTATTRS:
1577 rc = command_partattrs(sf, argc - optind, argv + optind);
1578 break;
1579
9c1f9dd3 1580 }
fd6b7a7f 1581
9c1f9dd3 1582 sfdisk_deinit(sf);
fd6b7a7f 1583
1881390d
KZ
1584 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
1585 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
4b1cc035
KZ
1586}
1587