]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/partx.c
partx: exit with error code when partition read failed
[thirdparty/util-linux.git] / disk-utils / partx.c
1 /*
2 * partx: tell the kernel about your disk's partitions
3 * [This is not an fdisk - adding and removing partitions
4 * is not a change of the disk, but just telling the kernel
5 * about presence and numbering of on-disk partitions.]
6 *
7 * aeb, 2000-03-21 -- sah is 42 now
8 *
9 * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
10 * Rewritten to use libblkid for util-linux
11 * based on ideas from Karel Zak <kzak@redhat.com>
12 */
13
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <getopt.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <dirent.h>
22
23 #include <blkid.h>
24 #include <libsmartcols.h>
25
26 #include "c.h"
27 #include "pathnames.h"
28 #include "nls.h"
29 #include "blkdev.h"
30 #include "strutils.h"
31 #include "xalloc.h"
32 #include "partx.h"
33 #include "sysfs.h"
34 #include "loopdev.h"
35 #include "closestream.h"
36 #include "optutils.h"
37
38 /* this is the default upper limit, could be modified by --nr */
39 #define SLICES_MAX 256
40
41 /* basic table settings */
42 enum {
43 PARTX_RAW = (1 << 0),
44 PARTX_NOHEADINGS = (1 << 1),
45 PARTX_EXPORT = (1 << 2),
46 };
47
48 /* all the columns (-o option) */
49 enum {
50 COL_PARTNO,
51 COL_START,
52 COL_END,
53 COL_SECTORS,
54 COL_SIZE,
55 COL_NAME,
56 COL_UUID,
57 COL_TYPE,
58 COL_FLAGS,
59 COL_SCHEME,
60 };
61
62 #define ACT_ERROR "--{add,delete,show,list,raw,pairs}"
63 enum {
64 ACT_NONE,
65 ACT_LIST,
66 ACT_SHOW,
67 ACT_ADD,
68 ACT_UPD,
69 ACT_DELETE
70 };
71
72 enum {
73 FL_BYTES = (1 << 1)
74 };
75
76 /* column names */
77 struct colinfo {
78 const char *name; /* header */
79 double whint; /* width hint (N < 1 is in percent of termwidth) */
80 int flags; /* SCOLS_FL_* */
81 const char *help;
82 };
83
84 /* columns descriptions */
85 static struct colinfo infos[] = {
86 [COL_PARTNO] = { "NR", 0.25, SCOLS_FL_RIGHT, N_("partition number") },
87 [COL_START] = { "START", 0.30, SCOLS_FL_RIGHT, N_("start of the partition in sectors") },
88 [COL_END] = { "END", 0.30, SCOLS_FL_RIGHT, N_("end of the partition in sectors") },
89 [COL_SECTORS] = { "SECTORS", 0.30, SCOLS_FL_RIGHT, N_("number of sectors") },
90 [COL_SIZE] = { "SIZE", 0.30, SCOLS_FL_RIGHT, N_("human readable size") },
91 [COL_NAME] = { "NAME", 0.30, SCOLS_FL_TRUNC, N_("partition name") },
92 [COL_UUID] = { "UUID", 36, 0, N_("partition UUID")},
93 [COL_SCHEME] = { "SCHEME", 0.1, SCOLS_FL_TRUNC, N_("partition table type (dos, gpt, ...)")},
94 [COL_FLAGS] = { "FLAGS", 0.1, SCOLS_FL_TRUNC, N_("partition flags")},
95 [COL_TYPE] = { "TYPE", 1, SCOLS_FL_RIGHT, N_("partition type (a string, a UUID, or hex)")},
96 };
97
98 #define NCOLS ARRAY_SIZE(infos)
99
100 /* array with IDs of enabled columns */
101 static int columns[NCOLS];
102 static size_t ncolumns;
103
104 static int verbose;
105 static int partx_flags;
106 static struct loopdev_cxt lc;
107 static int loopdev;
108
109 static void assoc_loopdev(const char *fname)
110 {
111 int rc;
112
113 if (loopcxt_init(&lc, 0))
114 err(EXIT_FAILURE, _("failed to initialize loopcxt"));
115
116 rc = loopcxt_find_unused(&lc);
117 if (rc)
118 err(EXIT_FAILURE, _("%s: failed to find unused loop device"),
119 fname);
120
121 if (verbose)
122 printf(_("Trying to use '%s' for the loop device\n"),
123 loopcxt_get_device(&lc));
124
125 if (loopcxt_set_backing_file(&lc, fname))
126 err(EXIT_FAILURE, _("%s: failed to set backing file"), fname);
127
128 rc = loopcxt_setup_device(&lc);
129
130 if (rc == -EBUSY)
131 err(EXIT_FAILURE, _("%s: failed to set up loop device"), fname);
132
133 loopdev = 1;
134 }
135
136 static inline int get_column_id(int num)
137 {
138 assert(ARRAY_SIZE(columns) == NCOLS);
139 assert((size_t)num < ncolumns);
140 assert(columns[num] < (int) NCOLS);
141 return columns[num];
142 }
143
144 static inline struct colinfo *get_column_info(int num)
145 {
146 return &infos[ get_column_id(num) ];
147 }
148
149 static int column_name_to_id(const char *name, size_t namesz)
150 {
151 size_t i;
152
153 assert(name);
154
155 for (i = 0; i < NCOLS; i++) {
156 const char *cn = infos[i].name;
157
158 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
159 return i;
160 }
161 warnx(_("unknown column: %s"), name);
162 return -1;
163 }
164
165 /*
166 * Given a partition return the corresponding partition number.
167 *
168 * Note that this function tries to use sysfs, otherwise it assumes that the
169 * last characters are always numeric (sda1, sdc20, etc).
170 */
171 static int get_partno_from_device(char *partition, dev_t devno)
172 {
173 int partno = 0;
174 size_t sz;
175 char *p, *end = NULL;
176
177 assert(partition);
178
179 if (devno) {
180 struct sysfs_cxt cxt;
181 int rc;
182
183 if (sysfs_init(&cxt, devno, NULL))
184 goto err;
185
186 rc = sysfs_read_int(&cxt, "partition", &partno);
187 sysfs_deinit(&cxt);
188
189 if (rc == 0)
190 return partno;
191 }
192
193 sz = strlen(partition);
194 p = partition + sz - 1;
195
196 if (!isdigit((unsigned char) *p))
197 goto err;
198
199 while (isdigit((unsigned char) *(p - 1))) p--;
200
201 errno = 0;
202 partno = strtol(p, &end, 10);
203 if (errno || !end || *end || p == end)
204 goto err;
205
206 return partno;
207 err:
208 errx(EXIT_FAILURE, _("%s: failed to get partition number"), partition);
209 }
210
211 static int get_max_partno(const char *disk, dev_t devno)
212 {
213 char path[PATH_MAX], *parent, *dirname = NULL;
214 struct stat st;
215 DIR *dir;
216 struct dirent *d;
217 int partno = 0;
218
219 if (!devno && !stat(disk, &st))
220 devno = st.st_rdev;
221 if (!devno)
222 goto dflt;
223 parent = strrchr(disk, '/');
224 if (!parent)
225 goto dflt;
226 parent++;
227
228 snprintf(path, sizeof(path), _PATH_SYS_DEVBLOCK "/%d:%d/",
229 major(devno), minor(devno));
230
231 dir = opendir(path);
232 if (!dir)
233 goto dflt;
234
235 dirname = xstrdup(path);
236
237 while ((d = readdir(dir))) {
238 int fd;
239
240 if (!strcmp(d->d_name, ".") ||
241 !strcmp(d->d_name, ".."))
242 continue;
243 #ifdef _DIRENT_HAVE_D_TYPE
244 if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN)
245 continue;
246 #endif
247 if (strncmp(parent, d->d_name, strlen(parent)))
248 continue;
249 snprintf(path, sizeof(path), "%s/partition", d->d_name);
250
251 fd = openat(dirfd(dir), path, O_RDONLY);
252 if (fd) {
253 int x = 0;
254 FILE *f = fdopen(fd, "r");
255 if (f) {
256 if (fscanf(f, "%d", &x) == 1 && x > partno)
257 partno = x;
258 fclose(f);
259 }
260 }
261 }
262
263 free(dirname);
264 closedir(dir);
265 return partno;
266 dflt:
267 return SLICES_MAX;
268 }
269
270 static int recount_range_by_pt(blkid_partlist ls, int *lower, int *upper)
271 {
272 int n = 0, i, nparts = blkid_partlist_numof_partitions(ls);
273
274 for (i = 0; i < nparts; i++) {
275 blkid_partition par = blkid_partlist_get_partition(ls, i);
276 int partno = blkid_partition_get_partno(par);
277 n = max(partno, n);
278 }
279
280 if (*lower < 0)
281 *lower = n + *lower + 1;
282 if (*upper < 0)
283 *upper = n + *upper + 1;
284
285 if (*lower > *upper && *upper != 0) {
286 warnx(_("specified range <%d:%d> does not make sense"), *lower, *upper);
287 return -EINVAL;
288 }
289 if (verbose)
290 printf(_("range recount: max partno=%d, lower=%d, upper=%d\n"), n, *lower, *upper);
291 return 0;
292 }
293
294 static void del_parts_warnx(const char *device, int first, int last)
295 {
296 if (first == last)
297 warnx(_("%s: error deleting partition %d"), device, first);
298 else
299 warnx(_("%s: error deleting partitions %d-%d"),
300 device, first, last);
301 }
302
303 static int del_parts(int fd, const char *device, dev_t devno,
304 int lower, int upper)
305 {
306 int rc = 0, i, errfirst = 0, errlast = 0;
307
308 assert(fd >= 0);
309 assert(device);
310
311 /* recount range by information in /sys */
312 if (!lower)
313 lower = 1;
314 if (!upper || lower < 0 || upper < 0) {
315 int n = get_max_partno(device, devno);
316 if (!upper)
317 upper = n;
318 else if (upper < 0)
319 upper = n + upper + 1;
320 if (lower < 0)
321 lower = n + lower + 1;
322 }
323 if (lower > upper) {
324 warnx(_("specified range <%d:%d> "
325 "does not make sense"), lower, upper);
326 return -1;
327 }
328
329 for (i = lower; i <= upper; i++) {
330 rc = partx_del_partition(fd, i);
331 if (rc == 0) {
332 if (verbose)
333 printf(_("%s: partition #%d removed\n"), device, i);
334 continue;
335 } else if (errno == ENXIO) {
336 if (verbose)
337 printf(_("%s: partition #%d doesn't exist\n"), device, i);
338 continue;
339 }
340 rc = -1;
341 if (verbose)
342 warn(_("%s: deleting partition #%d failed"), device, i);
343 if (!errfirst)
344 errlast = errfirst = i;
345 else if (errlast + 1 == i)
346 errlast++;
347 else {
348 del_parts_warnx(device, errfirst, errlast);
349 errlast = errfirst = i;
350 }
351 }
352
353 if (errfirst)
354 del_parts_warnx(device, errfirst, errlast);
355 return rc;
356 }
357
358
359 static void add_parts_warnx(const char *device, int first, int last)
360 {
361 if (first == last)
362 warnx(_("%s: error adding partition %d"), device, first);
363 else
364 warnx(_("%s: error adding partitions %d-%d"),
365 device, first, last);
366 }
367
368 static int add_parts(int fd, const char *device,
369 blkid_partlist ls, int lower, int upper)
370 {
371 int i, nparts, rc, errfirst = 0, errlast = 0;
372
373 assert(fd >= 0);
374 assert(device);
375 assert(ls);
376
377 rc = recount_range_by_pt(ls, &lower, &upper);
378 if (rc)
379 return rc;
380
381 nparts = blkid_partlist_numof_partitions(ls);
382
383 for (i = 0; i < nparts; i++) {
384 blkid_partition par = blkid_partlist_get_partition(ls, i);
385 int n = blkid_partition_get_partno(par);
386 uintmax_t start, size;
387
388 if (lower && n < lower)
389 continue;
390 if (upper && n > upper)
391 continue;
392
393 start = blkid_partition_get_start(par);
394 size = blkid_partition_get_size(par);
395
396 if (blkid_partition_is_extended(par))
397 /*
398 * Let's follow the Linux kernel and reduce
399 * DOS extended partition to 1 or 2 sectors.
400 */
401 size = min(size, (uintmax_t) 2);
402
403 if (partx_add_partition(fd, n, start, size) == 0) {
404 if (verbose)
405 printf(_("%s: partition #%d added\n"), device, n);
406 continue;
407 }
408 rc = -1;
409 if (verbose)
410 warn(_("%s: adding partition #%d failed"), device, n);
411 if (!errfirst)
412 errlast = errfirst = n;
413 else if (errlast + 1 == n)
414 errlast++;
415 else {
416 add_parts_warnx(device, errfirst, errlast);
417 errlast = errfirst = n;
418 }
419 }
420
421 if (errfirst)
422 add_parts_warnx(device, errfirst, errlast);
423
424 /*
425 * The kernel with enabled partitions scanner for loop devices add *all*
426 * partitions, so we should delete any extra, unwanted ones, when the -n
427 * option is passed.
428 */
429 if (loopdev && loopcxt_is_partscan(&lc) && (lower || upper)) {
430 for (i = 0; i < nparts; i++) {
431 blkid_partition par = blkid_partlist_get_partition(ls, i);
432 int n = blkid_partition_get_partno(par);
433
434 if (n < lower || n > upper)
435 partx_del_partition(fd, n);
436 }
437 }
438
439 return rc;
440 }
441
442 static void upd_parts_warnx(const char *device, int first, int last)
443 {
444 if (first == last)
445 warnx(_("%s: error updating partition %d"), device, first);
446 else
447 warnx(_("%s: error updating partitions %d-%d"),
448 device, first, last);
449 }
450
451 static int upd_parts(int fd, const char *device, dev_t devno,
452 blkid_partlist ls, int lower, int upper)
453 {
454 int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
455 blkid_partition par;
456 uintmax_t start, size;
457
458 assert(fd >= 0);
459 assert(device);
460 assert(ls);
461
462 /* recount range by information in /sys, if on disk number of
463 * partitions is greater than in /sys the use on-disk limit */
464 nparts = blkid_partlist_numof_partitions(ls);
465 if (!lower)
466 lower = 1;
467 if (!upper || lower < 0 || upper < 0) {
468 n = get_max_partno(device, devno);
469 if (!upper)
470 upper = n > nparts ? n : nparts;
471 else if (upper < 0)
472 upper = n + upper + 1;
473 if (lower < 0)
474 lower = n + lower + 1;
475 }
476 if (lower > upper) {
477 warnx(_("specified range <%d:%d> "
478 "does not make sense"), lower, upper);
479 return -1;
480 }
481
482 for (n = lower; n <= upper; n++) {
483 par = blkid_partlist_get_partition_by_partno(ls, n);
484 if (!par) {
485 if (verbose)
486 warn(_("%s: no partition #%d"), device, n);
487 continue;
488 }
489
490 start = blkid_partition_get_start(par);
491 size = blkid_partition_get_size(par);
492 if (blkid_partition_is_extended(par))
493 /*
494 * Let's follow the Linux kernel and reduce
495 * DOS extended partition to 1 or 2 sectors.
496 */
497 size = min(size, (uintmax_t) 2);
498
499 err = partx_del_partition(fd, n);
500 if (err == -1 && errno == ENXIO)
501 err = 0; /* good, it already doesn't exist */
502 if (err == -1 && errno == EBUSY)
503 {
504 /* try to resize */
505 err = partx_resize_partition(fd, n, start, size);
506 if (verbose)
507 printf(_("%s: partition #%d resized\n"), device, n);
508 if (err == 0)
509 continue;
510 }
511 if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
512 if (verbose)
513 printf(_("%s: partition #%d added\n"), device, n);
514 continue;
515 }
516
517 if (err == 0)
518 continue;
519 rc = -1;
520 if (verbose)
521 warn(_("%s: updating partition #%d failed"), device, n);
522 if (!errfirst)
523 errlast = errfirst = n;
524 else if (errlast + 1 == n)
525 errlast++;
526 else {
527 upd_parts_warnx(device, errfirst, errlast);
528 errlast = errfirst = n;
529 }
530 }
531
532 if (errfirst)
533 upd_parts_warnx(device, errfirst, errlast);
534 return rc;
535 }
536
537 static int list_parts(blkid_partlist ls, int lower, int upper)
538 {
539 int i, nparts, rc;
540
541 assert(ls);
542
543 rc = recount_range_by_pt(ls, &lower, &upper);
544 if (rc)
545 return rc;
546
547 nparts = blkid_partlist_numof_partitions(ls);
548
549 for (i = 0; i < nparts; i++) {
550 blkid_partition par = blkid_partlist_get_partition(ls, i);
551 int n = blkid_partition_get_partno(par);
552 uintmax_t start, size;
553
554 if (lower && n < lower)
555 continue;
556 if (upper && n > upper)
557 continue;
558
559 start = blkid_partition_get_start(par);
560 size = blkid_partition_get_size(par);
561
562 printf(P_("#%2d: %9ju-%9ju (%9ju sector, %6ju MB)\n",
563 "#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n",
564 size),
565 n, start, start + size -1,
566 size, (size << 9) / 1000000);
567 }
568 return 0;
569 }
570
571 static int add_scols_line(struct libscols_table *table, blkid_partition par)
572 {
573 struct libscols_line *line;
574 int i, rc = 0;
575
576 assert(table);
577 assert(par);
578
579 line = scols_table_new_line(table, NULL);
580 if (!line) {
581 warn(_("failed to allocate output line"));
582 return -ENOMEM;
583 }
584
585 for (i = 0; (size_t)i < ncolumns; i++) {
586 char *str = NULL; /* allocated string */
587 const char *cstr = NULL; /* foreign string */
588
589 switch (get_column_id(i)) {
590 case COL_PARTNO:
591 xasprintf(&str, "%d", blkid_partition_get_partno(par));
592 break;
593 case COL_START:
594 xasprintf(&str, "%ju", blkid_partition_get_start(par));
595 break;
596 case COL_END:
597 xasprintf(&str, "%ju",
598 blkid_partition_get_start(par) +
599 blkid_partition_get_size(par) - 1);
600 break;
601 case COL_SECTORS:
602 xasprintf(&str, "%ju", blkid_partition_get_size(par));
603 break;
604 case COL_SIZE:
605 if (partx_flags & FL_BYTES)
606 xasprintf(&str, "%ju", (uintmax_t)
607 blkid_partition_get_size(par) << 9);
608 else
609 str = size_to_human_string(SIZE_SUFFIX_1LETTER,
610 blkid_partition_get_size(par) << 9);
611 break;
612 case COL_NAME:
613 cstr = blkid_partition_get_name(par);
614 break;
615 case COL_UUID:
616 cstr = blkid_partition_get_uuid(par);
617 break;
618 case COL_TYPE:
619 if (blkid_partition_get_type_string(par))
620 cstr = blkid_partition_get_type_string(par);
621 else
622 xasprintf(&str, "0x%x",
623 blkid_partition_get_type(par));
624 break;
625 case COL_FLAGS:
626 xasprintf(&str, "0x%llx", blkid_partition_get_flags(par));
627 break;
628 case COL_SCHEME:
629 {
630 blkid_parttable tab = blkid_partition_get_table(par);
631 if (tab)
632 cstr = blkid_parttable_get_type(tab);
633 break;
634 }
635 default:
636 break;
637 }
638
639 if (cstr)
640 rc = scols_line_set_data(line, i, cstr);
641 else if (str)
642 rc = scols_line_refer_data(line, i, str);
643 if (rc) {
644 warn(_("failed to add output data"));
645 break;
646 }
647 }
648
649 return rc;
650 }
651
652 static int show_parts(blkid_partlist ls, int scols_flags, int lower, int upper)
653 {
654 int i, rc = -1;
655 struct libscols_table *table;
656 int nparts;
657
658 assert(ls);
659
660 nparts = blkid_partlist_numof_partitions(ls);
661 if (!nparts)
662 return 0;
663
664 scols_init_debug(0);
665 table = scols_new_table();
666 if (!table) {
667 warn(_("failed to allocate output table"));
668 return -1;
669 }
670 scols_table_enable_raw(table, !!(scols_flags & PARTX_RAW));
671 scols_table_enable_export(table, !!(scols_flags & PARTX_EXPORT));
672 scols_table_enable_noheadings(table, !!(scols_flags & PARTX_NOHEADINGS));
673
674 for (i = 0; (size_t)i < ncolumns; i++) {
675 struct colinfo *col = get_column_info(i);
676
677 if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
678 warnx(_("failed to allocate output column"));
679 goto done;
680 }
681 }
682
683 rc = recount_range_by_pt(ls, &lower, &upper);
684 if (rc)
685 goto done;
686
687 for (i = 0; i < nparts; i++) {
688 blkid_partition par = blkid_partlist_get_partition(ls, i);
689 int n = blkid_partition_get_partno(par);
690
691 if (lower && n < lower)
692 continue;
693 if (upper && n > upper)
694 continue;
695
696 rc = add_scols_line(table, par);
697 if (rc)
698 break;
699 }
700
701 rc = 0;
702 scols_print_table(table);
703 done:
704 scols_unref_table(table);
705 return rc;
706 }
707
708 static blkid_partlist get_partlist(blkid_probe pr,
709 const char *device, char *type)
710 {
711 blkid_partlist ls;
712 blkid_parttable tab;
713
714 assert(pr);
715 assert(device);
716
717 if (type) {
718 char *name[] = { type, NULL };
719
720 if (blkid_probe_filter_partitions_type(pr,
721 BLKID_FLTR_ONLYIN, name)) {
722 warnx(_("failed to initialize blkid "
723 "filter for '%s'"), type);
724 return NULL;
725 }
726 }
727
728 ls = blkid_probe_get_partitions(pr);
729 if (!ls) {
730 warnx(_("%s: failed to read partition table"), device);
731 return NULL;
732 }
733
734 tab = blkid_partlist_get_table(ls);
735 if (verbose && tab) {
736 printf(_("%s: partition table type '%s' detected\n"),
737 device, blkid_parttable_get_type(tab));
738
739 if (!blkid_partlist_numof_partitions(ls))
740 printf(_("%s: partition table with no partitions"), device);
741 }
742
743 return ls;
744 }
745
746 static void __attribute__((__noreturn__)) usage(void)
747 {
748 FILE *out = stdout;
749 size_t i;
750
751 fputs(USAGE_HEADER, out);
752 fprintf(out,
753 _(" %s [-a|-d|-s|-u] [--nr <n:m> | <partition>] <disk>\n"),
754 program_invocation_short_name);
755
756 fputs(USAGE_SEPARATOR, out);
757 fputs(_("Tell the kernel about the presence and numbering of partitions.\n"), out);
758
759 fputs(USAGE_OPTIONS, out);
760 fputs(_(" -a, --add add specified partitions or all of them\n"), out);
761 fputs(_(" -d, --delete delete specified partitions or all of them\n"), out);
762 fputs(_(" -u, --update update specified partitions or all of them\n"), out);
763 fputs(_(" -s, --show list partitions\n\n"), out);
764 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
765 fputs(_(" -g, --noheadings don't print headings for --show\n"), out);
766 fputs(_(" -n, --nr <n:m> specify the range of partitions (e.g. --nr 2:4)\n"), out);
767 fputs(_(" -o, --output <list> define which output columns to use\n"), out);
768 fputs(_(" --output-all output all columns\n"), out);
769 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
770 fputs(_(" -r, --raw use raw output format\n"), out);
771 fputs(_(" -S, --sector-size <num> overwrite sector size\n"), out);
772 fputs(_(" -t, --type <type> specify the partition type\n"), out);
773 fputs(_(" --list-types list supported partition types and exit\n"), out);
774 fputs(_(" -v, --verbose verbose mode\n"), out);
775
776 fputs(USAGE_SEPARATOR, out);
777 printf(USAGE_HELP_OPTIONS(22));
778
779 fputs(USAGE_COLUMNS, out);
780 for (i = 0; i < NCOLS; i++)
781 fprintf(out, " %10s %s\n", infos[i].name, _(infos[i].help));
782
783 printf(USAGE_MAN_TAIL("partx(8)"));
784
785 exit(EXIT_SUCCESS);
786 }
787
788 int main(int argc, char **argv)
789 {
790 int fd, c, what = ACT_NONE, lower = 0, upper = 0, rc = 0;
791 int scols_flags = 0;
792 char *type = NULL;
793 char *device = NULL; /* pointer to argv[], ie: /dev/sda1 */
794 char *wholedisk = NULL; /* allocated, ie: /dev/sda */
795 char *outarg = NULL;
796 dev_t disk_devno = 0, part_devno = 0;
797 unsigned int sector_size = 0;
798
799 enum {
800 OPT_LIST_TYPES = CHAR_MAX + 1,
801 OPT_OUTPUT_ALL
802 };
803 static const struct option long_opts[] = {
804 { "bytes", no_argument, NULL, 'b' },
805 { "noheadings", no_argument, NULL, 'g' },
806 { "raw", no_argument, NULL, 'r' },
807 { "list", no_argument, NULL, 'l' },
808 { "show", no_argument, NULL, 's' },
809 { "add", no_argument, NULL, 'a' },
810 { "delete", no_argument, NULL, 'd' },
811 { "update", no_argument, NULL, 'u' },
812 { "type", required_argument, NULL, 't' },
813 { "list-types", no_argument, NULL, OPT_LIST_TYPES },
814 { "nr", required_argument, NULL, 'n' },
815 { "output", required_argument, NULL, 'o' },
816 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
817 { "pairs", no_argument, NULL, 'P' },
818 { "sector-size",required_argument, NULL, 'S' },
819 { "help", no_argument, NULL, 'h' },
820 { "version", no_argument, NULL, 'V' },
821 { "verbose", no_argument, NULL, 'v' },
822 { NULL, 0, NULL, 0 }
823 };
824
825 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
826 { 'P','a','d','l','r','s','u' },
827 { 0 }
828 };
829 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
830
831 setlocale(LC_ALL, "");
832 bindtextdomain(PACKAGE, LOCALEDIR);
833 textdomain(PACKAGE);
834 atexit(close_stdout);
835
836 while ((c = getopt_long(argc, argv,
837 "abdglrsuvn:t:o:PS:hV", long_opts, NULL)) != -1) {
838
839 err_exclusive_options(c, long_opts, excl, excl_st);
840
841 switch(c) {
842 case 'a':
843 what = ACT_ADD;
844 break;
845 case 'b':
846 partx_flags |= FL_BYTES;
847 break;
848 case 'd':
849 what = ACT_DELETE;
850 break;
851 case 'g':
852 scols_flags |= PARTX_NOHEADINGS;
853 break;
854 case 'l':
855 what = ACT_LIST;
856 break;
857 case 'n':
858 if (parse_range(optarg, &lower, &upper, 0))
859 errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
860 break;
861 case 'o':
862 outarg = optarg;
863 break;
864 case OPT_OUTPUT_ALL:
865 for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++)
866 columns[ncolumns] = ncolumns;
867 break;
868 case 'P':
869 scols_flags |= PARTX_EXPORT;
870 what = ACT_SHOW;
871 break;
872 case 'r':
873 scols_flags |= PARTX_RAW;
874 what = ACT_SHOW;
875 break;
876 case 's':
877 what = ACT_SHOW;
878 break;
879 case 'S':
880 sector_size = strtou32_or_err(optarg, _("invalid sector size argument"));
881 break;
882 case 't':
883 type = optarg;
884 break;
885 case 'u':
886 what = ACT_UPD;
887 break;
888 case 'v':
889 verbose = 1;
890 break;
891 case OPT_LIST_TYPES:
892 {
893 size_t idx = 0;
894 const char *name = NULL;
895
896 while (blkid_partitions_get_name(idx++, &name) == 0)
897 puts(name);
898 return EXIT_SUCCESS;
899 }
900 case 'h':
901 usage();
902 case 'V':
903 printf(UTIL_LINUX_VERSION);
904 return EXIT_SUCCESS;
905 default:
906 errtryhelp(EXIT_FAILURE);
907 }
908 }
909
910 if (what == ACT_NONE)
911 what = ACT_SHOW;
912
913 /* --show default, could by modified by -o */
914 if (what == ACT_SHOW && !ncolumns) {
915 columns[ncolumns++] = COL_PARTNO;
916 columns[ncolumns++] = COL_START;
917 columns[ncolumns++] = COL_END;
918 columns[ncolumns++] = COL_SECTORS;
919 columns[ncolumns++] = COL_SIZE;
920 columns[ncolumns++] = COL_NAME;
921 columns[ncolumns++] = COL_UUID;
922 }
923
924 if (what == ACT_SHOW && outarg &&
925 string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
926 &ncolumns, column_name_to_id) < 0)
927 return EXIT_FAILURE;
928
929 /*
930 * Note that 'partx /dev/sda1' == 'partx /dev/sda1 /dev/sda'
931 * so assume that the device and/or disk are always the last
932 * arguments to be passed to partx.
933 */
934 if (optind == argc - 2) {
935 /* passed 2 arguments:
936 * /dev/sda1 /dev/sda : partition + whole-disk
937 * -- /dev/sda1 : partition that should be used as a whole-disk
938 */
939 device = argv[optind];
940
941 if (strcmp(device, "-") == 0) {
942 device = NULL;
943 wholedisk = xstrdup(argv[optind + 1]);
944 } else {
945 device = argv[optind];
946 wholedisk = xstrdup(argv[optind + 1]);
947
948 if (device && wholedisk && !startswith(device, wholedisk))
949 errx(EXIT_FAILURE, _("partition and disk name do not match"));
950 }
951 } else if (optind == argc - 1) {
952 /* passed only one arg (ie: /dev/sda3 or /dev/sda) */
953 struct stat sb;
954
955 device = argv[optind];
956
957 if (stat(device, &sb))
958 err(EXIT_FAILURE, _("stat of %s failed"), device);
959
960 part_devno = sb.st_rdev;
961
962 if (blkid_devno_to_wholedisk(part_devno,
963 NULL, 0, &disk_devno) == 0 &&
964 part_devno != disk_devno)
965 wholedisk = blkid_devno_to_devname(disk_devno);
966
967 if (!wholedisk) {
968 wholedisk = xstrdup(device);
969 disk_devno = part_devno;
970 device = NULL;
971 part_devno = 0;
972 }
973 } else {
974 warnx(_("bad usage"));
975 errtryhelp(EXIT_FAILURE);
976 }
977 if (device && (upper || lower))
978 errx(EXIT_FAILURE, _("--nr and <partition> are mutually exclusive"));
979
980 assert(wholedisk);
981
982 if (device) {
983 /* use partno from given partition instead of --nr range, e.g:
984 * partx -d /dev/sda3
985 * is the same like:
986 * partx -d --nr 3 /dev/sda
987 */
988 struct stat sb;
989
990 if (!part_devno && !stat(device, &sb))
991 part_devno = sb.st_rdev;
992
993 lower = upper = get_partno_from_device(device, part_devno);
994 }
995
996 if (verbose)
997 printf(_("partition: %s, disk: %s, lower: %d, upper: %d\n"),
998 device ? device : "none", wholedisk, lower, upper);
999
1000 if (what == ACT_ADD || what == ACT_DELETE) {
1001 struct stat x;
1002
1003 if (stat(wholedisk, &x))
1004 errx(EXIT_FAILURE, "%s", wholedisk);
1005
1006 if (S_ISREG(x.st_mode)) {
1007 /* not a blkdev, try to associate it to a loop device */
1008 if (what == ACT_DELETE)
1009 errx(EXIT_FAILURE, _("%s: cannot delete partitions"),
1010 wholedisk);
1011 if (!loopmod_supports_partscan())
1012 errx(EXIT_FAILURE, _("%s: partitioned loop devices unsupported"),
1013 wholedisk);
1014 assoc_loopdev(wholedisk);
1015 wholedisk = xstrdup(lc.device);
1016 } else if (!S_ISBLK(x.st_mode))
1017 errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk);
1018 }
1019 if ((fd = open(wholedisk, O_RDONLY)) == -1)
1020 err(EXIT_FAILURE, _("cannot open %s"), wholedisk);
1021
1022 if (what == ACT_DELETE)
1023 rc = del_parts(fd, wholedisk, disk_devno, lower, upper);
1024 else {
1025 blkid_probe pr = blkid_new_probe();
1026 blkid_partlist ls = NULL;
1027
1028 if (!pr || blkid_probe_set_device(pr, fd, 0, 0))
1029 warnx(_("%s: failed to initialize blkid prober"),
1030 wholedisk);
1031 else {
1032 if (sector_size)
1033 blkid_probe_set_sectorsize(pr, sector_size);
1034
1035 ls = get_partlist(pr, wholedisk, type);
1036 }
1037
1038 if (ls) {
1039 switch (what) {
1040 case ACT_SHOW:
1041 rc = show_parts(ls, scols_flags, lower, upper);
1042 break;
1043 case ACT_LIST:
1044 rc = list_parts(ls, lower, upper);
1045 break;
1046 case ACT_ADD:
1047 rc = add_parts(fd, wholedisk, ls, lower, upper);
1048 break;
1049 case ACT_UPD:
1050 rc = upd_parts(fd, wholedisk, disk_devno, ls, lower, upper);
1051 break;
1052 case ACT_NONE:
1053 break;
1054 default:
1055 abort();
1056 }
1057 } else
1058 rc = 1;
1059
1060 blkid_free_probe(pr);
1061 }
1062
1063 if (loopdev)
1064 loopcxt_deinit(&lc);
1065
1066 if (close_fd(fd) != 0)
1067 err(EXIT_FAILURE, _("write failed"));
1068
1069 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
1070 }