]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/fdisk.c
include/c.h: add errtryhelp()
[thirdparty/util-linux.git] / disk-utils / fdisk.c
CommitLineData
3f0ac587 1/*
6dbe3af9 2 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
38b36353 3 * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
6dbe3af9 4 *
3f0ac587
KZ
5 * Copyright (C) 2007-2013 Karel Zak <kzak@redhat.com>
6 *
6dbe3af9
KZ
7 * This program is free software. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation: either version 1 or
10 * (at your option) any later version.
6dbe3af9 11 */
6dbe3af9
KZ
12#include <unistd.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <fcntl.h>
17#include <ctype.h>
6dbe3af9 18#include <errno.h>
2b6fc908 19#include <getopt.h>
2b6fc908 20#include <sys/stat.h>
f26873dc 21#include <sys/time.h>
bb6aacfe 22#include <time.h>
ee5355e0 23#include <limits.h>
d44115f3 24#include <libsmartcols.h>
740c36f6
KZ
25#ifdef HAVE_LIBREADLINE
26# include <readline/readline.h>
27#endif
6dbe3af9 28
3f0ac587 29#include "c.h"
52b38677 30#include "xalloc.h"
e916600f 31#include "all-io.h"
22853e4a 32#include "nls.h"
e66ac5d3 33#include "rpmatch.h"
810f986b 34#include "blkdev.h"
b8d22034 35#include "mbsalign.h"
6bec8710
KZ
36#include "pathnames.h"
37#include "canonicalize.h"
20aa2570 38#include "strutils.h"
b2d28533 39#include "closestream.h"
8d2f4498 40#include "pager.h"
5c36a0eb 41
fdb006e8
KZ
42#include "fdisk.h"
43
b4bfbadd 44#include "pt-sun.h" /* to toggle flags */
5c36a0eb 45
48d7b13a 46#ifdef HAVE_LINUX_COMPILER_H
3f0ac587 47# include <linux/compiler.h>
48d7b13a
KZ
48#endif
49#ifdef HAVE_LINUX_BLKPG_H
3f0ac587 50# include <linux/blkpg.h>
7eda085c 51#endif
6dbe3af9 52
ba465623
KZ
53int pwipemode = WIPEMODE_AUTO;
54
e6d0c4c1
KZ
55/*
56 * fdisk debug stuff (see fdisk.h and include/debug.h)
57 */
58UL_DEBUG_DEFINE_MASK(fdisk);
819d9a29 59UL_DEBUG_DEFINE_MASKNAMES(fdisk) = UL_DEBUG_EMPTY_MASKNAMES;
d6b3ba41 60
e6d0c4c1
KZ
61static void fdiskprog_init_debug(void)
62{
63 __UL_INIT_DEBUG(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG);
64}
d6b3ba41 65
740c36f6
KZ
66#ifdef HAVE_LIBREADLINE
67static char *rl_fgets(char *s, int n, FILE *stream, const char *prompt)
68{
69 char *p;
70
71 rl_outstream = stream;
72 p = readline(prompt);
73 if (!p)
74 return NULL;
75
84ab260a
KZ
76 strncpy(s, p, n);
77 s[n - 1] = '\0';
740c36f6
KZ
78 free(p);
79 return s;
80}
81#endif
82
d6b3ba41
KZ
83int get_user_reply(struct fdisk_context *cxt, const char *prompt,
84 char *buf, size_t bufsz)
85{
86 char *p;
87 size_t sz;
88
89 do {
740c36f6
KZ
90#ifdef HAVE_LIBREADLINE
91 if (isatty(STDIN_FILENO)) {
92 if (!rl_fgets(buf, bufsz, stdout, prompt)) {
93 if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
94 if (rl_fgets(buf, bufsz, stderr,
95 _("\nDo you really want to quit? "))
96 && !rpmatch(buf))
97 continue;
98 }
99 fdisk_unref_context(cxt);
100 exit(EXIT_FAILURE);
101 } else
102 break;
103 }
104 else
105#endif
106 {
107 fputs(prompt, stdout);
108 fflush(stdout);
109 if (!fgets(buf, bufsz, stdin)) {
110 if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
111 fprintf(stderr, _("\nDo you really want to quit? "));
d6b3ba41 112
740c36f6
KZ
113 if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
114 continue;
115 }
116 fdisk_unref_context(cxt);
117 exit(EXIT_FAILURE);
118 } else
119 break;
120 }
d6b3ba41
KZ
121 } while (1);
122
123 for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
124
125 if (p > buf)
126 memmove(buf, p, p - buf); /* remove blank space */
127 sz = strlen(buf);
128 if (sz && *(buf + sz - 1) == '\n')
129 *(buf + sz - 1) = '\0';
130
88141067 131 DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
d6b3ba41
KZ
132 return 0;
133}
134
135static int ask_menu(struct fdisk_context *cxt, struct fdisk_ask *ask,
136 char *buf, size_t bufsz)
137
138{
139 const char *q = fdisk_ask_get_query(ask);
140 int dft = fdisk_ask_menu_get_default(ask);
141
142 if (q) {
143 fputs(q, stdout); /* print header */
144 fputc('\n', stdout);
145 }
146
147 do {
148 char prompt[128];
68ddb136 149 int key, c, rc;
d6b3ba41
KZ
150 const char *name, *desc;
151 size_t i = 0;
152
153 /* print menu items */
154 while (fdisk_ask_menu_get_item(ask, i++, &key, &name, &desc) == 0)
155 fprintf(stdout, " %c %s (%s)\n", key, name, desc);
156
157 /* ask for key */
158 snprintf(prompt, sizeof(prompt), _("Select (default %c): "), dft);
68ddb136
KZ
159 rc = get_user_reply(cxt, prompt, buf, bufsz);
160 if (rc)
161 return rc;
d6b3ba41
KZ
162 if (!*buf) {
163 fdisk_info(cxt, _("Using default response %c."), dft);
164 c = dft;
165 } else
166 c = tolower(buf[0]);
167
168 /* check result */
169 i = 0;
170 while (fdisk_ask_menu_get_item(ask, i++, &key, NULL, NULL) == 0) {
171 if (c == key) {
172 fdisk_ask_menu_set_result(ask, c);
173 return 0; /* success */
174 }
175 }
176 fdisk_warnx(cxt, _("Value out of range."));
177 } while (1);
178
179 return -EINVAL;
180}
181
182
183#define tochar(num) ((int) ('a' + num - 1))
184static int ask_number(struct fdisk_context *cxt,
185 struct fdisk_ask *ask,
186 char *buf, size_t bufsz)
187{
188 char prompt[128] = { '\0' };
189 const char *q = fdisk_ask_get_query(ask);
190 const char *range = fdisk_ask_number_get_range(ask);
191
192 uint64_t dflt = fdisk_ask_number_get_default(ask),
193 low = fdisk_ask_number_get_low(ask),
194 high = fdisk_ask_number_get_high(ask);
195 int inchar = fdisk_ask_number_inchars(ask);
196
197 assert(q);
198
88141067 199 DBG(ASK, ul_debug("asking for number "
fdbd7bb9 200 "['%s', <%"PRIu64",%"PRIu64">, default=%"PRIu64", range: %s]",
d6b3ba41
KZ
201 q, low, high, dflt, range));
202
203 if (range && dflt >= low && dflt <= high) {
204 if (inchar)
205 snprintf(prompt, sizeof(prompt), _("%s (%s, default %c): "),
206 q, range, tochar(dflt));
207 else
fdbd7bb9 208 snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "),
d6b3ba41
KZ
209 q, range, dflt);
210
211 } else if (dflt >= low && dflt <= high) {
212 if (inchar)
213 snprintf(prompt, sizeof(prompt), _("%s (%c-%c, default %c): "),
214 q, tochar(low), tochar(high), tochar(dflt));
215 else
fdbd7bb9
RM
216 snprintf(prompt, sizeof(prompt),
217 _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "),
d6b3ba41
KZ
218 q, low, high, dflt);
219 } else if (inchar)
220 snprintf(prompt, sizeof(prompt), _("%s (%c-%c): "),
221 q, tochar(low), tochar(high));
222 else
fdbd7bb9 223 snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "),
d6b3ba41
KZ
224 q, low, high);
225
226 do {
227 int rc = get_user_reply(cxt, prompt, buf, bufsz);
228 uint64_t num;
229
230 if (rc)
231 return rc;
232 if (!*buf && dflt >= low && dflt <= high)
233 return fdisk_ask_number_set_result(ask, dflt);
234
235 if (isdigit_string(buf)) {
236 char *end;
237
238 errno = 0;
239 num = strtoumax(buf, &end, 10);
240 if (errno || buf == end || (end && *end))
241 continue;
242 } else if (inchar && isalpha(*buf)) {
243 num = tolower(*buf) - 'a' + 1;
244 } else
245 rc = -EINVAL;
246
247 if (rc == 0 && num >= low && num <= high)
248 return fdisk_ask_number_set_result(ask, num);
249
250 fdisk_warnx(cxt, _("Value out of range."));
251 } while (1);
252
253 return -1;
254}
255
256static int ask_offset(struct fdisk_context *cxt,
257 struct fdisk_ask *ask,
258 char *buf, size_t bufsz)
259{
260 char prompt[128] = { '\0' };
261 const char *q = fdisk_ask_get_query(ask);
262 const char *range = fdisk_ask_number_get_range(ask);
263
264 uint64_t dflt = fdisk_ask_number_get_default(ask),
265 low = fdisk_ask_number_get_low(ask),
266 high = fdisk_ask_number_get_high(ask),
267 base = fdisk_ask_number_get_base(ask);
268
269 assert(q);
270
fdbd7bb9 271 DBG(ASK, ul_debug("asking for offset ['%s', <%"PRIu64",%"PRIu64">, base=%"PRIu64", default=%"PRIu64", range: %s]",
d6b3ba41
KZ
272 q, low, high, base, dflt, range));
273
274 if (range && dflt >= low && dflt <= high)
fdbd7bb9
RM
275 snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "),
276 q, range, dflt);
d6b3ba41 277 else if (dflt >= low && dflt <= high)
fdbd7bb9
RM
278 snprintf(prompt, sizeof(prompt),
279 _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "),
280 q, low, high, dflt);
d6b3ba41 281 else
fdbd7bb9
RM
282 snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "),
283 q, low, high);
d6b3ba41
KZ
284
285 do {
262c94c2 286 uintmax_t num = 0;
d6b3ba41
KZ
287 char sig = 0, *p;
288 int pwr = 0;
289
290 int rc = get_user_reply(cxt, prompt, buf, bufsz);
291 if (rc)
292 return rc;
293 if (!*buf && dflt >= low && dflt <= high)
294 return fdisk_ask_number_set_result(ask, dflt);
295
296 p = buf;
297 if (*p == '+' || *p == '-') {
298 sig = *buf;
299 p++;
300 }
301
302 rc = parse_size(p, &num, &pwr);
303 if (rc)
304 continue;
88141067 305 DBG(ASK, ul_debug("parsed size: %ju", num));
d6b3ba41
KZ
306 if (sig && pwr) {
307 /* +{size}{K,M,...} specified, the "num" is in bytes */
308 uint64_t unit = fdisk_ask_number_get_unit(ask);
309 num += unit/2; /* round */
310 num /= unit;
311 }
312 if (sig == '+')
313 num += base;
314 else if (sig == '-')
315 num = base - num;
316
88141067 317 DBG(ASK, ul_debug("final offset: %ju [sig: %c, power: %d, %s]",
d6b3ba41
KZ
318 num, sig, pwr,
319 sig ? "relative" : "absolute"));
320 if (num >= low && num <= high) {
8c73e509 321 if (sig && pwr)
d6b3ba41 322 fdisk_ask_number_set_relative(ask, 1);
262c94c2 323 return fdisk_ask_number_set_result(ask, (uint64_t)num);
d6b3ba41
KZ
324 }
325 fdisk_warnx(cxt, _("Value out of range."));
326 } while (1);
327
328 return -1;
329}
330
331static unsigned int info_count;
332
412791a9 333static void fputs_info(struct fdisk_ask *ask, FILE *out)
d6b3ba41
KZ
334{
335 const char *msg;
d6b3ba41
KZ
336 assert(ask);
337
338 msg = fdisk_ask_print_get_mesg(ask);
d6b3ba41
KZ
339 if (!msg)
340 return;
341 if (info_count == 1)
342 fputc('\n', out);
412791a9
KZ
343
344 fputs(msg, out);
d6b3ba41
KZ
345 fputc('\n', out);
346}
347
348int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
349 void *data __attribute__((__unused__)))
350{
351 int rc = 0;
352 char buf[BUFSIZ];
353
354 assert(cxt);
355 assert(ask);
356
357 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_INFO)
358 info_count = 0;
359
360 switch(fdisk_ask_get_type(ask)) {
361 case FDISK_ASKTYPE_MENU:
362 return ask_menu(cxt, ask, buf, sizeof(buf));
363 case FDISK_ASKTYPE_NUMBER:
364 return ask_number(cxt, ask, buf, sizeof(buf));
365 case FDISK_ASKTYPE_OFFSET:
366 return ask_offset(cxt, ask, buf, sizeof(buf));
367 case FDISK_ASKTYPE_INFO:
152788aa
KZ
368 if (!fdisk_is_listonly(cxt))
369 info_count++;
412791a9 370 fputs_info(ask, stdout);
d6b3ba41
KZ
371 break;
372 case FDISK_ASKTYPE_WARNX:
95aae4fc 373 fflush(stdout);
496c979a 374 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
d6b3ba41
KZ
375 fputs(fdisk_ask_print_get_mesg(ask), stderr);
376 color_fdisable(stderr);
377 fputc('\n', stderr);
378 break;
379 case FDISK_ASKTYPE_WARN:
95aae4fc 380 fflush(stdout);
496c979a 381 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
d6b3ba41
KZ
382 fputs(fdisk_ask_print_get_mesg(ask), stderr);
383 errno = fdisk_ask_print_get_errno(ask);
384 fprintf(stderr, ": %m\n");
385 color_fdisable(stderr);
386 break;
387 case FDISK_ASKTYPE_YESNO:
388 fputc('\n', stdout);
2d129032
KZ
389 do {
390 int x;
391 fputs(fdisk_ask_get_query(ask), stdout);
392 rc = get_user_reply(cxt, _(" [Y]es/[N]o: "), buf, sizeof(buf));
393 if (rc)
394 break;
395 x = rpmatch(buf);
cd2a6f1c 396 if (x == RPMATCH_YES || x == RPMATCH_NO) {
2d129032
KZ
397 fdisk_ask_yesno_set_result(ask, x);
398 break;
399 }
400 } while(1);
88141067 401 DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
d6b3ba41
KZ
402 break;
403 case FDISK_ASKTYPE_STRING:
404 {
405 char prmt[BUFSIZ];
406 snprintf(prmt, sizeof(prmt), "%s: ", fdisk_ask_get_query(ask));
407 fputc('\n', stdout);
408 rc = get_user_reply(cxt, prmt, buf, sizeof(buf));
409 if (rc == 0)
410 fdisk_ask_string_set_result(ask, xstrdup(buf));
88141067 411 DBG(ASK, ul_debug("string ask: reply '%s' [rc=%d]", buf, rc));
d6b3ba41
KZ
412 break;
413 }
414 default:
415 warnx(_("internal error: unsupported dialog type %d"), fdisk_ask_get_type(ask));
416 return -EINVAL;
417 }
418 return rc;
419}
420
4bd02cdf 421static struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt)
d6b3ba41
KZ
422{
423 const char *q;
a745611d
KZ
424 struct fdisk_label *lb;
425
426 assert(cxt);
427 lb = fdisk_get_label(cxt, NULL);
d6b3ba41 428
a745611d 429 if (!lb)
d6b3ba41
KZ
430 return NULL;
431
a745611d 432 q = fdisk_label_has_code_parttypes(lb) ?
d6b3ba41
KZ
433 _("Partition type (type L to list all types): ") :
434 _("Hex code (type L to list all codes): ");
435 do {
436 char buf[256];
437 int rc = get_user_reply(cxt, q, buf, sizeof(buf));
438
439 if (rc)
440 break;
441
442 if (buf[1] == '\0' && toupper(*buf) == 'L')
443 list_partition_types(cxt);
444 else if (*buf)
a745611d 445 return fdisk_label_parse_parttype(lb, buf);
d6b3ba41
KZ
446 } while (1);
447
448 return NULL;
449}
450
8d2f4498 451
7b575fcc 452void list_partition_types(struct fdisk_context *cxt)
6dbe3af9 453{
76f17cf2 454 size_t ntypes = 0;
f45f5c83 455 struct fdisk_label *lb;
6dbe3af9 456
a745611d
KZ
457 assert(cxt);
458 lb = fdisk_get_label(cxt, NULL);
459 if (!lb)
460 return;
461 ntypes = fdisk_label_get_nparttypes(lb);
462 if (!ntypes)
7b575fcc 463 return;
2b6fc908 464
a745611d 465 if (fdisk_label_has_code_parttypes(lb)) {
7b575fcc
KZ
466 /*
467 * Prints in 4 columns in format <hex> <name>
468 */
76f17cf2
KZ
469 size_t last[4], done = 0, next = 0, size;
470 int i;
b8d22034 471
76f17cf2 472 size = ntypes;
7b575fcc
KZ
473
474 for (i = 3; i >= 0; i--)
475 last[3 - i] = done += (size + i - done) / (i + 1);
476 i = done = 0;
477
478 do {
479 #define NAME_WIDTH 15
480 char name[NAME_WIDTH * MB_LEN_MAX];
481 size_t width = NAME_WIDTH;
a745611d 482 const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, next);
7b575fcc
KZ
483 size_t ret;
484
e6d0c4c1 485 if (fdisk_parttype_get_name(t)) {
a745611d
KZ
486 printf("%c%2x ", i ? ' ' : '\n',
487 fdisk_parttype_get_code(t));
488 ret = mbsalign(_(fdisk_parttype_get_name(t)),
489 name, sizeof(name),
490 &width, MBS_ALIGN_LEFT, 0);
7b575fcc 491
76f17cf2 492 if (ret == (size_t)-1 || ret >= sizeof(name))
a745611d
KZ
493 printf("%-15.15s",
494 _(fdisk_parttype_get_name(t)));
76f17cf2
KZ
495 else
496 fputs(name, stdout);
497 }
7b575fcc
KZ
498
499 next = last[i++] + done;
500 if (i > 3 || next >= last[i]) {
501 i = 0;
502 next = ++done;
503 }
504 } while (done < last[0]);
505
506 } else {
507 /*
508 * Prints 1 column in format <idx> <name> <typestr>
509 */
76f17cf2 510 size_t i;
7b575fcc 511
8d2f4498
KZ
512 pager_open();
513
a745611d
KZ
514 for (i = 0; i < ntypes; i++) {
515 const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, i);
516 printf("%3zu %-30s %s\n", i + 1,
517 fdisk_parttype_get_name(t),
518 fdisk_parttype_get_string(t));
76f17cf2 519 }
8d2f4498
KZ
520
521 pager_close();
7b575fcc 522 }
6dbe3af9
KZ
523 putchar('\n');
524}
525
f02fecd1 526void toggle_dos_compatibility_flag(struct fdisk_context *cxt)
852ce62b 527{
6a632136 528 struct fdisk_label *lb = fdisk_get_label(cxt, "dos");
852ce62b
KZ
529 int flag;
530
531 if (!lb)
532 return;
533
534 flag = !fdisk_dos_is_compatible(lb);
09f0c3d9
KZ
535 fdisk_info(cxt, flag ?
536 _("DOS Compatibility flag is set (DEPRECATED!)") :
537 _("DOS Compatibility flag is not set"));
edd7b958 538
852ce62b
KZ
539 fdisk_dos_enable_compatible(lb, flag);
540
aa36c2cf 541 if (fdisk_is_label(cxt, DOS))
09f0c3d9 542 fdisk_reset_alignment(cxt); /* reset the current label */
6dbe3af9
KZ
543}
544
27ddd4f1 545void change_partition_type(struct fdisk_context *cxt)
e53ced85 546{
641a75ca 547 size_t i;
85151521
KZ
548 struct fdisk_parttype *t = NULL;
549 struct fdisk_partition *pa = NULL;
550 const char *old = NULL;
6dbe3af9 551
e3661531 552 assert(cxt);
e3661531 553
641a75ca 554 if (fdisk_ask_partnum(cxt, &i, FALSE))
24f4bbff 555 return;
2b6fc908 556
85151521
KZ
557 if (fdisk_get_partition(cxt, i, &pa)) {
558 fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
559 return;
560 }
561
562 t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
e6d0c4c1 563 old = t ? fdisk_parttype_get_name(t) : _("Unknown");
85151521
KZ
564
565 do {
566 t = ask_partition_type(cxt);
567 } while (!t);
568
85151521 569 if (fdisk_set_partition_type(cxt, i, t) == 0)
0477369a 570 fdisk_info(cxt,
85151521 571 _("Changed type of partition '%s' to '%s'."),
a745611d 572 old, t ? fdisk_parttype_get_name(t) : _("Unknown"));
85151521
KZ
573 else
574 fdisk_info(cxt,
575 _("Type of partition %zu is unchanged: %s."),
576 i + 1, old);
577
d6cfa8b3 578 fdisk_unref_partition(pa);
4bd02cdf 579 fdisk_unref_parttype(t);
6dbe3af9
KZ
580}
581
3e3b51b3
JLB
582int print_partition_info(struct fdisk_context *cxt)
583{
584 struct fdisk_partition *pa = NULL;
6360bcca 585 int rc = 0;
3e3b51b3
JLB
586 size_t i, nfields;
587 int *fields = NULL;
588 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
589
590 if ((rc = fdisk_ask_partnum(cxt, &i, FALSE)))
591 return rc;
592
593 if ((rc = fdisk_get_partition(cxt, i, &pa))) {
594 fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
595 return rc;
596 }
597
6360bcca 598 if ((rc = fdisk_label_get_fields_ids_all(lb, cxt, &fields, &nfields)))
3e3b51b3 599 goto clean_data;
3e3b51b3
JLB
600
601 for (i = 0; i < nfields; ++i) {
602 int id = fields[i];
603 char *data = NULL;
604 const struct fdisk_field *fd = fdisk_label_get_field(lb, id);
605
606 if (!fd)
607 continue;
608
609 rc = fdisk_partition_to_string(pa, cxt, id, &data);
610 if (rc < 0)
611 goto clean_data;
6360bcca
KZ
612 if (!data || !*data)
613 continue;
3e3b51b3
JLB
614 fdisk_info(cxt, _("%15s: %s"), fdisk_field_get_name(fd), data);
615 free(data);
616 }
617
618clean_data:
619 fdisk_unref_partition(pa);
620 free(fields);
621 return rc;
622}
623
e916600f
KZ
624static size_t skip_empty(const unsigned char *buf, size_t i, size_t sz)
625{
626 size_t next;
627 const unsigned char *p0 = buf + i;
628
629 for (next = i + 16; next < sz; next += 16) {
630 if (memcmp(p0, buf + next, 16) != 0)
631 break;
632 }
633
634 return next == i + 16 ? i : next;
635}
6dbe3af9 636
e916600f 637static void dump_buffer(off_t base, unsigned char *buf, size_t sz, int all)
09f0c3d9 638{
e916600f
KZ
639 size_t i, l, next = 0;
640
641 if (!buf)
642 return;
643 for (i = 0, l = 0; i < sz; i++, l++) {
644 if (l == 0) {
645 if (all == 0 && !next)
646 next = skip_empty(buf, i, sz);
fdbd7bb9 647 printf("%08jx ", (intmax_t)base + i);
e916600f
KZ
648 }
649 printf(" %02x", buf[i]);
650 if (l == 7) /* words separator */
651 fputs(" ", stdout);
652 else if (l == 15) {
653 fputc('\n', stdout); /* next line */
6dbe3af9 654 l = -1;
e916600f
KZ
655 if (next > i) {
656 printf("*\n");
657 i = next - 1;
658 }
659 next = 0;
6dbe3af9
KZ
660 }
661 }
662 if (l > 0)
663 printf("\n");
6dbe3af9
KZ
664}
665
e916600f 666static void dump_blkdev(struct fdisk_context *cxt, const char *name,
9bbcf43f 667 uint64_t offset, size_t size, int all)
e53ced85 668{
8eccde20
KZ
669 int fd = fdisk_get_devfd(cxt);
670
fdbd7bb9 671 fdisk_info(cxt, _("\n%s: offset = %"PRIu64", size = %zu bytes."),
e916600f
KZ
672 name, offset, size);
673
8eccde20
KZ
674 assert(fd >= 0);
675
9bbcf43f 676 if (lseek(fd, (off_t) offset, SEEK_SET) == (off_t) -1)
e916600f 677 fdisk_warn(cxt, _("cannot seek"));
5afed187
KZ
678 else {
679 unsigned char *buf = xmalloc(size);
680
8eccde20 681 if (read_all(fd, (char *) buf, size) != (ssize_t) size)
5afed187
KZ
682 fdisk_warn(cxt, _("cannot read"));
683 else
684 dump_buffer(offset, buf, size, all);
685 free(buf);
686 }
e916600f
KZ
687}
688
689void dump_firstsector(struct fdisk_context *cxt)
690{
691 int all = !isatty(STDOUT_FILENO);
692
e3661531 693 assert(cxt);
6dbe3af9 694
8eccde20 695 dump_blkdev(cxt, _("First sector"), 0, fdisk_get_sector_size(cxt), all);
e916600f
KZ
696}
697
698void dump_disklabel(struct fdisk_context *cxt)
699{
700 int all = !isatty(STDOUT_FILENO);
701 int i = 0;
702 const char *name = NULL;
9bbcf43f 703 uint64_t offset = 0;
e916600f
KZ
704 size_t size = 0;
705
706 assert(cxt);
e3661531 707
e916600f
KZ
708 while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size)
709 dump_blkdev(cxt, name, offset, size, all);
6dbe3af9
KZ
710}
711
0073a4cf 712static fdisk_sector_t get_dev_blocks(char *dev)
6da365de 713{
bbe67996 714 int fd, ret;
0073a4cf 715 fdisk_sector_t size;
6da365de
DB
716
717 if ((fd = open(dev, O_RDONLY)) < 0)
289dcc90 718 err(EXIT_FAILURE, _("cannot open %s"), dev);
0073a4cf 719 ret = blkdev_get_sectors(fd, (unsigned long long *) &size);
6da365de 720 close(fd);
bbe67996
SK
721 if (ret < 0)
722 err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), dev);
6da365de
DB
723 return size/2;
724}
725
e3a4aaa7
KZ
726static void __attribute__ ((__noreturn__)) usage(FILE *out)
727{
728 fputs(USAGE_HEADER, out);
729
730 fprintf(out,
731 _(" %1$s [options] <disk> change partition table\n"
732 " %1$s [options] -l [<disk>] list partition table(s)\n"),
733 program_invocation_short_name);
734
451dbcfa
BS
735 fputs(USAGE_SEPARATOR, out);
736 fputs(_("Display or manipulate a disk partition table.\n"), out);
737
e3a4aaa7
KZ
738 fputs(USAGE_OPTIONS, out);
739 fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out);
718b6f0c 740 fputs(_(" -B, --protect-boot don't erase bootbits when creating a new label\n"), out);
e3a4aaa7
KZ
741 fputs(_(" -c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)\n"), out);
742 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out);
5d51dc2a
KZ
743 fprintf(out,
744 " %s\n", USAGE_COLORS_DEFAULT);
9e930041 745 fputs(_(" -l, --list display partitions and exit\n"), out);
2d7dffeb 746 fputs(_(" -o, --output <list> output columns\n"), out);
e3a4aaa7
KZ
747 fputs(_(" -t, --type <type> recognize specified partition table type only\n"), out);
748 fputs(_(" -u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out);
749 fputs(_(" -s, --getsz display device size in 512-byte sectors [DEPRECATED]\n"), out);
354f8cc8 750 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out);
cb9a4b00 751 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out);
ba465623 752 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out);
e3a4aaa7
KZ
753
754 fputs(USAGE_SEPARATOR, out);
4b4e391a
KZ
755 fputs(_(" -C, --cylinders <number> specify the number of cylinders\n"), out);
756 fputs(_(" -H, --heads <number> specify the number of heads\n"), out);
757 fputs(_(" -S, --sectors <number> specify the number of sectors per track\n"), out);
e3a4aaa7
KZ
758
759 fputs(USAGE_SEPARATOR, out);
760 fputs(USAGE_HELP, out);
761 fputs(USAGE_VERSION, out);
762
fff8ad58
KZ
763 list_available_columns(out);
764
e3a4aaa7
KZ
765 fprintf(out, USAGE_MAN_TAIL("fdisk(8)"));
766 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
767}
768
769
5b72b3dd
KZ
770enum {
771 ACT_FDISK = 0, /* default */
772 ACT_LIST,
773 ACT_SHOWSIZE
774};
775
e1144767
DB
776int main(int argc, char **argv)
777{
e146ae4e 778 int rc, i, c, act = ACT_FDISK;
d0c9ddc3 779 int colormode = UL_COLORMODE_UNDEF;
cb9a4b00 780 int wipemode = WIPEMODE_AUTO;
4e0e8253 781 struct fdisk_context *cxt;
fff8ad58 782 char *outarg = NULL;
354f8cc8
KZ
783 enum {
784 OPT_BYTES = CHAR_MAX + 1
785 };
e3a4aaa7 786 static const struct option longopts[] = {
354f8cc8 787 { "bytes", no_argument, NULL, OPT_BYTES },
e3a4aaa7
KZ
788 { "color", optional_argument, NULL, 'L' },
789 { "compatibility", optional_argument, NULL, 'c' },
4b4e391a
KZ
790 { "cylinders", required_argument, NULL, 'C' },
791 { "heads", required_argument, NULL, 'H' },
792 { "sectors", required_argument, NULL, 'S' },
e3a4aaa7
KZ
793 { "getsz", no_argument, NULL, 's' },
794 { "help", no_argument, NULL, 'h' },
795 { "list", no_argument, NULL, 'l' },
796 { "sector-size", required_argument, NULL, 'b' },
797 { "type", required_argument, NULL, 't' },
798 { "units", optional_argument, NULL, 'u' },
799 { "version", no_argument, NULL, 'V' },
2d7dffeb 800 { "output", no_argument, NULL, 'o' },
aeb9a30b 801 { "protect-boot", no_argument, NULL, 'B' },
cb9a4b00 802 { "wipe", required_argument, NULL, 'w' },
ba465623 803 { "wipe-partitions",required_argument, NULL, 'W' },
e3a4aaa7
KZ
804 { NULL, 0, NULL, 0 }
805 };
806
7eda085c
KZ
807 setlocale(LC_ALL, "");
808 bindtextdomain(PACKAGE, LOCALEDIR);
809 textdomain(PACKAGE);
b2d28533 810 atexit(close_stdout);
2b6fc908 811
4e0e8253 812 fdisk_init_debug(0);
053939a4 813 scols_init_debug(0);
e6d0c4c1
KZ
814 fdiskprog_init_debug();
815
4e0e8253
KZ
816 cxt = fdisk_new_context();
817 if (!cxt)
818 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
819
6a632136 820 fdisk_set_ask(cxt, ask_callback, NULL);
416c43a9 821
ba465623 822 while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::o:sS:t:u::vVw:W:",
e3a4aaa7 823 longopts, NULL)) != -1) {
2b6fc908
KZ
824 switch (c) {
825 case 'b':
6bcd192c
KZ
826 {
827 size_t sz = strtou32_or_err(optarg,
828 _("invalid sector size argument"));
829 if (sz != 512 && sz != 1024 && sz != 2048 && sz != 4096)
7c1db6b4 830 usage(stderr);
6bcd192c 831 fdisk_save_user_sector_size(cxt, sz, sz);
2b6fc908 832 break;
6bcd192c 833 }
9ff89c5d 834 case 'B':
aeb9a30b
KZ
835 fdisk_enable_bootbits_protection(cxt, 1);
836 break;
0e6f4a20 837 case 'C':
1653f0b0
KZ
838 fdisk_save_user_geometry(cxt,
839 strtou32_or_err(optarg,
840 _("invalid cylinders argument")),
841 0, 0);
0e6f4a20 842 break;
78498b7b 843 case 'c':
852ce62b 844 if (optarg) {
2b0bc17b 845 /* this setting is independent on the current
e3a4aaa7
KZ
846 * actively used label
847 */
848 char *p = *optarg == '=' ? optarg + 1 : optarg;
6a632136 849 struct fdisk_label *lb = fdisk_get_label(cxt, "dos");
e3a4aaa7 850
852ce62b
KZ
851 if (!lb)
852 err(EXIT_FAILURE, _("not found DOS label driver"));
e3a4aaa7 853 if (strcmp(p, "dos") == 0)
852ce62b 854 fdisk_dos_enable_compatible(lb, TRUE);
e3a4aaa7 855 else if (strcmp(p, "nondos") == 0)
852ce62b 856 fdisk_dos_enable_compatible(lb, FALSE);
e3a4aaa7
KZ
857 else {
858 warnx(_("unknown compatibility mode '%s'"), p);
852ce62b 859 usage(stderr);
e3a4aaa7 860 }
852ce62b
KZ
861 }
862 /* use default if no optarg specified */
78498b7b 863 break;
0e6f4a20 864 case 'H':
1653f0b0
KZ
865 fdisk_save_user_geometry(cxt, 0,
866 strtou32_or_err(optarg,
867 _("invalid heads argument")),
868 0);
0e6f4a20
KZ
869 break;
870 case 'S':
1653f0b0
KZ
871 fdisk_save_user_geometry(cxt, 0, 0,
872 strtou32_or_err(optarg,
873 _("invalid sectors argument")));
0e6f4a20 874 break;
2b6fc908 875 case 'l':
5b72b3dd 876 act = ACT_LIST;
2b6fc908 877 break;
80a1712f 878 case 'L':
5d51dc2a 879 colormode = UL_COLORMODE_AUTO;
80a1712f
KZ
880 if (optarg)
881 colormode = colormode_or_err(optarg,
882 _("unsupported color mode"));
883 break;
fff8ad58
KZ
884 case 'o':
885 outarg = optarg;
886 break;
2b6fc908 887 case 's':
5b72b3dd 888 act = ACT_SHOWSIZE;
2b6fc908 889 break;
565b7da6
KZ
890 case 't':
891 {
892 struct fdisk_label *lb = NULL;
893
6a632136 894 while (fdisk_next_label(cxt, &lb) == 0)
565b7da6
KZ
895 fdisk_label_set_disabled(lb, 1);
896
6a632136 897 lb = fdisk_get_label(cxt, optarg);
565b7da6
KZ
898 if (!lb)
899 errx(EXIT_FAILURE, _("unsupported disklabel: %s"), optarg);
900 fdisk_label_set_disabled(lb, 0);
1ce1a112 901 break;
565b7da6 902 }
2b6fc908 903 case 'u':
ec10aa67
KZ
904 if (optarg && *optarg == '=')
905 optarg++;
6a632136 906 if (fdisk_set_unit(cxt, optarg) != 0)
7c1db6b4 907 usage(stderr);
2b6fc908 908 break;
e3a4aaa7
KZ
909 case 'V': /* preferred for util-linux */
910 case 'v': /* for backward compatibility only */
a2482eb3
DB
911 printf(UTIL_LINUX_VERSION);
912 return EXIT_SUCCESS;
cb9a4b00
KZ
913 case 'w':
914 wipemode = wipemode_from_string(optarg);
915 if (wipemode < 0)
916 errx(EXIT_FAILURE, _("unsupported wipe mode"));
917 break;
ba465623
KZ
918 case 'W':
919 pwipemode = wipemode_from_string(optarg);
920 if (pwipemode < 0)
921 errx(EXIT_FAILURE, _("unsupported wipe mode"));
922 break;
e1144767
DB
923 case 'h':
924 usage(stdout);
354f8cc8
KZ
925 case OPT_BYTES:
926 fdisk_set_size_unit(cxt, FDISK_SIZEUNIT_BYTES);
927 break;
2b6fc908 928 default:
7c1db6b4 929 usage(stderr);
2b6fc908 930 }
6dbe3af9 931 }
2b6fc908 932
6bcd192c
KZ
933 if (argc-optind != 1 && fdisk_has_user_device_properties(cxt))
934 warnx(_("The device properties (sector size and geometry) should"
935 " be used with one specified device only."));
7eda085c 936
d0c9ddc3 937 colors_init(colormode, "fdisk");
ffcf0540 938
5b72b3dd
KZ
939 switch (act) {
940 case ACT_LIST:
6a632136 941 fdisk_enable_listonly(cxt, 1);
fff8ad58 942 init_fields(cxt, outarg, NULL);
ffcf0540 943
2b6fc908
KZ
944 if (argc > optind) {
945 int k;
04c163ed 946 int ct = 0;
8fdd483c
KZ
947
948 for (rc = 0, k = optind; k < argc; k++) {
04c163ed
TV
949 if (ct)
950 fputs("\n\n", stdout);
8fdd483c
KZ
951
952 rc += print_device_pt(cxt, argv[k], 1, 0);
04c163ed 953 ct++;
04c163ed 954 }
8fdd483c
KZ
955 if (rc)
956 return EXIT_FAILURE;
ea4824f1 957 } else
d2c47697 958 print_all_devices_pt(cxt, 0);
5b72b3dd 959 break;
2b6fc908 960
5b72b3dd 961 case ACT_SHOWSIZE:
9564e46c 962 /* deprecated */
5b72b3dd 963 if (argc - optind <= 0)
7c1db6b4 964 usage(stderr);
2b6fc908 965
6da365de 966 for (i = optind; i < argc; i++) {
0073a4cf
KZ
967 uintmax_t blks = get_dev_blocks(argv[i]);
968
5b72b3dd 969 if (argc - optind == 1)
0073a4cf 970 printf("%ju\n", blks);
2b6fc908 971 else
0073a4cf 972 printf("%s: %ju\n", argv[i], blks);
6dbe3af9 973 }
5b72b3dd 974 break;
6dbe3af9 975
5b72b3dd
KZ
976 case ACT_FDISK:
977 if (argc-optind != 1)
978 usage(stderr);
7eda085c 979
5b72b3dd 980 /* Here starts interactive mode, use fdisk_{warn,info,..} functions */
496c979a 981 color_scheme_enable("welcome", UL_COLOR_GREEN);
09f0c3d9 982 fdisk_info(cxt, _("Welcome to fdisk (%s)."), PACKAGE_STRING);
80a1712f
KZ
983 color_disable();
984 fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n"
985 "Be careful before using the write command.\n"));
2d8988bd 986
6a632136 987 rc = fdisk_assign_device(cxt, argv[optind], 0);
e146ae4e 988 if (rc == -EACCES) {
6a632136 989 rc = fdisk_assign_device(cxt, argv[optind], 1);
e146ae4e 990 if (rc == 0)
62eea9ce 991 fdisk_warnx(cxt, _("Device is open in read-only mode."));
e146ae4e
KZ
992 }
993 if (rc)
2d8988bd
KZ
994 err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
995
5b72b3dd 996 fflush(stdout);
56c07b96 997
cb9a4b00
KZ
998 if (fdisk_get_collision(cxt)) {
999 int dowipe = wipemode == WIPEMODE_ALWAYS ? 1 : 0;
1000
cb19f547 1001 fdisk_warnx(cxt, _("Device %s already contains a %s signature."),
cb9a4b00
KZ
1002 argv[optind], fdisk_get_collision(cxt));
1003
1004 if (isatty(STDIN_FILENO) && wipemode == WIPEMODE_AUTO)
1005 dowipe = 1; /* do it in interactive mode */
1006
1007 fdisk_enable_wipe(cxt, dowipe);
1008 if (dowipe)
1009 fdisk_warnx(cxt, _(
cb19f547 1010 "The signature will be removed by a write command."));
cb9a4b00
KZ
1011 else
1012 fdisk_warnx(cxt, _(
1013 "It is strongly recommended to wipe the device with "
1014 "wipefs(8), in order to avoid possible collisions."));
1015 }
aa36c2cf 1016 if (!fdisk_has_label(cxt)) {
2d8988bd 1017 fdisk_info(cxt, _("Device does not contain a recognized partition table."));
5b72b3dd 1018 fdisk_create_disklabel(cxt, NULL);
433d05ff 1019
aa36c2cf 1020 } else if (fdisk_is_label(cxt, GPT) && fdisk_gpt_is_hybrid(cxt))
433d05ff 1021 fdisk_warnx(cxt, _(
09af3db4 1022 "A hybrid GPT was detected. You have to sync "
433d05ff 1023 "the hybrid MBR manually (expert command 'M')."));
6dbe3af9 1024
fff8ad58
KZ
1025 init_fields(cxt, outarg, NULL); /* -o <columns> */
1026
5b72b3dd
KZ
1027 while (1)
1028 process_fdisk_menu(&cxt);
1029 }
9777759a 1030
c7119037 1031 fdisk_unref_context(cxt);
5b72b3dd 1032 return EXIT_SUCCESS;
6dbe3af9 1033}