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