]>
Commit | Line | Data |
---|---|---|
3f0ac587 | 1 | /* |
9e95aa12 KZ |
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 | * | |
6dbe3af9 | 9 | * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk) |
38b36353 | 10 | * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org> |
6dbe3af9 | 11 | * |
3f0ac587 | 12 | * Copyright (C) 2007-2013 Karel Zak <kzak@redhat.com> |
6dbe3af9 | 13 | */ |
6dbe3af9 KZ |
14 | #include <unistd.h> |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <fcntl.h> | |
19 | #include <ctype.h> | |
6dbe3af9 | 20 | #include <errno.h> |
2b6fc908 | 21 | #include <getopt.h> |
2b6fc908 | 22 | #include <sys/stat.h> |
f26873dc | 23 | #include <sys/time.h> |
bb6aacfe | 24 | #include <time.h> |
ee5355e0 | 25 | #include <limits.h> |
6c8c429d | 26 | #include <signal.h> |
c1154128 | 27 | #include <poll.h> |
d44115f3 | 28 | #include <libsmartcols.h> |
740c36f6 | 29 | #ifdef HAVE_LIBREADLINE |
57ebfde2 | 30 | # define _FUNCTION_DEF |
740c36f6 KZ |
31 | # include <readline/readline.h> |
32 | #endif | |
6dbe3af9 | 33 | |
3f0ac587 | 34 | #include "c.h" |
52b38677 | 35 | #include "xalloc.h" |
e916600f | 36 | #include "all-io.h" |
22853e4a | 37 | #include "nls.h" |
e66ac5d3 | 38 | #include "rpmatch.h" |
810f986b | 39 | #include "blkdev.h" |
b8d22034 | 40 | #include "mbsalign.h" |
6bec8710 KZ |
41 | #include "pathnames.h" |
42 | #include "canonicalize.h" | |
20aa2570 | 43 | #include "strutils.h" |
b2d28533 | 44 | #include "closestream.h" |
8d2f4498 | 45 | #include "pager.h" |
5c36a0eb | 46 | |
fdb006e8 KZ |
47 | #include "fdisk.h" |
48 | ||
b4bfbadd | 49 | #include "pt-sun.h" /* to toggle flags */ |
5c36a0eb | 50 | |
48d7b13a | 51 | #ifdef HAVE_LINUX_COMPILER_H |
3f0ac587 | 52 | # include <linux/compiler.h> |
48d7b13a KZ |
53 | #endif |
54 | #ifdef HAVE_LINUX_BLKPG_H | |
3f0ac587 | 55 | # include <linux/blkpg.h> |
7eda085c | 56 | #endif |
6dbe3af9 | 57 | |
a4c0dce8 KZ |
58 | #ifdef __linux__ |
59 | # ifdef HAVE_LINUX_FS_H | |
60 | # include <linux/fs.h> | |
61 | # endif | |
a4c0dce8 KZ |
62 | #endif |
63 | ||
ba465623 | 64 | int pwipemode = WIPEMODE_AUTO; |
fadd8e08 | 65 | int device_is_used; |
c1154128 | 66 | int is_interactive; |
fadd8e08 KZ |
67 | struct fdisk_table *original_layout; |
68 | ||
5635d195 | 69 | static int wipemode = WIPEMODE_AUTO; |
ba465623 | 70 | |
e6d0c4c1 KZ |
71 | /* |
72 | * fdisk debug stuff (see fdisk.h and include/debug.h) | |
73 | */ | |
74 | UL_DEBUG_DEFINE_MASK(fdisk); | |
819d9a29 | 75 | UL_DEBUG_DEFINE_MASKNAMES(fdisk) = UL_DEBUG_EMPTY_MASKNAMES; |
d6b3ba41 | 76 | |
e6d0c4c1 KZ |
77 | static void fdiskprog_init_debug(void) |
78 | { | |
a15dca2f | 79 | __UL_INIT_DEBUG_FROM_ENV(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG); |
e6d0c4c1 | 80 | } |
d6b3ba41 | 81 | |
c1154128 | 82 | static void reply_sighandler(int sig __attribute__((unused))) |
6c8c429d | 83 | { |
c1154128 | 84 | DBG(ASK, ul_debug("got signal")); |
6c8c429d VD |
85 | } |
86 | ||
c1154128 | 87 | static int reply_running; |
740c36f6 | 88 | |
c1154128 KZ |
89 | #ifdef HAVE_LIBREADLINE |
90 | static char *reply_line; | |
740c36f6 | 91 | |
c1154128 | 92 | static void reply_linehandler(char *line) |
55cf3716 | 93 | { |
c1154128 KZ |
94 | reply_line = line; |
95 | reply_running = 0; | |
96 | rl_callback_handler_remove(); /* avoid duplicate prompt */ | |
55cf3716 | 97 | } |
c1154128 | 98 | #endif |
55cf3716 | 99 | |
c1154128 | 100 | int get_user_reply(const char *prompt, char *buf, size_t bufsz) |
d6b3ba41 | 101 | { |
c1154128 KZ |
102 | struct sigaction oldact, act = { |
103 | .sa_handler = reply_sighandler | |
104 | }; | |
105 | struct pollfd fds[] = { | |
106 | { .fd = fileno(stdin), .events = POLLIN } | |
107 | }; | |
d6b3ba41 | 108 | size_t sz; |
6c8c429d | 109 | int ret = 0; |
6c8c429d | 110 | |
36787f7a | 111 | DBG(ASK, ul_debug("asking for user reply %s", is_interactive ? "[interactive]" : "")); |
6c8c429d | 112 | |
c1154128 | 113 | sigemptyset(&act.sa_mask); |
6c8c429d | 114 | sigaction(SIGINT, &act, &oldact); |
d6b3ba41 | 115 | |
c1154128 KZ |
116 | #ifdef HAVE_LIBREADLINE |
117 | if (is_interactive) | |
118 | rl_callback_handler_install(prompt, reply_linehandler); | |
119 | #endif | |
40af0db4 | 120 | errno = 0; |
c1154128 | 121 | reply_running = 1; |
d6b3ba41 | 122 | do { |
c1154128 | 123 | int rc; |
6c8c429d | 124 | |
c1154128 KZ |
125 | *buf = '\0'; |
126 | #ifdef HAVE_LIBREADLINE | |
127 | if (!is_interactive) | |
128 | #endif | |
129 | { | |
130 | fputs(prompt, stdout); | |
131 | fflush(stdout); | |
6c8c429d VD |
132 | } |
133 | ||
c1154128 KZ |
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); | |
30636704 | 150 | if (sz == 0) |
1d775aa2 | 151 | buf[sz++] = '\n'; |
30636704 VD |
152 | else |
153 | memcpy(buf, reply_line, min(sz, bufsz)); | |
1d775aa2 | 154 | buf[min(sz, bufsz - 1)] = '\0'; |
c1154128 KZ |
155 | free(reply_line); |
156 | reply_line = NULL; | |
55cf3716 | 157 | } |
55cf3716 | 158 | } else |
c1154128 KZ |
159 | #endif |
160 | { | |
161 | if (!fgets(buf, bufsz, stdin)) | |
162 | *buf = '\0'; | |
55cf3716 | 163 | break; |
c1154128 KZ |
164 | } |
165 | } while (reply_running); | |
166 | ||
167 | if (!*buf) { | |
168 | DBG(ASK, ul_debug("cancel by CTRL+D")); | |
169 | ret = -ECANCELED; | |
40af0db4 | 170 | clearerr(stdin); |
c1154128 KZ |
171 | goto done; |
172 | } | |
d6b3ba41 | 173 | |
c1154128 KZ |
174 | /* |
175 | * cleanup the reply | |
176 | */ | |
6c183c28 | 177 | sz = ltrim_whitespace((unsigned char *) buf); |
d6b3ba41 KZ |
178 | if (sz && *(buf + sz - 1) == '\n') |
179 | *(buf + sz - 1) = '\0'; | |
180 | ||
c1154128 KZ |
181 | done: |
182 | #ifdef HAVE_LIBREADLINE | |
183 | if (is_interactive) | |
184 | rl_callback_handler_remove(); | |
185 | #endif | |
6c8c429d | 186 | sigaction(SIGINT, &oldact, NULL); |
40af0db4 | 187 | DBG(ASK, ul_debug("user's reply: >>>%s<<< [rc=%d]", buf, ret)); |
6c8c429d | 188 | return ret; |
d6b3ba41 KZ |
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]; | |
68ddb136 | 205 | int key, c, rc; |
d6b3ba41 KZ |
206 | const char *name, *desc; |
207 | size_t i = 0; | |
208 | ||
209 | /* print menu items */ | |
a4c0dce8 KZ |
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 | } | |
d6b3ba41 KZ |
216 | |
217 | /* ask for key */ | |
218 | snprintf(prompt, sizeof(prompt), _("Select (default %c): "), dft); | |
c1154128 | 219 | rc = get_user_reply(prompt, buf, bufsz); |
68ddb136 KZ |
220 | if (rc) |
221 | return rc; | |
d6b3ba41 KZ |
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 | ||
88141067 | 259 | DBG(ASK, ul_debug("asking for number " |
fdbd7bb9 | 260 | "['%s', <%"PRIu64",%"PRIu64">, default=%"PRIu64", range: %s]", |
d6b3ba41 KZ |
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 | |
fdbd7bb9 | 268 | snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "), |
d6b3ba41 KZ |
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 | |
fdbd7bb9 RM |
276 | snprintf(prompt, sizeof(prompt), |
277 | _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "), | |
d6b3ba41 KZ |
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 | |
fdbd7bb9 | 283 | snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "), |
d6b3ba41 KZ |
284 | q, low, high); |
285 | ||
286 | do { | |
c1154128 | 287 | int rc = get_user_reply(prompt, buf, bufsz); |
5653c982 | 288 | uint64_t num = 0; |
d6b3ba41 KZ |
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 | ||
fdbd7bb9 | 331 | DBG(ASK, ul_debug("asking for offset ['%s', <%"PRIu64",%"PRIu64">, base=%"PRIu64", default=%"PRIu64", range: %s]", |
d6b3ba41 KZ |
332 | q, low, high, base, dflt, range)); |
333 | ||
334 | if (range && dflt >= low && dflt <= high) | |
fdbd7bb9 RM |
335 | snprintf(prompt, sizeof(prompt), _("%s (%s, default %"PRIu64"): "), |
336 | q, range, dflt); | |
d6b3ba41 | 337 | else if (dflt >= low && dflt <= high) |
fdbd7bb9 RM |
338 | snprintf(prompt, sizeof(prompt), |
339 | _("%s (%"PRIu64"-%"PRIu64", default %"PRIu64"): "), | |
340 | q, low, high, dflt); | |
d6b3ba41 | 341 | else |
fdbd7bb9 RM |
342 | snprintf(prompt, sizeof(prompt), _("%s (%"PRIu64"-%"PRIu64"): "), |
343 | q, low, high); | |
d6b3ba41 KZ |
344 | |
345 | do { | |
262c94c2 | 346 | uintmax_t num = 0; |
d6b3ba41 KZ |
347 | char sig = 0, *p; |
348 | int pwr = 0; | |
349 | ||
c1154128 | 350 | int rc = get_user_reply(prompt, buf, bufsz); |
d6b3ba41 KZ |
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 | ||
c6b3e988 | 362 | rc = ul_parse_size(p, &num, &pwr); |
d6b3ba41 KZ |
363 | if (rc) |
364 | continue; | |
88141067 | 365 | DBG(ASK, ul_debug("parsed size: %ju", num)); |
d6b3ba41 KZ |
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; | |
757cefbb AG |
374 | else if (sig == '-' && fdisk_ask_number_is_wrap_negative(ask)) |
375 | num = high - num; | |
d6b3ba41 KZ |
376 | else if (sig == '-') |
377 | num = base - num; | |
378 | ||
88141067 | 379 | DBG(ASK, ul_debug("final offset: %ju [sig: %c, power: %d, %s]", |
d6b3ba41 KZ |
380 | num, sig, pwr, |
381 | sig ? "relative" : "absolute")); | |
382 | if (num >= low && num <= high) { | |
8c73e509 | 383 | if (sig && pwr) |
d6b3ba41 | 384 | fdisk_ask_number_set_relative(ask, 1); |
262c94c2 | 385 | return fdisk_ask_number_set_result(ask, (uint64_t)num); |
d6b3ba41 KZ |
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 | ||
412791a9 | 395 | static void fputs_info(struct fdisk_ask *ask, FILE *out) |
d6b3ba41 KZ |
396 | { |
397 | const char *msg; | |
d6b3ba41 KZ |
398 | assert(ask); |
399 | ||
400 | msg = fdisk_ask_print_get_mesg(ask); | |
d6b3ba41 KZ |
401 | if (!msg) |
402 | return; | |
403 | if (info_count == 1) | |
404 | fputc('\n', out); | |
412791a9 KZ |
405 | |
406 | fputs(msg, out); | |
d6b3ba41 KZ |
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; | |
89770db4 | 414 | char buf[BUFSIZ] = { '\0' }; |
d6b3ba41 KZ |
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: | |
152788aa KZ |
430 | if (!fdisk_is_listonly(cxt)) |
431 | info_count++; | |
412791a9 | 432 | fputs_info(ask, stdout); |
d6b3ba41 KZ |
433 | break; |
434 | case FDISK_ASKTYPE_WARNX: | |
95aae4fc | 435 | fflush(stdout); |
496c979a | 436 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); |
d6b3ba41 KZ |
437 | fputs(fdisk_ask_print_get_mesg(ask), stderr); |
438 | color_fdisable(stderr); | |
439 | fputc('\n', stderr); | |
440 | break; | |
441 | case FDISK_ASKTYPE_WARN: | |
95aae4fc | 442 | fflush(stdout); |
496c979a | 443 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); |
d6b3ba41 KZ |
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); | |
2d129032 KZ |
451 | do { |
452 | int x; | |
453 | fputs(fdisk_ask_get_query(ask), stdout); | |
c1154128 | 454 | rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf)); |
2d129032 KZ |
455 | if (rc) |
456 | break; | |
457 | x = rpmatch(buf); | |
cd2a6f1c | 458 | if (x == RPMATCH_YES || x == RPMATCH_NO) { |
2d129032 KZ |
459 | fdisk_ask_yesno_set_result(ask, x); |
460 | break; | |
461 | } | |
462 | } while(1); | |
88141067 | 463 | DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc)); |
d6b3ba41 KZ |
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); | |
c1154128 | 470 | rc = get_user_reply(prmt, buf, sizeof(buf)); |
d6b3ba41 KZ |
471 | if (rc == 0) |
472 | fdisk_ask_string_set_result(ask, xstrdup(buf)); | |
88141067 | 473 | DBG(ASK, ul_debug("string ask: reply '%s' [rc=%d]", buf, rc)); |
d6b3ba41 KZ |
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 | ||
19e4cfef | 483 | static struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt, int *canceled) |
d6b3ba41 KZ |
484 | { |
485 | const char *q; | |
a745611d KZ |
486 | struct fdisk_label *lb; |
487 | ||
488 | assert(cxt); | |
489 | lb = fdisk_get_label(cxt, NULL); | |
d6b3ba41 | 490 | |
a745611d | 491 | if (!lb) |
d6b3ba41 KZ |
492 | return NULL; |
493 | ||
19e4cfef | 494 | *canceled = 0; |
006607ab KZ |
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): "); | |
d6b3ba41 | 504 | do { |
89770db4 | 505 | char buf[256] = { '\0' }; |
c1154128 | 506 | int rc = get_user_reply(q, buf, sizeof(buf)); |
d6b3ba41 | 507 | |
19e4cfef KZ |
508 | if (rc) { |
509 | if (rc == -ECANCELED) | |
510 | *canceled = 1; | |
d6b3ba41 | 511 | break; |
19e4cfef | 512 | } |
d6b3ba41 KZ |
513 | |
514 | if (buf[1] == '\0' && toupper(*buf) == 'L') | |
515 | list_partition_types(cxt); | |
e6effcac | 516 | else if (*buf) { |
006607ab KZ |
517 | struct fdisk_parttype *t = fdisk_label_advparse_parttype(lb, buf, |
518 | FDISK_PARTTYPE_PARSE_DATA | |
519 | | FDISK_PARTTYPE_PARSE_ALIAS | |
8d720dbe | 520 | | FDISK_PARTTYPE_PARSE_NAME |
006607ab | 521 | | FDISK_PARTTYPE_PARSE_SEQNUM); |
e6effcac KZ |
522 | if (!t) |
523 | fdisk_info(cxt, _("Failed to parse '%s' partition type."), buf); | |
524 | return t; | |
525 | } | |
d6b3ba41 KZ |
526 | } while (1); |
527 | ||
528 | return NULL; | |
529 | } | |
530 | ||
8d2f4498 | 531 | |
7b575fcc | 532 | void list_partition_types(struct fdisk_context *cxt) |
6dbe3af9 | 533 | { |
006607ab | 534 | size_t ntypes = 0, next = 0; |
f45f5c83 | 535 | struct fdisk_label *lb; |
006607ab | 536 | int pager = 0; |
6dbe3af9 | 537 | |
a745611d KZ |
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) | |
7b575fcc | 544 | return; |
2b6fc908 | 545 | |
a745611d | 546 | if (fdisk_label_has_code_parttypes(lb)) { |
7b575fcc KZ |
547 | /* |
548 | * Prints in 4 columns in format <hex> <name> | |
549 | */ | |
006607ab | 550 | size_t last[4], done = 0, size; |
76f17cf2 | 551 | int i; |
b8d22034 | 552 | |
76f17cf2 | 553 | size = ntypes; |
7b575fcc KZ |
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; | |
a745611d | 563 | const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, next); |
7b575fcc KZ |
564 | size_t ret; |
565 | ||
e6d0c4c1 | 566 | if (fdisk_parttype_get_name(t)) { |
5d271cef | 567 | printf("%s%02x ", i ? " " : "\n", |
a745611d KZ |
568 | fdisk_parttype_get_code(t)); |
569 | ret = mbsalign(_(fdisk_parttype_get_name(t)), | |
570 | name, sizeof(name), | |
571 | &width, MBS_ALIGN_LEFT, 0); | |
7b575fcc | 572 | |
76f17cf2 | 573 | if (ret == (size_t)-1 || ret >= sizeof(name)) |
a745611d KZ |
574 | printf("%-15.15s", |
575 | _(fdisk_parttype_get_name(t))); | |
76f17cf2 KZ |
576 | else |
577 | fputs(name, stdout); | |
578 | } | |
7b575fcc KZ |
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 | ||
006607ab | 587 | putchar('\n'); |
7b575fcc KZ |
588 | } else { |
589 | /* | |
590 | * Prints 1 column in format <idx> <name> <typestr> | |
591 | */ | |
76f17cf2 | 592 | size_t i; |
7b575fcc | 593 | |
8d2f4498 | 594 | pager_open(); |
006607ab | 595 | pager = 1; |
8d2f4498 | 596 | |
a745611d KZ |
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)); | |
76f17cf2 | 602 | } |
8d2f4498 | 603 | |
7b575fcc | 604 | } |
006607ab KZ |
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 | ||
6dbe3af9 KZ |
628 | } |
629 | ||
f02fecd1 | 630 | void toggle_dos_compatibility_flag(struct fdisk_context *cxt) |
852ce62b | 631 | { |
6a632136 | 632 | struct fdisk_label *lb = fdisk_get_label(cxt, "dos"); |
852ce62b KZ |
633 | int flag; |
634 | ||
635 | if (!lb) | |
636 | return; | |
637 | ||
638 | flag = !fdisk_dos_is_compatible(lb); | |
09f0c3d9 KZ |
639 | fdisk_info(cxt, flag ? |
640 | _("DOS Compatibility flag is set (DEPRECATED!)") : | |
641 | _("DOS Compatibility flag is not set")); | |
edd7b958 | 642 | |
852ce62b KZ |
643 | fdisk_dos_enable_compatible(lb, flag); |
644 | ||
aa36c2cf | 645 | if (fdisk_is_label(cxt, DOS)) |
09f0c3d9 | 646 | fdisk_reset_alignment(cxt); /* reset the current label */ |
6dbe3af9 KZ |
647 | } |
648 | ||
d45820df TW |
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; | |
71719680 | 655 | char *buf = NULL; |
d45820df TW |
656 | |
657 | if (!len) | |
658 | return 0; | |
659 | ||
660 | if (str[len - 1] == 'S' || str[len - 1] == 's') { | |
661 | insec = 1; | |
71719680 | 662 | str = buf = strndup(str, len - 1); /* strip trailing 's' */ |
d45820df TW |
663 | if (!str) |
664 | return -errno; | |
665 | } | |
666 | ||
667 | rc = strtosize(str, res); | |
71719680 | 668 | if (rc == 0 && insec) |
d45820df | 669 | *res *= sector_size; |
d45820df | 670 | |
71719680 KZ |
671 | free(buf); |
672 | return rc; | |
d45820df TW |
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 | ||
27ddd4f1 | 741 | void change_partition_type(struct fdisk_context *cxt) |
e53ced85 | 742 | { |
641a75ca | 743 | size_t i; |
85151521 KZ |
744 | struct fdisk_parttype *t = NULL; |
745 | struct fdisk_partition *pa = NULL; | |
746 | const char *old = NULL; | |
19e4cfef | 747 | int canceled = 0; |
6dbe3af9 | 748 | |
e3661531 | 749 | assert(cxt); |
e3661531 | 750 | |
641a75ca | 751 | if (fdisk_ask_partnum(cxt, &i, FALSE)) |
24f4bbff | 752 | return; |
2b6fc908 | 753 | |
85151521 KZ |
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); | |
e6d0c4c1 | 760 | old = t ? fdisk_parttype_get_name(t) : _("Unknown"); |
85151521 KZ |
761 | |
762 | do { | |
19e4cfef KZ |
763 | t = ask_partition_type(cxt, &canceled); |
764 | if (canceled) | |
765 | break; | |
85151521 KZ |
766 | } while (!t); |
767 | ||
e6effcac | 768 | if (canceled == 0 && t && fdisk_set_partition_type(cxt, i, t) == 0) |
0477369a | 769 | fdisk_info(cxt, |
85151521 | 770 | _("Changed type of partition '%s' to '%s'."), |
a745611d | 771 | old, t ? fdisk_parttype_get_name(t) : _("Unknown")); |
85151521 KZ |
772 | else |
773 | fdisk_info(cxt, | |
774 | _("Type of partition %zu is unchanged: %s."), | |
775 | i + 1, old); | |
776 | ||
d6cfa8b3 | 777 | fdisk_unref_partition(pa); |
4bd02cdf | 778 | fdisk_unref_parttype(t); |
6dbe3af9 KZ |
779 | } |
780 | ||
a4c0dce8 KZ |
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)) { | |
1b6adce2 | 830 | fdisk_warnx(cxt, _("Partition %zu has an unspecified range."), n + 1); |
a4c0dce8 KZ |
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)) { | |
1b6adce2 | 865 | fdisk_warnx(cxt, _("Free space %"PRIu64 "has an unspecified range"), n); |
a4c0dce8 KZ |
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', | |
b35e30e5 | 886 | _("free space sectors"), 'f', NULL) != 0) |
a4c0dce8 KZ |
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 | ||
3e3b51b3 JLB |
906 | int print_partition_info(struct fdisk_context *cxt) |
907 | { | |
908 | struct fdisk_partition *pa = NULL; | |
6360bcca | 909 | int rc = 0; |
3e3b51b3 JLB |
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 | ||
6360bcca | 922 | if ((rc = fdisk_label_get_fields_ids_all(lb, cxt, &fields, &nfields))) |
3e3b51b3 | 923 | goto clean_data; |
3e3b51b3 JLB |
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; | |
6360bcca KZ |
936 | if (!data || !*data) |
937 | continue; | |
14056588 | 938 | fdisk_info(cxt, "%15s: %s", fdisk_field_get_name(fd), data); |
3e3b51b3 JLB |
939 | free(data); |
940 | } | |
941 | ||
942 | clean_data: | |
943 | fdisk_unref_partition(pa); | |
944 | free(fields); | |
945 | return rc; | |
946 | } | |
947 | ||
e916600f KZ |
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 | } | |
6dbe3af9 | 960 | |
a970c2dd | 961 | static void dump_buffer(off_t base, unsigned char *buf, size_t sz) |
09f0c3d9 | 962 | { |
e916600f KZ |
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) { | |
a970c2dd | 969 | if (!next) |
e916600f | 970 | next = skip_empty(buf, i, sz); |
fdbd7bb9 | 971 | printf("%08jx ", (intmax_t)base + i); |
e916600f KZ |
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 */ | |
6dbe3af9 | 978 | l = -1; |
e916600f KZ |
979 | if (next > i) { |
980 | printf("*\n"); | |
981 | i = next - 1; | |
982 | } | |
983 | next = 0; | |
6dbe3af9 KZ |
984 | } |
985 | } | |
986 | if (l > 0) | |
987 | printf("\n"); | |
6dbe3af9 KZ |
988 | } |
989 | ||
e916600f | 990 | static void dump_blkdev(struct fdisk_context *cxt, const char *name, |
a970c2dd | 991 | uint64_t offset, size_t size) |
e53ced85 | 992 | { |
8eccde20 KZ |
993 | int fd = fdisk_get_devfd(cxt); |
994 | ||
fdbd7bb9 | 995 | fdisk_info(cxt, _("\n%s: offset = %"PRIu64", size = %zu bytes."), |
e916600f KZ |
996 | name, offset, size); |
997 | ||
8eccde20 KZ |
998 | assert(fd >= 0); |
999 | ||
9bbcf43f | 1000 | if (lseek(fd, (off_t) offset, SEEK_SET) == (off_t) -1) |
e916600f | 1001 | fdisk_warn(cxt, _("cannot seek")); |
5afed187 KZ |
1002 | else { |
1003 | unsigned char *buf = xmalloc(size); | |
1004 | ||
8eccde20 | 1005 | if (read_all(fd, (char *) buf, size) != (ssize_t) size) |
5afed187 KZ |
1006 | fdisk_warn(cxt, _("cannot read")); |
1007 | else | |
a970c2dd | 1008 | dump_buffer(offset, buf, size); |
5afed187 KZ |
1009 | free(buf); |
1010 | } | |
e916600f KZ |
1011 | } |
1012 | ||
1013 | void dump_firstsector(struct fdisk_context *cxt) | |
1014 | { | |
e3661531 | 1015 | assert(cxt); |
6dbe3af9 | 1016 | |
a970c2dd | 1017 | dump_blkdev(cxt, _("First sector"), 0, fdisk_get_sector_size(cxt)); |
e916600f KZ |
1018 | } |
1019 | ||
1020 | void dump_disklabel(struct fdisk_context *cxt) | |
1021 | { | |
e916600f KZ |
1022 | int i = 0; |
1023 | const char *name = NULL; | |
9bbcf43f | 1024 | uint64_t offset = 0; |
e916600f KZ |
1025 | size_t size = 0; |
1026 | ||
1027 | assert(cxt); | |
e3661531 | 1028 | |
e916600f | 1029 | while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size) |
a970c2dd | 1030 | dump_blkdev(cxt, name, offset, size); |
6dbe3af9 KZ |
1031 | } |
1032 | ||
0073a4cf | 1033 | static fdisk_sector_t get_dev_blocks(char *dev) |
6da365de | 1034 | { |
bbe67996 | 1035 | int fd, ret; |
0073a4cf | 1036 | fdisk_sector_t size; |
6da365de | 1037 | |
74c9dbae | 1038 | if ((fd = open(dev, O_RDONLY|O_NONBLOCK)) < 0) |
289dcc90 | 1039 | err(EXIT_FAILURE, _("cannot open %s"), dev); |
0073a4cf | 1040 | ret = blkdev_get_sectors(fd, (unsigned long long *) &size); |
6da365de | 1041 | close(fd); |
bbe67996 SK |
1042 | if (ret < 0) |
1043 | err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), dev); | |
6da365de DB |
1044 | return size/2; |
1045 | } | |
1046 | ||
5635d195 KZ |
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, _( | |
6cd671d4 | 1061 | "The device contains '%s' signature and it will be removed by a write command. " |
ce16f04e | 1062 | "See fdisk(8) man page and --wipe option for more details."), |
5635d195 KZ |
1063 | fdisk_get_collision(cxt)); |
1064 | else | |
1065 | fdisk_warnx(cxt, _( | |
6cd671d4 | 1066 | "The device contains '%s' signature and it may remain on the device. " |
5635d195 KZ |
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 | ||
6e1eda6f | 1072 | static void __attribute__((__noreturn__)) usage(void) |
e3a4aaa7 | 1073 | { |
6e1eda6f RM |
1074 | FILE *out = stdout; |
1075 | ||
e3a4aaa7 KZ |
1076 | fputs(USAGE_HEADER, out); |
1077 | ||
1078 | fprintf(out, | |
7eeb2c82 WS |
1079 | _(" %1$s [options] <disk> change partition table\n" |
1080 | " %1$s [options] -l [<disk>...] list partition table(s)\n"), | |
e3a4aaa7 KZ |
1081 | program_invocation_short_name); |
1082 | ||
451dbcfa BS |
1083 | fputs(USAGE_SEPARATOR, out); |
1084 | fputs(_("Display or manipulate a disk partition table.\n"), out); | |
1085 | ||
e3a4aaa7 KZ |
1086 | fputs(USAGE_OPTIONS, out); |
1087 | fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out); | |
718b6f0c | 1088 | fputs(_(" -B, --protect-boot don't erase bootbits when creating a new label\n"), out); |
e3a4aaa7 | 1089 | fputs(_(" -c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)\n"), out); |
a7466bdc SK |
1090 | fprintf(out, |
1091 | _(" -L, --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never"); | |
5d51dc2a KZ |
1092 | fprintf(out, |
1093 | " %s\n", USAGE_COLORS_DEFAULT); | |
9e930041 | 1094 | fputs(_(" -l, --list display partitions and exit\n"), out); |
99d78b2f KZ |
1095 | fputs(_(" -x, --list-details like --list but with more details\n"), out); |
1096 | ||
f2229320 | 1097 | fputs(_(" -n, --noauto-pt don't create default partition table on empty devices\n"), out); |
2d7dffeb | 1098 | fputs(_(" -o, --output <list> output columns\n"), out); |
e3a4aaa7 KZ |
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); | |
354f8cc8 | 1102 | fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out); |
9cd88771 KZ |
1103 | fprintf(out, |
1104 | _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); | |
a7466bdc SK |
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"); | |
e3a4aaa7 KZ |
1109 | |
1110 | fputs(USAGE_SEPARATOR, out); | |
4b4e391a KZ |
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); | |
e3a4aaa7 KZ |
1114 | |
1115 | fputs(USAGE_SEPARATOR, out); | |
bad4c729 | 1116 | fprintf(out, USAGE_HELP_OPTIONS(31)); |
e3a4aaa7 | 1117 | |
fff8ad58 KZ |
1118 | list_available_columns(out); |
1119 | ||
bad4c729 | 1120 | fprintf(out, USAGE_MAN_TAIL("fdisk(8)")); |
6e1eda6f | 1121 | exit(EXIT_SUCCESS); |
e3a4aaa7 KZ |
1122 | } |
1123 | ||
1124 | ||
5b72b3dd KZ |
1125 | enum { |
1126 | ACT_FDISK = 0, /* default */ | |
1127 | ACT_LIST, | |
99d78b2f | 1128 | ACT_LIST_DETAILS, |
5b72b3dd KZ |
1129 | ACT_SHOWSIZE |
1130 | }; | |
1131 | ||
e1144767 DB |
1132 | int main(int argc, char **argv) |
1133 | { | |
f2229320 | 1134 | int rc, i, c, act = ACT_FDISK, noauto_pt = 0; |
d0c9ddc3 | 1135 | int colormode = UL_COLORMODE_UNDEF; |
4e0e8253 | 1136 | struct fdisk_context *cxt; |
fff8ad58 | 1137 | char *outarg = NULL; |
9cd88771 | 1138 | const char *devname, *lockmode = NULL; |
354f8cc8 | 1139 | enum { |
9cd88771 KZ |
1140 | OPT_BYTES = CHAR_MAX + 1, |
1141 | OPT_LOCK | |
354f8cc8 | 1142 | }; |
e3a4aaa7 | 1143 | static const struct option longopts[] = { |
354f8cc8 | 1144 | { "bytes", no_argument, NULL, OPT_BYTES }, |
e3a4aaa7 KZ |
1145 | { "color", optional_argument, NULL, 'L' }, |
1146 | { "compatibility", optional_argument, NULL, 'c' }, | |
4b4e391a | 1147 | { "cylinders", required_argument, NULL, 'C' }, |
f1970cc5 | 1148 | { "heads", required_argument, NULL, 'H' }, |
4b4e391a | 1149 | { "sectors", required_argument, NULL, 'S' }, |
e3a4aaa7 KZ |
1150 | { "getsz", no_argument, NULL, 's' }, |
1151 | { "help", no_argument, NULL, 'h' }, | |
1152 | { "list", no_argument, NULL, 'l' }, | |
99d78b2f | 1153 | { "list-details", no_argument, NULL, 'x' }, |
9cd88771 | 1154 | { "lock", optional_argument, NULL, OPT_LOCK }, |
f2229320 | 1155 | { "noauto-pt", no_argument, NULL, 'n' }, |
e3a4aaa7 KZ |
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' }, | |
55ef5938 | 1160 | { "output", required_argument, NULL, 'o' }, |
aeb9a30b | 1161 | { "protect-boot", no_argument, NULL, 'B' }, |
cb9a4b00 | 1162 | { "wipe", required_argument, NULL, 'w' }, |
ba465623 | 1163 | { "wipe-partitions",required_argument, NULL, 'W' }, |
e3a4aaa7 KZ |
1164 | { NULL, 0, NULL, 0 } |
1165 | }; | |
1166 | ||
7eda085c KZ |
1167 | setlocale(LC_ALL, ""); |
1168 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1169 | textdomain(PACKAGE); | |
2c308875 | 1170 | close_stdout_atexit(); |
2b6fc908 | 1171 | |
4e0e8253 | 1172 | fdisk_init_debug(0); |
053939a4 | 1173 | scols_init_debug(0); |
e6d0c4c1 KZ |
1174 | fdiskprog_init_debug(); |
1175 | ||
4e0e8253 KZ |
1176 | cxt = fdisk_new_context(); |
1177 | if (!cxt) | |
1178 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
1179 | ||
6a632136 | 1180 | fdisk_set_ask(cxt, ask_callback, NULL); |
416c43a9 | 1181 | |
f2229320 | 1182 | while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::no:sS:t:u::vVw:W:x", |
e3a4aaa7 | 1183 | longopts, NULL)) != -1) { |
2b6fc908 KZ |
1184 | switch (c) { |
1185 | case 'b': | |
6bcd192c KZ |
1186 | { |
1187 | size_t sz = strtou32_or_err(optarg, | |
1188 | _("invalid sector size argument")); | |
1189 | if (sz != 512 && sz != 1024 && sz != 2048 && sz != 4096) | |
6e1eda6f | 1190 | errx(EXIT_FAILURE, _("invalid sector size argument")); |
6bcd192c | 1191 | fdisk_save_user_sector_size(cxt, sz, sz); |
2b6fc908 | 1192 | break; |
6bcd192c | 1193 | } |
9ff89c5d | 1194 | case 'B': |
aeb9a30b KZ |
1195 | fdisk_enable_bootbits_protection(cxt, 1); |
1196 | break; | |
0e6f4a20 | 1197 | case 'C': |
1653f0b0 KZ |
1198 | fdisk_save_user_geometry(cxt, |
1199 | strtou32_or_err(optarg, | |
1200 | _("invalid cylinders argument")), | |
1201 | 0, 0); | |
0e6f4a20 | 1202 | break; |
78498b7b | 1203 | case 'c': |
852ce62b | 1204 | if (optarg) { |
2b0bc17b | 1205 | /* this setting is independent on the current |
e3a4aaa7 KZ |
1206 | * actively used label |
1207 | */ | |
1208 | char *p = *optarg == '=' ? optarg + 1 : optarg; | |
6a632136 | 1209 | struct fdisk_label *lb = fdisk_get_label(cxt, "dos"); |
e3a4aaa7 | 1210 | |
852ce62b KZ |
1211 | if (!lb) |
1212 | err(EXIT_FAILURE, _("not found DOS label driver")); | |
e3a4aaa7 | 1213 | if (strcmp(p, "dos") == 0) |
852ce62b | 1214 | fdisk_dos_enable_compatible(lb, TRUE); |
e3a4aaa7 | 1215 | else if (strcmp(p, "nondos") == 0) |
852ce62b | 1216 | fdisk_dos_enable_compatible(lb, FALSE); |
6e1eda6f RM |
1217 | else |
1218 | errx(EXIT_FAILURE, _("unknown compatibility mode '%s'"), p); | |
852ce62b KZ |
1219 | } |
1220 | /* use default if no optarg specified */ | |
78498b7b | 1221 | break; |
0e6f4a20 | 1222 | case 'H': |
1653f0b0 KZ |
1223 | fdisk_save_user_geometry(cxt, 0, |
1224 | strtou32_or_err(optarg, | |
1225 | _("invalid heads argument")), | |
1226 | 0); | |
0e6f4a20 KZ |
1227 | break; |
1228 | case 'S': | |
1653f0b0 KZ |
1229 | fdisk_save_user_geometry(cxt, 0, 0, |
1230 | strtou32_or_err(optarg, | |
1231 | _("invalid sectors argument"))); | |
0e6f4a20 | 1232 | break; |
2b6fc908 | 1233 | case 'l': |
5b72b3dd | 1234 | act = ACT_LIST; |
2b6fc908 | 1235 | break; |
99d78b2f KZ |
1236 | case 'x': |
1237 | act = ACT_LIST_DETAILS; | |
1238 | break; | |
80a1712f | 1239 | case 'L': |
5d51dc2a | 1240 | colormode = UL_COLORMODE_AUTO; |
80a1712f | 1241 | if (optarg) |
42b60bce | 1242 | colormode = colormode_or_err(optarg); |
80a1712f | 1243 | break; |
f2229320 KZ |
1244 | case 'n': |
1245 | noauto_pt = 1; | |
1246 | break; | |
fff8ad58 KZ |
1247 | case 'o': |
1248 | outarg = optarg; | |
1249 | break; | |
2b6fc908 | 1250 | case 's': |
5b72b3dd | 1251 | act = ACT_SHOWSIZE; |
2b6fc908 | 1252 | break; |
565b7da6 KZ |
1253 | case 't': |
1254 | { | |
1255 | struct fdisk_label *lb = NULL; | |
1256 | ||
6a632136 | 1257 | while (fdisk_next_label(cxt, &lb) == 0) |
565b7da6 KZ |
1258 | fdisk_label_set_disabled(lb, 1); |
1259 | ||
6a632136 | 1260 | lb = fdisk_get_label(cxt, optarg); |
565b7da6 KZ |
1261 | if (!lb) |
1262 | errx(EXIT_FAILURE, _("unsupported disklabel: %s"), optarg); | |
1263 | fdisk_label_set_disabled(lb, 0); | |
1ce1a112 | 1264 | break; |
565b7da6 | 1265 | } |
2b6fc908 | 1266 | case 'u': |
ec10aa67 KZ |
1267 | if (optarg && *optarg == '=') |
1268 | optarg++; | |
6a632136 | 1269 | if (fdisk_set_unit(cxt, optarg) != 0) |
6e1eda6f | 1270 | errx(EXIT_FAILURE, _("unsupported unit")); |
2b6fc908 | 1271 | break; |
e3a4aaa7 KZ |
1272 | case 'V': /* preferred for util-linux */ |
1273 | case 'v': /* for backward compatibility only */ | |
2c308875 | 1274 | print_version(EXIT_SUCCESS); |
cb9a4b00 KZ |
1275 | case 'w': |
1276 | wipemode = wipemode_from_string(optarg); | |
1277 | if (wipemode < 0) | |
1278 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
1279 | break; | |
ba465623 KZ |
1280 | case 'W': |
1281 | pwipemode = wipemode_from_string(optarg); | |
1282 | if (pwipemode < 0) | |
1283 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
1284 | break; | |
e1144767 | 1285 | case 'h': |
6e1eda6f | 1286 | usage(); |
354f8cc8 KZ |
1287 | case OPT_BYTES: |
1288 | fdisk_set_size_unit(cxt, FDISK_SIZEUNIT_BYTES); | |
1289 | break; | |
9cd88771 KZ |
1290 | case OPT_LOCK: |
1291 | lockmode = "1"; | |
1292 | if (optarg) { | |
1293 | if (*optarg == '=') | |
1294 | optarg++; | |
1295 | lockmode = optarg; | |
1296 | } | |
1297 | break; | |
2b6fc908 | 1298 | default: |
677ec86c | 1299 | errtryhelp(EXIT_FAILURE); |
2b6fc908 | 1300 | } |
6dbe3af9 | 1301 | } |
2b6fc908 | 1302 | |
6bcd192c KZ |
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.")); | |
7eda085c | 1306 | |
d0c9ddc3 | 1307 | colors_init(colormode, "fdisk"); |
c1154128 | 1308 | is_interactive = isatty(STDIN_FILENO); |
ffcf0540 | 1309 | |
5b72b3dd KZ |
1310 | switch (act) { |
1311 | case ACT_LIST: | |
99d78b2f | 1312 | case ACT_LIST_DETAILS: |
6a632136 | 1313 | fdisk_enable_listonly(cxt, 1); |
99d78b2f KZ |
1314 | |
1315 | if (act == ACT_LIST_DETAILS) | |
1316 | fdisk_enable_details(cxt, 1); | |
1317 | ||
fff8ad58 | 1318 | init_fields(cxt, outarg, NULL); |
ffcf0540 | 1319 | |
2b6fc908 KZ |
1320 | if (argc > optind) { |
1321 | int k; | |
8fdd483c | 1322 | |
4a52959d WS |
1323 | for (rc = 0, k = optind; k < argc; k++) |
1324 | rc += print_device_pt(cxt, argv[k], 1, 0, k != optind); | |
8fdd483c | 1325 | |
8fdd483c KZ |
1326 | if (rc) |
1327 | return EXIT_FAILURE; | |
ea4824f1 | 1328 | } else |
d2c47697 | 1329 | print_all_devices_pt(cxt, 0); |
5b72b3dd | 1330 | break; |
2b6fc908 | 1331 | |
5b72b3dd | 1332 | case ACT_SHOWSIZE: |
9564e46c | 1333 | /* deprecated */ |
6e1eda6f RM |
1334 | if (argc - optind <= 0) { |
1335 | warnx(_("bad usage")); | |
1336 | errtryhelp(EXIT_FAILURE); | |
1337 | } | |
6da365de | 1338 | for (i = optind; i < argc; i++) { |
0073a4cf KZ |
1339 | uintmax_t blks = get_dev_blocks(argv[i]); |
1340 | ||
5b72b3dd | 1341 | if (argc - optind == 1) |
0073a4cf | 1342 | printf("%ju\n", blks); |
2b6fc908 | 1343 | else |
0073a4cf | 1344 | printf("%s: %ju\n", argv[i], blks); |
6dbe3af9 | 1345 | } |
5b72b3dd | 1346 | break; |
6dbe3af9 | 1347 | |
5b72b3dd | 1348 | case ACT_FDISK: |
6e1eda6f RM |
1349 | if (argc-optind != 1) { |
1350 | warnx(_("bad usage")); | |
1351 | errtryhelp(EXIT_FAILURE); | |
1352 | } | |
7eda085c | 1353 | |
5b72b3dd | 1354 | /* Here starts interactive mode, use fdisk_{warn,info,..} functions */ |
496c979a | 1355 | color_scheme_enable("welcome", UL_COLOR_GREEN); |
09f0c3d9 | 1356 | fdisk_info(cxt, _("Welcome to fdisk (%s)."), PACKAGE_STRING); |
80a1712f KZ |
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")); | |
2d8988bd | 1360 | |
9cd88771 KZ |
1361 | devname = argv[optind]; |
1362 | rc = fdisk_assign_device(cxt, devname, 0); | |
e146ae4e | 1363 | if (rc == -EACCES) { |
9cd88771 | 1364 | rc = fdisk_assign_device(cxt, devname, 1); |
e146ae4e | 1365 | if (rc == 0) |
62eea9ce | 1366 | fdisk_warnx(cxt, _("Device is open in read-only mode.")); |
e146ae4e KZ |
1367 | } |
1368 | if (rc) | |
9cd88771 | 1369 | err(EXIT_FAILURE, _("cannot open %s"), devname); |
2d8988bd | 1370 | |
dee0c29c KZ |
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 | ||
5b72b3dd | 1377 | fflush(stdout); |
56c07b96 | 1378 | |
9cd88771 KZ |
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 | ||
5635d195 KZ |
1386 | if (fdisk_get_collision(cxt)) |
1387 | follow_wipe_mode(cxt); | |
cb9a4b00 | 1388 | |
aa36c2cf | 1389 | if (!fdisk_has_label(cxt)) { |
2d8988bd | 1390 | fdisk_info(cxt, _("Device does not contain a recognized partition table.")); |
f2229320 KZ |
1391 | if (!noauto_pt) |
1392 | fdisk_create_disklabel(cxt, NULL); | |
433d05ff | 1393 | |
aa36c2cf | 1394 | } else if (fdisk_is_label(cxt, GPT) && fdisk_gpt_is_hybrid(cxt)) |
433d05ff | 1395 | fdisk_warnx(cxt, _( |
09af3db4 | 1396 | "A hybrid GPT was detected. You have to sync " |
433d05ff | 1397 | "the hybrid MBR manually (expert command 'M').")); |
6dbe3af9 | 1398 | |
fff8ad58 KZ |
1399 | init_fields(cxt, outarg, NULL); /* -o <columns> */ |
1400 | ||
fadd8e08 KZ |
1401 | if (!fdisk_is_readonly(cxt)) { |
1402 | fdisk_get_partitions(cxt, &original_layout); | |
1403 | device_is_used = fdisk_device_is_used(cxt); | |
1404 | } | |
1405 | ||
5b72b3dd KZ |
1406 | while (1) |
1407 | process_fdisk_menu(&cxt); | |
1408 | } | |
9777759a | 1409 | |
c7119037 | 1410 | fdisk_unref_context(cxt); |
5b72b3dd | 1411 | return EXIT_SUCCESS; |
6dbe3af9 | 1412 | } |