]>
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 KZ |
50 | UL_DEBUG_DEFINE_MASK(sfdisk); |
51 | UL_DEBUG_DEFINE_MASKANEMS(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, | |
9c1f9dd3 KZ |
73 | }; |
74 | ||
75 | struct sfdisk { | |
76 | int act; /* action */ | |
7324f1bf KZ |
77 | int partno; /* -N <partno>, default -1 */ |
78 | const char *label; /* --label <label> */ | |
ab02d87e | 79 | const char *backup_file; /* -O <path> */ |
9c1f9dd3 KZ |
80 | |
81 | struct fdisk_context *cxt; /* libfdisk context */ | |
d2c47697 | 82 | |
f0edb076 | 83 | unsigned int verify : 1, /* call fdisk_verify_disklabel() */ |
f01f2528 | 84 | quiet : 1, /* suppres extra messages */ |
db48b6a1 KZ |
85 | noreread : 1, /* don't check device is in use */ |
86 | force : 1, /* do also stupid things */ | |
ab02d87e | 87 | backup : 1, /* backup sectors before write PT */ |
f01f2528 | 88 | noact : 1; /* do not write to device */ |
9c1f9dd3 KZ |
89 | }; |
90 | ||
fd6b7a7f | 91 | |
1881390d KZ |
92 | static void sfdiskprog_init_debug(void) |
93 | { | |
94 | __UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG); | |
fd6b7a7f KZ |
95 | } |
96 | ||
e8813494 KZ |
97 | |
98 | static int get_user_reply(const char *prompt, char *buf, size_t bufsz) | |
99 | { | |
100 | char *p; | |
101 | size_t sz; | |
102 | ||
103 | fputs(prompt, stdout); | |
104 | fflush(stdout); | |
105 | ||
106 | if (!fgets(buf, bufsz, stdin)) | |
107 | return 1; | |
108 | ||
109 | for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ | |
110 | ||
111 | if (p > buf) | |
112 | memmove(buf, p, p - buf); /* remove blank space */ | |
113 | sz = strlen(buf); | |
114 | if (sz && *(buf + sz - 1) == '\n') | |
115 | *(buf + sz - 1) = '\0'; | |
116 | ||
117 | DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf)); | |
118 | return 0; | |
119 | } | |
120 | ||
f0edb076 KZ |
121 | static int ask_callback(struct fdisk_context *cxt, |
122 | struct fdisk_ask *ask, | |
123 | void *data) | |
9c1f9dd3 | 124 | { |
f0edb076 | 125 | struct sfdisk *sf = (struct sfdisk *) data; |
e8813494 KZ |
126 | int rc = 0; |
127 | ||
9c1f9dd3 KZ |
128 | assert(cxt); |
129 | assert(ask); | |
130 | ||
131 | switch(fdisk_ask_get_type(ask)) { | |
132 | case FDISK_ASKTYPE_INFO: | |
f0edb076 KZ |
133 | if (sf->quiet) |
134 | break; | |
9c1f9dd3 KZ |
135 | fputs(fdisk_ask_print_get_mesg(ask), stdout); |
136 | fputc('\n', stdout); | |
137 | break; | |
138 | case FDISK_ASKTYPE_WARNX: | |
139 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
140 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
141 | color_fdisable(stderr); | |
142 | fputc('\n', stderr); | |
143 | break; | |
144 | case FDISK_ASKTYPE_WARN: | |
145 | color_scheme_fenable("warn", UL_COLOR_RED, stderr); | |
146 | fputs(fdisk_ask_print_get_mesg(ask), stderr); | |
147 | errno = fdisk_ask_print_get_errno(ask); | |
148 | fprintf(stderr, ": %m\n"); | |
149 | color_fdisable(stderr); | |
150 | break; | |
e8813494 KZ |
151 | case FDISK_ASKTYPE_YESNO: |
152 | { | |
153 | char buf[BUFSIZ]; | |
154 | fputc('\n', stdout); | |
155 | do { | |
156 | int x; | |
157 | fputs(fdisk_ask_get_query(ask), stdout); | |
158 | rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf)); | |
159 | if (rc) | |
160 | break; | |
161 | x = rpmatch(buf); | |
162 | if (x == 1 || x == 0) { | |
163 | fdisk_ask_yesno_set_result(ask, x); | |
164 | break; | |
165 | } | |
166 | } while(1); | |
167 | DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc)); | |
168 | break; | |
169 | } | |
9c1f9dd3 KZ |
170 | default: |
171 | break; | |
172 | } | |
e8813494 | 173 | return rc; |
9c1f9dd3 KZ |
174 | } |
175 | ||
176 | static void sfdisk_init(struct sfdisk *sf) | |
177 | { | |
178 | fdisk_init_debug(0); | |
179 | sfdiskprog_init_debug(); | |
180 | ||
181 | colors_init(UL_COLORMODE_UNDEF, "sfdisk"); | |
182 | ||
183 | sf->cxt = fdisk_new_context(); | |
184 | if (!sf->cxt) | |
185 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
f0edb076 | 186 | fdisk_set_ask(sf->cxt, ask_callback, (void *) sf); |
9c1f9dd3 KZ |
187 | } |
188 | ||
189 | static int sfdisk_deinit(struct sfdisk *sf) | |
190 | { | |
191 | int rc; | |
192 | ||
193 | assert(sf); | |
194 | assert(sf->cxt); | |
195 | ||
9c1f9dd3 KZ |
196 | fdisk_unref_context(sf->cxt); |
197 | memset(sf, 0, sizeof(*sf)); | |
198 | ||
199 | return rc; | |
200 | } | |
201 | ||
ab02d87e KZ |
202 | static void backup_sectors(struct sfdisk *sf, |
203 | const char *tpl, | |
204 | const char *name, | |
205 | const char *devname, | |
206 | off_t offset, size_t size) | |
207 | { | |
208 | char *fname; | |
209 | int fd, devfd; | |
210 | ||
211 | devfd = fdisk_get_devfd(sf->cxt); | |
212 | assert(devfd >= 0); | |
213 | ||
214 | xasprintf(&fname, "%s0x%08jx.bak", tpl, offset); | |
215 | ||
216 | fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); | |
217 | if (fd < 0) | |
218 | goto fail; | |
219 | ||
220 | if (lseek(devfd, offset, SEEK_SET) == (off_t) -1) { | |
221 | fdisk_warn(sf->cxt, _("cannot seek %s"), devname); | |
222 | goto fail; | |
223 | } else { | |
224 | unsigned char *buf = xmalloc(size); | |
225 | ||
226 | if (read_all(devfd, (char *) buf, size) != (ssize_t) size) { | |
227 | fdisk_warn(sf->cxt, _("cannot read %s"), devname); | |
228 | goto fail; | |
229 | } | |
230 | if (write_all(fd, buf, size) != 0) { | |
231 | fdisk_warn(sf->cxt, _("cannot write %s"), fname); | |
232 | goto fail; | |
233 | } | |
234 | free(buf); | |
235 | } | |
236 | ||
237 | fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"), | |
238 | name, (uintmax_t) offset, (uintmax_t) size, fname); | |
239 | close(fd); | |
240 | free(fname); | |
241 | return; | |
242 | fail: | |
243 | errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname); | |
244 | } | |
245 | ||
246 | static void backup_partition_table(struct sfdisk *sf, const char *devname) | |
247 | { | |
248 | const char *name; | |
249 | char *tpl; | |
250 | off_t offset = 0; | |
251 | size_t size = 0; | |
252 | int i = 0; | |
253 | ||
254 | assert(sf); | |
255 | ||
256 | if (!fdisk_has_label(sf->cxt)) | |
257 | return; | |
258 | ||
259 | if (!sf->backup_file) { | |
260 | /* initialize default backup filename */ | |
261 | const char *home = getenv ("HOME"); | |
262 | if (!home) | |
263 | errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); | |
264 | xasprintf(&tpl, "%s/sfdisk-%s-", home, basename(devname)); | |
265 | } else | |
266 | xasprintf(&tpl, "%s-%s-", sf->backup_file, basename(devname)); | |
267 | ||
268 | color_scheme_enable("header", UL_COLOR_BOLD); | |
269 | fdisk_info(sf->cxt, _("Backup files:")); | |
270 | color_disable(); | |
271 | ||
272 | while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size) | |
273 | backup_sectors(sf, tpl, name, devname, offset, size); | |
274 | ||
275 | if (!sf->quiet) | |
276 | fputc('\n', stdout); | |
277 | free(tpl); | |
278 | } | |
279 | ||
280 | ||
148f6e6d KZ |
281 | /* |
282 | * sfdisk --list [<device ..] | |
283 | */ | |
9c1f9dd3 KZ |
284 | static int command_list_partitions(struct sfdisk *sf, int argc, char **argv) |
285 | { | |
9c1f9dd3 KZ |
286 | fdisk_enable_listonly(sf->cxt, 1); |
287 | ||
207de32a | 288 | if (argc) { |
d2eb1457 | 289 | int i, ct = 0; |
8a8d204c KZ |
290 | |
291 | for (i = 0; i < argc; i++) { | |
292 | if (ct) | |
293 | fputs("\n\n", stdout); | |
d2c47697 | 294 | if (print_device_pt(sf->cxt, argv[i], 0, sf->verify) == 0) |
8a8d204c KZ |
295 | ct++; |
296 | } | |
9c1f9dd3 | 297 | } else |
d2c47697 | 298 | print_all_devices_pt(sf->cxt, sf->verify); |
9c1f9dd3 KZ |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
058dd97a KZ |
303 | /* |
304 | * sfdisk --list-types | |
305 | */ | |
306 | static int command_list_types(struct sfdisk *sf) | |
307 | { | |
308 | const struct fdisk_parttype *t; | |
309 | struct fdisk_label *lb; | |
310 | const char *name; | |
311 | size_t i = 0; | |
312 | int codes; | |
313 | ||
314 | assert(sf); | |
315 | assert(sf->cxt); | |
316 | ||
317 | name = sf->label ? sf->label : "dos"; | |
318 | lb = fdisk_get_label(sf->cxt, name); | |
319 | if (!lb) | |
320 | errx(EXIT_FAILURE, _("unsupported label '%s'"), name); | |
321 | ||
322 | codes = fdisk_label_has_code_parttypes(lb); | |
323 | fputs(_("Id Name\n\n"), stdout); | |
324 | ||
325 | while ((t = fdisk_label_get_parttype(lb, i++))) { | |
326 | if (codes) | |
327 | printf("%2x %s\n", fdisk_parttype_get_code(t), | |
328 | fdisk_parttype_get_name(t)); | |
329 | else | |
330 | printf("%s %s\n", fdisk_parttype_get_string(t), | |
331 | fdisk_parttype_get_name(t)); | |
332 | } | |
333 | ||
334 | return 0; | |
335 | } | |
336 | ||
d2c47697 KZ |
337 | static int verify_device(struct sfdisk *sf, const char *devname) |
338 | { | |
339 | int rc = 1; | |
340 | ||
341 | fdisk_enable_listonly(sf->cxt, 1); | |
342 | ||
343 | if (fdisk_assign_device(sf->cxt, devname, 1)) { | |
344 | warn(_("cannot open: %s"), devname); | |
345 | return 1; | |
346 | } | |
347 | ||
348 | color_scheme_enable("header", UL_COLOR_BOLD); | |
349 | fdisk_info(sf->cxt, "%s:", devname); | |
350 | color_disable(); | |
351 | ||
352 | if (!fdisk_has_label(sf->cxt)) | |
353 | fdisk_info(sf->cxt, _("unrecognized partition table type")); | |
354 | else | |
355 | rc = fdisk_verify_disklabel(sf->cxt); | |
356 | ||
357 | fdisk_deassign_device(sf->cxt, 1); | |
358 | return rc; | |
359 | } | |
360 | ||
361 | /* | |
362 | * sfdisk --verify [<device ..] | |
363 | */ | |
364 | static int command_verify(struct sfdisk *sf, int argc, char **argv) | |
365 | { | |
366 | int nfails = 0, ct = 0; | |
367 | ||
368 | if (argc) { | |
369 | int i; | |
370 | for (i = 0; i < argc; i++) { | |
371 | if (i) | |
372 | fdisk_info(sf->cxt, " "); | |
373 | if (verify_device(sf, argv[i]) < 0) | |
374 | nfails++; | |
375 | } | |
376 | } else { | |
377 | FILE *f = NULL; | |
378 | char *dev; | |
379 | ||
380 | while ((dev = next_proc_partition(&f))) { | |
381 | if (ct) | |
382 | fdisk_info(sf->cxt, " "); | |
383 | if (verify_device(sf, dev) < 0) | |
384 | nfails++; | |
385 | free(dev); | |
386 | ct++; | |
387 | } | |
388 | } | |
389 | ||
390 | return nfails; | |
391 | } | |
392 | ||
148f6e6d | 393 | static int get_size(const char *dev, int silent, uintmax_t *sz) |
d2eb1457 | 394 | { |
148f6e6d KZ |
395 | int fd, rc = 0; |
396 | ||
397 | fd = open(dev, O_RDONLY); | |
d2eb1457 KZ |
398 | if (fd < 0) { |
399 | if (!silent) | |
400 | warn(_("cannot open: %s"), dev); | |
401 | return -errno; | |
402 | } | |
d2eb1457 KZ |
403 | |
404 | if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) { | |
405 | if (!silent) | |
406 | warn(_("Cannot get size of %s"), dev); | |
407 | rc = -errno; | |
408 | } | |
409 | ||
410 | close(fd); | |
411 | return rc; | |
412 | } | |
413 | ||
148f6e6d KZ |
414 | /* |
415 | * sfdisk --show-size [<device ..] | |
416 | * | |
417 | * (silly, but just for backward compatibility) | |
418 | */ | |
d2eb1457 KZ |
419 | static int command_show_size(struct sfdisk *sf __attribute__((__unused__)), |
420 | int argc, char **argv) | |
421 | { | |
422 | uintmax_t sz; | |
423 | ||
424 | if (argc) { | |
425 | int i; | |
426 | for (i = 0; i < argc; i++) { | |
427 | if (get_size(argv[i], 0, &sz) == 0) | |
428 | printf("%ju\n", sz / 2); | |
429 | } | |
430 | } else { | |
431 | FILE *f = NULL; | |
432 | uintmax_t total = 0; | |
433 | char *dev; | |
434 | ||
435 | while ((dev = next_proc_partition(&f))) { | |
436 | if (get_size(dev, 1, &sz) == 0) { | |
437 | printf("%s: %9ju\n", dev, sz / 2); | |
438 | total += sz / 2; | |
439 | } | |
440 | free(dev); | |
441 | } | |
442 | if (total) | |
443 | printf(_("total: %ju blocks\n"), total); | |
444 | } | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
d420a7f9 KZ |
449 | static int print_geom(struct sfdisk *sf, const char *devname) |
450 | { | |
451 | fdisk_enable_listonly(sf->cxt, 1); | |
452 | ||
453 | if (fdisk_assign_device(sf->cxt, devname, 1)) { | |
454 | warn(_("cannot open: %s"), devname); | |
455 | return 1; | |
456 | } | |
457 | ||
458 | fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track", | |
459 | devname, | |
460 | (uintmax_t) fdisk_get_geom_cylinders(sf->cxt), | |
461 | (uintmax_t) fdisk_get_geom_heads(sf->cxt), | |
462 | (uintmax_t) fdisk_get_geom_sectors(sf->cxt)); | |
463 | ||
464 | fdisk_deassign_device(sf->cxt, 1); | |
465 | return 0; | |
466 | } | |
467 | ||
468 | /* | |
469 | * sfdisk --show-geometry [<device ..] | |
470 | */ | |
471 | static int command_show_geometry(struct sfdisk *sf, int argc, char **argv) | |
472 | { | |
473 | int nfails = 0; | |
474 | ||
475 | if (argc) { | |
476 | int i; | |
477 | for (i = 0; i < argc; i++) { | |
478 | if (print_geom(sf, argv[i]) < 0) | |
479 | nfails++; | |
480 | } | |
481 | } else { | |
482 | FILE *f = NULL; | |
483 | char *dev; | |
484 | ||
485 | while ((dev = next_proc_partition(&f))) { | |
486 | if (print_geom(sf, dev) < 0) | |
487 | nfails++; | |
488 | free(dev); | |
489 | } | |
490 | } | |
491 | ||
492 | return nfails; | |
493 | } | |
494 | ||
254b1dfc KZ |
495 | /* |
496 | * sfdisk --activate <device> [<partno> ...] | |
497 | */ | |
54b13b0c KZ |
498 | static int command_activate(struct sfdisk *sf, int argc, char **argv) |
499 | { | |
21ca986d | 500 | int rc, nparts, i, listonly; |
54b13b0c KZ |
501 | struct fdisk_partition *pa = NULL; |
502 | const char *devname = NULL; | |
503 | ||
21ca986d | 504 | if (argc < 1) |
54b13b0c | 505 | errx(EXIT_FAILURE, _("no disk device specified")); |
8eab3194 KZ |
506 | devname = argv[0]; |
507 | ||
21ca986d KZ |
508 | /* --activate <device> */ |
509 | listonly = argc == 1; | |
54b13b0c | 510 | |
21ca986d | 511 | rc = fdisk_assign_device(sf->cxt, devname, listonly); |
54b13b0c KZ |
512 | if (rc) |
513 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
514 | ||
515 | if (!fdisk_is_label(sf->cxt, DOS)) | |
516 | errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR only")); | |
517 | ||
ab02d87e KZ |
518 | if (!listonly && sf->backup) |
519 | backup_partition_table(sf, devname); | |
21ca986d | 520 | |
54b13b0c KZ |
521 | nparts = fdisk_get_npartitions(sf->cxt); |
522 | for (i = 0; i < nparts; i++) { | |
523 | char *data = NULL; | |
524 | ||
525 | /* note that fdisk_get_partition() reuses the @pa pointer, you | |
526 | * don't have to (re)allocate it */ | |
527 | if (fdisk_get_partition(sf->cxt, i, &pa) != 0) | |
528 | continue; | |
529 | ||
530 | /* sfdisk --activate list bootable partitions */ | |
21ca986d | 531 | if (listonly) { |
54b13b0c KZ |
532 | if (!fdisk_partition_is_bootable(pa)) |
533 | continue; | |
534 | if (fdisk_partition_to_string(pa, sf->cxt, | |
535 | FDISK_FIELD_DEVICE, &data) == 0) { | |
536 | printf("%s\n", data); | |
537 | free(data); | |
538 | } | |
539 | ||
540 | /* deactivate all active partitions */ | |
541 | } else if (fdisk_partition_is_bootable(pa)) | |
542 | fdisk_partition_toggle_flag(sf->cxt, i, DOS_FLAG_ACTIVE); | |
543 | } | |
544 | ||
545 | /* sfdisk --activate <partno> [..] */ | |
546 | for (i = 1; i < argc; i++) { | |
547 | int n = strtou32_or_err(argv[i], _("failed to parse partition number")); | |
548 | ||
254b1dfc KZ |
549 | rc = fdisk_partition_toggle_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE); |
550 | if (rc) | |
551 | errx(EXIT_FAILURE, | |
43a1ccec | 552 | _("%s: partition %d: failed to toggle bootable flag"), |
254b1dfc | 553 | devname, i + 1); |
54b13b0c KZ |
554 | } |
555 | ||
556 | fdisk_unref_partition(pa); | |
21ca986d | 557 | |
f01f2528 | 558 | if (sf->noact == 0 && !listonly) |
21ca986d | 559 | rc = fdisk_write_disklabel(sf->cxt); |
54b13b0c | 560 | if (!rc) |
f01f2528 | 561 | rc = fdisk_deassign_device(sf->cxt, sf->noact); |
54b13b0c KZ |
562 | return rc; |
563 | } | |
564 | ||
148f6e6d KZ |
565 | /* |
566 | * sfdisk --dump <device> | |
567 | */ | |
8a8d204c KZ |
568 | static int command_dump(struct sfdisk *sf, int argc, char **argv) |
569 | { | |
570 | const char *devname = NULL; | |
571 | struct fdisk_script *dp; | |
572 | int rc; | |
573 | ||
574 | if (argc) | |
575 | devname = argv[0]; | |
576 | if (!devname) | |
577 | errx(EXIT_FAILURE, _("no disk device specified")); | |
578 | ||
f01f2528 | 579 | rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */ |
8a8d204c KZ |
580 | if (rc) |
581 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
582 | ||
583 | dp = fdisk_new_script(sf->cxt); | |
584 | if (!dp) | |
585 | err(EXIT_FAILURE, _("failed to allocate dump struct")); | |
586 | ||
587 | rc = fdisk_script_read_context(dp, NULL); | |
588 | if (rc) | |
589 | err(EXIT_FAILURE, _("failed to dump partition table")); | |
590 | ||
591 | fdisk_script_write_file(dp, stdout); | |
592 | ||
593 | fdisk_unref_script(dp); | |
f01f2528 | 594 | fdisk_deassign_device(sf->cxt, 1); /* no-sync() */ |
8a8d204c KZ |
595 | return 0; |
596 | } | |
597 | ||
8eab3194 | 598 | /* |
43a1ccec | 599 | * sfdisk --type <device> <partno> [<type>] |
8eab3194 KZ |
600 | */ |
601 | static int command_parttype(struct sfdisk *sf, int argc, char **argv) | |
602 | { | |
603 | int rc; | |
604 | size_t partno, n; | |
605 | struct fdisk_label *lb = NULL; | |
8eab3194 KZ |
606 | struct fdisk_parttype *type = NULL; |
607 | const char *devname = NULL, *typestr = NULL; | |
608 | ||
609 | if (!argc) | |
610 | errx(EXIT_FAILURE, _("no disk device specified")); | |
611 | devname = argv[0]; | |
612 | ||
613 | if (argc < 2) | |
614 | errx(EXIT_FAILURE, _("no partition number specified")); | |
615 | partno = strtou32_or_err(argv[1], _("failed to parse partition number")); | |
616 | ||
617 | if (argc == 3) | |
618 | typestr = argv[2]; | |
619 | else if (argc > 3) | |
620 | errx(EXIT_FAILURE, _("uneexpected arguments")); | |
621 | ||
622 | /* read-only when a new <type> undefined */ | |
623 | rc = fdisk_assign_device(sf->cxt, devname, !typestr); | |
624 | if (rc) | |
625 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
626 | ||
627 | lb = fdisk_get_label(sf->cxt, NULL); | |
628 | if (!lb) | |
629 | errx(EXIT_FAILURE, _("%s: not found partition table."), devname); | |
630 | ||
631 | n = fdisk_get_npartitions(sf->cxt); | |
632 | if (partno > n) | |
633 | errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains %zu " | |
634 | "partitions only."), devname, partno, n); | |
635 | if (!fdisk_is_partition_used(sf->cxt, partno - 1)) | |
636 | errx(EXIT_FAILURE, _("%s: partition %zu: partition unnused"), | |
637 | devname, partno); | |
638 | ||
639 | /* print partition type */ | |
640 | if (!typestr) { | |
641 | const struct fdisk_parttype *t = NULL; | |
2e73a998 | 642 | struct fdisk_partition *pa = NULL; |
8eab3194 KZ |
643 | |
644 | if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) | |
645 | t = fdisk_partition_get_type(pa); | |
646 | if (!t) | |
647 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"), | |
648 | devname, partno); | |
649 | ||
650 | if (fdisk_label_has_code_parttypes(lb)) | |
651 | printf("%2x\n", fdisk_parttype_get_code(t)); | |
652 | else | |
653 | printf("%s\n", fdisk_parttype_get_string(t)); | |
654 | ||
655 | fdisk_unref_partition(pa); | |
2e73a998 | 656 | fdisk_deassign_device(sf->cxt, 1); |
8eab3194 KZ |
657 | return 0; |
658 | } | |
659 | ||
ab02d87e KZ |
660 | if (sf->backup) |
661 | backup_partition_table(sf, devname); | |
662 | ||
8eab3194 KZ |
663 | /* parse <type> and apply yo PT */ |
664 | type = fdisk_label_parse_parttype(lb, typestr); | |
2e73a998 KZ |
665 | if (!type || fdisk_parttype_is_unknown(type)) |
666 | errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"), | |
8eab3194 | 667 | fdisk_label_get_name(lb), typestr); |
8eab3194 | 668 | |
2e73a998 KZ |
669 | else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0) |
670 | errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"), | |
671 | devname, partno); | |
8eab3194 | 672 | |
2e73a998 | 673 | fdisk_free_parttype(type); |
f01f2528 | 674 | if (sf->noact == 0 && !rc) |
8eab3194 KZ |
675 | rc = fdisk_write_disklabel(sf->cxt); |
676 | if (!rc) | |
f01f2528 | 677 | rc = fdisk_deassign_device(sf->cxt, 1); /* no-sync */ |
8eab3194 KZ |
678 | return rc; |
679 | } | |
680 | ||
b4df449f | 681 | static void sfdisk_print_partition(struct sfdisk *sf, size_t n) |
7324f1bf | 682 | { |
c3bc7483 | 683 | struct fdisk_partition *pa = NULL; |
7324f1bf KZ |
684 | char *data; |
685 | ||
686 | assert(sf); | |
687 | ||
f0edb076 KZ |
688 | if (sf->quiet) |
689 | return; | |
7324f1bf KZ |
690 | if (fdisk_get_partition(sf->cxt, n, &pa) != 0) |
691 | return; | |
692 | ||
693 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data); | |
694 | printf("%12s : ", data); | |
695 | ||
696 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data); | |
697 | printf("%12s ", data); | |
698 | ||
699 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data); | |
700 | printf("%12s ", data); | |
701 | ||
702 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data); | |
703 | printf("(%s) ", data); | |
704 | ||
705 | fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data); | |
706 | printf("%s\n", data); | |
707 | ||
708 | fdisk_unref_partition(pa); | |
709 | } | |
710 | ||
3186f4a9 KZ |
711 | static void command_fdisk_help(void) |
712 | { | |
713 | fputs(_("\nHelp:\n"), stdout); | |
714 | ||
715 | fputc('\n', stdout); | |
716 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
717 | fputs(_(" Commands:\n"), stdout); | |
718 | color_disable(); | |
e8813494 KZ |
719 | fputs(_(" write write table to disk and exit\n"), stdout); |
720 | fputs(_(" quit show new situation and wait for user's feedbadk before write\n"), stdout); | |
721 | fputs(_(" abort exit sfdisk shell\n"), stdout); | |
3186f4a9 KZ |
722 | fputs(_(" print print partition table.\n"), stdout); |
723 | fputs(_(" help this help.\n"), stdout); | |
724 | ||
725 | fputc('\n', stdout); | |
726 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
727 | fputs(_(" Input format:\n"), stdout); | |
728 | color_disable(); | |
729 | fputs(_(" <start> <size> <typy> <bootable>\n"), stdout); | |
730 | ||
731 | fputc('\n', stdout); | |
732 | fputs(_(" <start> begin of the partition in sectors. The default is the first\n" | |
733 | " free space.\n"), stdout); | |
734 | ||
735 | fputc('\n', stdout); | |
736 | fputs(_(" <size> size of the partition in sectors if specified in format\n" | |
737 | " <number>{K,M,G,T,P,E,Z,Y} then it's interpreted as size\n" | |
738 | " in bytes. The default is all available space.\n"), stdout); | |
739 | ||
740 | fputc('\n', stdout); | |
741 | fputs(_(" <type> partition type. The default is Linux data partition.\n"), stdout); | |
742 | fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout); | |
743 | fputs(_(" GPT: uuid or L,S,H shortcuts.\n"), stdout); | |
744 | ||
745 | fputc('\n', stdout); | |
746 | fputs(_(" <bootable> '*' to mark MBR partition as bootable. \n"), stdout); | |
747 | ||
748 | fputc('\n', stdout); | |
749 | color_scheme_enable("help-title", UL_COLOR_BOLD); | |
750 | fputs(_(" Example:\n"), stdout); | |
751 | color_disable(); | |
752 | fputs(_(" , 4G creates 4GiB partition on default start offset.\n"), stdout); | |
753 | fputc('\n', stdout); | |
754 | } | |
755 | ||
e8813494 KZ |
756 | enum { |
757 | SFDISK_DONE_NONE = 0, | |
758 | SFDISK_DONE_EOF, | |
759 | SFDISK_DONE_ABORT, | |
760 | SFDISK_DONE_WRITE, | |
761 | SFDISK_DONE_ASK | |
762 | }; | |
763 | ||
764 | /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */ | |
765 | static int loop_control_commands(struct sfdisk *sf, | |
766 | struct fdisk_script *dp, | |
767 | char *buf) | |
768 | { | |
769 | const char *p = skip_blank(buf); | |
770 | int rc = SFDISK_DONE_NONE; | |
771 | ||
772 | if (strcmp(p, "print") == 0) | |
773 | list_disklabel(sf->cxt); | |
774 | else if (strcmp(p, "help") == 0) | |
775 | command_fdisk_help(); | |
776 | else if (strcmp(p, "quit") == 0) | |
777 | rc = SFDISK_DONE_ASK; | |
778 | else if (strcmp(p, "write") == 0) | |
779 | rc = SFDISK_DONE_WRITE; | |
780 | else if (strcmp(p, "abort") == 0) | |
781 | rc = SFDISK_DONE_ABORT; | |
782 | else { | |
783 | if (isatty(STDIN_FILENO)) | |
784 | fdisk_warnx(sf->cxt, _("unsupported command")); | |
785 | else { | |
786 | fdisk_warnx(sf->cxt, _("line %d: unsupported command"), | |
787 | fdisk_script_get_nlines(dp)); | |
788 | rc = -EINVAL; | |
789 | } | |
790 | } | |
791 | return rc; | |
792 | } | |
793 | ||
db48b6a1 KZ |
794 | static int is_device_used(struct sfdisk *sf) |
795 | { | |
796 | #ifdef BLKRRPART | |
797 | struct stat st; | |
798 | int fd; | |
799 | ||
800 | assert(sf); | |
801 | assert(sf->cxt); | |
802 | ||
803 | fd = fdisk_get_devfd(sf->cxt); | |
804 | if (fd < 0) | |
805 | return 0; | |
806 | ||
807 | if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) | |
808 | return ioctl(fd, BLKRRPART) != 0; | |
809 | #endif | |
810 | return 0; | |
811 | } | |
3186f4a9 | 812 | |
254b1dfc KZ |
813 | /* |
814 | * sfdisk <device> [[-N] <partno>] | |
815 | * | |
816 | * Note that the option -N is there for backward compatibility only. | |
817 | */ | |
7324f1bf | 818 | static int command_fdisk(struct sfdisk *sf, int argc, char **argv) |
9c1f9dd3 | 819 | { |
7324f1bf KZ |
820 | int rc = 0, partno = sf->partno, created = 0; |
821 | struct fdisk_script *dp; | |
822 | struct fdisk_table *tb = NULL; | |
823 | const char *devname = NULL, *label; | |
824 | char buf[BUFSIZ]; | |
c3bc7483 | 825 | size_t next_partno = (size_t) -1; |
9c1f9dd3 | 826 | |
8a8d204c KZ |
827 | if (argc) |
828 | devname = argv[0]; | |
254b1dfc | 829 | if (partno < 0 && argc > 1) |
54b13b0c | 830 | partno = strtou32_or_err(argv[1], |
9c1f9dd3 | 831 | _("failed to parse partition number")); |
8a8d204c | 832 | if (!devname) |
9c1f9dd3 KZ |
833 | errx(EXIT_FAILURE, _("no disk device specified")); |
834 | ||
b4df449f | 835 | |
7324f1bf KZ |
836 | dp = fdisk_new_script(sf->cxt); |
837 | if (!dp) | |
838 | err(EXIT_FAILURE, _("failed to allocate script handler")); | |
839 | ||
8a8d204c KZ |
840 | rc = fdisk_assign_device(sf->cxt, devname, 0); |
841 | if (rc) | |
842 | err(EXIT_FAILURE, _("cannot open %s"), devname); | |
9c1f9dd3 | 843 | |
b4df449f KZ |
844 | /* |
845 | * Don't create a new disklabel when [-N] <partno> specified. In this | |
846 | * case reuse already specified disklabel. Let's check that the disk | |
847 | * really contains the partition. | |
848 | */ | |
849 | if (partno >= 0) { | |
850 | size_t n; | |
851 | if (!fdisk_has_label(sf->cxt)) | |
8eab3194 KZ |
852 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d, " |
853 | "not found partition table."), | |
854 | devname, partno); | |
b4df449f KZ |
855 | n = fdisk_get_npartitions(sf->cxt); |
856 | if ((size_t) partno > n) | |
8eab3194 | 857 | errx(EXIT_FAILURE, _("%s: cannot modify partition %d, " |
b4df449f | 858 | "partition table contains %zu " |
8eab3194 KZ |
859 | "partitions only."), |
860 | devname, partno, n); | |
b4df449f KZ |
861 | created = 1; |
862 | next_partno = partno; | |
863 | } | |
864 | ||
f0edb076 | 865 | if (!sf->quiet && isatty(STDIN_FILENO)) { |
3186f4a9 KZ |
866 | color_scheme_enable("welcome", UL_COLOR_GREEN); |
867 | fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING); | |
868 | color_disable(); | |
869 | fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n" | |
870 | "Be careful before using the write command.\n")); | |
871 | } | |
872 | ||
db48b6a1 KZ |
873 | if (!sf->noact && !sf->noreread) { |
874 | if (!sf->quiet) | |
875 | fputs(_("Checking that no-one is using this disk right now ..."), stdout); | |
876 | if (is_device_used(sf)) { | |
877 | fputs(_(" FAILED\n\n"), stdout); | |
878 | ||
879 | fdisk_warnx(sf->cxt, _( | |
880 | "This disk is currently in use - repartitioning is probably a bad idea.\n" | |
881 | "Umount all file systems, and swapoff all swap partitions on this disk.\n" | |
882 | "Use the --no-reread flag to suppress this check.\n")); | |
883 | ||
884 | if (!sf->force) | |
885 | errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks.")); | |
886 | } else | |
887 | fputs(_(" OK\n\n"), stdout); | |
888 | } | |
889 | ||
ab02d87e KZ |
890 | if (sf->backup) |
891 | backup_partition_table(sf, devname); | |
892 | ||
f0edb076 KZ |
893 | if (!sf->quiet) { |
894 | list_disk_geometry(sf->cxt); | |
895 | if (fdisk_has_label(sf->cxt)) { | |
896 | fdisk_info(sf->cxt, _("\nOld situation:")); | |
897 | list_disklabel(sf->cxt); | |
898 | } | |
3186f4a9 | 899 | } |
7324f1bf KZ |
900 | |
901 | if (sf->label) | |
902 | label = sf->label; | |
903 | else if (fdisk_has_label(sf->cxt)) | |
904 | label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL)); | |
905 | else | |
906 | label = "dos"; /* just for backward compatibility */ | |
907 | ||
908 | fdisk_script_set_header(dp, "label", label); | |
909 | ||
f0edb076 | 910 | if (!sf->quiet && isatty(STDIN_FILENO)) { |
3186f4a9 KZ |
911 | if (!fdisk_has_label(sf->cxt) && !sf->label) |
912 | fdisk_info(sf->cxt, | |
913 | _("\nsfdisk is going to create a new '%s' disk label.\n" | |
914 | "Use 'label: <name>' before you define a first partition\n" | |
915 | "to override the default."), label); | |
916 | fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n")); | |
f0edb076 | 917 | } else if (!sf->quiet) |
3186f4a9 | 918 | fputc('\n', stdout); |
7324f1bf KZ |
919 | |
920 | tb = fdisk_script_get_table(dp); | |
c3bc7483 | 921 | assert(tb); |
7324f1bf KZ |
922 | |
923 | do { | |
c3bc7483 | 924 | size_t nparts; |
7324f1bf | 925 | |
c3bc7483 KZ |
926 | DBG(PARSE, ul_debug("<---next-line--->")); |
927 | if (next_partno == (size_t) -1) | |
928 | next_partno = fdisk_table_get_nents(tb); | |
929 | ||
3186f4a9 KZ |
930 | if (created) { |
931 | char *partname = fdisk_partname(devname, next_partno + 1); | |
932 | if (!partname) | |
933 | err(EXIT_FAILURE, _("failed to allocate partition name")); | |
934 | printf("%s: ", partname); | |
935 | free(partname); | |
936 | } else | |
937 | printf(">>> "); | |
7324f1bf KZ |
938 | |
939 | rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); | |
e8813494 | 940 | if (rc < 0) { |
c3bc7483 | 941 | DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands")); |
7324f1bf | 942 | buf[sizeof(buf) - 1] = '\0'; |
e8813494 KZ |
943 | rc = loop_control_commands(sf, dp, buf); |
944 | if (rc) | |
7324f1bf | 945 | break; |
7324f1bf | 946 | continue; |
e8813494 KZ |
947 | } else if (rc == 1) { |
948 | rc = SFDISK_DONE_EOF; | |
949 | break; | |
7324f1bf KZ |
950 | } |
951 | ||
952 | nparts = fdisk_table_get_nents(tb); | |
953 | if (nparts) { | |
c3bc7483 | 954 | size_t cur_partno; |
7324f1bf KZ |
955 | struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1); |
956 | ||
c3bc7483 | 957 | assert(pa); |
3186f4a9 KZ |
958 | |
959 | if (!fdisk_partition_get_start(pa) && | |
960 | !fdisk_partition_start_is_default(pa)) { | |
961 | fdisk_info(sf->cxt, _("Ignore partition %zu"), next_partno + 1); | |
962 | continue; | |
963 | } | |
b4df449f | 964 | if (!created) { /* create a new disklabel */ |
7324f1bf KZ |
965 | rc = fdisk_apply_script_headers(sf->cxt, dp); |
966 | created = !rc; | |
967 | } | |
b4df449f | 968 | if (!rc && partno >= 0) { /* -N <partno>, modify partition */ |
0ecf3ab5 | 969 | rc = fdisk_set_partition(sf->cxt, partno, pa); |
b4df449f KZ |
970 | if (rc == 0) |
971 | rc = SFDISK_DONE_ASK; | |
972 | break; | |
973 | } else if (!rc) /* add partition */ | |
c3bc7483 | 974 | rc = fdisk_add_partition(sf->cxt, pa, &cur_partno); |
7324f1bf | 975 | |
c3bc7483 KZ |
976 | if (!rc) { /* success, print reult */ |
977 | sfdisk_print_partition(sf, cur_partno); | |
978 | next_partno = cur_partno + 1; | |
979 | } else if (pa) /* error, drop partition from script */ | |
7324f1bf KZ |
980 | fdisk_table_remove_partition(tb, pa); |
981 | } else | |
3186f4a9 | 982 | fdisk_info(sf->cxt, _("Script header accepted.")); |
7324f1bf | 983 | } while (1); |
254b1dfc | 984 | |
f0edb076 | 985 | if (!sf->quiet && rc != SFDISK_DONE_ABORT) { |
3186f4a9 | 986 | fdisk_info(sf->cxt, _("\nNew situation:")); |
7324f1bf KZ |
987 | list_disklabel(sf->cxt); |
988 | } | |
e8813494 KZ |
989 | |
990 | switch (rc) { | |
991 | case SFDISK_DONE_ASK: | |
992 | if (isatty(STDIN_FILENO)) { | |
993 | int yes = 0; | |
994 | fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes); | |
995 | if (!yes) { | |
f01f2528 | 996 | fdisk_info(sf->cxt, _("Leaving.")); |
e8813494 KZ |
997 | rc = 0; |
998 | break; | |
999 | } | |
1000 | } | |
1001 | case SFDISK_DONE_EOF: | |
1002 | case SFDISK_DONE_WRITE: | |
f01f2528 KZ |
1003 | rc = 0; |
1004 | if (sf->noact) | |
1005 | fdisk_info(sf->cxt, _("The partition table unchanged (--no-act).")); | |
1006 | else { | |
1007 | rc = fdisk_write_disklabel(sf->cxt); | |
1008 | if (!rc) { | |
1009 | fdisk_info(sf->cxt, _("\nThe partition table has been altered.")); | |
1010 | fdisk_reread_partition_table(sf->cxt); | |
1011 | } | |
e8813494 | 1012 | } |
f01f2528 KZ |
1013 | if (!rc) |
1014 | rc = fdisk_deassign_device(sf->cxt, sf->noact); /* no-sync when no-act */ | |
e8813494 KZ |
1015 | break; |
1016 | case SFDISK_DONE_ABORT: | |
b4df449f | 1017 | default: /* rc < 0 on error */ |
f0edb076 | 1018 | fdisk_info(sf->cxt, _("Leaving.\n")); |
e8813494 | 1019 | break; |
3186f4a9 | 1020 | } |
e8813494 | 1021 | |
7324f1bf | 1022 | fdisk_unref_script(dp); |
9c1f9dd3 KZ |
1023 | return rc; |
1024 | } | |
1025 | ||
1881390d KZ |
1026 | static void __attribute__ ((__noreturn__)) usage(FILE *out) |
1027 | { | |
1028 | fputs(USAGE_HEADER, out); | |
fd6b7a7f | 1029 | |
1881390d | 1030 | fprintf(out, |
43a1ccec KZ |
1031 | _(" %1$s [options] <dev> [[-N] <part>]\n" |
1032 | " %1$s [options] <command>\n"), program_invocation_short_name); | |
1033 | ||
1034 | fputs(_("\nCommands:\n"), out); | |
43a1ccec KZ |
1035 | fputs(_(" -a, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out); |
1036 | fputs(_(" -c, --type <dev> <part> [<type>] print or change partition type\n"), out); | |
d420a7f9 KZ |
1037 | fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out); |
1038 | fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out); | |
1039 | fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out); | |
43a1ccec KZ |
1040 | fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out); |
1041 | fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out); | |
1042 | fputs(_(" -V, --verify test whether partitions seem correct\n"), out); | |
1043 | ||
d420a7f9 | 1044 | |
43a1ccec | 1045 | fputs(USAGE_SEPARATOR, out); |
ab02d87e KZ |
1046 | fputs(_(" <dev> device (usually disk) path\n"), out); |
1047 | fputs(_(" <part> partition number\n"), out); | |
1048 | fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out); | |
fd6b7a7f | 1049 | |
1881390d | 1050 | fputs(USAGE_OPTIONS, out); |
ab02d87e KZ |
1051 | fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out); |
1052 | fputs(_(" -f, --force disable all consistency checking\n"), out); | |
1053 | fputs(_(" -O, --backup-file <path> override default backout file name\n"), out); | |
1054 | fputs(_(" -N, --partno <num> specify partition number\n"), out); | |
1055 | fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out); | |
1056 | fputs(_(" -q, --quiet suppress extra info messages\n"), out); | |
1057 | fputs(_(" -n, --no-act do everything except write to device\n"), out); | |
1058 | fputs(_(" --no-reread do not check whether the device is in use\n"), out); | |
35ce145f | 1059 | fputs(USAGE_SEPARATOR, out); |
ab02d87e KZ |
1060 | fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out); |
1061 | fputs(_(" -L, --Linux deprecated, only for backward copatibility\n"), out); | |
058dd97a | 1062 | |
1881390d KZ |
1063 | fputs(USAGE_SEPARATOR, out); |
1064 | fputs(USAGE_HELP, out); | |
1065 | fputs(USAGE_VERSION, out); | |
fd6b7a7f | 1066 | |
1881390d KZ |
1067 | fprintf(out, USAGE_MAN_TAIL("sfdisk(8)")); |
1068 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
fd6b7a7f KZ |
1069 | } |
1070 | ||
9c1f9dd3 | 1071 | |
1881390d KZ |
1072 | int main(int argc, char *argv[]) |
1073 | { | |
8eab3194 | 1074 | int rc = -EINVAL, c, longidx = -1; |
7324f1bf | 1075 | struct sfdisk _sf = { |
7324f1bf KZ |
1076 | .partno = -1 |
1077 | }, *sf = &_sf; | |
1881390d | 1078 | |
8eab3194 KZ |
1079 | enum { |
1080 | OPT_CHANGE_ID = CHAR_MAX + 1, | |
1081 | OPT_PRINT_ID, | |
db48b6a1 KZ |
1082 | OPT_ID, |
1083 | OPT_NOREREAD | |
8eab3194 KZ |
1084 | }; |
1085 | ||
1881390d | 1086 | static const struct option longopts[] = { |
54b13b0c | 1087 | { "activate",no_argument, NULL, 'a' }, |
ab02d87e KZ |
1088 | { "backup", no_argument, NULL, 'b' }, |
1089 | { "backup-file", required_argument, NULL, 'O' }, | |
8a8d204c | 1090 | { "dump", no_argument, NULL, 'd' }, |
1881390d | 1091 | { "help", no_argument, NULL, 'h' }, |
db48b6a1 | 1092 | { "force", no_argument, NULL, 'f' }, |
7324f1bf | 1093 | { "label", required_argument, NULL, 'X' }, |
d2c47697 | 1094 | { "list", no_argument, NULL, 'l' }, |
058dd97a | 1095 | { "list-types", no_argument, NULL, 'T' }, |
f01f2528 | 1096 | { "no-act", no_argument, NULL, 'n' }, |
db48b6a1 | 1097 | { "no-reread", no_argument, NULL, OPT_NOREREAD }, |
d2c47697 KZ |
1098 | { "partno", required_argument, NULL, 'N' }, |
1099 | { "show-size", no_argument, NULL, 's' }, | |
d420a7f9 | 1100 | { "show-geometry", no_argument, NULL, 'g' }, |
f0edb076 | 1101 | { "quiet", no_argument, NULL, 'q' }, |
d2c47697 KZ |
1102 | { "verify", no_argument, NULL, 'V' }, |
1103 | { "version", no_argument, NULL, 'v' }, | |
35ce145f | 1104 | |
d420a7f9 | 1105 | { "unit", required_argument, NULL, 'u' }, /* deprecated */ |
35ce145f | 1106 | { "Linux", no_argument, NULL, 'L' }, /* deprecated */ |
8eab3194 | 1107 | |
43a1ccec | 1108 | { "type",no_argument, NULL, 'c' }, /* wanted */ |
8eab3194 KZ |
1109 | { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */ |
1110 | { "id", no_argument, NULL, OPT_ID }, /* deprecated */ | |
1111 | { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */ | |
1112 | ||
1881390d KZ |
1113 | { NULL, 0, 0, 0 }, |
1114 | }; | |
1115 | ||
1116 | setlocale(LC_ALL, ""); | |
1117 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1118 | textdomain(PACKAGE); | |
1119 | atexit(close_stdout); | |
1120 | ||
ab02d87e | 1121 | while ((c = getopt_long(argc, argv, "adfhglLO:nN:qsTu:vVX:", |
8eab3194 | 1122 | longopts, &longidx)) != -1) { |
1881390d | 1123 | switch(c) { |
54b13b0c KZ |
1124 | case 'a': |
1125 | sf->act = ACT_ACTIVATE; | |
1126 | break; | |
ab02d87e KZ |
1127 | case 'b': |
1128 | sf->backup = 1; | |
1129 | break; | |
8eab3194 KZ |
1130 | case OPT_CHANGE_ID: |
1131 | case OPT_PRINT_ID: | |
1132 | case OPT_ID: | |
43a1ccec | 1133 | warnx(_("%s is deprecated in favour of --type"), |
8eab3194 KZ |
1134 | longopts[longidx].name); |
1135 | /* fallthrough */ | |
1136 | case 'c': | |
1137 | sf->act = ACT_PARTTYPE; | |
1138 | break; | |
db48b6a1 KZ |
1139 | case 'f': |
1140 | sf->force = 1; | |
1141 | break; | |
1881390d KZ |
1142 | case 'h': |
1143 | usage(stdout); | |
1144 | break; | |
9c1f9dd3 KZ |
1145 | case 'l': |
1146 | sf->act = ACT_LIST; | |
1147 | break; | |
35ce145f KZ |
1148 | case 'L': |
1149 | warnx(_("--Linux option is deprecated and unnecessary")); | |
1150 | break; | |
ab02d87e KZ |
1151 | case 'O': |
1152 | sf->backup = 1; | |
1153 | sf->backup_file = optarg; | |
1154 | break; | |
8a8d204c KZ |
1155 | case 'd': |
1156 | sf->act = ACT_DUMP; | |
1157 | break; | |
d420a7f9 KZ |
1158 | case 'g': |
1159 | sf->act = ACT_SHOW_GEOM; | |
1160 | break; | |
f01f2528 KZ |
1161 | case 'n': |
1162 | sf->noact = 1; | |
1163 | break; | |
254b1dfc | 1164 | case 'N': |
b4df449f | 1165 | sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1; |
7324f1bf | 1166 | break; |
f0edb076 KZ |
1167 | case 'q': |
1168 | sf->quiet = 1; | |
1169 | break; | |
7324f1bf KZ |
1170 | case 'X': |
1171 | sf->label = optarg; | |
254b1dfc | 1172 | break; |
d2eb1457 KZ |
1173 | case 's': |
1174 | sf->act = ACT_SHOW_SIZE; | |
1175 | break; | |
058dd97a KZ |
1176 | case 'T': |
1177 | sf->act = ACT_LIST_TYPES; | |
1178 | break; | |
d420a7f9 KZ |
1179 | case 'u': |
1180 | /* deprecated */ | |
1181 | warnx(_("--unit option is deprecated, only sectors are supported")); | |
1182 | if (*optarg != 'S') | |
1183 | errx(EXIT_FAILURE, _("unssupported unit '%c'"), *optarg); | |
1184 | break; | |
1881390d KZ |
1185 | case 'v': |
1186 | printf(_("%s from %s\n"), program_invocation_short_name, | |
1187 | PACKAGE_STRING); | |
1188 | return EXIT_SUCCESS; | |
d2c47697 KZ |
1189 | case 'V': |
1190 | sf->verify = 1; | |
1191 | break; | |
db48b6a1 KZ |
1192 | case OPT_NOREREAD: |
1193 | sf->noreread = 1; | |
1194 | break; | |
7324f1bf KZ |
1195 | default: |
1196 | usage(stderr); | |
37b94458 | 1197 | } |
37b94458 | 1198 | } |
fd6b7a7f | 1199 | |
9c1f9dd3 | 1200 | sfdisk_init(sf); |
22853e4a | 1201 | |
d2c47697 KZ |
1202 | if (sf->verify && !sf->act) |
1203 | sf->act = ACT_VERIFY; /* --verify make be used with --list too */ | |
1204 | else if (!sf->act) | |
1205 | sf->act = ACT_FDISK; /* default */ | |
1206 | ||
9c1f9dd3 | 1207 | switch (sf->act) { |
54b13b0c KZ |
1208 | case ACT_ACTIVATE: |
1209 | rc = command_activate(sf, argc - optind, argv + optind); | |
1210 | break; | |
1211 | ||
9c1f9dd3 KZ |
1212 | case ACT_LIST: |
1213 | rc = command_list_partitions(sf, argc - optind, argv + optind); | |
1214 | break; | |
fd6b7a7f | 1215 | |
058dd97a KZ |
1216 | case ACT_LIST_TYPES: |
1217 | rc = command_list_types(sf); | |
1218 | break; | |
1219 | ||
9c1f9dd3 | 1220 | case ACT_FDISK: |
7324f1bf | 1221 | rc = command_fdisk(sf, argc - optind, argv + optind); |
9c1f9dd3 | 1222 | break; |
8a8d204c KZ |
1223 | |
1224 | case ACT_DUMP: | |
1225 | rc = command_dump(sf, argc - optind, argv + optind); | |
1226 | break; | |
d2eb1457 KZ |
1227 | |
1228 | case ACT_SHOW_SIZE: | |
1229 | rc = command_show_size(sf, argc - optind, argv + optind); | |
1230 | break; | |
d2c47697 | 1231 | |
d420a7f9 KZ |
1232 | case ACT_SHOW_GEOM: |
1233 | rc = command_show_geometry(sf, argc - optind, argv + optind); | |
1234 | break; | |
1235 | ||
d2c47697 KZ |
1236 | case ACT_VERIFY: |
1237 | rc = command_verify(sf, argc - optind, argv + optind); | |
1238 | break; | |
8eab3194 KZ |
1239 | |
1240 | case ACT_PARTTYPE: | |
1241 | rc = command_parttype(sf, argc - optind, argv + optind); | |
1242 | break; | |
9c1f9dd3 | 1243 | } |
fd6b7a7f | 1244 | |
9c1f9dd3 | 1245 | sfdisk_deinit(sf); |
fd6b7a7f | 1246 | |
1881390d KZ |
1247 | DBG(MISC, ul_debug("bye! [rc=%d]", rc)); |
1248 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
4b1cc035 KZ |
1249 | } |
1250 |