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