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