]>
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> |
7159b496 KZ |
33 | #ifdef HAVE_LIBREADLINE |
34 | # include <readline/readline.h> | |
35 | #endif | |
11ef0c35 KZ |
36 | |
37 | #include "c.h" | |
109dbc4f | 38 | #include "xalloc.h" |
1881390d KZ |
39 | #include "nls.h" |
40 | #include "debug.h" | |
9912f01b | 41 | #include "strutils.h" |
1881390d | 42 | #include "closestream.h" |
9c1f9dd3 | 43 | #include "colors.h" |
d2eb1457 | 44 | #include "blkdev.h" |
ab02d87e | 45 | #include "all-io.h" |
6340e913 | 46 | #include "rpmatch.h" |
fd6b7a7f | 47 | |
1881390d | 48 | #include "libfdisk.h" |
9c1f9dd3 | 49 | #include "fdisk-list.h" |
fd6b7a7f KZ |
50 | |
51 | /* | |
1881390d | 52 | * sfdisk debug stuff (see fdisk.h and include/debug.h) |
fd6b7a7f | 53 | */ |
1881390d | 54 | UL_DEBUG_DEFINE_MASK(sfdisk); |
819d9a29 | 55 | UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES; |
fd6b7a7f | 56 | |
1881390d KZ |
57 | #define SFDISKPROG_DEBUG_INIT (1 << 1) |
58 | #define SFDISKPROG_DEBUG_PARSE (1 << 2) | |
59 | #define SFDISKPROG_DEBUG_MISC (1 << 3) | |
e8813494 | 60 | #define SFDISKPROG_DEBUG_ASK (1 << 4) |
1881390d | 61 | #define SFDISKPROG_DEBUG_ALL 0xFFFF |
fd6b7a7f | 62 | |
1881390d KZ |
63 | #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x) |
64 | #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x) | |
fd6b7a7f | 65 | |
9c1f9dd3 | 66 | enum { |
d2c47697 | 67 | ACT_FDISK = 1, |
9c1f9dd3 KZ |
68 | ACT_ACTIVATE, |
69 | ACT_CHANGE_ID, | |
70 | ACT_DUMP, | |
71 | ACT_LIST, | |
9c1f9dd3 | 72 | ACT_LIST_TYPES, |
d2eb1457 | 73 | ACT_SHOW_SIZE, |
d420a7f9 | 74 | ACT_SHOW_GEOM, |
8eab3194 KZ |
75 | ACT_VERIFY, |
76 | ACT_PARTTYPE, | |
351fad50 | 77 | ACT_PARTUUID, |
e36fb07a | 78 | ACT_PARTLABEL, |
bc9e8547 | 79 | ACT_PARTATTRS, |
9c1f9dd3 KZ |
80 | }; |
81 | ||
82 | struct sfdisk { | |
3692c28d | 83 | int act; /* ACT_* */ |
7324f1bf KZ |
84 | int partno; /* -N <partno>, default -1 */ |
85 | const char *label; /* --label <label> */ | |
e1422de3 | 86 | const char *label_nested; /* --label-nested <label> */ |
ab02d87e | 87 | const char *backup_file; /* -O <path> */ |
7159b496 | 88 | char *prompt; |
9c1f9dd3 KZ |
89 | |
90 | struct fdisk_context *cxt; /* libfdisk context */ | |
d2c47697 | 91 | |
f0edb076 | 92 | unsigned int verify : 1, /* call fdisk_verify_disklabel() */ |
f01f2528 | 93 | quiet : 1, /* suppres extra messages */ |
8be199ea | 94 | interactive : 1, /* running on tty */ |
db48b6a1 KZ |
95 | noreread : 1, /* don't check device is in use */ |
96 | force : 1, /* do also stupid things */ | |
ab02d87e | 97 | backup : 1, /* backup sectors before write PT */ |
e54b1c6f | 98 | container : 1, /* PT contains container (MBR extended) partitions */ |
347a7f77 | 99 | append : 1, /* don't create new PT, append partitions only */ |
f01f2528 | 100 | noact : 1; /* do not write to device */ |
9c1f9dd3 KZ |
101 | }; |
102 | ||
7159b496 | 103 | #define SFDISK_PROMPT ">>> " |
fd6b7a7f | 104 | |
1881390d KZ |
105 | static void sfdiskprog_init_debug(void) |
106 | { | |
107 | __UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG); | |
fd6b7a7f KZ |
108 | } |
109 | ||
e8813494 KZ |
110 | |
111 | static int get_user_reply(const char *prompt, char *buf, size_t bufsz) | |
112 | { | |
113 | char *p; | |
114 | size_t sz; | |
115 | ||
7159b496 KZ |
116 | #ifdef HAVE_LIBREADLINE |
117 | if (isatty(STDIN_FILENO)) { | |
118 | p = readline(prompt); | |
119 | if (!p) | |
120 | return 1; | |
121 | memcpy(buf, p, bufsz); | |
122 | free(p); | |
123 | } else | |
124 | #endif | |
125 | { | |
126 | fputs(prompt, stdout); | |
127 | fflush(stdout); | |
e8813494 | 128 | |
7159b496 KZ |
129 | if (!fgets(buf, bufsz, stdin)) |
130 | return 1; | |
131 | } | |
e8813494 KZ |
132 | |
133 | for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ | |
134 | ||
135 | if (p > buf) | |
136 | memmove(buf, p, p - buf); /* remove blank space */ | |
137 | sz = strlen(buf); | |
138 | if (sz && *(buf + sz - 1) == '\n') | |
139 | *(buf + sz - 1) = '\0'; | |
140 | ||
141 | DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf)); | |
142 | return 0; | |
143 | } | |
144 | ||
f0edb076 KZ |
145 | static int ask_callback(struct fdisk_context *cxt, |
146 | struct fdisk_ask *ask, | |
147 | void *data) | |
9c1f9dd3 | 148 | { |
f0edb076 | 149 | struct sfdisk *sf = (struct sfdisk *) data; |
e8813494 KZ |
150 | int rc = 0; |
151 | ||
9c1f9dd3 KZ |
152 | assert(cxt); |
153 | assert(ask); | |
154 | ||
155 | switch(fdisk_ask_get_type(ask)) { | |
156 | case FDISK_ASKTYPE_INFO: | |
f0edb076 KZ |
157 | if (sf->quiet) |
158 | break; | |
9c1f9dd3 KZ |
159 | fputs(fdisk_ask_print_get_mesg(ask), stdout); |
160 | fputc('\n', stdout); | |
161 | break; | |
162 | case FDISK_ASKTYPE_WARNX: | |
163 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
164 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
165 | color_fdisable(stderr); | |
166 | fputc('\n', stderr); | |
167 | break; | |
168 | case FDISK_ASKTYPE_WARN: | |
169 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
170 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
171 | errno = fdisk_ask_print_get_errno(ask); | |
172 | fprintf(stderr, ": %m\n"); | |
173 | color_fdisable(stderr); | |
174 | break; | |
e8813494 KZ |
175 | case FDISK_ASKTYPE_YESNO: |
176 | { | |
177 | char buf[BUFSIZ]; | |
178 | fputc('\n', stdout); | |
179 | do { | |
180 | int x; | |
181 | fputs(fdisk_ask_get_query(ask), stdout); | |
182 | rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf)); | |
183 | if (rc) | |
184 | break; | |
185 | x = rpmatch(buf); | |
cd2a6f1c | 186 | if (x == RPMATCH_YES || x == RPMATCH_NO) { |
e8813494 KZ |
187 | fdisk_ask_yesno_set_result(ask, x); |
188 | break; | |
189 | } | |
190 | } while(1); | |
191 | DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc)); | |
192 | break; | |
193 | } | |
9c1f9dd3 KZ |
194 | default: |
195 | break; | |
196 | } | |
e8813494 | 197 | return rc; |
9c1f9dd3 KZ |
198 | } |
199 | ||
200 | static void sfdisk_init(struct sfdisk *sf) | |
201 | { | |
202 | fdisk_init_debug(0); | |
203 | sfdiskprog_init_debug(); | |
204 | ||
9c1f9dd3 KZ |
205 | sf->cxt = fdisk_new_context(); |
206 | if (!sf->cxt) | |
207 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
f0edb076 | 208 | fdisk_set_ask(sf->cxt, ask_callback, (void *) sf); |
e1422de3 KZ |
209 | |
210 | if (sf->label_nested) { | |
211 | struct fdisk_context *x = fdisk_new_nested_context(sf->cxt, | |
212 | sf->label_nested); | |
213 | if (!x) | |
214 | err(EXIT_FAILURE, _("failed to allocate nested libfdisk context")); | |
215 | /* the original context is available by fdisk_get_parent() */ | |
216 | sf->cxt = x; | |
217 | } | |
9c1f9dd3 KZ |
218 | } |
219 | ||
220 | static int sfdisk_deinit(struct sfdisk *sf) | |
221 | { | |
e1422de3 | 222 | struct fdisk_context *parent; |
9c1f9dd3 KZ |
223 | |
224 | assert(sf); | |
225 | assert(sf->cxt); | |
226 | ||
e1422de3 KZ |
227 | parent = fdisk_get_parent(sf->cxt); |
228 | if (parent) { | |
229 | fdisk_unref_context(sf->cxt); | |
230 | sf->cxt = parent; | |
231 | } | |
232 | ||
9c1f9dd3 | 233 | fdisk_unref_context(sf->cxt); |
7159b496 | 234 | free(sf->prompt); |
9c1f9dd3 | 235 | |
7159b496 | 236 | memset(sf, 0, sizeof(*sf)); |
5d365139 | 237 | return 0; |
9c1f9dd3 KZ |
238 | } |
239 | ||
ab02d87e KZ |
240 | static void backup_sectors(struct sfdisk *sf, |
241 | const char *tpl, | |
242 | const char *name, | |
243 | const char *devname, | |
9bbcf43f | 244 | uint64_t offset, size_t size) |
ab02d87e KZ |
245 | { |
246 | char *fname; | |
247 | int fd, devfd; | |
248 | ||
249 | devfd = fdisk_get_devfd(sf->cxt); | |
250 | assert(devfd >= 0); | |
251 | ||
252 | xasprintf(&fname, "%s0x%08jx.bak", tpl, offset); | |
253 | ||
254 | fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); | |
255 | if (fd < 0) | |
256 | goto fail; | |
257 | ||
9bbcf43f | 258 | if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) { |
ab02d87e KZ |
259 | fdisk_warn(sf->cxt, _("cannot seek %s"), devname); |
260 | goto fail; | |
261 | } else { | |
262 | unsigned char *buf = xmalloc(size); | |
263 | ||
264 | if (read_all(devfd, (char *) buf, size) != (ssize_t) size) { | |
265 | fdisk_warn(sf->cxt, _("cannot read %s"), devname); | |
266 | goto fail; | |
267 | } | |
268 | if (write_all(fd, buf, size) != 0) { | |
269 | fdisk_warn(sf->cxt, _("cannot write %s"), fname); | |
270 | goto fail; | |
271 | } | |
272 | free(buf); | |
273 | } | |
274 | ||
275 | fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"), | |
276 | name, (uintmax_t) offset, (uintmax_t) size, fname); | |
277 | close(fd); | |
278 | free(fname); | |
279 | return; | |
280 | fail: | |
281 | errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname); | |
282 | } | |
283 | ||
284 | static void backup_partition_table(struct sfdisk *sf, const char *devname) | |
285 | { | |
286 | const char *name; | |
287 | char *tpl; | |
9bbcf43f | 288 | uint64_t offset = 0; |
ab02d87e KZ |
289 | size_t size = 0; |
290 | int i = 0; | |
291 | ||
292 | assert(sf); | |
293 | ||
294 | if (!fdisk_has_label(sf->cxt)) | |
295 | return; | |
296 | ||
297 | if (!sf->backup_file) { | |
298 | /* initialize default backup filename */ | |
299 | const char *home = getenv ("HOME"); | |
300 | if (!home) | |
301 | errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); | |
302 | xasprintf(&tpl, "%s/sfdisk-%s-", home, basename(devname)); | |
303 | } else | |
304 | xasprintf(&tpl, "%s-%s-", sf->backup_file, basename(devname)); | |
305 | ||
306 | color_scheme_enable("header", UL_COLOR_BOLD); | |
307 | fdisk_info(sf->cxt, _("Backup files:")); | |
308 | color_disable(); | |
309 | ||
310 | while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size) | |
311 | backup_sectors(sf, tpl, name, devname, offset, size); | |
312 | ||
313 | if (!sf->quiet) | |
314 | fputc('\n', stdout); | |
315 | free(tpl); | |
316 | } | |
317 | ||
351fad50 KZ |
318 | static int write_changes(struct sfdisk *sf) |
319 | { | |
320 | int rc = 0; | |
321 | ||
322 | if (sf->noact) | |
54fefa07 | 323 | fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act).")); |
351fad50 KZ |
324 | else { |
325 | rc = fdisk_write_disklabel(sf->cxt); | |
326 | if (!rc) { | |
327 | fdisk_info(sf->cxt, _("\nThe partition table has been altered.")); | |
328 | fdisk_reread_partition_table(sf->cxt); | |
329 | } | |
330 | } | |
331 | if (!rc) | |
332 | rc = fdisk_deassign_device(sf->cxt, sf->noact); /* no-sync when no-act */ | |
333 | return rc; | |
334 | } | |
ab02d87e | 335 | |
148f6e6d KZ |
336 | /* |
337 | * sfdisk --list [<device ..] | |
338 | */ | |
9c1f9dd3 KZ |
339 | static int command_list_partitions(struct sfdisk *sf, int argc, char **argv) |
340 | { | |
9c1f9dd3 KZ |
341 | fdisk_enable_listonly(sf->cxt, 1); |
342 | ||
207de32a | 343 | if (argc) { |
d2eb1457 | 344 | int i, ct = 0; |
8a8d204c KZ |
345 | |
346 | for (i = 0; i < argc; i++) { | |
347 | if (ct) | |
348 | fputs("\n\n", stdout); | |
d2c47697 | 349 | if (print_device_pt(sf->cxt, argv[i], 0, sf->verify) == 0) |
8a8d204c KZ |
350 | ct++; |
351 | } | |
9c1f9dd3 | 352 | } else |
d2c47697 | 353 | print_all_devices_pt(sf->cxt, sf->verify); |
9c1f9dd3 KZ |
354 | |
355 | return 0; | |
356 | } | |
357 | ||
058dd97a KZ |
358 | /* |
359 | * sfdisk --list-types | |
360 | */ | |
361 | static int command_list_types(struct sfdisk *sf) | |
362 | { | |
363 | const struct fdisk_parttype *t; | |
364 | struct fdisk_label *lb; | |
365 | const char *name; | |
366 | size_t i = 0; | |
367 | int codes; | |
368 | ||
369 | assert(sf); | |
370 | assert(sf->cxt); | |
371 | ||
372 | name = sf->label ? sf->label : "dos"; | |
373 | lb = fdisk_get_label(sf->cxt, name); | |
374 | if (!lb) | |
375 | errx(EXIT_FAILURE, _("unsupported label '%s'"), name); | |
376 | ||
377 | codes = fdisk_label_has_code_parttypes(lb); | |
378 | fputs(_("Id Name\n\n"), stdout); | |
379 | ||
380 | while ((t = fdisk_label_get_parttype(lb, i++))) { | |
381 | if (codes) | |
382 | printf("%2x %s\n", fdisk_parttype_get_code(t), | |
383 | fdisk_parttype_get_name(t)); | |
384 | else | |
385 | printf("%s %s\n", fdisk_parttype_get_string(t), | |
386 | fdisk_parttype_get_name(t)); | |
387 | } | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
d2c47697 KZ |
392 | static int verify_device(struct sfdisk *sf, const char *devname) |
393 | { | |
394 | int rc = 1; | |
395 | ||
396 | fdisk_enable_listonly(sf->cxt, 1); | |
397 | ||
398 | if (fdisk_assign_device(sf->cxt, devname, 1)) { | |
54fefa07 | 399 | warn(_("cannot open %s"), devname); |
d2c47697 KZ |
400 | return 1; |
401 | } | |
402 | ||
403 | color_scheme_enable("header", UL_COLOR_BOLD); | |
404 | fdisk_info(sf->cxt, "%s:", devname); | |
405 | color_disable(); | |
406 | ||
407 | if (!fdisk_has_label(sf->cxt)) | |
408 | fdisk_info(sf->cxt, _("unrecognized partition table type")); | |
409 | else | |
410 | rc = fdisk_verify_disklabel(sf->cxt); | |
411 | ||
412 | fdisk_deassign_device(sf->cxt, 1); | |
413 | return rc; | |
414 | } | |
415 | ||
416 | /* | |
417 | * sfdisk --verify [<device ..] | |
418 | */ | |
419 | static int command_verify(struct sfdisk *sf, int argc, char **argv) | |
420 | { | |
421 | int nfails = 0, ct = 0; | |
422 | ||
423 | if (argc) { | |
424 | int i; | |
425 | for (i = 0; i < argc; i++) { | |
426 | if (i) | |
427 | fdisk_info(sf->cxt, " "); | |
428 | if (verify_device(sf, argv[i]) < 0) | |
429 | nfails++; | |
430 | } | |
431 | } else { | |
432 | FILE *f = NULL; | |
433 | char *dev; | |
434 | ||
435 | while ((dev = next_proc_partition(&f))) { | |
436 | if (ct) | |
437 | fdisk_info(sf->cxt, " "); | |
438 | if (verify_device(sf, dev) < 0) | |
439 | nfails++; | |
440 | free(dev); | |
441 | ct++; | |
442 | } | |
443 | } | |
444 | ||
445 | return nfails; | |
446 | } | |
447 | ||
148f6e6d | 448 | static int get_size(const char *dev, int silent, uintmax_t *sz) |
d2eb1457 | 449 | { |
148f6e6d KZ |
450 | int fd, rc = 0; |
451 | ||
452 | fd = open(dev, O_RDONLY); | |
d2eb1457 KZ |
453 | if (fd < 0) { |
454 | if (!silent) | |
54fefa07 | 455 | warn(_("cannot open %s"), dev); |
d2eb1457 KZ |
456 | return -errno; |
457 | } | |
d2eb1457 KZ |
458 | |
459 | if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) { | |
460 | if (!silent) | |
461 | warn(_("Cannot get size of %s"), dev); | |
462 | rc = -errno; | |
463 | } | |
464 | ||
465 | close(fd); | |
466 | return rc; | |
467 | } | |
468 | ||
148f6e6d KZ |
469 | /* |
470 | * sfdisk --show-size [<device ..] | |
471 | * | |
472 | * (silly, but just for backward compatibility) | |
473 | */ | |
d2eb1457 KZ |
474 | static int command_show_size(struct sfdisk *sf __attribute__((__unused__)), |
475 | int argc, char **argv) | |
476 | { | |
477 | uintmax_t sz; | |
478 | ||
479 | if (argc) { | |
480 | int i; | |
481 | for (i = 0; i < argc; i++) { | |
482 | if (get_size(argv[i], 0, &sz) == 0) | |
483 | printf("%ju\n", sz / 2); | |
484 | } | |
485 | } else { | |
486 | FILE *f = NULL; | |
487 | uintmax_t total = 0; | |
488 | char *dev; | |
489 | ||
490 | while ((dev = next_proc_partition(&f))) { | |
491 | if (get_size(dev, 1, &sz) == 0) { | |
492 | printf("%s: %9ju\n", dev, sz / 2); | |
493 | total += sz / 2; | |
494 | } | |
495 | free(dev); | |
496 | } | |
497 | if (total) | |
498 | printf(_("total: %ju blocks\n"), total); | |
499 | } | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
d420a7f9 KZ |
504 | static int print_geom(struct sfdisk *sf, const char *devname) |
505 | { | |
506 | fdisk_enable_listonly(sf->cxt, 1); | |
507 | ||
508 | if (fdisk_assign_device(sf->cxt, devname, 1)) { | |
54fefa07 | 509 | warn(_("cannot open %s"), devname); |
d420a7f9 KZ |
510 | return 1; |
511 | } | |
512 | ||
513 | fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track", | |
514 | devname, | |
515 | (uintmax_t) fdisk_get_geom_cylinders(sf->cxt), | |
516 | (uintmax_t) fdisk_get_geom_heads(sf->cxt), | |
517 | (uintmax_t) fdisk_get_geom_sectors(sf->cxt)); | |
518 | ||
519 | fdisk_deassign_device(sf->cxt, 1); | |
520 | return 0; | |
521 | } | |
522 | ||
523 | /* | |
524 | * sfdisk --show-geometry [<device ..] | |
525 | */ | |
526 | static int command_show_geometry(struct sfdisk *sf, int argc, char **argv) | |
527 | { | |
528 | int nfails = 0; | |
529 | ||
530 | if (argc) { | |
531 | int i; | |
532 | for (i = 0; i < argc; i++) { | |
533 | if (print_geom(sf, argv[i]) < 0) | |
534 | nfails++; | |
535 | } | |
536 | } else { | |
537 | FILE *f = NULL; | |
538 | char *dev; | |
539 | ||
540 | while ((dev = next_proc_partition(&f))) { | |
541 | if (print_geom(sf, dev) < 0) | |
542 | nfails++; | |
543 | free(dev); | |
544 | } | |
545 | } | |
546 | ||
547 | return nfails; | |
548 | } | |
549 | ||
254b1dfc KZ |
550 | /* |
551 | * sfdisk --activate <device> [<partno> ...] | |
552 | */ | |
54b13b0c KZ |
553 | static int command_activate(struct sfdisk *sf, int argc, char **argv) |
554 | { | |
21ca986d | 555 | int rc, nparts, i, listonly; |
54b13b0c KZ |
556 | struct fdisk_partition *pa = NULL; |
557 | const char *devname = NULL; | |
558 | ||
21ca986d | 559 | if (argc < 1) |
54b13b0c | 560 | errx(EXIT_FAILURE, _("no disk device specified")); |
8eab3194 KZ |
561 | devname = argv[0]; |
562 | ||
21ca986d KZ |
563 | /* --activate <device> */ |
564 | listonly = argc == 1; | |
54b13b0c | 565 | |
21ca986d | 566 | rc = fdisk_assign_device(sf->cxt, devname, listonly); |
54b13b0c KZ |
567 | if (rc) |
568 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
569 | ||
570 | if (!fdisk_is_label(sf->cxt, DOS)) | |
571 | errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR only")); | |
572 | ||
ab02d87e KZ |
573 | if (!listonly && sf->backup) |
574 | backup_partition_table(sf, devname); | |
21ca986d | 575 | |
54b13b0c KZ |
576 | nparts = fdisk_get_npartitions(sf->cxt); |
577 | for (i = 0; i < nparts; i++) { | |
578 | char *data = NULL; | |
579 | ||
580 | /* note that fdisk_get_partition() reuses the @pa pointer, you | |
581 | * don't have to (re)allocate it */ | |
582 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0) | |
583 | continue; | |
584 | ||
585 | /* sfdisk --activate list bootable partitions */ | |
21ca986d | 586 | if (listonly) { |
54b13b0c KZ |
587 | if (!fdisk_partition_is_bootable(pa)) |
588 | continue; | |
589 | if (fdisk_partition_to_string(pa, sf->cxt, | |
590 | FDISK_FIELD_DEVICE, &data) == 0) { | |
591 | printf("%s\n", data); | |
592 | free(data); | |
593 | } | |
594 | ||
595 | /* deactivate all active partitions */ | |
596 | } else if (fdisk_partition_is_bootable(pa)) | |
a1ef792f | 597 | fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE); |
54b13b0c KZ |
598 | } |
599 | ||
600 | /* sfdisk --activate <partno> [..] */ | |
601 | for (i = 1; i < argc; i++) { | |
602 | int n = strtou32_or_err(argv[i], _("failed to parse partition number")); | |
603 | ||
a1ef792f | 604 | rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE); |
254b1dfc KZ |
605 | if (rc) |
606 | errx(EXIT_FAILURE, | |
43a1ccec | 607 | _("%s: partition %d: failed to toggle bootable flag"), |
254b1dfc | 608 | devname, i + 1); |
54b13b0c KZ |
609 | } |
610 | ||
611 | fdisk_unref_partition(pa); | |
351fad50 KZ |
612 | if (listonly) |
613 | rc = fdisk_deassign_device(sf->cxt, 1); | |
614 | else | |
615 | rc = write_changes(sf); | |
54b13b0c KZ |
616 | return rc; |
617 | } | |
618 | ||
148f6e6d KZ |
619 | /* |
620 | * sfdisk --dump <device> | |
621 | */ | |
8a8d204c KZ |
622 | static int command_dump(struct sfdisk *sf, int argc, char **argv) |
623 | { | |
624 | const char *devname = NULL; | |
625 | struct fdisk_script *dp; | |
626 | int rc; | |
627 | ||
628 | if (argc) | |
629 | devname = argv[0]; | |
630 | if (!devname) | |
631 | errx(EXIT_FAILURE, _("no disk device specified")); | |
632 | ||
f01f2528 | 633 | rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */ |
8a8d204c KZ |
634 | if (rc) |
635 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
636 | ||
637 | dp = fdisk_new_script(sf->cxt); | |
638 | if (!dp) | |
639 | err(EXIT_FAILURE, _("failed to allocate dump struct")); | |
640 | ||
641 | rc = fdisk_script_read_context(dp, NULL); | |
642 | if (rc) | |
643 | err(EXIT_FAILURE, _("failed to dump partition table")); | |
644 | ||
645 | fdisk_script_write_file(dp, stdout); | |
646 | ||
647 | fdisk_unref_script(dp); | |
f01f2528 | 648 | fdisk_deassign_device(sf->cxt, 1); /* no-sync() */ |
8a8d204c KZ |
649 | return 0; |
650 | } | |
651 | ||
3a5bdedf KZ |
652 | static void assign_device_partition(struct sfdisk *sf, |
653 | const char *devname, | |
654 | size_t partno, | |
655 | int rdonly) | |
656 | { | |
657 | int rc; | |
658 | size_t n; | |
659 | struct fdisk_label *lb = NULL; | |
660 | ||
661 | assert(sf); | |
662 | assert(devname); | |
663 | ||
664 | /* read-only when a new <type> undefined */ | |
665 | rc = fdisk_assign_device(sf->cxt, devname, rdonly); | |
666 | if (rc) | |
667 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
668 | ||
669 | lb = fdisk_get_label(sf->cxt, NULL); | |
670 | if (!lb) | |
671 | errx(EXIT_FAILURE, _("%s: not found partition table."), devname); | |
672 | ||
673 | n = fdisk_get_npartitions(sf->cxt); | |
674 | if (partno > n) | |
54fefa07 BS |
675 | errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains " |
676 | "only %zu partitions"), devname, partno, n); | |
3a5bdedf | 677 | if (!fdisk_is_partition_used(sf->cxt, partno - 1)) |
54fefa07 | 678 | errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"), |
3a5bdedf KZ |
679 | devname, partno); |
680 | } | |
681 | ||
8eab3194 | 682 | /* |
e36fb07a | 683 | * sfdisk --part-type <device> <partno> [<type>] |
8eab3194 KZ |
684 | */ |
685 | static int command_parttype(struct sfdisk *sf, int argc, char **argv) | |
686 | { | |
3a5bdedf | 687 | size_t partno; |
8eab3194 | 688 | struct fdisk_parttype *type = NULL; |
3a5bdedf | 689 | struct fdisk_label *lb; |
8eab3194 KZ |
690 | const char *devname = NULL, *typestr = NULL; |
691 | ||
692 | if (!argc) | |
693 | errx(EXIT_FAILURE, _("no disk device specified")); | |
694 | devname = argv[0]; | |
695 | ||
696 | if (argc < 2) | |
697 | errx(EXIT_FAILURE, _("no partition number specified")); | |
698 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
699 | ||
700 | if (argc == 3) | |
701 | typestr = argv[2]; | |
702 | else if (argc > 3) | |
6e834d67 | 703 | errx(EXIT_FAILURE, _("unexpected arguments")); |
8eab3194 KZ |
704 | |
705 | /* read-only when a new <type> undefined */ | |
3a5bdedf | 706 | assign_device_partition(sf, devname, partno, !typestr); |
8eab3194 KZ |
707 | |
708 | lb = fdisk_get_label(sf->cxt, NULL); | |
8eab3194 KZ |
709 | |
710 | /* print partition type */ | |
711 | if (!typestr) { | |
712 | const struct fdisk_parttype *t = NULL; | |
2e73a998 | 713 | struct fdisk_partition *pa = NULL; |
8eab3194 KZ |
714 | |
715 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
716 | t = fdisk_partition_get_type(pa); | |
717 | if (!t) | |
718 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"), | |
719 | devname, partno); | |
720 | ||
721 | if (fdisk_label_has_code_parttypes(lb)) | |
722 | printf("%2x\n", fdisk_parttype_get_code(t)); | |
723 | else | |
724 | printf("%s\n", fdisk_parttype_get_string(t)); | |
725 | ||
726 | fdisk_unref_partition(pa); | |
2e73a998 | 727 | fdisk_deassign_device(sf->cxt, 1); |
8eab3194 KZ |
728 | return 0; |
729 | } | |
730 | ||
ab02d87e KZ |
731 | if (sf->backup) |
732 | backup_partition_table(sf, devname); | |
733 | ||
bf031d89 | 734 | /* parse <type> and apply to PT */ |
8eab3194 | 735 | type = fdisk_label_parse_parttype(lb, typestr); |
bf031d89 | 736 | if (!type) |
2e73a998 | 737 | errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"), |
8eab3194 | 738 | fdisk_label_get_name(lb), typestr); |
8eab3194 | 739 | |
2e73a998 KZ |
740 | else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0) |
741 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"), | |
742 | devname, partno); | |
dfc6db2a | 743 | fdisk_unref_parttype(type); |
351fad50 | 744 | return write_changes(sf); |
3a5bdedf KZ |
745 | } |
746 | ||
747 | /* | |
e36fb07a | 748 | * sfdisk --part-uuid <device> <partno> [<uuid>] |
3a5bdedf KZ |
749 | */ |
750 | static int command_partuuid(struct sfdisk *sf, int argc, char **argv) | |
751 | { | |
3a5bdedf KZ |
752 | size_t partno; |
753 | struct fdisk_partition *pa = NULL; | |
754 | const char *devname = NULL, *uuid = NULL; | |
755 | ||
756 | if (!argc) | |
757 | errx(EXIT_FAILURE, _("no disk device specified")); | |
758 | devname = argv[0]; | |
759 | ||
760 | if (argc < 2) | |
761 | errx(EXIT_FAILURE, _("no partition number specified")); | |
762 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
763 | ||
764 | if (argc == 3) | |
765 | uuid = argv[2]; | |
766 | else if (argc > 3) | |
6e834d67 | 767 | errx(EXIT_FAILURE, _("unexpected arguments")); |
3a5bdedf KZ |
768 | |
769 | /* read-only if uuid not given */ | |
770 | assign_device_partition(sf, devname, partno, !uuid); | |
771 | ||
772 | /* print partition uuid */ | |
773 | if (!uuid) { | |
f0f2be20 | 774 | const char *str = NULL; |
3a5bdedf KZ |
775 | |
776 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
777 | str = fdisk_partition_get_uuid(pa); | |
778 | if (!str) | |
779 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"), | |
780 | devname, partno); | |
781 | printf("%s\n", str); | |
782 | fdisk_unref_partition(pa); | |
783 | fdisk_deassign_device(sf->cxt, 1); | |
784 | return 0; | |
785 | } | |
786 | ||
787 | if (sf->backup) | |
788 | backup_partition_table(sf, devname); | |
789 | ||
790 | pa = fdisk_new_partition(); | |
791 | if (!pa) | |
792 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
793 | ||
794 | if (fdisk_partition_set_uuid(pa, uuid) != 0 || | |
795 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
796 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"), | |
797 | devname, partno); | |
351fad50 KZ |
798 | fdisk_unref_partition(pa); |
799 | return write_changes(sf); | |
800 | } | |
801 | ||
802 | /* | |
e36fb07a | 803 | * sfdisk --part-label <device> <partno> [<label>] |
351fad50 | 804 | */ |
e36fb07a | 805 | static int command_partlabel(struct sfdisk *sf, int argc, char **argv) |
351fad50 KZ |
806 | { |
807 | size_t partno; | |
808 | struct fdisk_partition *pa = NULL; | |
809 | const char *devname = NULL, *name = NULL; | |
810 | ||
811 | if (!argc) | |
812 | errx(EXIT_FAILURE, _("no disk device specified")); | |
813 | devname = argv[0]; | |
814 | ||
815 | if (argc < 2) | |
816 | errx(EXIT_FAILURE, _("no partition number specified")); | |
817 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
818 | ||
819 | if (argc == 3) | |
820 | name = argv[2]; | |
821 | else if (argc > 3) | |
6e834d67 | 822 | errx(EXIT_FAILURE, _("unexpected arguments")); |
351fad50 KZ |
823 | |
824 | /* read-only if name not given */ | |
825 | assign_device_partition(sf, devname, partno, !name); | |
826 | ||
827 | /* print partition name */ | |
828 | if (!name) { | |
f0f2be20 | 829 | const char *str = NULL; |
351fad50 KZ |
830 | |
831 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
832 | str = fdisk_partition_get_name(pa); | |
833 | if (!str) | |
834 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"), | |
835 | devname, partno); | |
836 | printf("%s\n", str); | |
837 | fdisk_unref_partition(pa); | |
838 | fdisk_deassign_device(sf->cxt, 1); | |
839 | return 0; | |
840 | } | |
841 | ||
842 | if (sf->backup) | |
843 | backup_partition_table(sf, devname); | |
844 | ||
845 | pa = fdisk_new_partition(); | |
846 | if (!pa) | |
847 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
848 | ||
849 | if (fdisk_partition_set_name(pa, name) != 0 || | |
850 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
851 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"), | |
852 | devname, partno); | |
853 | ||
854 | fdisk_unref_partition(pa); | |
855 | return write_changes(sf); | |
8eab3194 KZ |
856 | } |
857 | ||
bc9e8547 KZ |
858 | /* |
859 | * sfdisk --part-attrs <device> <partno> [<attrs>] | |
860 | */ | |
861 | static int command_partattrs(struct sfdisk *sf, int argc, char **argv) | |
862 | { | |
863 | size_t partno; | |
864 | struct fdisk_partition *pa = NULL; | |
865 | const char *devname = NULL, *attrs = NULL; | |
866 | ||
867 | if (!argc) | |
868 | errx(EXIT_FAILURE, _("no disk device specified")); | |
869 | devname = argv[0]; | |
870 | ||
871 | if (argc < 2) | |
872 | errx(EXIT_FAILURE, _("no partition number specified")); | |
873 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
874 | ||
875 | if (argc == 3) | |
876 | attrs = argv[2]; | |
877 | else if (argc > 3) | |
6e834d67 | 878 | errx(EXIT_FAILURE, _("unexpected arguments")); |
bc9e8547 KZ |
879 | |
880 | /* read-only if name not given */ | |
881 | assign_device_partition(sf, devname, partno, !attrs); | |
882 | ||
883 | /* print partition name */ | |
884 | if (!attrs) { | |
f0f2be20 | 885 | const char *str = NULL; |
bc9e8547 KZ |
886 | |
887 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
888 | str = fdisk_partition_get_attrs(pa); | |
889 | if (str) | |
890 | printf("%s\n", str); | |
891 | fdisk_unref_partition(pa); | |
892 | fdisk_deassign_device(sf->cxt, 1); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | if (sf->backup) | |
897 | backup_partition_table(sf, devname); | |
898 | ||
899 | pa = fdisk_new_partition(); | |
900 | if (!pa) | |
901 | err(EXIT_FAILURE, _("failed to allocate partition object")); | |
902 | ||
903 | if (fdisk_partition_set_attrs(pa, attrs) != 0 || | |
904 | fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) | |
905 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"), | |
906 | devname, partno); | |
907 | ||
908 | fdisk_unref_partition(pa); | |
909 | return write_changes(sf); | |
910 | } | |
911 | ||
b4df449f | 912 | static void sfdisk_print_partition(struct sfdisk *sf, size_t n) |
7324f1bf | 913 | { |
c3bc7483 | 914 | struct fdisk_partition *pa = NULL; |
7324f1bf KZ |
915 | char *data; |
916 | ||
917 | assert(sf); | |
918 | ||
f0edb076 KZ |
919 | if (sf->quiet) |
920 | return; | |
7324f1bf KZ |
921 | if (fdisk_get_partition(sf->cxt, n, &pa) != 0) |
922 | return; | |
923 | ||
924 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data); | |
925 | printf("%12s : ", data); | |
926 | ||
927 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data); | |
928 | printf("%12s ", data); | |
929 | ||
930 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data); | |
931 | printf("%12s ", data); | |
932 | ||
933 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data); | |
934 | printf("(%s) ", data); | |
935 | ||
936 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data); | |
937 | printf("%s\n", data); | |
938 | ||
939 | fdisk_unref_partition(pa); | |
940 | } | |
941 | ||
3186f4a9 KZ |
942 | static void command_fdisk_help(void) |
943 | { | |
944 | fputs(_("\nHelp:\n"), stdout); | |
945 | ||
946 | fputc('\n', stdout); | |
947 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
948 | fputs(_(" Commands:\n"), stdout); | |
949 | color_disable(); | |
e8813494 | 950 | fputs(_(" write write table to disk and exit\n"), stdout); |
6e834d67 | 951 | fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout); |
e8813494 | 952 | fputs(_(" abort exit sfdisk shell\n"), stdout); |
3186f4a9 KZ |
953 | fputs(_(" print print partition table.\n"), stdout); |
954 | fputs(_(" help this help.\n"), stdout); | |
333c3761 KZ |
955 | fputc('\n', stdout); |
956 | fputs(_(" CTRL-D the same like 'quit' command\n"), stdout); | |
3186f4a9 KZ |
957 | |
958 | fputc('\n', stdout); | |
959 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
960 | fputs(_(" Input format:\n"), stdout); | |
961 | color_disable(); | |
6e834d67 | 962 | fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout); |
3186f4a9 KZ |
963 | |
964 | fputc('\n', stdout); | |
ce9f568c KZ |
965 | fputs(_(" <start> begin of the partition in sectors or bytes if specified\n" |
966 | " in format <number>{K,M,G,T,P,E,Z,Y}. The default is\n" | |
967 | " the first free space.\n"), stdout); | |
3186f4a9 KZ |
968 | |
969 | fputc('\n', stdout); | |
970 | fputs(_(" <size> size of the partition in sectors if specified in format\n" | |
971 | " <number>{K,M,G,T,P,E,Z,Y} then it's interpreted as size\n" | |
972 | " in bytes. The default is all available space.\n"), stdout); | |
973 | ||
974 | fputc('\n', stdout); | |
975 | fputs(_(" <type> partition type. The default is Linux data partition.\n"), stdout); | |
976 | fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout); | |
977 | fputs(_(" GPT: uuid or L,S,H shortcuts.\n"), stdout); | |
978 | ||
979 | fputc('\n', stdout); | |
980 | fputs(_(" <bootable> '*' to mark MBR partition as bootable. \n"), stdout); | |
981 | ||
982 | fputc('\n', stdout); | |
983 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
984 | fputs(_(" Example:\n"), stdout); | |
985 | color_disable(); | |
986 | fputs(_(" , 4G creates 4GiB partition on default start offset.\n"), stdout); | |
987 | fputc('\n', stdout); | |
988 | } | |
989 | ||
e8813494 KZ |
990 | enum { |
991 | SFDISK_DONE_NONE = 0, | |
992 | SFDISK_DONE_EOF, | |
993 | SFDISK_DONE_ABORT, | |
994 | SFDISK_DONE_WRITE, | |
995 | SFDISK_DONE_ASK | |
996 | }; | |
997 | ||
998 | /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */ | |
999 | static int loop_control_commands(struct sfdisk *sf, | |
1000 | struct fdisk_script *dp, | |
1001 | char *buf) | |
1002 | { | |
1003 | const char *p = skip_blank(buf); | |
1004 | int rc = SFDISK_DONE_NONE; | |
1005 | ||
1006 | if (strcmp(p, "print") == 0) | |
1007 | list_disklabel(sf->cxt); | |
1008 | else if (strcmp(p, "help") == 0) | |
1009 | command_fdisk_help(); | |
1010 | else if (strcmp(p, "quit") == 0) | |
1011 | rc = SFDISK_DONE_ASK; | |
1012 | else if (strcmp(p, "write") == 0) | |
1013 | rc = SFDISK_DONE_WRITE; | |
1014 | else if (strcmp(p, "abort") == 0) | |
1015 | rc = SFDISK_DONE_ABORT; | |
1016 | else { | |
8be199ea | 1017 | if (sf->interactive) |
e8813494 KZ |
1018 | fdisk_warnx(sf->cxt, _("unsupported command")); |
1019 | else { | |
1020 | fdisk_warnx(sf->cxt, _("line %d: unsupported command"), | |
1021 | fdisk_script_get_nlines(dp)); | |
1022 | rc = -EINVAL; | |
1023 | } | |
1024 | } | |
1025 | return rc; | |
1026 | } | |
1027 | ||
e54b1c6f KZ |
1028 | static int has_container(struct sfdisk *sf) |
1029 | { | |
1030 | size_t i, nparts; | |
1031 | struct fdisk_partition *pa = NULL; | |
1032 | ||
1033 | if (sf->container) | |
1034 | return sf->container; | |
1035 | ||
1036 | nparts = fdisk_get_npartitions(sf->cxt); | |
e54b1c6f KZ |
1037 | for (i = 0; i < nparts; i++) { |
1038 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0) | |
1039 | continue; | |
1040 | if (fdisk_partition_is_container(pa)) { | |
1041 | sf->container = 1; | |
1042 | break; | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | fdisk_unref_partition(pa); | |
1047 | return sf->container; | |
1048 | } | |
1049 | ||
347a7f77 KZ |
1050 | static size_t last_pt_partno(struct sfdisk *sf) |
1051 | { | |
1052 | size_t i, nparts, partno = 0; | |
1053 | struct fdisk_partition *pa = NULL; | |
1054 | ||
1055 | ||
1056 | nparts = fdisk_get_npartitions(sf->cxt); | |
1057 | for (i = 0; i < nparts; i++) { | |
1058 | size_t x; | |
1059 | ||
1060 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0 || | |
1061 | !fdisk_partition_is_used(pa)) | |
1062 | continue; | |
1063 | x = fdisk_partition_get_partno(pa); | |
1064 | if (x > partno) | |
1065 | partno = x; | |
1066 | } | |
1067 | ||
1068 | fdisk_unref_partition(pa); | |
1069 | return partno; | |
1070 | } | |
1071 | ||
db48b6a1 KZ |
1072 | static int is_device_used(struct sfdisk *sf) |
1073 | { | |
1074 | #ifdef BLKRRPART | |
1075 | struct stat st; | |
1076 | int fd; | |
1077 | ||
1078 | assert(sf); | |
1079 | assert(sf->cxt); | |
1080 | ||
1081 | fd = fdisk_get_devfd(sf->cxt); | |
1082 | if (fd < 0) | |
1083 | return 0; | |
1084 | ||
1085 | if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) | |
1086 | return ioctl(fd, BLKRRPART) != 0; | |
1087 | #endif | |
1088 | return 0; | |
1089 | } | |
3186f4a9 | 1090 | |
3ba8e422 | 1091 | #ifdef HAVE_LIBREADLINE |
7159b496 KZ |
1092 | static char *sfdisk_fgets(struct fdisk_script *dp, |
1093 | char *buf, size_t bufsz, FILE *f) | |
1094 | { | |
1095 | struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp); | |
1096 | ||
1097 | assert(dp); | |
1098 | assert(buf); | |
1099 | assert(bufsz > 2); | |
1100 | ||
3ba8e422 | 1101 | if (sf->interactive) { |
7159b496 KZ |
1102 | char *p = readline(sf->prompt); |
1103 | size_t len; | |
1104 | ||
1105 | if (!p) | |
1106 | return NULL; | |
1107 | len = strlen(p); | |
1108 | if (len > bufsz - 2) | |
1109 | len = bufsz - 2; | |
1110 | ||
1111 | memcpy(buf, p, len); | |
1112 | buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */ | |
1113 | buf[len + 1] = '\0'; | |
1114 | free(p); | |
3ba8e422 | 1115 | fflush(stdout); |
7159b496 KZ |
1116 | return buf; |
1117 | } | |
7159b496 KZ |
1118 | return fgets(buf, bufsz, f); |
1119 | } | |
3ba8e422 | 1120 | #endif |
7159b496 | 1121 | |
254b1dfc KZ |
1122 | /* |
1123 | * sfdisk <device> [[-N] <partno>] | |
1124 | * | |
1125 | * Note that the option -N is there for backward compatibility only. | |
1126 | */ | |
7324f1bf | 1127 | static int command_fdisk(struct sfdisk *sf, int argc, char **argv) |
9c1f9dd3 | 1128 | { |
7324f1bf KZ |
1129 | int rc = 0, partno = sf->partno, created = 0; |
1130 | struct fdisk_script *dp; | |
1131 | struct fdisk_table *tb = NULL; | |
1132 | const char *devname = NULL, *label; | |
1133 | char buf[BUFSIZ]; | |
c3bc7483 | 1134 | size_t next_partno = (size_t) -1; |
9c1f9dd3 | 1135 | |
8a8d204c KZ |
1136 | if (argc) |
1137 | devname = argv[0]; | |
254b1dfc | 1138 | if (partno < 0 && argc > 1) |
54b13b0c | 1139 | partno = strtou32_or_err(argv[1], |
9c1f9dd3 | 1140 | _("failed to parse partition number")); |
8a8d204c | 1141 | if (!devname) |
9c1f9dd3 KZ |
1142 | errx(EXIT_FAILURE, _("no disk device specified")); |
1143 | ||
347a7f77 KZ |
1144 | rc = fdisk_assign_device(sf->cxt, devname, 0); |
1145 | if (rc) | |
1146 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
b4df449f | 1147 | |
7324f1bf KZ |
1148 | dp = fdisk_new_script(sf->cxt); |
1149 | if (!dp) | |
1150 | err(EXIT_FAILURE, _("failed to allocate script handler")); | |
347a7f77 | 1151 | fdisk_set_script(sf->cxt, dp); |
3ba8e422 | 1152 | #ifdef HAVE_LIBREADLINE |
7159b496 | 1153 | fdisk_script_set_fgets(dp, sfdisk_fgets); |
3ba8e422 | 1154 | #endif |
7159b496 | 1155 | fdisk_script_set_userdata(dp, (void *) sf); |
9c1f9dd3 | 1156 | |
b4df449f KZ |
1157 | /* |
1158 | * Don't create a new disklabel when [-N] <partno> specified. In this | |
1159 | * case reuse already specified disklabel. Let's check that the disk | |
1160 | * really contains the partition. | |
1161 | */ | |
1162 | if (partno >= 0) { | |
1163 | size_t n; | |
d754d554 | 1164 | |
b4df449f | 1165 | if (!fdisk_has_label(sf->cxt)) |
54fefa07 BS |
1166 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d: " |
1167 | "no partition table was found"), | |
d754d554 | 1168 | devname, partno + 1); |
b4df449f KZ |
1169 | n = fdisk_get_npartitions(sf->cxt); |
1170 | if ((size_t) partno > n) | |
54fefa07 BS |
1171 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d: " |
1172 | "partition table contains only %zu " | |
1173 | "partitions"), | |
d754d554 KZ |
1174 | devname, partno + 1, n); |
1175 | ||
1176 | if (!fdisk_is_partition_used(sf->cxt, partno)) | |
1177 | fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"), | |
1178 | devname, partno + 1); | |
b4df449f KZ |
1179 | created = 1; |
1180 | next_partno = partno; | |
1181 | } | |
1182 | ||
347a7f77 KZ |
1183 | if (sf->append) { |
1184 | created = 1; | |
1185 | next_partno = last_pt_partno(sf) + 1; | |
1186 | } | |
1187 | ||
8be199ea | 1188 | if (!sf->quiet && sf->interactive) { |
3186f4a9 KZ |
1189 | color_scheme_enable("welcome", UL_COLOR_GREEN); |
1190 | fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING); | |
1191 | color_disable(); | |
1192 | fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n" | |
1193 | "Be careful before using the write command.\n")); | |
1194 | } | |
1195 | ||
db48b6a1 KZ |
1196 | if (!sf->noact && !sf->noreread) { |
1197 | if (!sf->quiet) | |
1198 | fputs(_("Checking that no-one is using this disk right now ..."), stdout); | |
1199 | if (is_device_used(sf)) { | |
1200 | fputs(_(" FAILED\n\n"), stdout); | |
1201 | ||
1202 | fdisk_warnx(sf->cxt, _( | |
1203 | "This disk is currently in use - repartitioning is probably a bad idea.\n" | |
1204 | "Umount all file systems, and swapoff all swap partitions on this disk.\n" | |
1205 | "Use the --no-reread flag to suppress this check.\n")); | |
1206 | ||
1207 | if (!sf->force) | |
1208 | errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks.")); | |
1209 | } else | |
1210 | fputs(_(" OK\n\n"), stdout); | |
1211 | } | |
1212 | ||
ab02d87e KZ |
1213 | if (sf->backup) |
1214 | backup_partition_table(sf, devname); | |
1215 | ||
f0edb076 KZ |
1216 | if (!sf->quiet) { |
1217 | list_disk_geometry(sf->cxt); | |
1218 | if (fdisk_has_label(sf->cxt)) { | |
1219 | fdisk_info(sf->cxt, _("\nOld situation:")); | |
1220 | list_disklabel(sf->cxt); | |
1221 | } | |
3186f4a9 | 1222 | } |
7324f1bf KZ |
1223 | |
1224 | if (sf->label) | |
1225 | label = sf->label; | |
1226 | else if (fdisk_has_label(sf->cxt)) | |
1227 | label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL)); | |
1228 | else | |
1229 | label = "dos"; /* just for backward compatibility */ | |
1230 | ||
1231 | fdisk_script_set_header(dp, "label", label); | |
1232 | ||
8be199ea | 1233 | if (!sf->quiet && sf->interactive) { |
3186f4a9 KZ |
1234 | if (!fdisk_has_label(sf->cxt) && !sf->label) |
1235 | fdisk_info(sf->cxt, | |
1236 | _("\nsfdisk is going to create a new '%s' disk label.\n" | |
1237 | "Use 'label: <name>' before you define a first partition\n" | |
1238 | "to override the default."), label); | |
1239 | fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n")); | |
f0edb076 | 1240 | } else if (!sf->quiet) |
3186f4a9 | 1241 | fputc('\n', stdout); |
7324f1bf KZ |
1242 | |
1243 | tb = fdisk_script_get_table(dp); | |
c3bc7483 | 1244 | assert(tb); |
7324f1bf KZ |
1245 | |
1246 | do { | |
c3bc7483 | 1247 | size_t nparts; |
7324f1bf | 1248 | |
c3bc7483 KZ |
1249 | DBG(PARSE, ul_debug("<---next-line--->")); |
1250 | if (next_partno == (size_t) -1) | |
1251 | next_partno = fdisk_table_get_nents(tb); | |
1252 | ||
e54b1c6f KZ |
1253 | if (created |
1254 | && partno < 0 | |
347a7f77 | 1255 | && next_partno == fdisk_get_npartitions(sf->cxt) |
e54b1c6f KZ |
1256 | && !has_container(sf)) { |
1257 | fdisk_info(sf->cxt, _("All partitions used.")); | |
1258 | rc = SFDISK_DONE_ASK; | |
1259 | break; | |
1260 | } | |
1261 | ||
3186f4a9 KZ |
1262 | if (created) { |
1263 | char *partname = fdisk_partname(devname, next_partno + 1); | |
1264 | if (!partname) | |
1265 | err(EXIT_FAILURE, _("failed to allocate partition name")); | |
7159b496 KZ |
1266 | if (!sf->prompt || !startswith(sf->prompt, partname)) { |
1267 | free(sf->prompt); | |
1268 | xasprintf(&sf->prompt,"%s: ", partname); | |
1269 | } | |
3186f4a9 | 1270 | free(partname); |
7159b496 KZ |
1271 | } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) { |
1272 | free(sf->prompt); | |
1273 | sf->prompt = xstrdup(SFDISK_PROMPT); | |
1274 | } | |
7324f1bf | 1275 | |
3ba8e422 KZ |
1276 | #ifndef HAVE_LIBREADLINE |
1277 | if (sf->prompt) | |
1278 | fputs(sf->prompt, stdout); | |
1279 | #else | |
1280 | if (!sf->interactive && sf->prompt) | |
1281 | fputs(sf->prompt, stdout); | |
1282 | #endif | |
7324f1bf | 1283 | rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); |
e8813494 | 1284 | if (rc < 0) { |
c3bc7483 | 1285 | DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands")); |
7324f1bf | 1286 | buf[sizeof(buf) - 1] = '\0'; |
e8813494 KZ |
1287 | rc = loop_control_commands(sf, dp, buf); |
1288 | if (rc) | |
7324f1bf | 1289 | break; |
7324f1bf | 1290 | continue; |
e8813494 KZ |
1291 | } else if (rc == 1) { |
1292 | rc = SFDISK_DONE_EOF; | |
1293 | break; | |
7324f1bf KZ |
1294 | } |
1295 | ||
1296 | nparts = fdisk_table_get_nents(tb); | |
1297 | if (nparts) { | |
c3bc7483 | 1298 | size_t cur_partno; |
7324f1bf KZ |
1299 | struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1); |
1300 | ||
c3bc7483 | 1301 | assert(pa); |
3186f4a9 | 1302 | |
ecf40cda | 1303 | if (!fdisk_partition_has_start(pa) && |
3186f4a9 | 1304 | !fdisk_partition_start_is_default(pa)) { |
54fefa07 | 1305 | fdisk_info(sf->cxt, _("Ignoring partition %zu."), next_partno + 1); |
3186f4a9 KZ |
1306 | continue; |
1307 | } | |
b4df449f | 1308 | if (!created) { /* create a new disklabel */ |
7324f1bf KZ |
1309 | rc = fdisk_apply_script_headers(sf->cxt, dp); |
1310 | created = !rc; | |
90dc69e4 KZ |
1311 | if (rc) |
1312 | fdisk_warnx(sf->cxt, _( | |
1313 | "Failed to apply script headers, " | |
1314 | "disk label not created.")); | |
7324f1bf | 1315 | } |
b4df449f | 1316 | if (!rc && partno >= 0) { /* -N <partno>, modify partition */ |
0ecf3ab5 | 1317 | rc = fdisk_set_partition(sf->cxt, partno, pa); |
b4df449f KZ |
1318 | if (rc == 0) |
1319 | rc = SFDISK_DONE_ASK; | |
1320 | break; | |
a8a4887b | 1321 | } else if (!rc) { /* add partition */ |
c3bc7483 | 1322 | rc = fdisk_add_partition(sf->cxt, pa, &cur_partno); |
a8a4887b KZ |
1323 | if (rc) { |
1324 | errno = -rc; | |
90dc69e4 | 1325 | fdisk_warn(sf->cxt, _("Failed to add partition")); |
a8a4887b KZ |
1326 | } |
1327 | } | |
7324f1bf | 1328 | |
c3bc7483 | 1329 | if (!rc) { /* success, print reult */ |
8be199ea KZ |
1330 | if (sf->interactive) |
1331 | sfdisk_print_partition(sf, cur_partno); | |
c3bc7483 | 1332 | next_partno = cur_partno + 1; |
90dc69e4 | 1333 | } else if (pa) /* error, drop partition from script */ |
7324f1bf KZ |
1334 | fdisk_table_remove_partition(tb, pa); |
1335 | } else | |
3186f4a9 | 1336 | fdisk_info(sf->cxt, _("Script header accepted.")); |
e54b1c6f | 1337 | |
90dc69e4 KZ |
1338 | if (rc && !sf->interactive) { |
1339 | rc = SFDISK_DONE_ABORT; | |
1340 | break; | |
1341 | } | |
7324f1bf | 1342 | } while (1); |
254b1dfc | 1343 | |
f0edb076 | 1344 | if (!sf->quiet && rc != SFDISK_DONE_ABORT) { |
3186f4a9 | 1345 | fdisk_info(sf->cxt, _("\nNew situation:")); |
7324f1bf KZ |
1346 | list_disklabel(sf->cxt); |
1347 | } | |
e8813494 KZ |
1348 | |
1349 | switch (rc) { | |
1350 | case SFDISK_DONE_ASK: | |
05af8bd4 | 1351 | case SFDISK_DONE_EOF: |
8be199ea | 1352 | if (sf->interactive) { |
e8813494 KZ |
1353 | int yes = 0; |
1354 | fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes); | |
1355 | if (!yes) { | |
f01f2528 | 1356 | fdisk_info(sf->cxt, _("Leaving.")); |
e8813494 KZ |
1357 | rc = 0; |
1358 | break; | |
1359 | } | |
1360 | } | |
e8813494 | 1361 | case SFDISK_DONE_WRITE: |
351fad50 | 1362 | rc = write_changes(sf); |
e8813494 KZ |
1363 | break; |
1364 | case SFDISK_DONE_ABORT: | |
b4df449f | 1365 | default: /* rc < 0 on error */ |
f0edb076 | 1366 | fdisk_info(sf->cxt, _("Leaving.\n")); |
e8813494 | 1367 | break; |
3186f4a9 | 1368 | } |
e8813494 | 1369 | |
7324f1bf | 1370 | fdisk_unref_script(dp); |
9c1f9dd3 KZ |
1371 | return rc; |
1372 | } | |
1373 | ||
1881390d KZ |
1374 | static void __attribute__ ((__noreturn__)) usage(FILE *out) |
1375 | { | |
1376 | fputs(USAGE_HEADER, out); | |
fd6b7a7f | 1377 | |
1881390d | 1378 | fprintf(out, |
43a1ccec KZ |
1379 | _(" %1$s [options] <dev> [[-N] <part>]\n" |
1380 | " %1$s [options] <command>\n"), program_invocation_short_name); | |
1381 | ||
451dbcfa BS |
1382 | fputs(USAGE_SEPARATOR, out); |
1383 | fputs(_("Display or manipulate a disk partition table.\n"), out); | |
1384 | ||
43a1ccec | 1385 | fputs(_("\nCommands:\n"), out); |
703ab082 | 1386 | fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out); |
d420a7f9 KZ |
1387 | fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out); |
1388 | fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out); | |
1389 | fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out); | |
43a1ccec KZ |
1390 | fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out); |
1391 | fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out); | |
1392 | fputs(_(" -V, --verify test whether partitions seem correct\n"), out); | |
1393 | ||
e36fb07a KZ |
1394 | fputs(USAGE_SEPARATOR, out); |
1395 | fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out); | |
1396 | fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out); | |
1397 | fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out); | |
bc9e8547 | 1398 | fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out); |
e36fb07a | 1399 | |
43a1ccec | 1400 | fputs(USAGE_SEPARATOR, out); |
ab02d87e KZ |
1401 | fputs(_(" <dev> device (usually disk) path\n"), out); |
1402 | fputs(_(" <part> partition number\n"), out); | |
1403 | fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out); | |
fd6b7a7f | 1404 | |
1881390d | 1405 | fputs(USAGE_OPTIONS, out); |
703ab082 | 1406 | fputs(_(" -a, --append append partitions to existing partition table\n"), out); |
ab02d87e | 1407 | fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out); |
57eae223 | 1408 | fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out); |
ab02d87e | 1409 | fputs(_(" -f, --force disable all consistency checking\n"), out); |
da25898b KZ |
1410 | fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out); |
1411 | fprintf(out, | |
1412 | " %s\n", USAGE_COLORS_DEFAULT); | |
ab02d87e | 1413 | fputs(_(" -N, --partno <num> specify partition number\n"), out); |
ab02d87e KZ |
1414 | fputs(_(" -n, --no-act do everything except write to device\n"), out); |
1415 | fputs(_(" --no-reread do not check whether the device is in use\n"), out); | |
57eae223 BS |
1416 | fputs(_(" -O, --backup-file <path> override default backup file name\n"), out); |
1417 | fputs(_(" -o, --output <list> output columns\n"), out); | |
1418 | fputs(_(" -q, --quiet suppress extra info messages\n"), out); | |
1419 | fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out); | |
1420 | fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out); | |
35ce145f | 1421 | fputs(USAGE_SEPARATOR, out); |
6e834d67 | 1422 | fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out); |
57eae223 | 1423 | fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out); |
058dd97a | 1424 | |
1881390d KZ |
1425 | fputs(USAGE_SEPARATOR, out); |
1426 | fputs(USAGE_HELP, out); | |
edac3e2c | 1427 | fputs(_(" -v, --version output version information and exit\n"), out); |
fd6b7a7f | 1428 | |
01f9286c KZ |
1429 | list_available_columns(out); |
1430 | ||
1881390d KZ |
1431 | fprintf(out, USAGE_MAN_TAIL("sfdisk(8)")); |
1432 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
fd6b7a7f KZ |
1433 | } |
1434 | ||
9c1f9dd3 | 1435 | |
1881390d KZ |
1436 | int main(int argc, char *argv[]) |
1437 | { | |
01f9286c | 1438 | const char *outarg = NULL; |
8de07279 | 1439 | int rc = -EINVAL, c, longidx = -1, bytes = 0; |
da25898b | 1440 | int colormode = UL_COLORMODE_UNDEF; |
7324f1bf | 1441 | struct sfdisk _sf = { |
8be199ea KZ |
1442 | .partno = -1, |
1443 | .interactive = isatty(STDIN_FILENO) ? 1 : 0, | |
7324f1bf | 1444 | }, *sf = &_sf; |
1881390d | 1445 | |
8eab3194 KZ |
1446 | enum { |
1447 | OPT_CHANGE_ID = CHAR_MAX + 1, | |
1448 | OPT_PRINT_ID, | |
db48b6a1 | 1449 | OPT_ID, |
351fad50 | 1450 | OPT_NOREREAD, |
e36fb07a KZ |
1451 | OPT_PARTUUID, |
1452 | OPT_PARTLABEL, | |
1453 | OPT_PARTTYPE, | |
bc9e8547 | 1454 | OPT_PARTATTRS, |
da25898b KZ |
1455 | OPT_BYTES, |
1456 | OPT_COLOR | |
8eab3194 KZ |
1457 | }; |
1458 | ||
1881390d | 1459 | static const struct option longopts[] = { |
703ab082 KZ |
1460 | { "activate",no_argument, NULL, 'A' }, |
1461 | { "append", no_argument, NULL, 'a' }, | |
ab02d87e KZ |
1462 | { "backup", no_argument, NULL, 'b' }, |
1463 | { "backup-file", required_argument, NULL, 'O' }, | |
354f8cc8 | 1464 | { "bytes", no_argument, NULL, OPT_BYTES }, |
da25898b | 1465 | { "color", optional_argument, NULL, OPT_COLOR }, |
8a8d204c | 1466 | { "dump", no_argument, NULL, 'd' }, |
1881390d | 1467 | { "help", no_argument, NULL, 'h' }, |
db48b6a1 | 1468 | { "force", no_argument, NULL, 'f' }, |
7324f1bf | 1469 | { "label", required_argument, NULL, 'X' }, |
e1422de3 | 1470 | { "label-nested", required_argument, NULL, 'Y' }, |
d2c47697 | 1471 | { "list", no_argument, NULL, 'l' }, |
058dd97a | 1472 | { "list-types", no_argument, NULL, 'T' }, |
f01f2528 | 1473 | { "no-act", no_argument, NULL, 'n' }, |
db48b6a1 | 1474 | { "no-reread", no_argument, NULL, OPT_NOREREAD }, |
01f9286c | 1475 | { "output", required_argument, NULL, 'o' }, |
d2c47697 KZ |
1476 | { "partno", required_argument, NULL, 'N' }, |
1477 | { "show-size", no_argument, NULL, 's' }, | |
d420a7f9 | 1478 | { "show-geometry", no_argument, NULL, 'g' }, |
f0edb076 | 1479 | { "quiet", no_argument, NULL, 'q' }, |
d2c47697 KZ |
1480 | { "verify", no_argument, NULL, 'V' }, |
1481 | { "version", no_argument, NULL, 'v' }, | |
e36fb07a KZ |
1482 | |
1483 | { "part-uuid", no_argument, NULL, OPT_PARTUUID }, | |
1484 | { "part-label", no_argument, NULL, OPT_PARTLABEL }, | |
1485 | { "part-type", no_argument, NULL, OPT_PARTTYPE }, | |
bc9e8547 | 1486 | { "part-attrs", no_argument, NULL, OPT_PARTATTRS }, |
35ce145f | 1487 | |
8be199ea | 1488 | { "unit", required_argument, NULL, 'u' }, |
35ce145f | 1489 | { "Linux", no_argument, NULL, 'L' }, /* deprecated */ |
8eab3194 | 1490 | |
8eab3194 | 1491 | { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */ |
e36fb07a | 1492 | { "id", no_argument, NULL, 'c' }, /* deprecated */ |
8eab3194 KZ |
1493 | { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */ |
1494 | ||
1881390d KZ |
1495 | { NULL, 0, 0, 0 }, |
1496 | }; | |
1497 | ||
1498 | setlocale(LC_ALL, ""); | |
1499 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1500 | textdomain(PACKAGE); | |
1501 | atexit(close_stdout); | |
1502 | ||
e1422de3 | 1503 | while ((c = getopt_long(argc, argv, "aAbcdfghlLo:O:nN:qsTu:vVX:Y:", |
8eab3194 | 1504 | longopts, &longidx)) != -1) { |
1881390d | 1505 | switch(c) { |
703ab082 | 1506 | case 'A': |
54b13b0c KZ |
1507 | sf->act = ACT_ACTIVATE; |
1508 | break; | |
703ab082 | 1509 | case 'a': |
347a7f77 KZ |
1510 | sf->append = 1; |
1511 | break; | |
ab02d87e KZ |
1512 | case 'b': |
1513 | sf->backup = 1; | |
1514 | break; | |
8eab3194 KZ |
1515 | case OPT_CHANGE_ID: |
1516 | case OPT_PRINT_ID: | |
1517 | case OPT_ID: | |
6e834d67 | 1518 | warnx(_("%s is deprecated in favour of --part-type"), |
8eab3194 | 1519 | longopts[longidx].name); |
e36fb07a | 1520 | sf->act = ACT_PARTTYPE; |
54efd378 | 1521 | break; |
8eab3194 | 1522 | case 'c': |
6e834d67 | 1523 | warnx(_("--id is deprecated in favour of --part-type")); |
8eab3194 KZ |
1524 | sf->act = ACT_PARTTYPE; |
1525 | break; | |
e36fb07a KZ |
1526 | case 'd': |
1527 | sf->act = ACT_DUMP; | |
1528 | break; | |
db48b6a1 KZ |
1529 | case 'f': |
1530 | sf->force = 1; | |
1531 | break; | |
e36fb07a KZ |
1532 | case 'g': |
1533 | sf->act = ACT_SHOW_GEOM; | |
1534 | break; | |
1881390d KZ |
1535 | case 'h': |
1536 | usage(stdout); | |
1537 | break; | |
9c1f9dd3 KZ |
1538 | case 'l': |
1539 | sf->act = ACT_LIST; | |
1540 | break; | |
35ce145f | 1541 | case 'L': |
e36fb07a | 1542 | warnx(_("--Linux option is unnecessary and deprecated")); |
35ce145f | 1543 | break; |
01f9286c KZ |
1544 | case 'o': |
1545 | outarg = optarg; | |
1546 | break; | |
ab02d87e KZ |
1547 | case 'O': |
1548 | sf->backup = 1; | |
1549 | sf->backup_file = optarg; | |
1550 | break; | |
f01f2528 KZ |
1551 | case 'n': |
1552 | sf->noact = 1; | |
1553 | break; | |
254b1dfc | 1554 | case 'N': |
b4df449f | 1555 | sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1; |
7324f1bf | 1556 | break; |
f0edb076 KZ |
1557 | case 'q': |
1558 | sf->quiet = 1; | |
1559 | break; | |
d2eb1457 KZ |
1560 | case 's': |
1561 | sf->act = ACT_SHOW_SIZE; | |
1562 | break; | |
058dd97a KZ |
1563 | case 'T': |
1564 | sf->act = ACT_LIST_TYPES; | |
1565 | break; | |
d420a7f9 | 1566 | case 'u': |
d420a7f9 | 1567 | if (*optarg != 'S') |
54fefa07 | 1568 | errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg); |
d420a7f9 | 1569 | break; |
1881390d KZ |
1570 | case 'v': |
1571 | printf(_("%s from %s\n"), program_invocation_short_name, | |
1572 | PACKAGE_STRING); | |
1573 | return EXIT_SUCCESS; | |
d2c47697 KZ |
1574 | case 'V': |
1575 | sf->verify = 1; | |
1576 | break; | |
e36fb07a KZ |
1577 | case 'X': |
1578 | sf->label = optarg; | |
1579 | break; | |
e1422de3 KZ |
1580 | case 'Y': |
1581 | sf->label_nested = optarg; | |
1582 | break; | |
e36fb07a KZ |
1583 | |
1584 | case OPT_PARTUUID: | |
1585 | sf->act = ACT_PARTUUID; | |
1586 | break; | |
1587 | case OPT_PARTTYPE: | |
1588 | sf->act = ACT_PARTTYPE; | |
1589 | break; | |
1590 | case OPT_PARTLABEL: | |
1591 | sf->act = ACT_PARTLABEL; | |
351fad50 | 1592 | break; |
bc9e8547 KZ |
1593 | case OPT_PARTATTRS: |
1594 | sf->act = ACT_PARTATTRS; | |
1595 | break; | |
db48b6a1 KZ |
1596 | case OPT_NOREREAD: |
1597 | sf->noreread = 1; | |
1598 | break; | |
354f8cc8 | 1599 | case OPT_BYTES: |
8de07279 | 1600 | bytes = 1; |
354f8cc8 | 1601 | break; |
da25898b KZ |
1602 | case OPT_COLOR: |
1603 | colormode = UL_COLORMODE_AUTO; | |
1604 | if (optarg) | |
1605 | colormode = colormode_or_err(optarg, | |
1606 | _("unsupported color mode")); | |
1607 | break; | |
7324f1bf KZ |
1608 | default: |
1609 | usage(stderr); | |
37b94458 | 1610 | } |
37b94458 | 1611 | } |
fd6b7a7f | 1612 | |
da25898b KZ |
1613 | colors_init(colormode, "sfdisk"); |
1614 | ||
8de07279 KZ |
1615 | sfdisk_init(sf); |
1616 | if (bytes) | |
1617 | fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES); | |
1618 | ||
01f9286c KZ |
1619 | if (outarg) |
1620 | init_fields(NULL, outarg, NULL); | |
22853e4a | 1621 | |
d2c47697 KZ |
1622 | if (sf->verify && !sf->act) |
1623 | sf->act = ACT_VERIFY; /* --verify make be used with --list too */ | |
1624 | else if (!sf->act) | |
1625 | sf->act = ACT_FDISK; /* default */ | |
1626 | ||
9c1f9dd3 | 1627 | switch (sf->act) { |
54b13b0c KZ |
1628 | case ACT_ACTIVATE: |
1629 | rc = command_activate(sf, argc - optind, argv + optind); | |
1630 | break; | |
1631 | ||
9c1f9dd3 KZ |
1632 | case ACT_LIST: |
1633 | rc = command_list_partitions(sf, argc - optind, argv + optind); | |
1634 | break; | |
fd6b7a7f | 1635 | |
058dd97a KZ |
1636 | case ACT_LIST_TYPES: |
1637 | rc = command_list_types(sf); | |
1638 | break; | |
1639 | ||
9c1f9dd3 | 1640 | case ACT_FDISK: |
7324f1bf | 1641 | rc = command_fdisk(sf, argc - optind, argv + optind); |
9c1f9dd3 | 1642 | break; |
8a8d204c KZ |
1643 | |
1644 | case ACT_DUMP: | |
1645 | rc = command_dump(sf, argc - optind, argv + optind); | |
1646 | break; | |
d2eb1457 KZ |
1647 | |
1648 | case ACT_SHOW_SIZE: | |
1649 | rc = command_show_size(sf, argc - optind, argv + optind); | |
1650 | break; | |
d2c47697 | 1651 | |
d420a7f9 KZ |
1652 | case ACT_SHOW_GEOM: |
1653 | rc = command_show_geometry(sf, argc - optind, argv + optind); | |
1654 | break; | |
1655 | ||
d2c47697 KZ |
1656 | case ACT_VERIFY: |
1657 | rc = command_verify(sf, argc - optind, argv + optind); | |
1658 | break; | |
8eab3194 KZ |
1659 | |
1660 | case ACT_PARTTYPE: | |
1661 | rc = command_parttype(sf, argc - optind, argv + optind); | |
1662 | break; | |
3a5bdedf KZ |
1663 | |
1664 | case ACT_PARTUUID: | |
1665 | rc = command_partuuid(sf, argc - optind, argv + optind); | |
1666 | break; | |
1667 | ||
e36fb07a KZ |
1668 | case ACT_PARTLABEL: |
1669 | rc = command_partlabel(sf, argc - optind, argv + optind); | |
351fad50 KZ |
1670 | break; |
1671 | ||
bc9e8547 KZ |
1672 | case ACT_PARTATTRS: |
1673 | rc = command_partattrs(sf, argc - optind, argv + optind); | |
1674 | break; | |
1675 | ||
9c1f9dd3 | 1676 | } |
fd6b7a7f | 1677 | |
9c1f9dd3 | 1678 | sfdisk_deinit(sf); |
fd6b7a7f | 1679 | |
1881390d KZ |
1680 | DBG(MISC, ul_debug("bye! [rc=%d]", rc)); |
1681 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
4b1cc035 KZ |
1682 | } |
1683 |