]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * SPDX-License-Identifier: GPL-1.0-or-later | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 1 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk) | |
10 | * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org> | |
11 | * | |
12 | * Copyright (C) 2007-2013 Karel Zak <kzak@redhat.com> | |
13 | */ | |
14 | #include <unistd.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <fcntl.h> | |
19 | #include <ctype.h> | |
20 | #include <errno.h> | |
21 | #include <getopt.h> | |
22 | #include <sys/stat.h> | |
23 | #include <sys/time.h> | |
24 | #include <time.h> | |
25 | #include <limits.h> | |
26 | #include <signal.h> | |
27 | #include <poll.h> | |
28 | #include <libsmartcols.h> | |
29 | #ifdef HAVE_LIBREADLINE | |
30 | # define _FUNCTION_DEF | |
31 | # include <readline/readline.h> | |
32 | #endif | |
33 | ||
34 | #include "c.h" | |
35 | #include "xalloc.h" | |
36 | #include "all-io.h" | |
37 | #include "nls.h" | |
38 | #include "rpmatch.h" | |
39 | #include "blkdev.h" | |
40 | #include "mbsalign.h" | |
41 | #include "pathnames.h" | |
42 | #include "canonicalize.h" | |
43 | #include "strutils.h" | |
44 | #include "closestream.h" | |
45 | #include "pager.h" | |
46 | ||
47 | #include "fdisk.h" | |
48 | ||
49 | #include "pt-sun.h" /* to toggle flags */ | |
50 | ||
51 | #ifdef HAVE_LINUX_COMPILER_H | |
52 | # include <linux/compiler.h> | |
53 | #endif | |
54 | #ifdef HAVE_LINUX_BLKPG_H | |
55 | # include <linux/blkpg.h> | |
56 | #endif | |
57 | ||
58 | #ifdef __linux__ | |
59 | # ifdef HAVE_LINUX_FS_H | |
60 | # include <linux/fs.h> | |
61 | # endif | |
62 | #endif | |
63 | ||
64 | int pwipemode = WIPEMODE_AUTO; | |
65 | int device_is_used; | |
66 | int is_interactive; | |
67 | struct fdisk_table *original_layout; | |
68 | ||
69 | static int wipemode = WIPEMODE_AUTO; | |
70 | ||
71 | /* | |
72 | * fdisk debug stuff (see fdisk.h and include/debug.h) | |
73 | */ | |
74 | UL_DEBUG_DEFINE_MASK(fdisk); | |
75 | UL_DEBUG_DEFINE_MASKNAMES(fdisk) = UL_DEBUG_EMPTY_MASKNAMES; | |
76 | ||
77 | static void fdiskprog_init_debug(void) | |
78 | { | |
79 | __UL_INIT_DEBUG_FROM_ENV(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG); | |
80 | } | |
81 | ||
82 | static void reply_sighandler(int sig __attribute__((unused))) | |
83 | { | |
84 | DBG(ASK, ul_debug("got signal")); | |
85 | } | |
86 | ||
87 | static int reply_running; | |
88 | ||
89 | #ifdef HAVE_LIBREADLINE | |
90 | static char *reply_line; | |
91 | ||
92 | static void reply_linehandler(char *line) | |
93 | { | |
94 | reply_line = line; | |
95 | reply_running = 0; | |
96 | rl_callback_handler_remove(); /* avoid duplicate prompt */ | |
97 | } | |
98 | #endif | |
99 | ||
100 | int get_user_reply(const char *prompt, char *buf, size_t bufsz) | |
101 | { | |
102 | struct sigaction oldact, act = { | |
103 | .sa_handler = reply_sighandler | |
104 | }; | |
105 | struct pollfd fds[] = { | |
106 | { .fd = fileno(stdin), .events = POLLIN } | |
107 | }; | |
108 | size_t sz; | |
109 | int ret = 0; | |
110 | ||
111 | DBG(ASK, ul_debug("asking for user reply %s", is_interactive ? "[interactive]" : "")); | |
112 | ||
113 | sigemptyset(&act.sa_mask); | |
114 | sigaction(SIGINT, &act, &oldact); | |
115 | ||
116 | #ifdef HAVE_LIBREADLINE | |
117 | if (is_interactive) | |
118 | rl_callback_handler_install(prompt, reply_linehandler); | |
119 | #endif | |
120 | errno = 0; | |
121 | reply_running = 1; | |
122 | do { | |
123 | int rc; | |
124 | ||
125 | *buf = '\0'; | |
126 | #ifdef HAVE_LIBREADLINE | |
127 | if (!is_interactive) | |
128 | #endif | |
129 | { | |
130 | fputs(prompt, stdout); | |
131 | fflush(stdout); | |
132 | } | |
133 | ||
134 | rc = poll(fds, 1, -1); | |
135 | if (rc == -1 && errno == EINTR) { /* interrupted by signal */ | |
136 | DBG(ASK, ul_debug("cancel by CTRL+C")); | |
137 | ret = -ECANCELED; | |
138 | goto done; | |
139 | } | |
140 | if (rc == -1 && errno != EAGAIN) { /* error */ | |
141 | ret = -errno; | |
142 | goto done; | |
143 | } | |
144 | #ifdef HAVE_LIBREADLINE | |
145 | if (is_interactive) { | |
146 | /* read input and copy to buf[] */ | |
147 | rl_callback_read_char(); | |
148 | if (!reply_running && reply_line) { | |
149 | sz = strlen(reply_line); | |
150 | if (sz == 0) | |
151 | buf[sz++] = '\n'; | |
152 | else | |
153 | memcpy(buf, reply_line, min(sz, bufsz)); | |
154 | buf[min(sz, bufsz - 1)] = '\0'; | |
155 | free(reply_line); | |
156 | reply_line = NULL; | |
157 | } | |
158 | } else | |
159 | #endif | |
160 | { | |
161 | if (!fgets(buf, bufsz, stdin)) | |
162 | *buf = '\0'; | |
163 | break; | |
164 | } | |
165 | } while (reply_running); | |
166 | ||
167 | if (!*buf) { | |
168 | DBG(ASK, ul_debug("cancel by CTRL+D")); | |
169 | ret = -ECANCELED; | |
170 | clearerr(stdin); | |
171 | goto done; | |
172 | } | |
173 | ||
174 | /* | |
175 | * cleanup the reply | |
176 | */ | |
177 | sz = ltrim_whitespace((unsigned char *) buf); | |
178 | if (sz && *(buf + sz - 1) == '\n') | |
179 | *(buf + sz - 1) = '\0'; | |
180 | ||
181 | done: | |
182 | #ifdef HAVE_LIBREADLINE | |
183 | if (is_interactive) | |
184 | rl_callback_handler_remove(); | |
185 | #endif | |
186 | sigaction(SIGINT, &oldact, NULL); | |
187 | DBG(ASK, ul_debug("user's reply: >>>%s<<< [rc=%d]", buf, ret)); | |
188 | return ret; | |
189 | } | |
190 | ||
191 | static int ask_menu(struct fdisk_context *cxt, struct fdisk_ask *ask, | |
192 | char *buf, size_t bufsz) | |
193 | ||
194 | { | |
195 | const char *q = fdisk_ask_get_query(ask); | |
196 | int dft = fdisk_ask_menu_get_default(ask); | |
197 | ||
198 | if (q) { | |
199 | fputs(q, stdout); /* print header */ | |
200 | fputc('\n', stdout); | |
201 | } | |
202 | ||
203 | do { | |
204 | char prompt[128]; | |
205 | int key, c, rc; | |
206 | const char *name, *desc; | |
207 | size_t i = 0; | |
208 | ||
209 | /* print menu items */ | |
210 | while (fdisk_ask_menu_get_item(ask, i++, &key, &name, &desc) == 0) { | |
211 | if (desc) | |
212 | fprintf(stdout, " %c %s (%s)\n", key, name, desc); | |
213 | else | |
214 | fprintf(stdout, " %c %s\n", key, name); | |
215 | } | |
216 | ||
217 | /* ask for key */ | |
218 | snprintf(prompt, sizeof(prompt), _("Select (default %c): "), dft); | |
219 | rc = get_user_reply(prompt, buf, bufsz); | |
220 | if (rc) | |
221 | return rc; | |
222 | if (!*buf) { | |
223 | fdisk_info(cxt, _("Using default response %c."), dft); | |
224 | c = dft; | |
225 | } else | |
226 | c = tolower(buf[0]); | |
227 | ||
228 | /* check result */ | |
229 | i = 0; | |
230 | while (fdisk_ask_menu_get_item(ask, i++, &key, NULL, NULL) == 0) { | |
231 | if (c == key) { | |
232 | fdisk_ask_menu_set_result(ask, c); | |
233 | return 0; /* success */ | |
234 | } | |
235 | } | |
236 | fdisk_warnx(cxt, _("Value out of range.")); | |
237 | } while (1); | |
238 | ||
239 | return -EINVAL; | |
240 | } | |
241 | ||
242 | ||
243 | #define tochar(num) ((int) ('a' + num - 1)) | |
244 | static int ask_number(struct fdisk_context *cxt, | |
245 | struct fdisk_ask *ask, | |
246 | char *buf, size_t bufsz) | |
247 | { | |
248 | char prompt[128] = { '\0' }; | |
249 | const char *q = fdisk_ask_get_query(ask); | |
250 | const char *range = fdisk_ask_number_get_range(ask); | |
251 | ||
252 | uint64_t dflt = fdisk_ask_number_get_default(ask), | |
253 | low = fdisk_ask_number_get_low(ask), | |
254 | high = fdisk_ask_number_get_high(ask); | |
255 | int inchar = fdisk_ask_number_inchars(ask); | |
256 | ||
257 | assert(q); | |
258 | ||
259 | DBG(ASK, ul_debug("asking for number " | |
260 | "['%s', <%"PRIu64",%"PRIu64">, default=%"PRIu64", range: %s]", | |
261 | q, low, high, dflt, range)); | |
262 | ||
263 | if (range && dflt >= low && dflt <= high) { | |
264 | if (inchar) | |
265 | snprintf(prompt, sizeof(prompt), _("%s (%s, default %c): "), | |
266 | q, range, tochar(dflt)); | |
267 | else | |
268 | snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "), | |
269 | q, range, dflt); | |
270 | ||
271 | } else if (dflt >= low && dflt <= high) { | |
272 | if (inchar) | |
273 | snprintf(prompt, sizeof(prompt), _("%s (%c-%c, default %c): "), | |
274 | q, tochar(low), tochar(high), tochar(dflt)); | |
275 | else | |
276 | snprintf(prompt, sizeof(prompt), | |
277 | _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "), | |
278 | q, low, high, dflt); | |
279 | } else if (inchar) | |
280 | snprintf(prompt, sizeof(prompt), _("%s (%c-%c): "), | |
281 | q, tochar(low), tochar(high)); | |
282 | else | |
283 | snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "), | |
284 | q, low, high); | |
285 | ||
286 | do { | |
287 | int rc = get_user_reply(prompt, buf, bufsz); | |
288 | uint64_t num = 0; | |
289 | ||
290 | if (rc) | |
291 | return rc; | |
292 | if (!*buf && dflt >= low && dflt <= high) | |
293 | return fdisk_ask_number_set_result(ask, dflt); | |
294 | ||
295 | if (isdigit_string(buf)) { | |
296 | char *end; | |
297 | ||
298 | errno = 0; | |
299 | num = strtoumax(buf, &end, 10); | |
300 | if (errno || buf == end || (end && *end)) | |
301 | continue; | |
302 | } else if (inchar && isalpha(*buf)) { | |
303 | num = tolower(*buf) - 'a' + 1; | |
304 | } else | |
305 | rc = -EINVAL; | |
306 | ||
307 | if (rc == 0 && num >= low && num <= high) | |
308 | return fdisk_ask_number_set_result(ask, num); | |
309 | ||
310 | fdisk_warnx(cxt, _("Value out of range.")); | |
311 | } while (1); | |
312 | ||
313 | return -1; | |
314 | } | |
315 | ||
316 | static int ask_offset(struct fdisk_context *cxt, | |
317 | struct fdisk_ask *ask, | |
318 | char *buf, size_t bufsz) | |
319 | { | |
320 | char prompt[128] = { '\0' }; | |
321 | const char *q = fdisk_ask_get_query(ask); | |
322 | const char *range = fdisk_ask_number_get_range(ask); | |
323 | ||
324 | uint64_t dflt = fdisk_ask_number_get_default(ask), | |
325 | low = fdisk_ask_number_get_low(ask), | |
326 | high = fdisk_ask_number_get_high(ask), | |
327 | base = fdisk_ask_number_get_base(ask); | |
328 | ||
329 | assert(q); | |
330 | ||
331 | DBG(ASK, ul_debug("asking for offset ['%s', <%"PRIu64",%"PRIu64">, base=%"PRIu64", default=%"PRIu64", range: %s]", | |
332 | q, low, high, base, dflt, range)); | |
333 | ||
334 | if (range && dflt >= low && dflt <= high) | |
335 | snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "), | |
336 | q, range, dflt); | |
337 | else if (dflt >= low && dflt <= high) | |
338 | snprintf(prompt, sizeof(prompt), | |
339 | _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "), | |
340 | q, low, high, dflt); | |
341 | else | |
342 | snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "), | |
343 | q, low, high); | |
344 | ||
345 | do { | |
346 | uintmax_t num = 0; | |
347 | char sig = 0, *p; | |
348 | int pwr = 0; | |
349 | ||
350 | int rc = get_user_reply(prompt, buf, bufsz); | |
351 | if (rc) | |
352 | return rc; | |
353 | if (!*buf && dflt >= low && dflt <= high) | |
354 | return fdisk_ask_number_set_result(ask, dflt); | |
355 | ||
356 | p = buf; | |
357 | if (*p == '+' || *p == '-') { | |
358 | sig = *buf; | |
359 | p++; | |
360 | } | |
361 | ||
362 | rc = ul_parse_size(p, &num, &pwr); | |
363 | if (rc) | |
364 | continue; | |
365 | DBG(ASK, ul_debug("parsed size: %ju", num)); | |
366 | if (sig && pwr) { | |
367 | /* +{size}{K,M,...} specified, the "num" is in bytes */ | |
368 | uint64_t unit = fdisk_ask_number_get_unit(ask); | |
369 | num += unit/2; /* round */ | |
370 | num /= unit; | |
371 | } | |
372 | if (sig == '+') | |
373 | num += base; | |
374 | else if (sig == '-' && fdisk_ask_number_is_wrap_negative(ask)) | |
375 | num = high - num; | |
376 | else if (sig == '-') | |
377 | num = base - num; | |
378 | ||
379 | DBG(ASK, ul_debug("final offset: %ju [sig: %c, power: %d, %s]", | |
380 | num, sig, pwr, | |
381 | sig ? "relative" : "absolute")); | |
382 | if (num >= low && num <= high) { | |
383 | if (sig && pwr) | |
384 | fdisk_ask_number_set_relative(ask, 1); | |
385 | return fdisk_ask_number_set_result(ask, (uint64_t)num); | |
386 | } | |
387 | fdisk_warnx(cxt, _("Value out of range.")); | |
388 | } while (1); | |
389 | ||
390 | return -1; | |
391 | } | |
392 | ||
393 | static unsigned int info_count; | |
394 | ||
395 | static void fputs_info(struct fdisk_ask *ask, FILE *out) | |
396 | { | |
397 | const char *msg; | |
398 | assert(ask); | |
399 | ||
400 | msg = fdisk_ask_print_get_mesg(ask); | |
401 | if (!msg) | |
402 | return; | |
403 | if (info_count == 1) | |
404 | fputc('\n', out); | |
405 | ||
406 | fputs(msg, out); | |
407 | fputc('\n', out); | |
408 | } | |
409 | ||
410 | int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, | |
411 | void *data __attribute__((__unused__))) | |
412 | { | |
413 | int rc = 0; | |
414 | char buf[BUFSIZ] = { '\0' }; | |
415 | ||
416 | assert(cxt); | |
417 | assert(ask); | |
418 | ||
419 | if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_INFO) | |
420 | info_count = 0; | |
421 | ||
422 | switch(fdisk_ask_get_type(ask)) { | |
423 | case FDISK_ASKTYPE_MENU: | |
424 | return ask_menu(cxt, ask, buf, sizeof(buf)); | |
425 | case FDISK_ASKTYPE_NUMBER: | |
426 | return ask_number(cxt, ask, buf, sizeof(buf)); | |
427 | case FDISK_ASKTYPE_OFFSET: | |
428 | return ask_offset(cxt, ask, buf, sizeof(buf)); | |
429 | case FDISK_ASKTYPE_INFO: | |
430 | if (!fdisk_is_listonly(cxt)) | |
431 | info_count++; | |
432 | fputs_info(ask, stdout); | |
433 | break; | |
434 | case FDISK_ASKTYPE_WARNX: | |
435 | fflush(stdout); | |
436 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
437 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
438 | color_fdisable(stderr); | |
439 | fputc('\n', stderr); | |
440 | break; | |
441 | case FDISK_ASKTYPE_WARN: | |
442 | fflush(stdout); | |
443 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
444 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
445 | errno = fdisk_ask_print_get_errno(ask); | |
446 | fprintf(stderr, ": %m\n"); | |
447 | color_fdisable(stderr); | |
448 | break; | |
449 | case FDISK_ASKTYPE_YESNO: | |
450 | fputc('\n', stdout); | |
451 | do { | |
452 | int x; | |
453 | fputs(fdisk_ask_get_query(ask), stdout); | |
454 | rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf)); | |
455 | if (rc) | |
456 | break; | |
457 | x = rpmatch(buf); | |
458 | if (x == RPMATCH_YES || x == RPMATCH_NO) { | |
459 | fdisk_ask_yesno_set_result(ask, x); | |
460 | break; | |
461 | } | |
462 | } while(1); | |
463 | DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc)); | |
464 | break; | |
465 | case FDISK_ASKTYPE_STRING: | |
466 | { | |
467 | char prmt[BUFSIZ]; | |
468 | snprintf(prmt, sizeof(prmt), "%s: ", fdisk_ask_get_query(ask)); | |
469 | fputc('\n', stdout); | |
470 | rc = get_user_reply(prmt, buf, sizeof(buf)); | |
471 | if (rc == 0) | |
472 | fdisk_ask_string_set_result(ask, xstrdup(buf)); | |
473 | DBG(ASK, ul_debug("string ask: reply '%s' [rc=%d]", buf, rc)); | |
474 | break; | |
475 | } | |
476 | default: | |
477 | warnx(_("internal error: unsupported dialog type %d"), fdisk_ask_get_type(ask)); | |
478 | return -EINVAL; | |
479 | } | |
480 | return rc; | |
481 | } | |
482 | ||
483 | static struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt, int *canceled) | |
484 | { | |
485 | const char *q; | |
486 | struct fdisk_label *lb; | |
487 | ||
488 | assert(cxt); | |
489 | lb = fdisk_get_label(cxt, NULL); | |
490 | ||
491 | if (!lb) | |
492 | return NULL; | |
493 | ||
494 | *canceled = 0; | |
495 | ||
496 | if (fdisk_label_has_parttypes_shortcuts(lb)) | |
497 | q = fdisk_label_has_code_parttypes(lb) ? | |
498 | _("Hex code or alias (type L to list all): ") : | |
499 | _("Partition type or alias (type L to list all): "); | |
500 | else | |
501 | q = fdisk_label_has_code_parttypes(lb) ? | |
502 | _("Hex code (type L to list all codes): ") : | |
503 | _("Partition type (type L to list all types): "); | |
504 | do { | |
505 | char buf[256] = { '\0' }; | |
506 | int rc = get_user_reply(q, buf, sizeof(buf)); | |
507 | ||
508 | if (rc) { | |
509 | if (rc == -ECANCELED) | |
510 | *canceled = 1; | |
511 | break; | |
512 | } | |
513 | ||
514 | if (buf[1] == '\0' && toupper(*buf) == 'L') | |
515 | list_partition_types(cxt); | |
516 | else if (*buf) { | |
517 | struct fdisk_parttype *t = fdisk_label_advparse_parttype(lb, buf, | |
518 | FDISK_PARTTYPE_PARSE_DATA | |
519 | | FDISK_PARTTYPE_PARSE_ALIAS | |
520 | | FDISK_PARTTYPE_PARSE_NAME | |
521 | | FDISK_PARTTYPE_PARSE_SEQNUM); | |
522 | if (!t) | |
523 | fdisk_info(cxt, _("Failed to parse '%s' partition type."), buf); | |
524 | return t; | |
525 | } | |
526 | } while (1); | |
527 | ||
528 | return NULL; | |
529 | } | |
530 | ||
531 | ||
532 | void list_partition_types(struct fdisk_context *cxt) | |
533 | { | |
534 | size_t ntypes = 0, next = 0; | |
535 | struct fdisk_label *lb; | |
536 | int pager = 0; | |
537 | ||
538 | assert(cxt); | |
539 | lb = fdisk_get_label(cxt, NULL); | |
540 | if (!lb) | |
541 | return; | |
542 | ntypes = fdisk_label_get_nparttypes(lb); | |
543 | if (!ntypes) | |
544 | return; | |
545 | ||
546 | if (fdisk_label_has_code_parttypes(lb)) { | |
547 | /* | |
548 | * Prints in 4 columns in format <hex> <name> | |
549 | */ | |
550 | size_t last[4], done = 0, size; | |
551 | int i; | |
552 | ||
553 | size = ntypes; | |
554 | ||
555 | for (i = 3; i >= 0; i--) | |
556 | last[3 - i] = done += (size + i - done) / (i + 1); | |
557 | i = done = 0; | |
558 | ||
559 | do { | |
560 | #define NAME_WIDTH 15 | |
561 | char name[NAME_WIDTH * MB_LEN_MAX]; | |
562 | size_t width = NAME_WIDTH; | |
563 | const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, next); | |
564 | size_t ret; | |
565 | ||
566 | if (fdisk_parttype_get_name(t)) { | |
567 | printf("%s%02x ", i ? " " : "\n", | |
568 | fdisk_parttype_get_code(t)); | |
569 | ret = mbsalign(_(fdisk_parttype_get_name(t)), | |
570 | name, sizeof(name), | |
571 | &width, MBS_ALIGN_LEFT, 0); | |
572 | ||
573 | if (ret == (size_t)-1 || ret >= sizeof(name)) | |
574 | printf("%-15.15s", | |
575 | _(fdisk_parttype_get_name(t))); | |
576 | else | |
577 | fputs(name, stdout); | |
578 | } | |
579 | ||
580 | next = last[i++] + done; | |
581 | if (i > 3 || next >= last[i]) { | |
582 | i = 0; | |
583 | next = ++done; | |
584 | } | |
585 | } while (done < last[0]); | |
586 | ||
587 | putchar('\n'); | |
588 | } else { | |
589 | /* | |
590 | * Prints 1 column in format <idx> <name> <typestr> | |
591 | */ | |
592 | size_t i; | |
593 | ||
594 | pager_open(); | |
595 | pager = 1; | |
596 | ||
597 | for (i = 0; i < ntypes; i++) { | |
598 | const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, i); | |
599 | printf("%3zu %-30s %s\n", i + 1, | |
600 | fdisk_parttype_get_name(t), | |
601 | fdisk_parttype_get_string(t)); | |
602 | } | |
603 | ||
604 | } | |
605 | ||
606 | ||
607 | /* | |
608 | * Aliases | |
609 | */ | |
610 | if (fdisk_label_has_parttypes_shortcuts(lb)) { | |
611 | const char *alias = NULL, *typestr = NULL; | |
612 | int rc = 0; | |
613 | ||
614 | fputs(_("\nAliases:\n"), stdout); | |
615 | ||
616 | for (next = 0; rc == 0 || rc == 2; next++) { | |
617 | /* rc: <0 error, 0 success, 1 end, 2 deprecated */ | |
618 | rc = fdisk_label_get_parttype_shortcut(lb, | |
619 | next, &typestr, NULL, &alias); | |
620 | if (rc == 0) | |
621 | printf(" %-14s - %s\n", alias, typestr); | |
622 | } | |
623 | } | |
624 | ||
625 | if (pager) | |
626 | pager_close(); | |
627 | ||
628 | } | |
629 | ||
630 | void toggle_dos_compatibility_flag(struct fdisk_context *cxt) | |
631 | { | |
632 | struct fdisk_label *lb = fdisk_get_label(cxt, "dos"); | |
633 | int flag; | |
634 | ||
635 | if (!lb) | |
636 | return; | |
637 | ||
638 | flag = !fdisk_dos_is_compatible(lb); | |
639 | fdisk_info(cxt, flag ? | |
640 | _("DOS Compatibility flag is set (DEPRECATED!)") : | |
641 | _("DOS Compatibility flag is not set")); | |
642 | ||
643 | fdisk_dos_enable_compatible(lb, flag); | |
644 | ||
645 | if (fdisk_is_label(cxt, DOS)) | |
646 | fdisk_reset_alignment(cxt); /* reset the current label */ | |
647 | } | |
648 | ||
649 | static int strtosize_sectors(const char *str, unsigned long sector_size, | |
650 | uintmax_t *res) | |
651 | { | |
652 | size_t len = strlen(str); | |
653 | int insec = 0; | |
654 | int rc; | |
655 | char *buf = NULL; | |
656 | ||
657 | if (!len) | |
658 | return 0; | |
659 | ||
660 | if (str[len - 1] == 'S' || str[len - 1] == 's') { | |
661 | insec = 1; | |
662 | str = buf = strndup(str, len - 1); /* strip trailing 's' */ | |
663 | if (!str) | |
664 | return -errno; | |
665 | } | |
666 | ||
667 | rc = strtosize(str, res); | |
668 | if (rc == 0 && insec) | |
669 | *res *= sector_size; | |
670 | ||
671 | free(buf); | |
672 | return rc; | |
673 | } | |
674 | ||
675 | void resize_partition(struct fdisk_context *cxt) | |
676 | { | |
677 | struct fdisk_partition *pa = NULL, *npa = NULL, *next = NULL; | |
678 | char *query = NULL, *response = NULL, *default_size; | |
679 | struct fdisk_table *tb = NULL; | |
680 | uint64_t max_size, size, secs; | |
681 | size_t i; | |
682 | int rc; | |
683 | ||
684 | assert(cxt); | |
685 | ||
686 | rc = fdisk_ask_partnum(cxt, &i, FALSE); | |
687 | if (rc) | |
688 | goto err; | |
689 | ||
690 | rc = fdisk_partition_get_max_size(cxt, i, &max_size); | |
691 | if (rc) | |
692 | goto err; | |
693 | ||
694 | max_size *= fdisk_get_sector_size(cxt); | |
695 | ||
696 | default_size = size_to_human_string(0, max_size); | |
697 | xasprintf(&query, _("New <size>{K,M,G,T,P} in bytes or <size>S in sectors (default %s)"), | |
698 | default_size); | |
699 | free(default_size); | |
700 | ||
701 | rc = fdisk_ask_string(cxt, query, &response); | |
702 | if (rc) | |
703 | goto err; | |
704 | ||
705 | size = max_size; | |
706 | rc = strtosize_sectors(response, fdisk_get_sector_size(cxt), &size); | |
707 | if (rc || size > max_size) { | |
708 | fdisk_warnx(cxt, _("Invalid size")); | |
709 | goto err; | |
710 | } | |
711 | ||
712 | npa = fdisk_new_partition(); | |
713 | if (!npa) | |
714 | goto err; | |
715 | ||
716 | secs = size / fdisk_get_sector_size(cxt); | |
717 | fdisk_partition_size_explicit(npa, 1); | |
718 | fdisk_partition_set_size(npa, secs); | |
719 | ||
720 | rc = fdisk_set_partition(cxt, i, npa); | |
721 | if (rc) | |
722 | goto err; | |
723 | ||
724 | fdisk_info(cxt, _("Partition %zu has been resized."), i + 1); | |
725 | ||
726 | out: | |
727 | free(query); | |
728 | free(response); | |
729 | fdisk_unref_partition(next); | |
730 | fdisk_unref_partition(pa); | |
731 | fdisk_unref_partition(npa); | |
732 | fdisk_unref_table(tb); | |
733 | return; | |
734 | ||
735 | err: | |
736 | fdisk_warnx(cxt, _("Could not resize partition %zu: %s"), | |
737 | i + 1, strerror(-rc)); | |
738 | goto out; | |
739 | } | |
740 | ||
741 | void change_partition_type(struct fdisk_context *cxt) | |
742 | { | |
743 | size_t i; | |
744 | struct fdisk_parttype *t = NULL; | |
745 | struct fdisk_partition *pa = NULL; | |
746 | const char *old = NULL; | |
747 | int canceled = 0; | |
748 | ||
749 | assert(cxt); | |
750 | ||
751 | if (fdisk_ask_partnum(cxt, &i, FALSE)) | |
752 | return; | |
753 | ||
754 | if (fdisk_get_partition(cxt, i, &pa)) { | |
755 | fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1); | |
756 | return; | |
757 | } | |
758 | ||
759 | t = (struct fdisk_parttype *) fdisk_partition_get_type(pa); | |
760 | old = t ? fdisk_parttype_get_name(t) : _("Unknown"); | |
761 | ||
762 | do { | |
763 | t = ask_partition_type(cxt, &canceled); | |
764 | if (canceled) | |
765 | break; | |
766 | } while (!t); | |
767 | ||
768 | if (canceled == 0 && t && fdisk_set_partition_type(cxt, i, t) == 0) | |
769 | fdisk_info(cxt, | |
770 | _("Changed type of partition '%s' to '%s'."), | |
771 | old, t ? fdisk_parttype_get_name(t) : _("Unknown")); | |
772 | else | |
773 | fdisk_info(cxt, | |
774 | _("Type of partition %zu is unchanged: %s."), | |
775 | i + 1, old); | |
776 | ||
777 | fdisk_unref_partition(pa); | |
778 | fdisk_unref_parttype(t); | |
779 | } | |
780 | ||
781 | #ifdef BLKDISCARD | |
782 | ||
783 | static int do_discard(struct fdisk_context *cxt, struct fdisk_partition *pa) | |
784 | { | |
785 | char buf[512]; | |
786 | unsigned long ss; | |
787 | uint64_t range[2]; | |
788 | int yes = 0; | |
789 | ||
790 | ss = fdisk_get_sector_size(cxt); | |
791 | ||
792 | range[0] = (uint64_t) fdisk_partition_get_start(pa); | |
793 | range[1] = (uint64_t) fdisk_partition_get_size(pa); | |
794 | ||
795 | snprintf(buf, sizeof(buf), _("All data in the region (%"PRIu64 | |
796 | "-%"PRIu64") will be lost! Continue?"), | |
797 | range[0], range[0] + range[1] - 1); | |
798 | ||
799 | range[0] *= (uint64_t) ss; | |
800 | range[1] *= (uint64_t) ss; | |
801 | ||
802 | fdisk_ask_yesno(cxt, buf, &yes); | |
803 | if (!yes) | |
804 | return 1; | |
805 | ||
806 | errno = 0; | |
807 | if (ioctl(fdisk_get_devfd(cxt), BLKDISCARD, &range)) { | |
808 | fdisk_warn(cxt, _("BLKDISCARD ioctl failed")); | |
809 | return -errno; | |
810 | } | |
811 | return 0; | |
812 | } | |
813 | ||
814 | static void discard_partition(struct fdisk_context *cxt) | |
815 | { | |
816 | struct fdisk_partition *pa = NULL; | |
817 | size_t n = 0; | |
818 | ||
819 | fdisk_info(cxt, _("\nThe partition sectors will be immediately discarded.\n" | |
820 | "You can exit this dialog by pressing CTRL+C.\n")); | |
821 | ||
822 | if (fdisk_ask_partnum(cxt, &n, FALSE)) | |
823 | goto done; | |
824 | if (fdisk_get_partition(cxt, n, &pa)) { | |
825 | fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), n + 1); | |
826 | goto done; | |
827 | } | |
828 | ||
829 | if (!fdisk_partition_has_size(pa) || !fdisk_partition_has_start(pa)) { | |
830 | fdisk_warnx(cxt, _("Partition %zu has an unspecified range."), n + 1); | |
831 | goto done; | |
832 | } | |
833 | ||
834 | if (do_discard(cxt, pa) == 0) | |
835 | fdisk_info(cxt, _("Discarded sectors on partition %zu."), n + 1); | |
836 | done: | |
837 | fdisk_unref_partition(pa); | |
838 | } | |
839 | ||
840 | static void discard_freespace(struct fdisk_context *cxt) | |
841 | { | |
842 | struct fdisk_partition *pa = NULL; | |
843 | struct fdisk_table *tb = NULL; | |
844 | size_t best = 0; | |
845 | uintmax_t n = 0; | |
846 | int ct; | |
847 | ||
848 | ct = list_freespace_get_table(cxt, &tb, &best); | |
849 | if (ct <= 0) { | |
850 | fdisk_info(cxt, _("No free space.")); | |
851 | goto done; | |
852 | } | |
853 | fdisk_info(cxt, _("\nThe unused sectors will be immediately discarded.\n" | |
854 | "You can exit this dialog by pressing CTRL+C.\n")); | |
855 | ||
856 | if (fdisk_ask_number(cxt, 1, best + 1, (uintmax_t) ct, | |
857 | _("Free space number"), &n) != 0) | |
858 | goto done; | |
859 | ||
860 | pa = fdisk_table_get_partition(tb, n - 1); | |
861 | if (!pa) | |
862 | goto done; | |
863 | ||
864 | if (!fdisk_partition_has_size(pa) || !fdisk_partition_has_start(pa)) { | |
865 | fdisk_warnx(cxt, _("Free space %"PRIu64 "has an unspecified range"), n); | |
866 | goto done; | |
867 | } | |
868 | ||
869 | if (do_discard(cxt, pa) == 0) | |
870 | fdisk_info(cxt, _("Discarded sectors on free space.")); | |
871 | done: | |
872 | fdisk_unref_table(tb); | |
873 | } | |
874 | ||
875 | void discard_sectors(struct fdisk_context *cxt) | |
876 | { | |
877 | int c; | |
878 | ||
879 | if (fdisk_is_readonly(cxt)) { | |
880 | fdisk_warnx(cxt, _("Discarding sectors is not possible in read-only mode.")); | |
881 | return; | |
882 | } | |
883 | ||
884 | if (fdisk_ask_menu(cxt, _("Type of area to be discarded"), | |
885 | &c, 'p', _("partition sectors"), 'p', | |
886 | _("free space sectors"), 'f', NULL) != 0) | |
887 | return; | |
888 | ||
889 | switch (c) { | |
890 | case 'p': | |
891 | discard_partition(cxt); | |
892 | break; | |
893 | case 'f': | |
894 | discard_freespace(cxt); | |
895 | break; | |
896 | } | |
897 | } | |
898 | ||
899 | #else /* !BLKDISCARD */ | |
900 | void discard_sectors(struct fdisk_context *cxt) | |
901 | { | |
902 | fdisk_warnx(cxt, _("Discard unsupported on your system.")); | |
903 | } | |
904 | #endif /* BLKDISCARD */ | |
905 | ||
906 | int print_partition_info(struct fdisk_context *cxt) | |
907 | { | |
908 | struct fdisk_partition *pa = NULL; | |
909 | int rc = 0; | |
910 | size_t i, nfields; | |
911 | int *fields = NULL; | |
912 | struct fdisk_label *lb = fdisk_get_label(cxt, NULL); | |
913 | ||
914 | if ((rc = fdisk_ask_partnum(cxt, &i, FALSE))) | |
915 | return rc; | |
916 | ||
917 | if ((rc = fdisk_get_partition(cxt, i, &pa))) { | |
918 | fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1); | |
919 | return rc; | |
920 | } | |
921 | ||
922 | if ((rc = fdisk_label_get_fields_ids_all(lb, cxt, &fields, &nfields))) | |
923 | goto clean_data; | |
924 | ||
925 | for (i = 0; i < nfields; ++i) { | |
926 | int id = fields[i]; | |
927 | char *data = NULL; | |
928 | const struct fdisk_field *fd = fdisk_label_get_field(lb, id); | |
929 | ||
930 | if (!fd) | |
931 | continue; | |
932 | ||
933 | rc = fdisk_partition_to_string(pa, cxt, id, &data); | |
934 | if (rc < 0) | |
935 | goto clean_data; | |
936 | if (!data || !*data) | |
937 | continue; | |
938 | fdisk_info(cxt, "%15s: %s", fdisk_field_get_name(fd), data); | |
939 | free(data); | |
940 | } | |
941 | ||
942 | clean_data: | |
943 | fdisk_unref_partition(pa); | |
944 | free(fields); | |
945 | return rc; | |
946 | } | |
947 | ||
948 | static size_t skip_empty(const unsigned char *buf, size_t i, size_t sz) | |
949 | { | |
950 | size_t next; | |
951 | const unsigned char *p0 = buf + i; | |
952 | ||
953 | for (next = i + 16; next < sz; next += 16) { | |
954 | if (memcmp(p0, buf + next, 16) != 0) | |
955 | break; | |
956 | } | |
957 | ||
958 | return next == i + 16 ? i : next; | |
959 | } | |
960 | ||
961 | static void dump_buffer(off_t base, unsigned char *buf, size_t sz) | |
962 | { | |
963 | size_t i, l, next = 0; | |
964 | ||
965 | if (!buf) | |
966 | return; | |
967 | for (i = 0, l = 0; i < sz; i++, l++) { | |
968 | if (l == 0) { | |
969 | if (!next) | |
970 | next = skip_empty(buf, i, sz); | |
971 | printf("%08jx ", (intmax_t)base + i); | |
972 | } | |
973 | printf(" %02x", buf[i]); | |
974 | if (l == 7) /* words separator */ | |
975 | fputs(" ", stdout); | |
976 | else if (l == 15) { | |
977 | fputc('\n', stdout); /* next line */ | |
978 | l = -1; | |
979 | if (next > i) { | |
980 | printf("*\n"); | |
981 | i = next - 1; | |
982 | } | |
983 | next = 0; | |
984 | } | |
985 | } | |
986 | if (l > 0) | |
987 | printf("\n"); | |
988 | } | |
989 | ||
990 | static void dump_blkdev(struct fdisk_context *cxt, const char *name, | |
991 | uint64_t offset, size_t size) | |
992 | { | |
993 | int fd = fdisk_get_devfd(cxt); | |
994 | ||
995 | fdisk_info(cxt, _("\n%s: offset = %"PRIu64", size = %zu bytes."), | |
996 | name, offset, size); | |
997 | ||
998 | assert(fd >= 0); | |
999 | ||
1000 | if (lseek(fd, (off_t) offset, SEEK_SET) == (off_t) -1) | |
1001 | fdisk_warn(cxt, _("cannot seek")); | |
1002 | else { | |
1003 | unsigned char *buf = xmalloc(size); | |
1004 | ||
1005 | if (read_all(fd, (char *) buf, size) != (ssize_t) size) | |
1006 | fdisk_warn(cxt, _("cannot read")); | |
1007 | else | |
1008 | dump_buffer(offset, buf, size); | |
1009 | free(buf); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | void dump_firstsector(struct fdisk_context *cxt) | |
1014 | { | |
1015 | assert(cxt); | |
1016 | ||
1017 | dump_blkdev(cxt, _("First sector"), 0, fdisk_get_sector_size(cxt)); | |
1018 | } | |
1019 | ||
1020 | void dump_disklabel(struct fdisk_context *cxt) | |
1021 | { | |
1022 | int i = 0; | |
1023 | const char *name = NULL; | |
1024 | uint64_t offset = 0; | |
1025 | size_t size = 0; | |
1026 | ||
1027 | assert(cxt); | |
1028 | ||
1029 | while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size) | |
1030 | dump_blkdev(cxt, name, offset, size); | |
1031 | } | |
1032 | ||
1033 | static fdisk_sector_t get_dev_blocks(char *dev) | |
1034 | { | |
1035 | int fd, ret; | |
1036 | fdisk_sector_t size; | |
1037 | ||
1038 | if ((fd = open(dev, O_RDONLY|O_NONBLOCK)) < 0) | |
1039 | err(EXIT_FAILURE, _("cannot open %s"), dev); | |
1040 | ret = blkdev_get_sectors(fd, (unsigned long long *) &size); | |
1041 | close(fd); | |
1042 | if (ret < 0) | |
1043 | err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), dev); | |
1044 | return size/2; | |
1045 | } | |
1046 | ||
1047 | ||
1048 | void follow_wipe_mode(struct fdisk_context *cxt) | |
1049 | { | |
1050 | int dowipe = wipemode == WIPEMODE_ALWAYS ? 1 : 0; | |
1051 | ||
1052 | if (isatty(STDIN_FILENO) && wipemode == WIPEMODE_AUTO) | |
1053 | dowipe = 1; /* do it in interactive mode */ | |
1054 | ||
1055 | if (fdisk_is_ptcollision(cxt) && wipemode != WIPEMODE_NEVER) | |
1056 | dowipe = 1; /* always remove old PT */ | |
1057 | ||
1058 | fdisk_enable_wipe(cxt, dowipe); | |
1059 | if (dowipe) | |
1060 | fdisk_warnx(cxt, _( | |
1061 | "The device contains '%s' signature and it will be removed by a write command. " | |
1062 | "See fdisk(8) man page and --wipe option for more details."), | |
1063 | fdisk_get_collision(cxt)); | |
1064 | else | |
1065 | fdisk_warnx(cxt, _( | |
1066 | "The device contains '%s' signature and it may remain on the device. " | |
1067 | "It is recommended to wipe the device with wipefs(8) or " | |
1068 | "fdisk --wipe, in order to avoid possible collisions."), | |
1069 | fdisk_get_collision(cxt)); | |
1070 | } | |
1071 | ||
1072 | static void __attribute__((__noreturn__)) usage(void) | |
1073 | { | |
1074 | FILE *out = stdout; | |
1075 | ||
1076 | fputs(USAGE_HEADER, out); | |
1077 | ||
1078 | fprintf(out, | |
1079 | _(" %1$s [options] <disk> change partition table\n" | |
1080 | " %1$s [options] -l [<disk>...] list partition table(s)\n"), | |
1081 | program_invocation_short_name); | |
1082 | ||
1083 | fputs(USAGE_SEPARATOR, out); | |
1084 | fputs(_("Display or manipulate a disk partition table.\n"), out); | |
1085 | ||
1086 | fputs(USAGE_OPTIONS, out); | |
1087 | fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out); | |
1088 | fputs(_(" -B, --protect-boot don't erase bootbits when creating a new label\n"), out); | |
1089 | fputs(_(" -c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)\n"), out); | |
1090 | fprintf(out, | |
1091 | _(" -L, --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never"); | |
1092 | fprintf(out, | |
1093 | " %s\n", USAGE_COLORS_DEFAULT); | |
1094 | fputs(_(" -l, --list display partitions and exit\n"), out); | |
1095 | fputs(_(" -x, --list-details like --list but with more details\n"), out); | |
1096 | ||
1097 | fputs(_(" -n, --noauto-pt don't create default partition table on empty devices\n"), out); | |
1098 | fputs(_(" -o, --output <list> output columns\n"), out); | |
1099 | fputs(_(" -t, --type <type> recognize specified partition table type only\n"), out); | |
1100 | fputs(_(" -u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out); | |
1101 | fputs(_(" -s, --getsz display device size in 512-byte sectors [DEPRECATED]\n"), out); | |
1102 | fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out); | |
1103 | fprintf(out, | |
1104 | _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); | |
1105 | fprintf(out, | |
1106 | _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never"); | |
1107 | fprintf(out, | |
1108 | _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never"); | |
1109 | ||
1110 | fputs(USAGE_SEPARATOR, out); | |
1111 | fputs(_(" -C, --cylinders <number> specify the number of cylinders\n"), out); | |
1112 | fputs(_(" -H, --heads <number> specify the number of heads\n"), out); | |
1113 | fputs(_(" -S, --sectors <number> specify the number of sectors per track\n"), out); | |
1114 | ||
1115 | fputs(USAGE_SEPARATOR, out); | |
1116 | fprintf(out, USAGE_HELP_OPTIONS(31)); | |
1117 | ||
1118 | list_available_columns(out); | |
1119 | ||
1120 | fprintf(out, USAGE_MAN_TAIL("fdisk(8)")); | |
1121 | exit(EXIT_SUCCESS); | |
1122 | } | |
1123 | ||
1124 | ||
1125 | enum { | |
1126 | ACT_FDISK = 0, /* default */ | |
1127 | ACT_LIST, | |
1128 | ACT_LIST_DETAILS, | |
1129 | ACT_SHOWSIZE | |
1130 | }; | |
1131 | ||
1132 | int main(int argc, char **argv) | |
1133 | { | |
1134 | int rc, i, c, act = ACT_FDISK, noauto_pt = 0; | |
1135 | int colormode = UL_COLORMODE_UNDEF; | |
1136 | struct fdisk_context *cxt; | |
1137 | char *outarg = NULL; | |
1138 | const char *devname, *lockmode = NULL; | |
1139 | enum { | |
1140 | OPT_BYTES = CHAR_MAX + 1, | |
1141 | OPT_LOCK | |
1142 | }; | |
1143 | static const struct option longopts[] = { | |
1144 | { "bytes", no_argument, NULL, OPT_BYTES }, | |
1145 | { "color", optional_argument, NULL, 'L' }, | |
1146 | { "compatibility", optional_argument, NULL, 'c' }, | |
1147 | { "cylinders", required_argument, NULL, 'C' }, | |
1148 | { "heads", required_argument, NULL, 'H' }, | |
1149 | { "sectors", required_argument, NULL, 'S' }, | |
1150 | { "getsz", no_argument, NULL, 's' }, | |
1151 | { "help", no_argument, NULL, 'h' }, | |
1152 | { "list", no_argument, NULL, 'l' }, | |
1153 | { "list-details", no_argument, NULL, 'x' }, | |
1154 | { "lock", optional_argument, NULL, OPT_LOCK }, | |
1155 | { "noauto-pt", no_argument, NULL, 'n' }, | |
1156 | { "sector-size", required_argument, NULL, 'b' }, | |
1157 | { "type", required_argument, NULL, 't' }, | |
1158 | { "units", optional_argument, NULL, 'u' }, | |
1159 | { "version", no_argument, NULL, 'V' }, | |
1160 | { "output", required_argument, NULL, 'o' }, | |
1161 | { "protect-boot", no_argument, NULL, 'B' }, | |
1162 | { "wipe", required_argument, NULL, 'w' }, | |
1163 | { "wipe-partitions",required_argument, NULL, 'W' }, | |
1164 | { NULL, 0, NULL, 0 } | |
1165 | }; | |
1166 | ||
1167 | setlocale(LC_ALL, ""); | |
1168 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1169 | textdomain(PACKAGE); | |
1170 | close_stdout_atexit(); | |
1171 | ||
1172 | fdisk_init_debug(0); | |
1173 | scols_init_debug(0); | |
1174 | fdiskprog_init_debug(); | |
1175 | ||
1176 | cxt = fdisk_new_context(); | |
1177 | if (!cxt) | |
1178 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
1179 | ||
1180 | fdisk_set_ask(cxt, ask_callback, NULL); | |
1181 | ||
1182 | while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::no:sS:t:u::vVw:W:x", | |
1183 | longopts, NULL)) != -1) { | |
1184 | switch (c) { | |
1185 | case 'b': | |
1186 | { | |
1187 | size_t sz = strtou32_or_err(optarg, | |
1188 | _("invalid sector size argument")); | |
1189 | if (sz != 512 && sz != 1024 && sz != 2048 && sz != 4096) | |
1190 | errx(EXIT_FAILURE, _("invalid sector size argument")); | |
1191 | fdisk_save_user_sector_size(cxt, sz, sz); | |
1192 | break; | |
1193 | } | |
1194 | case 'B': | |
1195 | fdisk_enable_bootbits_protection(cxt, 1); | |
1196 | break; | |
1197 | case 'C': | |
1198 | fdisk_save_user_geometry(cxt, | |
1199 | strtou32_or_err(optarg, | |
1200 | _("invalid cylinders argument")), | |
1201 | 0, 0); | |
1202 | break; | |
1203 | case 'c': | |
1204 | if (optarg) { | |
1205 | /* this setting is independent on the current | |
1206 | * actively used label | |
1207 | */ | |
1208 | char *p = *optarg == '=' ? optarg + 1 : optarg; | |
1209 | struct fdisk_label *lb = fdisk_get_label(cxt, "dos"); | |
1210 | ||
1211 | if (!lb) | |
1212 | err(EXIT_FAILURE, _("not found DOS label driver")); | |
1213 | if (strcmp(p, "dos") == 0) | |
1214 | fdisk_dos_enable_compatible(lb, TRUE); | |
1215 | else if (strcmp(p, "nondos") == 0) | |
1216 | fdisk_dos_enable_compatible(lb, FALSE); | |
1217 | else | |
1218 | errx(EXIT_FAILURE, _("unknown compatibility mode '%s'"), p); | |
1219 | } | |
1220 | /* use default if no optarg specified */ | |
1221 | break; | |
1222 | case 'H': | |
1223 | fdisk_save_user_geometry(cxt, 0, | |
1224 | strtou32_or_err(optarg, | |
1225 | _("invalid heads argument")), | |
1226 | 0); | |
1227 | break; | |
1228 | case 'S': | |
1229 | fdisk_save_user_geometry(cxt, 0, 0, | |
1230 | strtou32_or_err(optarg, | |
1231 | _("invalid sectors argument"))); | |
1232 | break; | |
1233 | case 'l': | |
1234 | act = ACT_LIST; | |
1235 | break; | |
1236 | case 'x': | |
1237 | act = ACT_LIST_DETAILS; | |
1238 | break; | |
1239 | case 'L': | |
1240 | colormode = UL_COLORMODE_AUTO; | |
1241 | if (optarg) | |
1242 | colormode = colormode_or_err(optarg); | |
1243 | break; | |
1244 | case 'n': | |
1245 | noauto_pt = 1; | |
1246 | break; | |
1247 | case 'o': | |
1248 | outarg = optarg; | |
1249 | break; | |
1250 | case 's': | |
1251 | act = ACT_SHOWSIZE; | |
1252 | break; | |
1253 | case 't': | |
1254 | { | |
1255 | struct fdisk_label *lb = NULL; | |
1256 | ||
1257 | while (fdisk_next_label(cxt, &lb) == 0) | |
1258 | fdisk_label_set_disabled(lb, 1); | |
1259 | ||
1260 | lb = fdisk_get_label(cxt, optarg); | |
1261 | if (!lb) | |
1262 | errx(EXIT_FAILURE, _("unsupported disklabel: %s"), optarg); | |
1263 | fdisk_label_set_disabled(lb, 0); | |
1264 | break; | |
1265 | } | |
1266 | case 'u': | |
1267 | if (optarg && *optarg == '=') | |
1268 | optarg++; | |
1269 | if (fdisk_set_unit(cxt, optarg) != 0) | |
1270 | errx(EXIT_FAILURE, _("unsupported unit")); | |
1271 | break; | |
1272 | case 'V': /* preferred for util-linux */ | |
1273 | case 'v': /* for backward compatibility only */ | |
1274 | print_version(EXIT_SUCCESS); | |
1275 | case 'w': | |
1276 | wipemode = wipemode_from_string(optarg); | |
1277 | if (wipemode < 0) | |
1278 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
1279 | break; | |
1280 | case 'W': | |
1281 | pwipemode = wipemode_from_string(optarg); | |
1282 | if (pwipemode < 0) | |
1283 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
1284 | break; | |
1285 | case 'h': | |
1286 | usage(); | |
1287 | case OPT_BYTES: | |
1288 | fdisk_set_size_unit(cxt, FDISK_SIZEUNIT_BYTES); | |
1289 | break; | |
1290 | case OPT_LOCK: | |
1291 | lockmode = "1"; | |
1292 | if (optarg) { | |
1293 | if (*optarg == '=') | |
1294 | optarg++; | |
1295 | lockmode = optarg; | |
1296 | } | |
1297 | break; | |
1298 | default: | |
1299 | errtryhelp(EXIT_FAILURE); | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | if (argc-optind != 1 && fdisk_has_user_device_properties(cxt)) | |
1304 | warnx(_("The device properties (sector size and geometry) should" | |
1305 | " be used with one specified device only.")); | |
1306 | ||
1307 | colors_init(colormode, "fdisk"); | |
1308 | is_interactive = isatty(STDIN_FILENO); | |
1309 | ||
1310 | switch (act) { | |
1311 | case ACT_LIST: | |
1312 | case ACT_LIST_DETAILS: | |
1313 | fdisk_enable_listonly(cxt, 1); | |
1314 | ||
1315 | if (act == ACT_LIST_DETAILS) | |
1316 | fdisk_enable_details(cxt, 1); | |
1317 | ||
1318 | init_fields(cxt, outarg, NULL); | |
1319 | ||
1320 | if (argc > optind) { | |
1321 | int k; | |
1322 | ||
1323 | for (rc = 0, k = optind; k < argc; k++) | |
1324 | rc += print_device_pt(cxt, argv[k], 1, 0, k != optind); | |
1325 | ||
1326 | if (rc) | |
1327 | return EXIT_FAILURE; | |
1328 | } else | |
1329 | print_all_devices_pt(cxt, 0); | |
1330 | break; | |
1331 | ||
1332 | case ACT_SHOWSIZE: | |
1333 | /* deprecated */ | |
1334 | if (argc - optind <= 0) { | |
1335 | warnx(_("bad usage")); | |
1336 | errtryhelp(EXIT_FAILURE); | |
1337 | } | |
1338 | for (i = optind; i < argc; i++) { | |
1339 | uintmax_t blks = get_dev_blocks(argv[i]); | |
1340 | ||
1341 | if (argc - optind == 1) | |
1342 | printf("%ju\n", blks); | |
1343 | else | |
1344 | printf("%s: %ju\n", argv[i], blks); | |
1345 | } | |
1346 | break; | |
1347 | ||
1348 | case ACT_FDISK: | |
1349 | if (argc-optind != 1) { | |
1350 | warnx(_("bad usage")); | |
1351 | errtryhelp(EXIT_FAILURE); | |
1352 | } | |
1353 | ||
1354 | /* Here starts interactive mode, use fdisk_{warn,info,..} functions */ | |
1355 | color_scheme_enable("welcome", UL_COLOR_GREEN); | |
1356 | fdisk_info(cxt, _("Welcome to fdisk (%s)."), PACKAGE_STRING); | |
1357 | color_disable(); | |
1358 | fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n" | |
1359 | "Be careful before using the write command.\n")); | |
1360 | ||
1361 | devname = argv[optind]; | |
1362 | rc = fdisk_assign_device(cxt, devname, 0); | |
1363 | if (rc == -EACCES) { | |
1364 | rc = fdisk_assign_device(cxt, devname, 1); | |
1365 | if (rc == 0) | |
1366 | fdisk_warnx(cxt, _("Device is open in read-only mode.")); | |
1367 | } | |
1368 | if (rc) | |
1369 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
1370 | ||
1371 | if (fdisk_device_is_used(cxt)) | |
1372 | fdisk_warnx(cxt, _( | |
1373 | "This disk is currently in use - repartitioning is probably a bad idea.\n" | |
1374 | "It's recommended to umount all file systems, and swapoff all swap\n" | |
1375 | "partitions on this disk.\n")); | |
1376 | ||
1377 | fflush(stdout); | |
1378 | ||
1379 | if (!fdisk_is_readonly(cxt) | |
1380 | && blkdev_lock(fdisk_get_devfd(cxt), devname, lockmode) != 0) { | |
1381 | fdisk_deassign_device(cxt, 1); | |
1382 | fdisk_unref_context(cxt); | |
1383 | return EXIT_FAILURE; | |
1384 | } | |
1385 | ||
1386 | if (fdisk_get_collision(cxt)) | |
1387 | follow_wipe_mode(cxt); | |
1388 | ||
1389 | if (!fdisk_has_label(cxt)) { | |
1390 | fdisk_info(cxt, _("Device does not contain a recognized partition table.")); | |
1391 | if (!noauto_pt) | |
1392 | fdisk_create_disklabel(cxt, NULL); | |
1393 | ||
1394 | } else if (fdisk_is_label(cxt, GPT) && fdisk_gpt_is_hybrid(cxt)) | |
1395 | fdisk_warnx(cxt, _( | |
1396 | "A hybrid GPT was detected. You have to sync " | |
1397 | "the hybrid MBR manually (expert command 'M').")); | |
1398 | ||
1399 | init_fields(cxt, outarg, NULL); /* -o <columns> */ | |
1400 | ||
1401 | if (!fdisk_is_readonly(cxt)) { | |
1402 | fdisk_get_partitions(cxt, &original_layout); | |
1403 | device_is_used = fdisk_device_is_used(cxt); | |
1404 | } | |
1405 | ||
1406 | while (1) | |
1407 | process_fdisk_menu(&cxt); | |
1408 | } | |
1409 | ||
1410 | fdisk_unref_context(cxt); | |
1411 | return EXIT_SUCCESS; | |
1412 | } |