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