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