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