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