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