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