]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl) | |
3 | * Copyright (C) 2014 Karel Zak <kzak@redhat.com> | |
4 | * | |
5 | * This program is free software. You can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation: either Version 1 | |
8 | * or (at your option) any later version. | |
9 | * | |
10 | * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994, | |
11 | * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu, | |
12 | * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl) | |
13 | * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e. | |
14 | * This program had (head,sector,cylinder) as basic unit, and was | |
15 | * (therefore) broken in several ways for the use on larger disks - | |
16 | * for example, my last patch (from 2.0d to 2.0e) was required | |
17 | * to allow a partition to cross cylinder 8064, and to write an | |
18 | * extended partition past the 4GB mark. | |
19 | * | |
20 | * Karel Zak wrote new sfdisk based on libfdisk from util-linux | |
21 | * in 2014. | |
22 | */ | |
23 | ||
24 | #include <unistd.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <ctype.h> | |
29 | #include <errno.h> | |
30 | #include <getopt.h> | |
31 | #include <sys/stat.h> | |
32 | #include <assert.h> | |
33 | #include <fcntl.h> | |
34 | #include <libsmartcols.h> | |
35 | #ifdef HAVE_LIBREADLINE | |
36 | # define _FUNCTION_DEF | |
37 | # include <readline/readline.h> | |
38 | #endif | |
39 | #include <libgen.h> | |
40 | #include <sys/time.h> | |
41 | ||
42 | #include "c.h" | |
43 | #include "xalloc.h" | |
44 | #include "nls.h" | |
45 | #include "debug.h" | |
46 | #include "strutils.h" | |
47 | #include "closestream.h" | |
48 | #include "colors.h" | |
49 | #include "blkdev.h" | |
50 | #include "all-io.h" | |
51 | #include "rpmatch.h" | |
52 | #include "optutils.h" | |
53 | #include "ttyutils.h" | |
54 | ||
55 | #include "libfdisk.h" | |
56 | #include "fdisk-list.h" | |
57 | ||
58 | /* | |
59 | * sfdisk debug stuff (see fdisk.h and include/debug.h) | |
60 | */ | |
61 | static UL_DEBUG_DEFINE_MASK(sfdisk); | |
62 | UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES; | |
63 | ||
64 | #define SFDISKPROG_DEBUG_INIT (1 << 1) | |
65 | #define SFDISKPROG_DEBUG_PARSE (1 << 2) | |
66 | #define SFDISKPROG_DEBUG_MISC (1 << 3) | |
67 | #define SFDISKPROG_DEBUG_ASK (1 << 4) | |
68 | #define SFDISKPROG_DEBUG_ALL 0xFFFF | |
69 | ||
70 | #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x) | |
71 | #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x) | |
72 | ||
73 | enum { | |
74 | ACT_FDISK = 1, | |
75 | ACT_ACTIVATE, | |
76 | ACT_CHANGE_ID, | |
77 | ACT_DUMP, | |
78 | ACT_LIST, | |
79 | ACT_LIST_FREE, | |
80 | ACT_LIST_TYPES, | |
81 | ACT_REORDER, | |
82 | ACT_RELOCATE, | |
83 | ACT_SHOW_SIZE, | |
84 | ACT_SHOW_GEOM, | |
85 | ACT_VERIFY, | |
86 | ACT_PARTTYPE, | |
87 | ACT_PARTUUID, | |
88 | ACT_PARTLABEL, | |
89 | ACT_PARTATTRS, | |
90 | ACT_DISCARD_FREE, | |
91 | ACT_DISKID, | |
92 | ACT_DELETE, | |
93 | ACT_BACKUP_SECTORS, | |
94 | }; | |
95 | ||
96 | struct sfdisk { | |
97 | int act; /* ACT_* */ | |
98 | int partno; /* -N <partno>, default -1 */ | |
99 | int wipemode; /* remove foreign signatures from disk */ | |
100 | int pwipemode; /* remove foreign signatures from partitions */ | |
101 | const char *lockmode; /* as specified by --lock */ | |
102 | const char *label; /* --label <label> */ | |
103 | const char *label_nested; /* --label-nested <label> */ | |
104 | const char *backup_file; /* -O <path> */ | |
105 | const char *move_typescript; /* --movedata <typescript> */ | |
106 | char *prompt; | |
107 | ||
108 | struct fdisk_context *cxt; /* libfdisk context */ | |
109 | struct fdisk_partition *orig_pa; /* -N <partno> before the change */ | |
110 | ||
111 | unsigned int verify : 1, /* call fdisk_verify_disklabel() */ | |
112 | quiet : 1, /* suppress extra messages */ | |
113 | interactive : 1, /* running on tty */ | |
114 | noreread : 1, /* don't check device is in use */ | |
115 | force : 1, /* do also stupid things */ | |
116 | backup : 1, /* backup sectors before write PT */ | |
117 | container : 1, /* PT contains container (MBR extended) partitions */ | |
118 | unused : 1, /* PT contains unused partition */ | |
119 | append : 1, /* don't create new PT, append partitions only */ | |
120 | json : 1, /* JSON dump */ | |
121 | movedata: 1, /* move data after resize */ | |
122 | movefsync: 1, /* use fsync() after each write() */ | |
123 | notell : 1, /* don't tell kernel about new PT */ | |
124 | noact : 1; /* do not write to device */ | |
125 | }; | |
126 | ||
127 | #define SFDISK_PROMPT ">>> " | |
128 | ||
129 | static void sfdiskprog_init_debug(void) | |
130 | { | |
131 | __UL_INIT_DEBUG_FROM_ENV(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG); | |
132 | } | |
133 | ||
134 | ||
135 | static int get_user_reply(const char *prompt, char *buf, size_t bufsz) | |
136 | { | |
137 | char *p; | |
138 | size_t sz; | |
139 | ||
140 | #ifdef HAVE_LIBREADLINE | |
141 | if (isatty(STDIN_FILENO)) { | |
142 | p = readline(prompt); | |
143 | if (!p) | |
144 | return 1; | |
145 | xstrncpy(buf, p, bufsz); | |
146 | free(p); | |
147 | } else | |
148 | #endif | |
149 | { | |
150 | fputs(prompt, stdout); | |
151 | fflush(stdout); | |
152 | ||
153 | if (!fgets(buf, bufsz, stdin)) | |
154 | return 1; | |
155 | } | |
156 | ||
157 | for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ | |
158 | ||
159 | if (p > buf) | |
160 | memmove(buf, p, p - buf); /* remove blank space */ | |
161 | sz = strlen(buf); | |
162 | if (sz && *(buf + sz - 1) == '\n') | |
163 | *(buf + sz - 1) = '\0'; | |
164 | ||
165 | DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf)); | |
166 | return 0; | |
167 | } | |
168 | ||
169 | static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)), | |
170 | struct fdisk_ask *ask, | |
171 | void *data) | |
172 | { | |
173 | struct sfdisk *sf = (struct sfdisk *) data; | |
174 | int rc = 0; | |
175 | ||
176 | assert(ask); | |
177 | ||
178 | switch(fdisk_ask_get_type(ask)) { | |
179 | case FDISK_ASKTYPE_INFO: | |
180 | if (sf->quiet) | |
181 | break; | |
182 | fputs(fdisk_ask_print_get_mesg(ask), stdout); | |
183 | fputc('\n', stdout); | |
184 | break; | |
185 | case FDISK_ASKTYPE_WARNX: | |
186 | fflush(stdout); | |
187 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
188 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
189 | color_fdisable(stderr); | |
190 | fputc('\n', stderr); | |
191 | break; | |
192 | case FDISK_ASKTYPE_WARN: | |
193 | fflush(stdout); | |
194 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
195 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
196 | errno = fdisk_ask_print_get_errno(ask); | |
197 | fprintf(stderr, ": %m\n"); | |
198 | color_fdisable(stderr); | |
199 | break; | |
200 | case FDISK_ASKTYPE_YESNO: | |
201 | { | |
202 | char buf[BUFSIZ] = { '\0' }; | |
203 | fputc('\n', stdout); | |
204 | do { | |
205 | int x; | |
206 | fputs(fdisk_ask_get_query(ask), stdout); | |
207 | rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf)); | |
208 | if (rc) | |
209 | break; | |
210 | x = rpmatch(buf); | |
211 | if (x == RPMATCH_YES || x == RPMATCH_NO) { | |
212 | fdisk_ask_yesno_set_result(ask, x); | |
213 | break; | |
214 | } | |
215 | } while(1); | |
216 | DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc)); | |
217 | break; | |
218 | } | |
219 | default: | |
220 | break; | |
221 | } | |
222 | return rc; | |
223 | } | |
224 | ||
225 | static void sfdisk_init(struct sfdisk *sf) | |
226 | { | |
227 | fdisk_init_debug(0); | |
228 | scols_init_debug(0); | |
229 | sfdiskprog_init_debug(); | |
230 | ||
231 | sf->cxt = fdisk_new_context(); | |
232 | if (!sf->cxt) | |
233 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
234 | fdisk_set_ask(sf->cxt, ask_callback, (void *) sf); | |
235 | ||
236 | if (sf->wipemode != WIPEMODE_ALWAYS) | |
237 | fdisk_enable_bootbits_protection(sf->cxt, 1); | |
238 | ||
239 | if (sf->label_nested) { | |
240 | struct fdisk_context *x = fdisk_new_nested_context(sf->cxt, | |
241 | sf->label_nested); | |
242 | if (!x) | |
243 | err(EXIT_FAILURE, _("failed to allocate nested libfdisk context")); | |
244 | /* the original context is available by fdisk_get_parent() */ | |
245 | sf->cxt = x; | |
246 | } | |
247 | } | |
248 | ||
249 | static int sfdisk_deinit(struct sfdisk *sf) | |
250 | { | |
251 | struct fdisk_context *parent; | |
252 | ||
253 | assert(sf); | |
254 | assert(sf->cxt); | |
255 | ||
256 | parent = fdisk_get_parent(sf->cxt); | |
257 | if (parent) { | |
258 | fdisk_unref_context(sf->cxt); | |
259 | sf->cxt = parent; | |
260 | } | |
261 | ||
262 | fdisk_unref_context(sf->cxt); | |
263 | free(sf->prompt); | |
264 | ||
265 | memset(sf, 0, sizeof(*sf)); | |
266 | return 0; | |
267 | } | |
268 | ||
269 | static struct fdisk_partition *get_partition(struct fdisk_context *cxt, size_t partno) | |
270 | { | |
271 | struct fdisk_table *tb = NULL; | |
272 | struct fdisk_partition *pa; | |
273 | ||
274 | if (fdisk_get_partitions(cxt, &tb) != 0) | |
275 | return NULL; | |
276 | ||
277 | pa = fdisk_table_get_partition_by_partno(tb, partno); | |
278 | if (pa) | |
279 | fdisk_ref_partition(pa); | |
280 | fdisk_unref_table(tb); | |
281 | return pa; | |
282 | } | |
283 | ||
284 | static void backup_sectors(struct sfdisk *sf, | |
285 | const char *tpl, | |
286 | const char *name, | |
287 | const char *devname, | |
288 | uint64_t offset, size_t size) | |
289 | { | |
290 | char *fname; | |
291 | int fd, devfd; | |
292 | ||
293 | devfd = fdisk_get_devfd(sf->cxt); | |
294 | assert(devfd >= 0); | |
295 | ||
296 | xasprintf(&fname, "%s0x%08"PRIx64".bak", tpl, offset); | |
297 | ||
298 | fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); | |
299 | if (fd < 0) | |
300 | goto fail; | |
301 | ||
302 | if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) { | |
303 | fdisk_warn(sf->cxt, _("cannot seek %s"), devname); | |
304 | goto fail; | |
305 | } else { | |
306 | unsigned char *buf = xmalloc(size); | |
307 | ||
308 | if (read_all(devfd, (char *) buf, size) != (ssize_t) size) { | |
309 | fdisk_warn(sf->cxt, _("cannot read %s"), devname); | |
310 | free(buf); | |
311 | goto fail; | |
312 | } | |
313 | if (write_all(fd, buf, size) != 0) { | |
314 | fdisk_warn(sf->cxt, _("cannot write %s"), fname); | |
315 | free(buf); | |
316 | goto fail; | |
317 | } | |
318 | free(buf); | |
319 | } | |
320 | ||
321 | fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"), | |
322 | name, (uintmax_t) offset, (uintmax_t) size, fname); | |
323 | close(fd); | |
324 | free(fname); | |
325 | return; | |
326 | fail: | |
327 | errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname); | |
328 | } | |
329 | ||
330 | static char *mk_backup_filename_tpl(const char *filename, const char *devname, const char *suffix) | |
331 | { | |
332 | char *tpl = NULL; | |
333 | char *name, *buf = xstrdup(devname); | |
334 | ||
335 | name = basename(buf); | |
336 | ||
337 | if (!filename || strcmp(filename, "@default") == 0) { | |
338 | const char *home = getenv ("HOME"); | |
339 | if (!home) | |
340 | errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined")); | |
341 | xasprintf(&tpl, "%s/sfdisk-%s%s", home, name, suffix); | |
342 | } else | |
343 | xasprintf(&tpl, "%s-%s%s", filename, name, suffix); | |
344 | ||
345 | free(buf); | |
346 | return tpl; | |
347 | } | |
348 | ||
349 | ||
350 | static void backup_partition_table(struct sfdisk *sf, const char *devname) | |
351 | { | |
352 | const char *name; | |
353 | char *tpl; | |
354 | uint64_t offset = 0; | |
355 | size_t size = 0; | |
356 | int i = 0; | |
357 | ||
358 | assert(sf); | |
359 | ||
360 | if (!fdisk_has_label(sf->cxt)) | |
361 | return; | |
362 | ||
363 | tpl = mk_backup_filename_tpl(sf->backup_file, devname, "-"); | |
364 | ||
365 | color_scheme_enable("header", UL_COLOR_BOLD); | |
366 | fdisk_info(sf->cxt, _("Backup files:")); | |
367 | color_disable(); | |
368 | ||
369 | while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size) | |
370 | backup_sectors(sf, tpl, name, devname, offset, size); | |
371 | ||
372 | if (!sf->quiet) | |
373 | fputc('\n', stdout); | |
374 | free(tpl); | |
375 | } | |
376 | ||
377 | static int assign_device(struct sfdisk *sf, const char *devname, int rdonly) | |
378 | { | |
379 | struct fdisk_context *cxt = sf->cxt; | |
380 | ||
381 | if (fdisk_assign_device(cxt, devname, rdonly) != 0) | |
382 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
383 | ||
384 | if (!fdisk_is_readonly(cxt)) { | |
385 | if (blkdev_lock(fdisk_get_devfd(cxt), devname, sf->lockmode) != 0) { | |
386 | fdisk_deassign_device(cxt, 1); | |
387 | exit(EXIT_FAILURE); | |
388 | } | |
389 | if (sf->backup) | |
390 | backup_partition_table(sf, devname); | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
395 | ||
396 | static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa) | |
397 | { | |
398 | struct fdisk_partition *pa = get_partition(sf->cxt, partno); | |
399 | char *devname = NULL, *typescript = NULL, *buf = NULL; | |
400 | FILE *f = NULL; | |
401 | int ok = 0, fd, backward = 0; | |
402 | fdisk_sector_t nsectors, from, to, step, i, prev; | |
403 | size_t io, ss, step_bytes, cc, ioerr = 0; | |
404 | uintmax_t src, dst, nbytes; | |
405 | int progress = 0, rc = 0; | |
406 | struct timeval prev_time; | |
407 | uint64_t bytes_per_sec = 0; | |
408 | ||
409 | assert(sf->movedata); | |
410 | ||
411 | if (!pa) | |
412 | warnx(_("failed to read new partition from device; ignoring --move-data")); | |
413 | else if (!fdisk_partition_has_size(pa)) | |
414 | warnx(_("failed to get size of the new partition; ignoring --move-data")); | |
415 | else if (!fdisk_partition_has_start(pa)) | |
416 | warnx(_("failed to get start of the new partition; ignoring --move-data")); | |
417 | else if (!fdisk_partition_has_size(orig_pa)) | |
418 | warnx(_("failed to get size of the old partition; ignoring --move-data")); | |
419 | else if (!fdisk_partition_has_start(orig_pa)) | |
420 | warnx(_("failed to get start of the old partition; ignoring --move-data")); | |
421 | else if (fdisk_partition_get_start(pa) == fdisk_partition_get_start(orig_pa)) | |
422 | warnx(_("start of the partition has not been moved; ignoring --move-data")); | |
423 | else if (fdisk_partition_get_size(orig_pa) > fdisk_partition_get_size(pa)) | |
424 | warnx(_("new partition is smaller than original; ignoring --move-data")); | |
425 | else | |
426 | ok = 1; | |
427 | if (!ok) | |
428 | return -EINVAL; | |
429 | ||
430 | DBG(MISC, ul_debug("moving data")); | |
431 | ||
432 | fd = fdisk_get_devfd(sf->cxt); | |
433 | ||
434 | /* set move direction and overlay */ | |
435 | nsectors = fdisk_partition_get_size(orig_pa); | |
436 | from = fdisk_partition_get_start(orig_pa); | |
437 | to = fdisk_partition_get_start(pa); | |
438 | ||
439 | if ((to >= from && from + nsectors >= to) || | |
440 | (from >= to && to + nsectors >= from)) { | |
441 | /* source and target overlay, check if we need to copy | |
442 | * backwardly from end of the source */ | |
443 | DBG(MISC, ul_debug("overlay between source and target")); | |
444 | backward = from < to; | |
445 | DBG(MISC, ul_debug(" copy order: %s", backward ? "backward" : "forward")); | |
446 | } | |
447 | ||
448 | /* set optimal step size -- nearest to 1MiB aligned to optimal I/O */ | |
449 | io = fdisk_get_optimal_iosize(sf->cxt); | |
450 | ss = fdisk_get_sector_size(sf->cxt); | |
451 | if (!io) | |
452 | io = ss; | |
453 | if (io < 1024 * 1024) | |
454 | step_bytes = ((1024 * 1024) + io/2) / io * io; | |
455 | else | |
456 | step_bytes = io; | |
457 | ||
458 | step = step_bytes / ss; | |
459 | nbytes = nsectors * ss; | |
460 | ||
461 | DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes)); | |
462 | ||
463 | #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) | |
464 | if (!backward) | |
465 | ignore_result( posix_fadvise(fd, from * ss, | |
466 | nsectors * ss, POSIX_FADV_SEQUENTIAL) ); | |
467 | #endif | |
468 | devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1); | |
469 | if (sf->move_typescript) | |
470 | typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move"); | |
471 | ||
472 | if (!sf->quiet) { | |
473 | fdisk_info(sf->cxt, "%s", ""); | |
474 | color_scheme_enable("header", UL_COLOR_BOLD); | |
475 | fdisk_info(sf->cxt, sf->noact ? _("Data move: (--no-act)") : _("Data move:")); | |
476 | color_disable(); | |
477 | if (typescript) | |
478 | fdisk_info(sf->cxt, _(" typescript file: %s"), typescript); | |
479 | printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from, (uintmax_t) to); | |
480 | printf(_(" sectors: %ju\n"), (uintmax_t) nsectors); | |
481 | printf(_(" step size: %zu bytes\n"), step_bytes); | |
482 | putchar('\n'); | |
483 | fflush(stdout); | |
484 | ||
485 | if (isatty(fileno(stdout))) | |
486 | progress = 1; | |
487 | } | |
488 | ||
489 | if (sf->interactive) { | |
490 | int yes = 0; | |
491 | fdisk_ask_yesno(sf->cxt, _("Do you want to move partition data?"), &yes); | |
492 | if (!yes) { | |
493 | fdisk_info(sf->cxt, _("Leaving.")); | |
494 | free(devname); | |
495 | return 0; | |
496 | } | |
497 | } | |
498 | ||
499 | if (typescript) { | |
500 | f = fopen(typescript, "w"); | |
501 | if (!f) { | |
502 | rc = -errno; | |
503 | fdisk_warn(sf->cxt, _("cannot open %s"), typescript); | |
504 | goto done; | |
505 | } | |
506 | ||
507 | /* don't translate */ | |
508 | fprintf(f, "# sfdisk: " PACKAGE_STRING "\n"); | |
509 | fprintf(f, "# Disk: %s\n", devname); | |
510 | fprintf(f, "# Partition: %zu\n", partno + 1); | |
511 | fprintf(f, "# Operation: move data\n"); | |
512 | fprintf(f, "# Sector size: %zu\n", ss); | |
513 | fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n", | |
514 | (uintmax_t)from, (uintmax_t)from * ss); | |
515 | fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n", | |
516 | (uintmax_t)to, (uintmax_t)to * ss); | |
517 | fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n", | |
518 | (uintmax_t)nsectors, (uintmax_t)nsectors * ss); | |
519 | fprintf(f, "# Step size (sectors/bytes): %" PRIu64 "/%zu\n", step, step_bytes); | |
520 | fprintf(f, "# Steps: %ju\n", ((uintmax_t) nsectors / step) + 1); | |
521 | fprintf(f, "#\n"); | |
522 | fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n"); | |
523 | } | |
524 | ||
525 | src = (backward ? from + nsectors : from) * ss; | |
526 | dst = (backward ? to + nsectors : to) * ss; | |
527 | buf = xmalloc(step_bytes); | |
528 | ||
529 | DBG(MISC, ul_debug(" initial: src=%ju dst=%ju", src, dst)); | |
530 | ||
531 | gettimeofday(&prev_time, NULL); | |
532 | prev = 0; | |
533 | ||
534 | for (cc = 1, i = 0; i < nsectors && nbytes > 0; i += step, cc++) { | |
535 | ||
536 | if (nbytes < step_bytes) { | |
537 | DBG(MISC, ul_debug("aligning step #%05zu from %zu to %ju", | |
538 | cc, step_bytes, nbytes)); | |
539 | step_bytes = nbytes; | |
540 | } | |
541 | nbytes -= step_bytes; | |
542 | ||
543 | if (backward) | |
544 | src -= step_bytes, dst -= step_bytes; | |
545 | ||
546 | DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst)); | |
547 | ||
548 | if (!sf->noact) { | |
549 | /* read source */ | |
550 | if (lseek(fd, src, SEEK_SET) == (off_t) -1 || | |
551 | read_all(fd, buf, step_bytes) != (ssize_t) step_bytes) { | |
552 | if (f) | |
553 | fprintf(f, "%05zu: read error %12ju %12ju\n", cc, src, dst); | |
554 | fdisk_warn(sf->cxt, | |
555 | _("cannot read at offset: %ju; continue"), src); | |
556 | ioerr++; | |
557 | goto next; | |
558 | } | |
559 | ||
560 | /* write target */ | |
561 | if (lseek(fd, dst, SEEK_SET) == (off_t) -1 || | |
562 | write_all(fd, buf, step_bytes) != 0) { | |
563 | if (f) | |
564 | fprintf(f, "%05zu: write error %12ju %12ju\n", cc, src, dst); | |
565 | fdisk_warn(sf->cxt, | |
566 | _("cannot write at offset: %ju; continue"), dst); | |
567 | ioerr++; | |
568 | goto next; | |
569 | } | |
570 | if (sf->movefsync && fsync(fd) != 0) | |
571 | fdisk_warn(sf->cxt, | |
572 | _("cannot fsync at offset: %ju; continue"), dst); | |
573 | } | |
574 | ||
575 | /* write log */ | |
576 | if (f) | |
577 | fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst); | |
578 | ||
579 | if (progress && i % 10 == 0) { | |
580 | unsigned int elapsed = 0; /* usec */ | |
581 | struct timeval cur_time; | |
582 | ||
583 | gettimeofday(&cur_time, NULL); | |
584 | if (cur_time.tv_sec - prev_time.tv_sec > 1) { | |
585 | elapsed = ((cur_time.tv_sec - prev_time.tv_sec) * 1000000) + | |
586 | (cur_time.tv_usec - prev_time.tv_usec); | |
587 | ||
588 | bytes_per_sec = ((i - prev) * ss) / elapsed; /* per usec */ | |
589 | bytes_per_sec *= 1000000; /* per sec */ | |
590 | ||
591 | prev_time = cur_time; | |
592 | prev = i; | |
593 | } | |
594 | ||
595 | if (bytes_per_sec) | |
596 | fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."), | |
597 | i + 1, nsectors, | |
598 | 100.0 / ((double) nsectors/(i+1)), | |
599 | (double) (bytes_per_sec / (1024 * 1024))); | |
600 | else | |
601 | fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%)."), | |
602 | i + 1, nsectors, | |
603 | 100.0 / ((double) nsectors/(i+1))); | |
604 | fflush(stdout); | |
605 | fputc('\r', stdout); | |
606 | ||
607 | } | |
608 | next: | |
609 | if (!backward) | |
610 | src += step_bytes, dst += step_bytes; | |
611 | } | |
612 | ||
613 | if (progress && nsectors) { | |
614 | int x = get_terminal_width(80); | |
615 | for (; x > 0; x--) | |
616 | fputc(' ', stdout); | |
617 | fflush(stdout); | |
618 | fputc('\r', stdout); | |
619 | ||
620 | if (i > nsectors) | |
621 | /* see for() above; @i has to be greater than @nsectors | |
622 | * on success due to i += step */ | |
623 | i = nsectors; | |
624 | ||
625 | fprintf(stdout, _("Moved %ju from %ju sectors (%.0f%%)."), | |
626 | i, nsectors, | |
627 | 100.0 / ((double) nsectors/(i+1))); | |
628 | fputc('\n', stdout); | |
629 | } | |
630 | rc = 0; | |
631 | done: | |
632 | if (f) | |
633 | fclose(f); | |
634 | free(buf); | |
635 | free(typescript); | |
636 | ||
637 | if (sf->noact) | |
638 | fdisk_info(sf->cxt, _("Your data has not been moved (--no-act).")); | |
639 | if (ioerr) { | |
640 | fdisk_info(sf->cxt, _("%zu I/O errors detected!"), ioerr); | |
641 | rc = -EIO; | |
642 | } else if (rc) | |
643 | warn(_("%s: failed to move data"), devname); | |
644 | ||
645 | free(devname); | |
646 | ||
647 | return rc; | |
648 | } | |
649 | ||
650 | static int write_changes(struct sfdisk *sf) | |
651 | { | |
652 | int rc = 0; | |
653 | ||
654 | if (sf->noact) | |
655 | fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act).")); | |
656 | else | |
657 | rc = fdisk_write_disklabel(sf->cxt); | |
658 | ||
659 | if (rc == 0 && sf->movedata && sf->orig_pa) | |
660 | rc = move_partition_data(sf, sf->partno, sf->orig_pa); | |
661 | ||
662 | if (!sf->noact && !rc) { | |
663 | fdisk_info(sf->cxt, _("\nThe partition table has been altered.")); | |
664 | if (!sf->notell) { | |
665 | /* Let's wait a little bit. It's possible that our | |
666 | * system is still busy with a previous re-read | |
667 | * ioctl (on sfdisk start) or with another task | |
668 | * related to the write to the device. | |
669 | */ | |
670 | xusleep(250000); | |
671 | fdisk_reread_partition_table(sf->cxt); | |
672 | } | |
673 | } | |
674 | ||
675 | if (!rc) | |
676 | rc = fdisk_deassign_device(sf->cxt, | |
677 | sf->noact || sf->notell); /* no-sync */ | |
678 | return rc; | |
679 | } | |
680 | ||
681 | /* | |
682 | * sfdisk --list [<device ..] | |
683 | */ | |
684 | static int command_list_partitions(struct sfdisk *sf, int argc, char **argv) | |
685 | { | |
686 | int fail = 0; | |
687 | fdisk_enable_listonly(sf->cxt, 1); | |
688 | ||
689 | if (argc) { | |
690 | int i; | |
691 | ||
692 | for (i = 0; i < argc; i++) | |
693 | if (print_device_pt(sf->cxt, argv[i], 1, sf->verify, i) != 0) | |
694 | fail++; | |
695 | } else | |
696 | print_all_devices_pt(sf->cxt, sf->verify); | |
697 | ||
698 | return fail; | |
699 | } | |
700 | ||
701 | /* | |
702 | * sfdisk --list-free [<device ..] | |
703 | */ | |
704 | static int command_list_freespace(struct sfdisk *sf, int argc, char **argv) | |
705 | { | |
706 | int fail = 0; | |
707 | fdisk_enable_listonly(sf->cxt, 1); | |
708 | ||
709 | if (argc) { | |
710 | int i; | |
711 | ||
712 | for (i = 0; i < argc; i++) | |
713 | if (print_device_freespace(sf->cxt, argv[i], 1, i) != 0) | |
714 | fail++; | |
715 | } else | |
716 | print_all_devices_freespace(sf->cxt); | |
717 | ||
718 | return fail; | |
719 | } | |
720 | ||
721 | /* | |
722 | * sfdisk --list-types | |
723 | */ | |
724 | static int command_list_types(struct sfdisk *sf) | |
725 | { | |
726 | const struct fdisk_parttype *t; | |
727 | struct fdisk_label *lb; | |
728 | const char *name; | |
729 | size_t i = 0; | |
730 | int codes; | |
731 | ||
732 | assert(sf); | |
733 | assert(sf->cxt); | |
734 | ||
735 | name = sf->label ? sf->label : "dos"; | |
736 | lb = fdisk_get_label(sf->cxt, name); | |
737 | if (!lb) | |
738 | errx(EXIT_FAILURE, _("unsupported label '%s'"), name); | |
739 | ||
740 | codes = fdisk_label_has_code_parttypes(lb); | |
741 | fputs(_("Id Name\n\n"), stdout); | |
742 | ||
743 | while ((t = fdisk_label_get_parttype(lb, i++))) { | |
744 | if (codes) | |
745 | printf("%2x %s\n", fdisk_parttype_get_code(t), | |
746 | fdisk_parttype_get_name(t)); | |
747 | else | |
748 | printf("%s %s\n", fdisk_parttype_get_string(t), | |
749 | fdisk_parttype_get_name(t)); | |
750 | } | |
751 | ||
752 | return 0; | |
753 | } | |
754 | ||
755 | static int verify_device(struct sfdisk *sf, const char *devname) | |
756 | { | |
757 | int rc = 1; | |
758 | ||
759 | fdisk_enable_listonly(sf->cxt, 1); | |
760 | ||
761 | assign_device(sf, devname, 1); | |
762 | ||
763 | color_scheme_enable("header", UL_COLOR_BOLD); | |
764 | fdisk_info(sf->cxt, "%s:", devname); | |
765 | color_disable(); | |
766 | ||
767 | if (!fdisk_has_label(sf->cxt)) | |
768 | fdisk_info(sf->cxt, _("unrecognized partition table type")); | |
769 | else | |
770 | rc = fdisk_verify_disklabel(sf->cxt); | |
771 | ||
772 | fdisk_deassign_device(sf->cxt, 1); | |
773 | return rc; | |
774 | } | |
775 | ||
776 | /* | |
777 | * sfdisk --verify [<device ..] | |
778 | */ | |
779 | static int command_verify(struct sfdisk *sf, int argc, char **argv) | |
780 | { | |
781 | int nfails = 0, ct = 0; | |
782 | ||
783 | if (argc) { | |
784 | int i; | |
785 | for (i = 0; i < argc; i++) { | |
786 | if (i) | |
787 | fdisk_info(sf->cxt, " "); | |
788 | if (verify_device(sf, argv[i]) < 0) | |
789 | nfails++; | |
790 | } | |
791 | } else { | |
792 | FILE *f = NULL; | |
793 | char *dev; | |
794 | ||
795 | while ((dev = next_proc_partition(&f))) { | |
796 | if (ct) | |
797 | fdisk_info(sf->cxt, " "); | |
798 | if (verify_device(sf, dev) < 0) | |
799 | nfails++; | |
800 | free(dev); | |
801 | ct++; | |
802 | } | |
803 | } | |
804 | ||
805 | return nfails; | |
806 | } | |
807 | ||
808 | static int get_size(const char *dev, int silent, uintmax_t *sz) | |
809 | { | |
810 | int fd, rc = 0; | |
811 | ||
812 | fd = open(dev, O_RDONLY); | |
813 | if (fd < 0) { | |
814 | if (!silent) | |
815 | warn(_("cannot open %s"), dev); | |
816 | return -errno; | |
817 | } | |
818 | ||
819 | if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) { | |
820 | if (!silent) | |
821 | warn(_("Cannot get size of %s"), dev); | |
822 | rc = -errno; | |
823 | } | |
824 | ||
825 | close(fd); | |
826 | return rc; | |
827 | } | |
828 | ||
829 | /* | |
830 | * sfdisk --show-size [<device ..] | |
831 | * | |
832 | * (silly, but just for backward compatibility) | |
833 | */ | |
834 | static int command_show_size(struct sfdisk *sf __attribute__((__unused__)), | |
835 | int argc, char **argv) | |
836 | { | |
837 | uintmax_t sz; | |
838 | ||
839 | if (argc) { | |
840 | int i; | |
841 | for (i = 0; i < argc; i++) { | |
842 | if (get_size(argv[i], 0, &sz) == 0) | |
843 | printf("%ju\n", sz / 2); | |
844 | } | |
845 | } else { | |
846 | FILE *f = NULL; | |
847 | uintmax_t total = 0; | |
848 | char *dev; | |
849 | ||
850 | while ((dev = next_proc_partition(&f))) { | |
851 | if (get_size(dev, 1, &sz) == 0) { | |
852 | printf("%s: %9ju\n", dev, sz / 2); | |
853 | total += sz / 2; | |
854 | } | |
855 | free(dev); | |
856 | } | |
857 | if (total) | |
858 | printf(_("total: %ju blocks\n"), total); | |
859 | } | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
864 | static int print_geom(struct sfdisk *sf, const char *devname) | |
865 | { | |
866 | fdisk_enable_listonly(sf->cxt, 1); | |
867 | ||
868 | assign_device(sf, devname, 1); | |
869 | ||
870 | fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track", | |
871 | devname, | |
872 | (uintmax_t) fdisk_get_geom_cylinders(sf->cxt), | |
873 | (uintmax_t) fdisk_get_geom_heads(sf->cxt), | |
874 | (uintmax_t) fdisk_get_geom_sectors(sf->cxt)); | |
875 | ||
876 | fdisk_deassign_device(sf->cxt, 1); | |
877 | return 0; | |
878 | } | |
879 | ||
880 | /* | |
881 | * sfdisk --show-geometry [<device ..] | |
882 | */ | |
883 | static int command_show_geometry(struct sfdisk *sf, int argc, char **argv) | |
884 | { | |
885 | int nfails = 0; | |
886 | ||
887 | if (argc) { | |
888 | int i; | |
889 | for (i = 0; i < argc; i++) { | |
890 | if (print_geom(sf, argv[i]) < 0) | |
891 | nfails++; | |
892 | } | |
893 | } else { | |
894 | FILE *f = NULL; | |
895 | char *dev; | |
896 | ||
897 | while ((dev = next_proc_partition(&f))) { | |
898 | if (print_geom(sf, dev) < 0) | |
899 | nfails++; | |
900 | free(dev); | |
901 | } | |
902 | } | |
903 | ||
904 | return nfails; | |
905 | } | |
906 | ||
907 | /* | |
908 | * sfdisk --activate <device> [<partno> ...] | |
909 | */ | |
910 | static int command_activate(struct sfdisk *sf, int argc, char **argv) | |
911 | { | |
912 | int rc, nparts, i, listonly; | |
913 | struct fdisk_partition *pa = NULL; | |
914 | const char *devname = NULL; | |
915 | ||
916 | if (argc < 1) | |
917 | errx(EXIT_FAILURE, _("no disk device specified")); | |
918 | devname = argv[0]; | |
919 | ||
920 | /* --activate <device> */ | |
921 | listonly = argc == 1; | |
922 | ||
923 | assign_device(sf, devname, listonly); | |
924 | ||
925 | if (fdisk_is_label(sf->cxt, GPT)) { | |
926 | if (fdisk_gpt_is_hybrid(sf->cxt)) | |
927 | errx(EXIT_FAILURE, _("toggle boot flags is unsupported for Hybrid GPT/MBR")); | |
928 | ||
929 | /* Switch from GPT to PMBR */ | |
930 | sf->cxt = fdisk_new_nested_context(sf->cxt, "dos"); | |
931 | if (!sf->cxt) | |
932 | err(EXIT_FAILURE, _("cannot switch to PMBR")); | |
933 | fdisk_info(sf->cxt, _("Activation is unsupported for GPT -- entering nested PMBR.")); | |
934 | ||
935 | } else if (!fdisk_is_label(sf->cxt, DOS)) | |
936 | errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only")); | |
937 | ||
938 | nparts = fdisk_get_npartitions(sf->cxt); | |
939 | for (i = 0; i < nparts; i++) { | |
940 | char *data = NULL; | |
941 | ||
942 | /* note that fdisk_get_partition() reuses the @pa pointer, you | |
943 | * don't have to (re)allocate it */ | |
944 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0) | |
945 | continue; | |
946 | ||
947 | /* sfdisk --activate list bootable partitions */ | |
948 | if (listonly) { | |
949 | if (!fdisk_partition_is_bootable(pa)) | |
950 | continue; | |
951 | if (fdisk_partition_to_string(pa, sf->cxt, | |
952 | FDISK_FIELD_DEVICE, &data) == 0) { | |
953 | printf("%s\n", data); | |
954 | free(data); | |
955 | } | |
956 | ||
957 | /* deactivate all active partitions */ | |
958 | } else if (fdisk_partition_is_bootable(pa)) | |
959 | fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE); | |
960 | } | |
961 | ||
962 | /* sfdisk --activate <partno> [..] */ | |
963 | for (i = 1; i < argc; i++) { | |
964 | int n; | |
965 | ||
966 | if (i == 1 && strcmp(argv[1], "-") == 0) | |
967 | break; | |
968 | n = strtou32_or_err(argv[i], _("failed to parse partition number")); | |
969 | ||
970 | rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE); | |
971 | if (rc) | |
972 | errx(EXIT_FAILURE, | |
973 | _("%s: partition %d: failed to toggle bootable flag"), | |
974 | devname, i + 1); | |
975 | } | |
976 | ||
977 | fdisk_unref_partition(pa); | |
978 | ||
979 | if (listonly) | |
980 | rc = fdisk_deassign_device(sf->cxt, 1); | |
981 | else | |
982 | rc = write_changes(sf); | |
983 | return rc; | |
984 | } | |
985 | ||
986 | /* | |
987 | * sfdisk --delete <device> [<partno> ...] | |
988 | */ | |
989 | static int command_delete(struct sfdisk *sf, int argc, char **argv) | |
990 | { | |
991 | size_t i; | |
992 | const char *devname = NULL; | |
993 | ||
994 | if (argc < 1) | |
995 | errx(EXIT_FAILURE, _("no disk device specified")); | |
996 | devname = argv[0]; | |
997 | ||
998 | assign_device(sf, devname, 0); | |
999 | ||
1000 | /* delete all */ | |
1001 | if (argc == 1) { | |
1002 | size_t nparts = fdisk_get_npartitions(sf->cxt); | |
1003 | for (i = 0; i < nparts; i++) { | |
1004 | if (fdisk_is_partition_used(sf->cxt, i) && | |
1005 | fdisk_delete_partition(sf->cxt, i) != 0) | |
1006 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, i + 1); | |
1007 | } | |
1008 | /* delete specified */ | |
1009 | } else { | |
1010 | for (i = 1; i < (size_t) argc; i++) { | |
1011 | size_t n = strtou32_or_err(argv[i], _("failed to parse partition number")); | |
1012 | ||
1013 | if (n == 0) | |
1014 | errx(EXIT_FAILURE, _("partition number must be a positive number")); | |
1015 | if (fdisk_delete_partition(sf->cxt, n - 1) != 0) | |
1016 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, n); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | return write_changes(sf); | |
1021 | } | |
1022 | ||
1023 | /* | |
1024 | * sfdisk --reorder <device> | |
1025 | */ | |
1026 | static int command_reorder(struct sfdisk *sf, int argc, char **argv) | |
1027 | { | |
1028 | const char *devname = NULL; | |
1029 | int rc; | |
1030 | ||
1031 | if (argc) | |
1032 | devname = argv[0]; | |
1033 | if (!devname) | |
1034 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1035 | ||
1036 | assign_device(sf, devname, 0); /* read-write */ | |
1037 | ||
1038 | if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchanged */ | |
1039 | rc = fdisk_deassign_device(sf->cxt, 1); | |
1040 | else | |
1041 | rc = write_changes(sf); | |
1042 | ||
1043 | return rc; | |
1044 | } | |
1045 | ||
1046 | ||
1047 | /* | |
1048 | * sfdisk --dump <device> | |
1049 | */ | |
1050 | static int command_dump(struct sfdisk *sf, int argc, char **argv) | |
1051 | { | |
1052 | const char *devname = NULL; | |
1053 | struct fdisk_script *dp; | |
1054 | int rc; | |
1055 | ||
1056 | if (argc) | |
1057 | devname = argv[0]; | |
1058 | if (!devname) | |
1059 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1060 | ||
1061 | assign_device(sf, devname, 1); /* read-only */ | |
1062 | ||
1063 | if (!fdisk_has_label(sf->cxt)) | |
1064 | errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname); | |
1065 | ||
1066 | dp = fdisk_new_script(sf->cxt); | |
1067 | if (!dp) | |
1068 | err(EXIT_FAILURE, _("failed to allocate dump struct")); | |
1069 | ||
1070 | rc = fdisk_script_read_context(dp, NULL); | |
1071 | if (rc) | |
1072 | errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname); | |
1073 | ||
1074 | if (sf->json) | |
1075 | fdisk_script_enable_json(dp, 1); | |
1076 | fdisk_script_write_file(dp, stdout); | |
1077 | ||
1078 | fdisk_unref_script(dp); | |
1079 | fdisk_deassign_device(sf->cxt, 1); /* no-sync() */ | |
1080 | return 0; | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * sfdisk --backup-pt-sectors <device> | |
1085 | */ | |
1086 | static int command_backup_sectors(struct sfdisk *sf, int argc, char **argv) | |
1087 | { | |
1088 | const char *devname = NULL; | |
1089 | ||
1090 | if (argc) | |
1091 | devname = argv[0]; | |
1092 | if (!devname) | |
1093 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1094 | ||
1095 | assign_device(sf, devname, 1); /* read-only */ | |
1096 | ||
1097 | if (!fdisk_has_label(sf->cxt)) | |
1098 | errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname); | |
1099 | ||
1100 | backup_partition_table(sf, devname); | |
1101 | ||
1102 | fdisk_deassign_device(sf->cxt, 1); /* no-sync() */ | |
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | static void assign_device_partition(struct sfdisk *sf, | |
1107 | const char *devname, | |
1108 | size_t partno, | |
1109 | int rdonly) | |
1110 | { | |
1111 | int rc; | |
1112 | size_t n; | |
1113 | struct fdisk_label *lb = NULL; | |
1114 | ||
1115 | assert(sf); | |
1116 | assert(devname); | |
1117 | ||
1118 | /* read-only when a new <type> undefined */ | |
1119 | rc = fdisk_assign_device(sf->cxt, devname, rdonly); | |
1120 | if (rc) | |
1121 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
1122 | ||
1123 | if (!fdisk_is_readonly(sf->cxt) | |
1124 | && blkdev_lock(fdisk_get_devfd(sf->cxt), devname, sf->lockmode) != 0) { | |
1125 | fdisk_deassign_device(sf->cxt, 1); | |
1126 | return; | |
1127 | } | |
1128 | lb = fdisk_get_label(sf->cxt, NULL); | |
1129 | if (!lb) | |
1130 | errx(EXIT_FAILURE, _("%s: no partition table found"), devname); | |
1131 | ||
1132 | n = fdisk_get_npartitions(sf->cxt); | |
1133 | if (partno > n) | |
1134 | errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains " | |
1135 | "only %zu partitions"), devname, partno, n); | |
1136 | if (!fdisk_is_partition_used(sf->cxt, partno - 1)) | |
1137 | errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"), | |
1138 | devname, partno); | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * sfdisk --part-type <device> <partno> [<type>] | |
1143 | */ | |
1144 | static int command_parttype(struct sfdisk *sf, int argc, char **argv) | |
1145 | { | |
1146 | size_t partno; | |
1147 | struct fdisk_parttype *type = NULL; | |
1148 | struct fdisk_label *lb; | |
1149 | const char *devname = NULL, *typestr = NULL; | |
1150 | ||
1151 | if (!argc) | |
1152 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1153 | devname = argv[0]; | |
1154 | ||
1155 | if (argc < 2) | |
1156 | errx(EXIT_FAILURE, _("no partition number specified")); | |
1157 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
1158 | ||
1159 | if (argc == 3) | |
1160 | typestr = argv[2]; | |
1161 | else if (argc > 3) | |
1162 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1163 | ||
1164 | /* read-only when a new <type> undefined */ | |
1165 | assign_device_partition(sf, devname, partno, !typestr); | |
1166 | ||
1167 | lb = fdisk_get_label(sf->cxt, NULL); | |
1168 | ||
1169 | /* print partition type */ | |
1170 | if (!typestr) { | |
1171 | const struct fdisk_parttype *t = NULL; | |
1172 | struct fdisk_partition *pa = NULL; | |
1173 | ||
1174 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
1175 | t = fdisk_partition_get_type(pa); | |
1176 | if (!t) | |
1177 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"), | |
1178 | devname, partno); | |
1179 | ||
1180 | if (fdisk_label_has_code_parttypes(lb)) | |
1181 | printf("%2x\n", fdisk_parttype_get_code(t)); | |
1182 | else | |
1183 | printf("%s\n", fdisk_parttype_get_string(t)); | |
1184 | ||
1185 | fdisk_unref_partition(pa); | |
1186 | fdisk_deassign_device(sf->cxt, 1); | |
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | if (sf->backup) | |
1191 | backup_partition_table(sf, devname); | |
1192 | ||
1193 | /* parse <type> and apply to PT */ | |
1194 | type = fdisk_label_advparse_parttype(lb, typestr, | |
1195 | FDISK_PARTTYPE_PARSE_DATA | |
1196 | | FDISK_PARTTYPE_PARSE_ALIAS | |
1197 | | FDISK_PARTTYPE_PARSE_NAME | |
1198 | | FDISK_PARTTYPE_PARSE_SHORTCUT); | |
1199 | if (!type) | |
1200 | errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"), | |
1201 | fdisk_label_get_name(lb), typestr); | |
1202 | ||
1203 | else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0) | |
1204 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"), | |
1205 | devname, partno); | |
1206 | fdisk_unref_parttype(type); | |
1207 | return write_changes(sf); | |
1208 | } | |
1209 | ||
1210 | /* | |
1211 | * sfdisk --part-uuid <device> <partno> [<uuid>] | |
1212 | */ | |
1213 | static int command_partuuid(struct sfdisk *sf, int argc, char **argv) | |
1214 | { | |
1215 | size_t partno; | |
1216 | struct fdisk_partition *pa = NULL; | |
1217 | const char *devname = NULL, *uuid = NULL; | |
1218 | ||
1219 | if (!argc) | |
1220 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1221 | devname = argv[0]; | |
1222 | ||
1223 | if (argc < 2) | |
1224 | errx(EXIT_FAILURE, _("no partition number specified")); | |
1225 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
1226 | ||
1227 | if (argc == 3) | |
1228 | uuid = argv[2]; | |
1229 | else if (argc > 3) | |
1230 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1231 | ||
1232 | /* read-only if uuid not given */ | |
1233 | assign_device_partition(sf, devname, partno, !uuid); | |
1234 | ||
1235 | /* print partition uuid */ | |
1236 | if (!uuid) { | |
1237 | const char *str = NULL; | |
1238 | ||
1239 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
1240 | str = fdisk_partition_get_uuid(pa); | |
1241 | if (!str) | |
1242 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"), | |
1243 | devname, partno); | |
1244 | printf("%s\n", str); | |
1245 | fdisk_unref_partition(pa); | |
1246 | fdisk_deassign_device(sf->cxt, 1); | |
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | if (sf->backup) | |
1251 | backup_partition_table(sf, devname); | |
1252 | ||
1253 | pa = fdisk_new_partition(); | |
1254 | if (!pa) | |
1255 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
1256 | ||
1257 | if (fdisk_partition_set_uuid(pa, uuid) != 0 || | |
1258 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
1259 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"), | |
1260 | devname, partno); | |
1261 | fdisk_unref_partition(pa); | |
1262 | return write_changes(sf); | |
1263 | } | |
1264 | ||
1265 | /* | |
1266 | * sfdisk --part-label <device> <partno> [<label>] | |
1267 | */ | |
1268 | static int command_partlabel(struct sfdisk *sf, int argc, char **argv) | |
1269 | { | |
1270 | size_t partno; | |
1271 | struct fdisk_partition *pa = NULL; | |
1272 | const char *devname = NULL, *name = NULL; | |
1273 | ||
1274 | if (!argc) | |
1275 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1276 | devname = argv[0]; | |
1277 | ||
1278 | if (argc < 2) | |
1279 | errx(EXIT_FAILURE, _("no partition number specified")); | |
1280 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
1281 | ||
1282 | if (argc == 3) | |
1283 | name = argv[2]; | |
1284 | else if (argc > 3) | |
1285 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1286 | ||
1287 | /* read-only if name not given */ | |
1288 | assign_device_partition(sf, devname, partno, !name); | |
1289 | ||
1290 | /* print partition name */ | |
1291 | if (!name) { | |
1292 | const char *str = NULL; | |
1293 | ||
1294 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
1295 | str = fdisk_partition_get_name(pa); | |
1296 | if (!str) | |
1297 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"), | |
1298 | devname, partno); | |
1299 | printf("%s\n", str); | |
1300 | fdisk_unref_partition(pa); | |
1301 | fdisk_deassign_device(sf->cxt, 1); | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | if (sf->backup) | |
1306 | backup_partition_table(sf, devname); | |
1307 | ||
1308 | pa = fdisk_new_partition(); | |
1309 | if (!pa) | |
1310 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
1311 | ||
1312 | if (fdisk_partition_set_name(pa, name) != 0 || | |
1313 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
1314 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"), | |
1315 | devname, partno); | |
1316 | ||
1317 | fdisk_unref_partition(pa); | |
1318 | return write_changes(sf); | |
1319 | } | |
1320 | ||
1321 | /* | |
1322 | * sfdisk --part-attrs <device> <partno> [<attrs>] | |
1323 | */ | |
1324 | static int command_partattrs(struct sfdisk *sf, int argc, char **argv) | |
1325 | { | |
1326 | size_t partno; | |
1327 | struct fdisk_partition *pa = NULL; | |
1328 | const char *devname = NULL, *attrs = NULL; | |
1329 | ||
1330 | if (!argc) | |
1331 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1332 | devname = argv[0]; | |
1333 | ||
1334 | if (argc < 2) | |
1335 | errx(EXIT_FAILURE, _("no partition number specified")); | |
1336 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
1337 | ||
1338 | if (argc == 3) | |
1339 | attrs = argv[2]; | |
1340 | else if (argc > 3) | |
1341 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1342 | ||
1343 | /* read-only if name not given */ | |
1344 | assign_device_partition(sf, devname, partno, !attrs); | |
1345 | ||
1346 | /* print partition name */ | |
1347 | if (!attrs) { | |
1348 | const char *str = NULL; | |
1349 | ||
1350 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
1351 | str = fdisk_partition_get_attrs(pa); | |
1352 | if (str) | |
1353 | printf("%s\n", str); | |
1354 | fdisk_unref_partition(pa); | |
1355 | fdisk_deassign_device(sf->cxt, 1); | |
1356 | return 0; | |
1357 | } | |
1358 | ||
1359 | if (sf->backup) | |
1360 | backup_partition_table(sf, devname); | |
1361 | ||
1362 | pa = fdisk_new_partition(); | |
1363 | if (!pa) | |
1364 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
1365 | ||
1366 | if (fdisk_partition_set_attrs(pa, attrs) != 0 || | |
1367 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
1368 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"), | |
1369 | devname, partno); | |
1370 | ||
1371 | fdisk_unref_partition(pa); | |
1372 | return write_changes(sf); | |
1373 | } | |
1374 | ||
1375 | #ifdef BLKDISCARD | |
1376 | /* | |
1377 | * sfdisk --discard-free <device> | |
1378 | */ | |
1379 | static int command_discard_free(struct sfdisk *sf, int argc, char **argv) | |
1380 | { | |
1381 | struct fdisk_table *tb = NULL; | |
1382 | struct fdisk_iter *itr = NULL; | |
1383 | struct fdisk_partition *pa = NULL; | |
1384 | const char *devname = NULL; | |
1385 | uint64_t ss; | |
1386 | int rc; | |
1387 | ||
1388 | if (!argc) | |
1389 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1390 | devname = argv[0]; | |
1391 | if (argc > 1) | |
1392 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1393 | ||
1394 | itr = fdisk_new_iter(FDISK_ITER_FORWARD); | |
1395 | if (!itr) | |
1396 | err(EXIT_FAILURE, _("failed to allocate iterator")); | |
1397 | ||
1398 | assign_device(sf, devname, 0); | |
1399 | ||
1400 | ss = fdisk_get_sector_size(sf->cxt); | |
1401 | ||
1402 | rc = fdisk_get_freespaces(sf->cxt, &tb); | |
1403 | if (rc) { | |
1404 | fdisk_warn(sf->cxt, _("failed to gather unpartitioned space")); | |
1405 | goto done; | |
1406 | } | |
1407 | ||
1408 | while (fdisk_table_next_partition(tb, itr, &pa) == 0) { | |
1409 | uint64_t range[2]; | |
1410 | ||
1411 | if (!fdisk_partition_has_size(pa) || | |
1412 | !fdisk_partition_has_start(pa)) | |
1413 | continue; | |
1414 | ||
1415 | range[0] = (uint64_t) fdisk_partition_get_start(pa); | |
1416 | range[1] = (uint64_t) fdisk_partition_get_size(pa); | |
1417 | ||
1418 | fdisk_info(sf->cxt, _("Discarding region %"PRIu64 | |
1419 | "-%"PRIu64""), | |
1420 | range[0], range[0] + range[1] - 1); | |
1421 | ||
1422 | range[0] *= ss; | |
1423 | range[1] *= ss; | |
1424 | ||
1425 | errno = 0; | |
1426 | if (ioctl(fdisk_get_devfd(sf->cxt), BLKDISCARD, &range)) { | |
1427 | rc = -errno; | |
1428 | fdisk_warn(sf->cxt, _("BLKDISCARD ioctl failed")); | |
1429 | break; | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | done: | |
1434 | fdisk_free_iter(itr); | |
1435 | fdisk_unref_table(tb); | |
1436 | return rc; | |
1437 | } | |
1438 | #else /* BLKDISCARD */ | |
1439 | static int command_discard_free(struct sfdisk *sf, int argc, char **argv) | |
1440 | { | |
1441 | fdisk_warnx(sf->cxt, _("Discard unsupported on your system.")); | |
1442 | } | |
1443 | #endif /* BLKDISCARD */ | |
1444 | ||
1445 | /* | |
1446 | * sfdisk --disk-id <device> [<str>] | |
1447 | */ | |
1448 | static int command_diskid(struct sfdisk *sf, int argc, char **argv) | |
1449 | { | |
1450 | const char *devname = NULL; | |
1451 | char *str = NULL; | |
1452 | ||
1453 | if (!argc) | |
1454 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1455 | devname = argv[0]; | |
1456 | ||
1457 | if (argc == 2) | |
1458 | str = argv[1]; | |
1459 | else if (argc > 2) | |
1460 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1461 | ||
1462 | assign_device(sf, devname, !str); | |
1463 | ||
1464 | /* print */ | |
1465 | if (!str) { | |
1466 | fdisk_get_disklabel_id(sf->cxt, &str); | |
1467 | if (str) | |
1468 | printf("%s\n", str); | |
1469 | free(str); | |
1470 | fdisk_deassign_device(sf->cxt, 1); | |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | if (fdisk_set_disklabel_id_from_string(sf->cxt, str) != 0) | |
1475 | errx(EXIT_FAILURE, _("%s: failed to set disklabel ID"), devname); | |
1476 | ||
1477 | return write_changes(sf); | |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * sfdisk --relocate <mode> <device> | |
1482 | */ | |
1483 | static int command_relocate(struct sfdisk *sf, int argc, char **argv) | |
1484 | { | |
1485 | const char *devname = NULL; | |
1486 | const char *oper = NULL; | |
1487 | struct fdisk_label *lb; | |
1488 | ||
1489 | if (!argc) | |
1490 | errx(EXIT_FAILURE, _("no relocate operation specified")); | |
1491 | if (argc < 2) | |
1492 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1493 | if (argc > 2) | |
1494 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
1495 | ||
1496 | oper = argv[0]; | |
1497 | devname = argv[1]; | |
1498 | lb = fdisk_get_label(sf->cxt, "gpt"); | |
1499 | ||
1500 | if (strcmp(oper, "gpt-bak-mini") == 0) | |
1501 | fdisk_gpt_enable_minimize(lb, 1); | |
1502 | ||
1503 | else if (strcmp(oper, "gpt-bak-std") != 0) | |
1504 | errx(EXIT_FAILURE, _("unsupported relocation operation")); | |
1505 | ||
1506 | assign_device(sf, devname, 0); | |
1507 | ||
1508 | fdisk_label_set_changed(lb, 1); | |
1509 | ||
1510 | return write_changes(sf); | |
1511 | } | |
1512 | ||
1513 | static void sfdisk_print_partition(struct sfdisk *sf, size_t n) | |
1514 | { | |
1515 | struct fdisk_partition *pa = NULL; | |
1516 | char *data; | |
1517 | ||
1518 | assert(sf); | |
1519 | ||
1520 | if (sf->quiet) | |
1521 | return; | |
1522 | if (fdisk_get_partition(sf->cxt, n, &pa) != 0) | |
1523 | return; | |
1524 | ||
1525 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data); | |
1526 | printf("%12s : ", data); | |
1527 | ||
1528 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data); | |
1529 | printf("%12s ", data); | |
1530 | ||
1531 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data); | |
1532 | printf("%12s ", data); | |
1533 | ||
1534 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data); | |
1535 | printf("(%s) ", data); | |
1536 | ||
1537 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data); | |
1538 | printf("%s\n", data); | |
1539 | ||
1540 | fdisk_unref_partition(pa); | |
1541 | } | |
1542 | ||
1543 | static void command_fdisk_help(void) | |
1544 | { | |
1545 | fputs(_("\nHelp:\n"), stdout); | |
1546 | ||
1547 | fputc('\n', stdout); | |
1548 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
1549 | fputs(_(" Commands:\n"), stdout); | |
1550 | color_disable(); | |
1551 | fputs(_(" write write table to disk and exit\n"), stdout); | |
1552 | fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout); | |
1553 | fputs(_(" abort exit sfdisk shell\n"), stdout); | |
1554 | fputs(_(" print display the partition table\n"), stdout); | |
1555 | fputs(_(" help show this help text\n"), stdout); | |
1556 | fputc('\n', stdout); | |
1557 | fputs(_(" Ctrl-D the same as 'quit'\n"), stdout); | |
1558 | ||
1559 | fputc('\n', stdout); | |
1560 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
1561 | fputs(_(" Input format:\n"), stdout); | |
1562 | color_disable(); | |
1563 | fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout); | |
1564 | ||
1565 | fputc('\n', stdout); | |
1566 | fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n" | |
1567 | " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n" | |
1568 | " The default is the first free space.\n"), stdout); | |
1569 | ||
1570 | fputc('\n', stdout); | |
1571 | fputs(_(" <size> Size of the partition in sectors, or bytes if\n" | |
1572 | " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n" | |
1573 | " The default is all available space.\n"), stdout); | |
1574 | ||
1575 | fputc('\n', stdout); | |
1576 | fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout); | |
1577 | fputs(_(" MBR: hex or L,S,Ex,X,U,R,V shortcuts.\n"), stdout); | |
1578 | fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout); | |
1579 | ||
1580 | fputc('\n', stdout); | |
1581 | fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout); | |
1582 | ||
1583 | fputc('\n', stdout); | |
1584 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
1585 | fputs(_(" Example:\n"), stdout); | |
1586 | color_disable(); | |
1587 | fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout); | |
1588 | fputc('\n', stdout); | |
1589 | } | |
1590 | ||
1591 | enum { | |
1592 | SFDISK_DONE_NONE = 0, | |
1593 | SFDISK_DONE_EOF, | |
1594 | SFDISK_DONE_ABORT, | |
1595 | SFDISK_DONE_WRITE, | |
1596 | SFDISK_DONE_ASK | |
1597 | }; | |
1598 | ||
1599 | /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */ | |
1600 | static int loop_control_commands(struct sfdisk *sf, | |
1601 | struct fdisk_script *dp, | |
1602 | char *buf) | |
1603 | { | |
1604 | const char *p = skip_blank(buf); | |
1605 | int rc = SFDISK_DONE_NONE; | |
1606 | ||
1607 | if (strcmp(p, "print") == 0) | |
1608 | list_disklabel(sf->cxt); | |
1609 | else if (strcmp(p, "help") == 0) | |
1610 | command_fdisk_help(); | |
1611 | else if (strcmp(p, "quit") == 0) | |
1612 | rc = SFDISK_DONE_ASK; | |
1613 | else if (strcmp(p, "write") == 0) | |
1614 | rc = SFDISK_DONE_WRITE; | |
1615 | else if (strcmp(p, "abort") == 0) | |
1616 | rc = SFDISK_DONE_ABORT; | |
1617 | else { | |
1618 | if (sf->interactive) | |
1619 | fdisk_warnx(sf->cxt, _("unsupported command")); | |
1620 | else { | |
1621 | fdisk_warnx(sf->cxt, _("line %d: unsupported command"), | |
1622 | fdisk_script_get_nlines(dp)); | |
1623 | rc = -EINVAL; | |
1624 | } | |
1625 | } | |
1626 | return rc; | |
1627 | } | |
1628 | ||
1629 | static int has_container_or_unused(struct sfdisk *sf) | |
1630 | { | |
1631 | size_t i, nparts; | |
1632 | struct fdisk_partition *pa = NULL; | |
1633 | ||
1634 | if (sf->container || sf->unused) | |
1635 | return 1; | |
1636 | ||
1637 | nparts = fdisk_get_npartitions(sf->cxt); | |
1638 | for (i = 0; i < nparts; i++) { | |
1639 | ||
1640 | if (!fdisk_is_partition_used(sf->cxt, i)) { | |
1641 | sf->unused = 1; | |
1642 | continue; | |
1643 | } | |
1644 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0) | |
1645 | continue; | |
1646 | if (fdisk_partition_is_container(pa)) | |
1647 | sf->container = 1; | |
1648 | } | |
1649 | ||
1650 | fdisk_unref_partition(pa); | |
1651 | return sf->container || sf->unused; | |
1652 | } | |
1653 | ||
1654 | static size_t last_pt_partno(struct sfdisk *sf) | |
1655 | { | |
1656 | size_t i, nparts, partno = 0; | |
1657 | struct fdisk_partition *pa = NULL; | |
1658 | ||
1659 | ||
1660 | nparts = fdisk_get_npartitions(sf->cxt); | |
1661 | for (i = 0; i < nparts; i++) { | |
1662 | size_t x; | |
1663 | ||
1664 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0 || | |
1665 | !fdisk_partition_is_used(pa)) | |
1666 | continue; | |
1667 | x = fdisk_partition_get_partno(pa); | |
1668 | if (x > partno) | |
1669 | partno = x; | |
1670 | } | |
1671 | ||
1672 | fdisk_unref_partition(pa); | |
1673 | return partno; | |
1674 | } | |
1675 | ||
1676 | #ifdef HAVE_LIBREADLINE | |
1677 | static char *sfdisk_fgets(struct fdisk_script *dp, | |
1678 | char *buf, size_t bufsz, FILE *f) | |
1679 | { | |
1680 | struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp); | |
1681 | ||
1682 | assert(dp); | |
1683 | assert(buf); | |
1684 | assert(bufsz > 2); | |
1685 | ||
1686 | if (sf->interactive) { | |
1687 | char *p = readline(sf->prompt); | |
1688 | size_t len; | |
1689 | ||
1690 | if (!p) | |
1691 | return NULL; | |
1692 | len = strlen(p); | |
1693 | if (len > bufsz - 2) | |
1694 | len = bufsz - 2; | |
1695 | ||
1696 | memcpy(buf, p, len); | |
1697 | buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */ | |
1698 | buf[len + 1] = '\0'; | |
1699 | free(p); | |
1700 | fflush(stdout); | |
1701 | return buf; | |
1702 | } | |
1703 | return fgets(buf, bufsz, f); | |
1704 | } | |
1705 | #endif | |
1706 | ||
1707 | static int ignore_partition(struct fdisk_partition *pa) | |
1708 | { | |
1709 | /* incomplete partition setting */ | |
1710 | if (!fdisk_partition_has_start(pa) && !fdisk_partition_start_is_default(pa)) | |
1711 | return 1; | |
1712 | if (!fdisk_partition_has_size(pa) && !fdisk_partition_end_is_default(pa)) | |
1713 | return 1; | |
1714 | ||
1715 | /* probably dump from old sfdisk with start=0 size=0 */ | |
1716 | if (fdisk_partition_has_start(pa) && fdisk_partition_get_start(pa) == 0 && | |
1717 | fdisk_partition_has_size(pa) && fdisk_partition_get_size(pa) == 0) | |
1718 | return 1; | |
1719 | ||
1720 | return 0; | |
1721 | } | |
1722 | ||
1723 | static void follow_wipe_mode(struct sfdisk *sf) | |
1724 | { | |
1725 | int dowipe = sf->wipemode == WIPEMODE_ALWAYS ? 1 : 0; | |
1726 | ||
1727 | if (sf->interactive && sf->wipemode == WIPEMODE_AUTO) | |
1728 | dowipe = 1; /* do it in interactive mode */ | |
1729 | ||
1730 | if (fdisk_is_ptcollision(sf->cxt) && sf->wipemode != WIPEMODE_NEVER) | |
1731 | dowipe = 1; /* always wipe old PT */ | |
1732 | ||
1733 | fdisk_enable_wipe(sf->cxt, dowipe); | |
1734 | if (sf->quiet) | |
1735 | return; | |
1736 | ||
1737 | if (dowipe) { | |
1738 | if (!fdisk_is_ptcollision(sf->cxt)) { | |
1739 | fdisk_warnx(sf->cxt, _( | |
1740 | "The device contains '%s' signature and it may be removed by a write command. " | |
1741 | "See sfdisk(8) man page and --wipe option for more details."), | |
1742 | fdisk_get_collision(sf->cxt)); | |
1743 | fputc('\n', stdout); | |
1744 | } | |
1745 | } else { | |
1746 | fdisk_warnx(sf->cxt, _( | |
1747 | "The device contains '%s' signature and it may remain on the device. " | |
1748 | "It is recommended to wipe the device with wipefs(8) or " | |
1749 | "sfdisk --wipe, in order to avoid possible collisions."), | |
1750 | fdisk_get_collision(sf->cxt)); | |
1751 | fputc('\n', stderr); | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | static int wipe_partition(struct sfdisk *sf, size_t partno) | |
1756 | { | |
1757 | int rc, yes = 0; | |
1758 | char *fstype = NULL; | |
1759 | struct fdisk_partition *tmp = NULL; | |
1760 | ||
1761 | DBG(MISC, ul_debug("checking for signature")); | |
1762 | ||
1763 | rc = fdisk_get_partition(sf->cxt, partno, &tmp); | |
1764 | if (rc) | |
1765 | goto done; | |
1766 | ||
1767 | rc = fdisk_partition_to_string(tmp, sf->cxt, FDISK_FIELD_FSTYPE, &fstype); | |
1768 | if (rc || fstype == NULL) | |
1769 | goto done; | |
1770 | ||
1771 | fdisk_warnx(sf->cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype); | |
1772 | ||
1773 | if (sf->pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO)) | |
1774 | fdisk_ask_yesno(sf->cxt, _("Do you want to remove the signature?"), &yes); | |
1775 | else if (sf->pwipemode == WIPEMODE_ALWAYS) | |
1776 | yes = 1; | |
1777 | ||
1778 | if (yes) { | |
1779 | fdisk_info(sf->cxt, _("The signature will be removed by a write command.")); | |
1780 | rc = fdisk_wipe_partition(sf->cxt, partno, TRUE); | |
1781 | } | |
1782 | done: | |
1783 | fdisk_unref_partition(tmp); | |
1784 | free(fstype); | |
1785 | DBG(MISC, ul_debug("partition wipe check end [rc=%d]", rc)); | |
1786 | return rc; | |
1787 | } | |
1788 | ||
1789 | static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname, | |
1790 | size_t next_partno, int created) | |
1791 | { | |
1792 | if (created) { | |
1793 | char *partname = fdisk_partname(devname, next_partno + 1); | |
1794 | if (!partname) | |
1795 | err(EXIT_FAILURE, _("failed to allocate partition name")); | |
1796 | ||
1797 | if (!sf->prompt || !ul_startswith(sf->prompt, partname)) { | |
1798 | free(sf->prompt); | |
1799 | xasprintf(&sf->prompt,"%s: ", partname); | |
1800 | } | |
1801 | free(partname); | |
1802 | } else if (!sf->prompt || !ul_startswith(sf->prompt, SFDISK_PROMPT)) { | |
1803 | free(sf->prompt); | |
1804 | sf->prompt = xstrdup(SFDISK_PROMPT); | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | /* | |
1809 | * sfdisk <device> [[-N] <partno>] | |
1810 | * | |
1811 | * Note that the option -N is there for backward compatibility only. | |
1812 | */ | |
1813 | static int command_fdisk(struct sfdisk *sf, int argc, char **argv) | |
1814 | { | |
1815 | int rc = 0, partno = sf->partno, created = 0, unused = 0, ignored = 0; | |
1816 | struct fdisk_script *dp; | |
1817 | struct fdisk_table *tb = NULL; | |
1818 | const char *devname = NULL, *label; | |
1819 | char buf[BUFSIZ]; | |
1820 | size_t next_partno = (size_t) -1; | |
1821 | ||
1822 | if (argc) | |
1823 | devname = argv[0]; | |
1824 | if (partno < 0 && argc > 1) | |
1825 | partno = strtou32_or_err(argv[1], | |
1826 | _("failed to parse partition number")); | |
1827 | if (!devname) | |
1828 | errx(EXIT_FAILURE, _("no disk device specified")); | |
1829 | ||
1830 | assign_device(sf, devname, 0); | |
1831 | ||
1832 | dp = fdisk_new_script(sf->cxt); | |
1833 | if (!dp) | |
1834 | err(EXIT_FAILURE, _("failed to allocate script handler")); | |
1835 | fdisk_set_script(sf->cxt, dp); | |
1836 | #ifdef HAVE_LIBREADLINE | |
1837 | fdisk_script_set_fgets(dp, sfdisk_fgets); | |
1838 | #endif | |
1839 | fdisk_script_set_userdata(dp, (void *) sf); | |
1840 | ||
1841 | /* | |
1842 | * Don't create a new disklabel when [-N] <partno> specified. In this | |
1843 | * case reuse already specified disklabel. Let's check that the disk | |
1844 | * really contains the partition. | |
1845 | */ | |
1846 | if (partno >= 0) { | |
1847 | size_t n; | |
1848 | ||
1849 | if (!fdisk_has_label(sf->cxt)) | |
1850 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d: " | |
1851 | "no partition table was found"), | |
1852 | devname, partno + 1); | |
1853 | n = fdisk_get_npartitions(sf->cxt); | |
1854 | if ((size_t) partno > n) | |
1855 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d: " | |
1856 | "partition table contains only %zu " | |
1857 | "partitions"), | |
1858 | devname, partno + 1, n); | |
1859 | ||
1860 | if (!fdisk_is_partition_used(sf->cxt, partno)) { | |
1861 | fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"), | |
1862 | devname, partno + 1); | |
1863 | unused = 1; | |
1864 | } | |
1865 | created = 1; | |
1866 | next_partno = partno; | |
1867 | ||
1868 | if (sf->movedata) | |
1869 | sf->orig_pa = get_partition(sf->cxt, partno); | |
1870 | } | |
1871 | ||
1872 | if (sf->append) { | |
1873 | created = 1; | |
1874 | next_partno = last_pt_partno(sf) + 1; | |
1875 | } | |
1876 | ||
1877 | if (!sf->quiet && sf->interactive) { | |
1878 | color_scheme_enable("welcome", UL_COLOR_GREEN); | |
1879 | fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING); | |
1880 | color_disable(); | |
1881 | fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n" | |
1882 | "Be careful before using the write command.\n")); | |
1883 | } | |
1884 | ||
1885 | if (!sf->noact && !sf->noreread) { | |
1886 | if (!sf->quiet) | |
1887 | fputs(_("Checking that no-one is using this disk right now ..."), stdout); | |
1888 | if (fdisk_device_is_used(sf->cxt)) { | |
1889 | if (!sf->quiet) | |
1890 | fputs(_(" FAILED\n\n"), stdout); | |
1891 | ||
1892 | fdisk_warnx(sf->cxt, _( | |
1893 | "This disk is currently in use - repartitioning is probably a bad idea.\n" | |
1894 | "Umount all file systems, and swapoff all swap partitions on this disk.\n" | |
1895 | "Use the --no-reread flag to suppress this check.\n")); | |
1896 | ||
1897 | if (!sf->force) | |
1898 | errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks.")); | |
1899 | } else if (!sf->quiet) | |
1900 | fputs(_(" OK\n\n"), stdout); | |
1901 | } | |
1902 | ||
1903 | if (fdisk_get_collision(sf->cxt)) | |
1904 | follow_wipe_mode(sf); | |
1905 | ||
1906 | if (!sf->quiet) { | |
1907 | list_disk_geometry(sf->cxt); | |
1908 | if (fdisk_has_label(sf->cxt)) { | |
1909 | fdisk_info(sf->cxt, _("\nOld situation:")); | |
1910 | list_disklabel(sf->cxt); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | if (sf->label) | |
1915 | label = sf->label; | |
1916 | else if (fdisk_has_label(sf->cxt)) | |
1917 | label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL)); | |
1918 | else | |
1919 | label = "dos"; /* just for backward compatibility */ | |
1920 | ||
1921 | if (fdisk_script_set_header(dp, "label", label) != 0) | |
1922 | errx(EXIT_FAILURE, _("failed to set script header")); | |
1923 | ||
1924 | if (!sf->quiet && sf->interactive) { | |
1925 | if (!fdisk_has_label(sf->cxt) && !sf->label) | |
1926 | fdisk_info(sf->cxt, | |
1927 | _("\nsfdisk is going to create a new '%s' disk label.\n" | |
1928 | "Use 'label: <name>' before you define a first partition\n" | |
1929 | "to override the default."), label); | |
1930 | fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n")); | |
1931 | } else if (!sf->quiet) | |
1932 | fputc('\n', stdout); | |
1933 | ||
1934 | tb = fdisk_script_get_table(dp); | |
1935 | assert(tb); | |
1936 | ||
1937 | do { | |
1938 | size_t nparts; | |
1939 | ||
1940 | DBG(PARSE, ul_debug("<---next-line--->")); | |
1941 | if (next_partno == (size_t) -1) | |
1942 | next_partno = fdisk_table_get_nents(tb); | |
1943 | ||
1944 | if (created | |
1945 | && partno < 0 | |
1946 | && next_partno == fdisk_get_npartitions(sf->cxt) | |
1947 | && !has_container_or_unused(sf)) { | |
1948 | fdisk_info(sf->cxt, _("All partitions used.")); | |
1949 | rc = SFDISK_DONE_ASK; | |
1950 | break; | |
1951 | } | |
1952 | ||
1953 | refresh_prompt_buffer(sf, devname, next_partno, created); | |
1954 | ||
1955 | ||
1956 | if (sf->prompt && (sf->interactive || !sf->quiet)) { | |
1957 | #ifndef HAVE_LIBREADLINE | |
1958 | fputs(sf->prompt, stdout); | |
1959 | #else | |
1960 | if (!sf->interactive) | |
1961 | fputs(sf->prompt, stdout); | |
1962 | #endif | |
1963 | } | |
1964 | ||
1965 | rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); | |
1966 | if (rc == -ENOTSUP) { | |
1967 | buf[sizeof(buf) - 1] = '\0'; | |
1968 | fdisk_warnx(sf->cxt, _("Unknown script header '%s' -- ignore."), buf); | |
1969 | continue; | |
1970 | } | |
1971 | ||
1972 | if (rc < 0) { | |
1973 | DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands")); | |
1974 | buf[sizeof(buf) - 1] = '\0'; | |
1975 | rc = loop_control_commands(sf, dp, buf); | |
1976 | if (rc) | |
1977 | break; | |
1978 | continue; | |
1979 | } | |
1980 | ||
1981 | if (rc == 1) { | |
1982 | rc = SFDISK_DONE_EOF; | |
1983 | if (!sf->quiet) | |
1984 | fputs(_("Done.\n"), stdout); | |
1985 | break; | |
1986 | } | |
1987 | ||
1988 | nparts = fdisk_table_get_nents(tb); | |
1989 | if (nparts) { | |
1990 | size_t cur_partno = (size_t) -1; | |
1991 | struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1); | |
1992 | ||
1993 | assert(pa); | |
1994 | ||
1995 | if (ignore_partition(pa)) { | |
1996 | fdisk_info(sf->cxt, _("Ignoring partition.")); | |
1997 | next_partno++; | |
1998 | ignored++; | |
1999 | continue; | |
2000 | } | |
2001 | if (!created) { | |
2002 | /* ignore "last-lba" and use default if --force specified */ | |
2003 | if (sf->force && fdisk_script_get_header(dp, "last-lba")) { | |
2004 | fdisk_info(sf->cxt, _("Ignoring last-lba script header.")); | |
2005 | fdisk_script_set_header(dp, "last-lba", NULL); | |
2006 | } | |
2007 | ||
2008 | /* create a new disklabel */ | |
2009 | rc = fdisk_apply_script_headers(sf->cxt, dp); | |
2010 | created = !rc; | |
2011 | if (rc) { | |
2012 | errno = -rc; | |
2013 | fdisk_warn(sf->cxt, _( | |
2014 | "Failed to apply script headers, disk label not created")); | |
2015 | } | |
2016 | if (rc == 0 && fdisk_get_collision(sf->cxt)) | |
2017 | follow_wipe_mode(sf); | |
2018 | } | |
2019 | if (!rc && partno >= 0) { /* -N <partno>, modify partition */ | |
2020 | rc = fdisk_set_partition(sf->cxt, partno, pa); | |
2021 | rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT; | |
2022 | break; | |
2023 | } | |
2024 | ||
2025 | if (!rc) { /* add partition */ | |
2026 | if (!sf->interactive && !sf->quiet && | |
2027 | (!sf->prompt || ul_startswith(sf->prompt, SFDISK_PROMPT))) { | |
2028 | refresh_prompt_buffer(sf, devname, next_partno, created); | |
2029 | fputs(sf->prompt, stdout); | |
2030 | } | |
2031 | rc = fdisk_add_partition(sf->cxt, pa, &cur_partno); | |
2032 | if (rc) { | |
2033 | errno = -rc; | |
2034 | fdisk_warn(sf->cxt, _("Failed to add #%zu partition"), next_partno + 1); | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | /* wipe partition on success | |
2039 | * | |
2040 | * Note that unused=1 means -N <partno> for unused, | |
2041 | * otherwise we wipe only newly created partitions. | |
2042 | */ | |
2043 | if (rc == 0 && (unused || partno < 0)) { | |
2044 | rc = wipe_partition(sf, unused ? (size_t) partno : cur_partno); | |
2045 | if (rc) | |
2046 | errno = -rc; | |
2047 | } | |
2048 | ||
2049 | if (!rc) { | |
2050 | /* success print result */ | |
2051 | if (sf->interactive) | |
2052 | sfdisk_print_partition(sf, cur_partno); | |
2053 | next_partno = cur_partno + 1; | |
2054 | } else if (pa) /* error, drop partition from script */ | |
2055 | fdisk_table_remove_partition(tb, pa); | |
2056 | } else | |
2057 | fdisk_info(sf->cxt, _("Script header accepted.")); | |
2058 | ||
2059 | if (rc && !sf->interactive) { | |
2060 | rc = SFDISK_DONE_ABORT; | |
2061 | break; | |
2062 | } | |
2063 | } while (1); | |
2064 | ||
2065 | /* create empty disk label if label, but no partition specified */ | |
2066 | if ((rc == SFDISK_DONE_EOF || rc == SFDISK_DONE_WRITE) && created == 0 | |
2067 | && fdisk_script_has_force_label(dp) == 1 | |
2068 | && fdisk_table_get_nents(tb) == (size_t) ignored | |
2069 | && fdisk_script_get_header(dp, "label")) { | |
2070 | ||
2071 | int xrc = fdisk_apply_script_headers(sf->cxt, dp); | |
2072 | if (xrc) { | |
2073 | fdisk_warnx(sf->cxt, _( | |
2074 | "Failed to apply script headers, " | |
2075 | "disk label not created.")); | |
2076 | rc = SFDISK_DONE_ABORT; | |
2077 | } | |
2078 | } | |
2079 | ||
2080 | if (!sf->quiet && rc != SFDISK_DONE_ABORT) { | |
2081 | fdisk_info(sf->cxt, _("\nNew situation:")); | |
2082 | list_disk_identifier(sf->cxt); | |
2083 | list_disklabel(sf->cxt); | |
2084 | } | |
2085 | ||
2086 | switch (rc) { | |
2087 | case SFDISK_DONE_ASK: | |
2088 | case SFDISK_DONE_EOF: | |
2089 | if (sf->interactive) { | |
2090 | int yes = 0; | |
2091 | fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes); | |
2092 | if (!yes) { | |
2093 | fdisk_info(sf->cxt, _("Leaving.")); | |
2094 | rc = 0; | |
2095 | break; | |
2096 | } | |
2097 | } | |
2098 | FALLTHROUGH; | |
2099 | case SFDISK_DONE_WRITE: | |
2100 | rc = write_changes(sf); | |
2101 | break; | |
2102 | case SFDISK_DONE_ABORT: | |
2103 | default: /* rc < 0 on error */ | |
2104 | fdisk_info(sf->cxt, _("Leaving.\n")); | |
2105 | break; | |
2106 | } | |
2107 | ||
2108 | fdisk_set_script(sf->cxt, NULL); | |
2109 | fdisk_unref_script(dp); | |
2110 | return rc; | |
2111 | } | |
2112 | ||
2113 | static void __attribute__((__noreturn__)) usage(void) | |
2114 | { | |
2115 | FILE *out = stdout; | |
2116 | fputs(USAGE_HEADER, out); | |
2117 | ||
2118 | fprintf(out, | |
2119 | _(" %1$s [options] <dev> [[-N] <part>]\n" | |
2120 | " %1$s [options] <command>\n"), program_invocation_short_name); | |
2121 | ||
2122 | fputs(USAGE_SEPARATOR, out); | |
2123 | fputs(_("Display or manipulate a disk partition table.\n"), out); | |
2124 | ||
2125 | fputs(USAGE_COMMANDS, out); | |
2126 | fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out); | |
2127 | fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out); | |
2128 | fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out); | |
2129 | fputs(_(" -B, --backup-pt-sectors <dev> binary partition table backup (see -b and -O)\n"), out); | |
2130 | fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out); | |
2131 | fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out); | |
2132 | fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out); | |
2133 | fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out); | |
2134 | fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out); | |
2135 | fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out); | |
2136 | fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out); | |
2137 | fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out); | |
2138 | ||
2139 | fputs(USAGE_SEPARATOR, out); | |
2140 | fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out); | |
2141 | fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out); | |
2142 | fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out); | |
2143 | fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out); | |
2144 | ||
2145 | fputs(USAGE_SEPARATOR, out); | |
2146 | fputs(_(" --discard-free <dev> discard (trim) unpartitioned areas\n"), out); | |
2147 | fputs(_(" --disk-id <dev> [<str>] print or change disk label ID (UUID)\n"), out); | |
2148 | fputs(_(" --relocate <oper> <dev> move partition header\n"), out); | |
2149 | ||
2150 | fputs(USAGE_ARGUMENTS, out); | |
2151 | fputs(_(" <dev> device (usually disk) path\n"), out); | |
2152 | fputs(_(" <part> partition number\n"), out); | |
2153 | fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out); | |
2154 | ||
2155 | fputs(USAGE_OPTIONS, out); | |
2156 | fputs(_(" -a, --append append partitions to existing partition table\n"), out); | |
2157 | fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out); | |
2158 | fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out); | |
2159 | fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out); | |
2160 | fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out); | |
2161 | fputs(_(" -f, --force disable all consistency checking\n"), out); | |
2162 | ||
2163 | fprintf(out, | |
2164 | _(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never"); | |
2165 | fprintf(out, | |
2166 | " %s\n", USAGE_COLORS_DEFAULT); | |
2167 | fputs(_(" --sector-size <size> physical and logical sector size\n"), out); | |
2168 | ||
2169 | fprintf(out, | |
2170 | _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); | |
2171 | fputs(_(" -N, --partno <num> specify partition number\n"), out); | |
2172 | fputs(_(" -n, --no-act do everything except write to device\n"), out); | |
2173 | fputs(_(" --no-reread do not check whether the device is in use\n"), out); | |
2174 | fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out); | |
2175 | fputs(_(" -O, --backup-file <path> override default backup file name\n"), out); | |
2176 | fputs(_(" -o, --output <list> output columns\n"), out); | |
2177 | fputs(_(" -q, --quiet suppress extra info messages\n"), out); | |
2178 | fprintf(out, | |
2179 | _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never"); | |
2180 | fprintf(out, | |
2181 | _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never"); | |
2182 | fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out); | |
2183 | fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out); | |
2184 | fputs(USAGE_SEPARATOR, out); | |
2185 | fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out); | |
2186 | fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out); | |
2187 | fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out); | |
2188 | ||
2189 | fputs(USAGE_SEPARATOR, out); | |
2190 | fprintf(out, " -h, --help %s\n", USAGE_OPTSTR_HELP); | |
2191 | fprintf(out, " -v, --version %s\n", USAGE_OPTSTR_VERSION); | |
2192 | ||
2193 | list_available_columns(out); | |
2194 | ||
2195 | fprintf(out, USAGE_MAN_TAIL("sfdisk(8)")); | |
2196 | exit(EXIT_SUCCESS); | |
2197 | } | |
2198 | ||
2199 | ||
2200 | int main(int argc, char *argv[]) | |
2201 | { | |
2202 | const char *outarg = NULL; | |
2203 | int rc = -EINVAL, c, longidx = -1, bytes = 0; | |
2204 | int colormode = UL_COLORMODE_UNDEF; | |
2205 | size_t user_ss = 0; | |
2206 | struct sfdisk _sf = { | |
2207 | .partno = -1, | |
2208 | .wipemode = WIPEMODE_AUTO, | |
2209 | .pwipemode = WIPEMODE_AUTO, | |
2210 | .interactive = isatty(STDIN_FILENO) ? 1 : 0, | |
2211 | }, *sf = &_sf; | |
2212 | ||
2213 | enum { | |
2214 | OPT_CHANGE_ID = CHAR_MAX + 1, | |
2215 | OPT_PRINT_ID, | |
2216 | OPT_ID, | |
2217 | OPT_NOREREAD, | |
2218 | OPT_PARTUUID, | |
2219 | OPT_PARTLABEL, | |
2220 | OPT_PARTTYPE, | |
2221 | OPT_PARTATTRS, | |
2222 | OPT_DISCARDFREE, | |
2223 | OPT_DISKID, | |
2224 | OPT_BYTES, | |
2225 | OPT_COLOR, | |
2226 | OPT_MOVEDATA, | |
2227 | OPT_MOVEFSYNC, | |
2228 | OPT_DELETE, | |
2229 | OPT_NOTELL, | |
2230 | OPT_RELOCATE, | |
2231 | OPT_LOCK, | |
2232 | OPT_SECTORSIZE | |
2233 | }; | |
2234 | ||
2235 | static const struct option longopts[] = { | |
2236 | { "activate",no_argument, NULL, 'A' }, | |
2237 | { "append", no_argument, NULL, 'a' }, | |
2238 | { "backup-pt-sectors", no_argument, NULL, 'B' }, | |
2239 | { "backup", no_argument, NULL, 'b' }, | |
2240 | { "backup-file", required_argument, NULL, 'O' }, | |
2241 | { "bytes", no_argument, NULL, OPT_BYTES }, | |
2242 | { "color", optional_argument, NULL, OPT_COLOR }, | |
2243 | { "lock", optional_argument, NULL, OPT_LOCK }, | |
2244 | { "delete", no_argument, NULL, OPT_DELETE }, | |
2245 | { "dump", no_argument, NULL, 'd' }, | |
2246 | { "help", no_argument, NULL, 'h' }, | |
2247 | { "force", no_argument, NULL, 'f' }, | |
2248 | { "json", no_argument, NULL, 'J' }, | |
2249 | { "label", required_argument, NULL, 'X' }, | |
2250 | { "label-nested", required_argument, NULL, 'Y' }, | |
2251 | { "list", no_argument, NULL, 'l' }, | |
2252 | { "list-free", no_argument, NULL, 'F' }, | |
2253 | { "list-types", no_argument, NULL, 'T' }, | |
2254 | { "no-act", no_argument, NULL, 'n' }, | |
2255 | { "no-reread", no_argument, NULL, OPT_NOREREAD }, | |
2256 | { "no-tell-kernel", no_argument, NULL, OPT_NOTELL }, | |
2257 | { "move-data", optional_argument, NULL, OPT_MOVEDATA }, | |
2258 | { "move-use-fsync", no_argument, NULL, OPT_MOVEFSYNC }, | |
2259 | { "output", required_argument, NULL, 'o' }, | |
2260 | { "partno", required_argument, NULL, 'N' }, | |
2261 | { "reorder", no_argument, NULL, 'r' }, | |
2262 | { "sector-size", required_argument, NULL, OPT_SECTORSIZE }, | |
2263 | { "show-geometry", no_argument, NULL, 'g' }, | |
2264 | { "quiet", no_argument, NULL, 'q' }, | |
2265 | { "verify", no_argument, NULL, 'V' }, | |
2266 | { "version", no_argument, NULL, 'v' }, | |
2267 | { "wipe", required_argument, NULL, 'w' }, | |
2268 | { "wipe-partitions", required_argument, NULL, 'W' }, | |
2269 | ||
2270 | { "relocate", no_argument, NULL, OPT_RELOCATE }, | |
2271 | ||
2272 | { "part-uuid", no_argument, NULL, OPT_PARTUUID }, | |
2273 | { "part-label", no_argument, NULL, OPT_PARTLABEL }, | |
2274 | { "part-type", no_argument, NULL, OPT_PARTTYPE }, | |
2275 | { "part-attrs", no_argument, NULL, OPT_PARTATTRS }, | |
2276 | ||
2277 | { "discard-free", no_argument, NULL, OPT_DISCARDFREE }, | |
2278 | ||
2279 | { "disk-id", no_argument, NULL, OPT_DISKID }, | |
2280 | ||
2281 | { "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */ | |
2282 | { "unit", required_argument, NULL, 'u' }, /* deprecated */ | |
2283 | { "Linux", no_argument, NULL, 'L' }, /* deprecated */ | |
2284 | { "show-size", no_argument, NULL, 's' }, /* deprecated */ | |
2285 | ||
2286 | { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */ | |
2287 | { "id", no_argument, NULL, 'c' }, /* deprecated */ | |
2288 | { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */ | |
2289 | ||
2290 | { NULL, 0, NULL, 0 }, | |
2291 | }; | |
2292 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ | |
2293 | { 'F','d'}, /* --list-free --dump */ | |
2294 | { 'F','J'}, /* --list-free --json */ | |
2295 | { 's','u'}, /* --show-size --unit */ | |
2296 | { 0 } | |
2297 | }; | |
2298 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
2299 | ||
2300 | ||
2301 | setlocale(LC_ALL, ""); | |
2302 | bindtextdomain(PACKAGE, LOCALEDIR); | |
2303 | textdomain(PACKAGE); | |
2304 | close_stdout_atexit(); | |
2305 | ||
2306 | while ((c = getopt_long(argc, argv, "aAbBcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:", | |
2307 | longopts, &longidx)) != -1) { | |
2308 | ||
2309 | err_exclusive_options(c, longopts, excl, excl_st); | |
2310 | ||
2311 | switch(c) { | |
2312 | case 'A': | |
2313 | sf->act = ACT_ACTIVATE; | |
2314 | break; | |
2315 | case 'a': | |
2316 | sf->append = 1; | |
2317 | break; | |
2318 | case 'b': | |
2319 | sf->backup = 1; | |
2320 | break; | |
2321 | case 'B': | |
2322 | sf->act = ACT_BACKUP_SECTORS; | |
2323 | break; | |
2324 | case OPT_CHANGE_ID: | |
2325 | case OPT_PRINT_ID: | |
2326 | case OPT_ID: | |
2327 | warnx(_("%s is deprecated in favour of --part-type"), | |
2328 | longopts[longidx].name); | |
2329 | sf->act = ACT_PARTTYPE; | |
2330 | break; | |
2331 | case 'c': | |
2332 | warnx(_("--id is deprecated in favour of --part-type")); | |
2333 | sf->act = ACT_PARTTYPE; | |
2334 | break; | |
2335 | case 'J': | |
2336 | sf->json = 1; | |
2337 | FALLTHROUGH; | |
2338 | case 'd': | |
2339 | sf->act = ACT_DUMP; | |
2340 | break; | |
2341 | case 'F': | |
2342 | sf->act = ACT_LIST_FREE; | |
2343 | break; | |
2344 | case 'f': | |
2345 | sf->force = 1; | |
2346 | break; | |
2347 | case 'G': | |
2348 | warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry.")); | |
2349 | FALLTHROUGH; | |
2350 | case 'g': | |
2351 | sf->act = ACT_SHOW_GEOM; | |
2352 | break; | |
2353 | case 'h': | |
2354 | usage(); | |
2355 | break; | |
2356 | case 'l': | |
2357 | sf->act = ACT_LIST; | |
2358 | break; | |
2359 | case 'L': | |
2360 | warnx(_("--Linux option is unnecessary and deprecated")); | |
2361 | break; | |
2362 | case 'o': | |
2363 | outarg = optarg; | |
2364 | break; | |
2365 | case 'O': | |
2366 | sf->backup = 1; | |
2367 | sf->backup_file = optarg; | |
2368 | break; | |
2369 | case 'n': | |
2370 | sf->noact = 1; | |
2371 | break; | |
2372 | case 'N': | |
2373 | sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1; | |
2374 | break; | |
2375 | case 'q': | |
2376 | sf->quiet = 1; | |
2377 | break; | |
2378 | case 'r': | |
2379 | sf->act = ACT_REORDER; | |
2380 | break; | |
2381 | case 's': | |
2382 | sf->act = ACT_SHOW_SIZE; | |
2383 | break; | |
2384 | case 'T': | |
2385 | sf->act = ACT_LIST_TYPES; | |
2386 | break; | |
2387 | case 'u': | |
2388 | if (*optarg != 'S') | |
2389 | errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg); | |
2390 | break; | |
2391 | case 'v': | |
2392 | print_version(EXIT_SUCCESS); | |
2393 | case 'V': | |
2394 | sf->verify = 1; | |
2395 | break; | |
2396 | case 'w': | |
2397 | sf->wipemode = wipemode_from_string(optarg); | |
2398 | if (sf->wipemode < 0) | |
2399 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
2400 | break; | |
2401 | case 'W': | |
2402 | sf->pwipemode = wipemode_from_string(optarg); | |
2403 | if (sf->pwipemode < 0) | |
2404 | errx(EXIT_FAILURE, _("unsupported wipe mode")); | |
2405 | break; | |
2406 | case 'X': | |
2407 | sf->label = optarg; | |
2408 | break; | |
2409 | case 'Y': | |
2410 | sf->label_nested = optarg; | |
2411 | break; | |
2412 | ||
2413 | case OPT_PARTUUID: | |
2414 | sf->act = ACT_PARTUUID; | |
2415 | break; | |
2416 | case OPT_PARTTYPE: | |
2417 | sf->act = ACT_PARTTYPE; | |
2418 | break; | |
2419 | case OPT_PARTLABEL: | |
2420 | sf->act = ACT_PARTLABEL; | |
2421 | break; | |
2422 | case OPT_PARTATTRS: | |
2423 | sf->act = ACT_PARTATTRS; | |
2424 | break; | |
2425 | case OPT_DISCARDFREE: | |
2426 | sf->act = ACT_DISCARD_FREE; | |
2427 | break; | |
2428 | case OPT_DISKID: | |
2429 | sf->act = ACT_DISKID; | |
2430 | break; | |
2431 | case OPT_NOREREAD: | |
2432 | sf->noreread = 1; | |
2433 | break; | |
2434 | case OPT_BYTES: | |
2435 | bytes = 1; | |
2436 | break; | |
2437 | case OPT_COLOR: | |
2438 | colormode = UL_COLORMODE_AUTO; | |
2439 | if (optarg) | |
2440 | colormode = colormode_or_err(optarg); | |
2441 | break; | |
2442 | case OPT_MOVEDATA: | |
2443 | sf->movedata = 1; | |
2444 | sf->move_typescript = optarg; | |
2445 | break; | |
2446 | case OPT_MOVEFSYNC: | |
2447 | sf->movefsync = 1; | |
2448 | break; | |
2449 | case OPT_DELETE: | |
2450 | sf->act = ACT_DELETE; | |
2451 | break; | |
2452 | case OPT_NOTELL: | |
2453 | sf->notell = 1; | |
2454 | break; | |
2455 | case OPT_RELOCATE: | |
2456 | sf->act = ACT_RELOCATE; | |
2457 | break; | |
2458 | case OPT_LOCK: | |
2459 | sf->lockmode = "1"; | |
2460 | if (optarg) { | |
2461 | if (*optarg == '=') | |
2462 | optarg++; | |
2463 | sf->lockmode = optarg; | |
2464 | } | |
2465 | break; | |
2466 | case OPT_SECTORSIZE: | |
2467 | user_ss = strtou32_or_err(optarg, | |
2468 | _("invalid sector size argument")); | |
2469 | if (user_ss != 512 && user_ss != 1024 && | |
2470 | user_ss != 2048 && user_ss != 4096) | |
2471 | errx(EXIT_FAILURE, _("invalid sector size argument")); | |
2472 | break; | |
2473 | default: | |
2474 | errtryhelp(EXIT_FAILURE); | |
2475 | } | |
2476 | } | |
2477 | ||
2478 | colors_init(colormode, "sfdisk"); | |
2479 | ||
2480 | sfdisk_init(sf); | |
2481 | if (bytes) | |
2482 | fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES); | |
2483 | if (user_ss) | |
2484 | fdisk_save_user_sector_size(sf->cxt, user_ss, user_ss); | |
2485 | ||
2486 | if (outarg) | |
2487 | init_fields(NULL, outarg, NULL); | |
2488 | ||
2489 | if (sf->verify && !sf->act) | |
2490 | sf->act = ACT_VERIFY; /* --verify make be used with --list too */ | |
2491 | else if (!sf->act) | |
2492 | sf->act = ACT_FDISK; /* default */ | |
2493 | ||
2494 | if (sf->movedata && !(sf->act == ACT_FDISK && sf->partno >= 0)) | |
2495 | errx(EXIT_FAILURE, _("--movedata requires -N")); | |
2496 | ||
2497 | switch (sf->act) { | |
2498 | case ACT_ACTIVATE: | |
2499 | rc = command_activate(sf, argc - optind, argv + optind); | |
2500 | break; | |
2501 | ||
2502 | case ACT_BACKUP_SECTORS: | |
2503 | rc = command_backup_sectors(sf, argc - optind, argv + optind); | |
2504 | break; | |
2505 | ||
2506 | case ACT_DELETE: | |
2507 | rc = command_delete(sf, argc - optind, argv + optind); | |
2508 | break; | |
2509 | ||
2510 | case ACT_LIST: | |
2511 | rc = command_list_partitions(sf, argc - optind, argv + optind); | |
2512 | break; | |
2513 | ||
2514 | case ACT_LIST_TYPES: | |
2515 | rc = command_list_types(sf); | |
2516 | break; | |
2517 | ||
2518 | case ACT_LIST_FREE: | |
2519 | rc = command_list_freespace(sf, argc - optind, argv + optind); | |
2520 | break; | |
2521 | ||
2522 | case ACT_FDISK: | |
2523 | rc = command_fdisk(sf, argc - optind, argv + optind); | |
2524 | break; | |
2525 | ||
2526 | case ACT_DUMP: | |
2527 | rc = command_dump(sf, argc - optind, argv + optind); | |
2528 | break; | |
2529 | ||
2530 | case ACT_SHOW_SIZE: | |
2531 | rc = command_show_size(sf, argc - optind, argv + optind); | |
2532 | break; | |
2533 | ||
2534 | case ACT_SHOW_GEOM: | |
2535 | rc = command_show_geometry(sf, argc - optind, argv + optind); | |
2536 | break; | |
2537 | ||
2538 | case ACT_VERIFY: | |
2539 | rc = command_verify(sf, argc - optind, argv + optind); | |
2540 | break; | |
2541 | ||
2542 | case ACT_PARTTYPE: | |
2543 | rc = command_parttype(sf, argc - optind, argv + optind); | |
2544 | break; | |
2545 | ||
2546 | case ACT_PARTUUID: | |
2547 | rc = command_partuuid(sf, argc - optind, argv + optind); | |
2548 | break; | |
2549 | ||
2550 | case ACT_PARTLABEL: | |
2551 | rc = command_partlabel(sf, argc - optind, argv + optind); | |
2552 | break; | |
2553 | ||
2554 | case ACT_PARTATTRS: | |
2555 | rc = command_partattrs(sf, argc - optind, argv + optind); | |
2556 | break; | |
2557 | ||
2558 | case ACT_DISCARD_FREE: | |
2559 | rc = command_discard_free(sf, argc - optind, argv + optind); | |
2560 | break; | |
2561 | ||
2562 | case ACT_DISKID: | |
2563 | rc = command_diskid(sf, argc - optind, argv + optind); | |
2564 | break; | |
2565 | ||
2566 | case ACT_REORDER: | |
2567 | rc = command_reorder(sf, argc - optind, argv + optind); | |
2568 | break; | |
2569 | ||
2570 | case ACT_RELOCATE: | |
2571 | rc = command_relocate(sf, argc - optind, argv + optind); | |
2572 | break; | |
2573 | } | |
2574 | ||
2575 | sfdisk_deinit(sf); | |
2576 | ||
2577 | DBG(MISC, ul_debug("bye! [rc=%d]", rc)); | |
2578 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
2579 | } |