]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/partx.c
partx: use new ul_path_* API
[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)))
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 rc = partx_del_partition(fd, i);
332 if (rc == 0) {
333 if (verbose)
334 printf(_("%s: partition #%d removed\n"), device, i);
335 continue;
336 } else if (errno == ENXIO) {
337 if (verbose)
338 printf(_("%s: partition #%d doesn't exist\n"), device, i);
339 continue;
340 }
341 rc = -1;
342 if (verbose)
343 warn(_("%s: deleting partition #%d failed"), device, i);
344 if (!errfirst)
345 errlast = errfirst = i;
346 else if (errlast + 1 == i)
347 errlast++;
348 else {
349 del_parts_warnx(device, errfirst, errlast);
350 errlast = errfirst = i;
351 }
352 }
353
354 if (errfirst)
355 del_parts_warnx(device, errfirst, errlast);
356 return rc;
357 }
358
359
360 static void add_parts_warnx(const char *device, int first, int last)
361 {
362 if (first == last)
363 warnx(_("%s: error adding partition %d"), device, first);
364 else
365 warnx(_("%s: error adding partitions %d-%d"),
366 device, first, last);
367 }
368
369 static int add_parts(int fd, const char *device,
370 blkid_partlist ls, int lower, int upper)
371 {
372 int i, nparts, rc, errfirst = 0, errlast = 0;
373
374 assert(fd >= 0);
375 assert(device);
376 assert(ls);
377
378 rc = recount_range_by_pt(ls, &lower, &upper);
379 if (rc)
380 return rc;
381
382 nparts = blkid_partlist_numof_partitions(ls);
383
384 for (i = 0; i < nparts; i++) {
385 blkid_partition par = blkid_partlist_get_partition(ls, i);
386 int n = blkid_partition_get_partno(par);
387 uintmax_t start, size;
388
389 if (lower && n < lower)
390 continue;
391 if (upper && n > upper)
392 continue;
393
394 start = blkid_partition_get_start(par);
395 size = blkid_partition_get_size(par);
396
397 if (blkid_partition_is_extended(par))
398 /*
399 * Let's follow the Linux kernel and reduce
400 * DOS extended partition to 1 or 2 sectors.
401 */
402 size = min(size, (uintmax_t) 2);
403
404 if (partx_add_partition(fd, n, start, size) == 0) {
405 if (verbose)
406 printf(_("%s: partition #%d added\n"), device, n);
407 continue;
408 }
409 rc = -1;
410 if (verbose)
411 warn(_("%s: adding partition #%d failed"), device, n);
412 if (!errfirst)
413 errlast = errfirst = n;
414 else if (errlast + 1 == n)
415 errlast++;
416 else {
417 add_parts_warnx(device, errfirst, errlast);
418 errlast = errfirst = n;
419 }
420 }
421
422 if (errfirst)
423 add_parts_warnx(device, errfirst, errlast);
424
425 /*
426 * The kernel with enabled partitions scanner for loop devices add *all*
427 * partitions, so we should delete any extra, unwanted ones, when the -n
428 * option is passed.
429 */
430 if (loopdev && loopcxt_is_partscan(&lc) && (lower || upper)) {
431 for (i = 0; i < nparts; i++) {
432 blkid_partition par = blkid_partlist_get_partition(ls, i);
433 int n = blkid_partition_get_partno(par);
434
435 if (n < lower || n > upper)
436 partx_del_partition(fd, n);
437 }
438 }
439
440 return rc;
441 }
442
443 static void upd_parts_warnx(const char *device, int first, int last)
444 {
445 if (first == last)
446 warnx(_("%s: error updating partition %d"), device, first);
447 else
448 warnx(_("%s: error updating partitions %d-%d"),
449 device, first, last);
450 }
451
452 static int upd_parts(int fd, const char *device, dev_t devno,
453 blkid_partlist ls, int lower, int upper)
454 {
455 int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
456 blkid_partition par;
457 uintmax_t start, size;
458
459 assert(fd >= 0);
460 assert(device);
461 assert(ls);
462
463 /* recount range by information in /sys, if on disk number of
464 * partitions is greater than in /sys the use on-disk limit */
465 nparts = blkid_partlist_numof_partitions(ls);
466 if (!lower)
467 lower = 1;
468 if (!upper || lower < 0 || upper < 0) {
469 n = get_max_partno(device, devno);
470 if (!upper)
471 upper = n > nparts ? n : nparts;
472 else if (upper < 0)
473 upper = n + upper + 1;
474 if (lower < 0)
475 lower = n + lower + 1;
476 }
477 if (lower > upper) {
478 warnx(_("specified range <%d:%d> "
479 "does not make sense"), lower, upper);
480 return -1;
481 }
482
483 for (n = lower; n <= upper; n++) {
484 par = blkid_partlist_get_partition_by_partno(ls, n);
485 if (!par) {
486 if (verbose)
487 warn(_("%s: no partition #%d"), device, n);
488 continue;
489 }
490
491 start = blkid_partition_get_start(par);
492 size = blkid_partition_get_size(par);
493 if (blkid_partition_is_extended(par))
494 /*
495 * Let's follow the Linux kernel and reduce
496 * DOS extended partition to 1 or 2 sectors.
497 */
498 size = min(size, (uintmax_t) 2);
499
500 err = partx_del_partition(fd, n);
501 if (err == -1 && errno == ENXIO)
502 err = 0; /* good, it already doesn't exist */
503 if (err == -1 && errno == EBUSY)
504 {
505 /* try to resize */
506 err = partx_resize_partition(fd, n, start, size);
507 if (verbose)
508 printf(_("%s: partition #%d resized\n"), device, n);
509 if (err == 0)
510 continue;
511 }
512 if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
513 if (verbose)
514 printf(_("%s: partition #%d added\n"), device, n);
515 continue;
516 }
517
518 if (err == 0)
519 continue;
520 rc = -1;
521 if (verbose)
522 warn(_("%s: updating partition #%d failed"), device, n);
523 if (!errfirst)
524 errlast = errfirst = n;
525 else if (errlast + 1 == n)
526 errlast++;
527 else {
528 upd_parts_warnx(device, errfirst, errlast);
529 errlast = errfirst = n;
530 }
531 }
532
533 if (errfirst)
534 upd_parts_warnx(device, errfirst, errlast);
535 return rc;
536 }
537
538 static int list_parts(blkid_partlist ls, int lower, int upper)
539 {
540 int i, nparts, rc;
541
542 assert(ls);
543
544 rc = recount_range_by_pt(ls, &lower, &upper);
545 if (rc)
546 return rc;
547
548 nparts = blkid_partlist_numof_partitions(ls);
549
550 for (i = 0; i < nparts; i++) {
551 blkid_partition par = blkid_partlist_get_partition(ls, i);
552 int n = blkid_partition_get_partno(par);
553 uintmax_t start, size;
554
555 if (lower && n < lower)
556 continue;
557 if (upper && n > upper)
558 continue;
559
560 start = blkid_partition_get_start(par);
561 size = blkid_partition_get_size(par);
562
563 printf(P_("#%2d: %9ju-%9ju (%9ju sector, %6ju MB)\n",
564 "#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n",
565 size),
566 n, start, start + size -1,
567 size, (size << 9) / 1000000);
568 }
569 return 0;
570 }
571
572 static int add_scols_line(struct libscols_table *table, blkid_partition par)
573 {
574 struct libscols_line *line;
575 int i, rc = 0;
576
577 assert(table);
578 assert(par);
579
580 line = scols_table_new_line(table, NULL);
581 if (!line) {
582 warn(_("failed to allocate output line"));
583 return -ENOMEM;
584 }
585
586 for (i = 0; (size_t)i < ncolumns; i++) {
587 char *str = NULL; /* allocated string */
588 const char *cstr = NULL; /* foreign string */
589
590 switch (get_column_id(i)) {
591 case COL_PARTNO:
592 xasprintf(&str, "%d", blkid_partition_get_partno(par));
593 break;
594 case COL_START:
595 xasprintf(&str, "%ju", blkid_partition_get_start(par));
596 break;
597 case COL_END:
598 xasprintf(&str, "%ju",
599 blkid_partition_get_start(par) +
600 blkid_partition_get_size(par) - 1);
601 break;
602 case COL_SECTORS:
603 xasprintf(&str, "%ju", blkid_partition_get_size(par));
604 break;
605 case COL_SIZE:
606 if (partx_flags & FL_BYTES)
607 xasprintf(&str, "%ju", (uintmax_t)
608 blkid_partition_get_size(par) << 9);
609 else
610 str = size_to_human_string(SIZE_SUFFIX_1LETTER,
611 blkid_partition_get_size(par) << 9);
612 break;
613 case COL_NAME:
614 cstr = blkid_partition_get_name(par);
615 break;
616 case COL_UUID:
617 cstr = blkid_partition_get_uuid(par);
618 break;
619 case COL_TYPE:
620 if (blkid_partition_get_type_string(par))
621 cstr = blkid_partition_get_type_string(par);
622 else
623 xasprintf(&str, "0x%x",
624 blkid_partition_get_type(par));
625 break;
626 case COL_FLAGS:
627 xasprintf(&str, "0x%llx", blkid_partition_get_flags(par));
628 break;
629 case COL_SCHEME:
630 {
631 blkid_parttable tab = blkid_partition_get_table(par);
632 if (tab)
633 cstr = blkid_parttable_get_type(tab);
634 break;
635 }
636 default:
637 break;
638 }
639
640 if (cstr)
641 rc = scols_line_set_data(line, i, cstr);
642 else if (str)
643 rc = scols_line_refer_data(line, i, str);
644 if (rc) {
645 warn(_("failed to add output data"));
646 break;
647 }
648 }
649
650 return rc;
651 }
652
653 static int show_parts(blkid_partlist ls, int scols_flags, int lower, int upper)
654 {
655 int i, rc = -1;
656 struct libscols_table *table;
657 int nparts;
658
659 assert(ls);
660
661 nparts = blkid_partlist_numof_partitions(ls);
662 if (!nparts)
663 return 0;
664
665 scols_init_debug(0);
666 table = scols_new_table();
667 if (!table) {
668 warn(_("failed to allocate output table"));
669 return -1;
670 }
671 scols_table_enable_raw(table, !!(scols_flags & PARTX_RAW));
672 scols_table_enable_export(table, !!(scols_flags & PARTX_EXPORT));
673 scols_table_enable_noheadings(table, !!(scols_flags & PARTX_NOHEADINGS));
674
675 for (i = 0; (size_t)i < ncolumns; i++) {
676 struct colinfo *col = get_column_info(i);
677
678 if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
679 warnx(_("failed to allocate output column"));
680 goto done;
681 }
682 }
683
684 rc = recount_range_by_pt(ls, &lower, &upper);
685 if (rc)
686 goto done;
687
688 for (i = 0; i < nparts; i++) {
689 blkid_partition par = blkid_partlist_get_partition(ls, i);
690 int n = blkid_partition_get_partno(par);
691
692 if (lower && n < lower)
693 continue;
694 if (upper && n > upper)
695 continue;
696
697 rc = add_scols_line(table, par);
698 if (rc)
699 break;
700 }
701
702 rc = 0;
703 scols_print_table(table);
704 done:
705 scols_unref_table(table);
706 return rc;
707 }
708
709 static blkid_partlist get_partlist(blkid_probe pr,
710 const char *device, char *type)
711 {
712 blkid_partlist ls;
713 blkid_parttable tab;
714
715 assert(pr);
716 assert(device);
717
718 if (type) {
719 char *name[] = { type, NULL };
720
721 if (blkid_probe_filter_partitions_type(pr,
722 BLKID_FLTR_ONLYIN, name)) {
723 warnx(_("failed to initialize blkid "
724 "filter for '%s'"), type);
725 return NULL;
726 }
727 }
728
729 ls = blkid_probe_get_partitions(pr);
730 if (!ls) {
731 warnx(_("%s: failed to read partition table"), device);
732 return NULL;
733 }
734
735 tab = blkid_partlist_get_table(ls);
736 if (verbose && tab) {
737 printf(_("%s: partition table type '%s' detected\n"),
738 device, blkid_parttable_get_type(tab));
739
740 if (!blkid_partlist_numof_partitions(ls))
741 printf(_("%s: partition table with no partitions"), device);
742 }
743
744 return ls;
745 }
746
747 static void __attribute__((__noreturn__)) usage(void)
748 {
749 FILE *out = stdout;
750 size_t i;
751
752 fputs(USAGE_HEADER, out);
753 fprintf(out,
754 _(" %s [-a|-d|-s|-u] [--nr <n:m> | <partition>] <disk>\n"),
755 program_invocation_short_name);
756
757 fputs(USAGE_SEPARATOR, out);
758 fputs(_("Tell the kernel about the presence and numbering of partitions.\n"), out);
759
760 fputs(USAGE_OPTIONS, out);
761 fputs(_(" -a, --add add specified partitions or all of them\n"), out);
762 fputs(_(" -d, --delete delete specified partitions or all of them\n"), out);
763 fputs(_(" -u, --update update specified partitions or all of them\n"), out);
764 fputs(_(" -s, --show list partitions\n\n"), out);
765 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
766 fputs(_(" -g, --noheadings don't print headings for --show\n"), out);
767 fputs(_(" -n, --nr <n:m> specify the range of partitions (e.g. --nr 2:4)\n"), out);
768 fputs(_(" -o, --output <list> define which output columns to use\n"), out);
769 fputs(_(" --output-all output all columns\n"), out);
770 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
771 fputs(_(" -r, --raw use raw output format\n"), out);
772 fputs(_(" -S, --sector-size <num> overwrite sector size\n"), out);
773 fputs(_(" -t, --type <type> specify the partition type\n"), out);
774 fputs(_(" --list-types list supported partition types and exit\n"), out);
775 fputs(_(" -v, --verbose verbose mode\n"), out);
776
777 fputs(USAGE_SEPARATOR, out);
778 printf(USAGE_HELP_OPTIONS(22));
779
780 fputs(USAGE_COLUMNS, out);
781 for (i = 0; i < NCOLS; i++)
782 fprintf(out, " %10s %s\n", infos[i].name, _(infos[i].help));
783
784 printf(USAGE_MAN_TAIL("partx(8)"));
785
786 exit(EXIT_SUCCESS);
787 }
788
789 int main(int argc, char **argv)
790 {
791 int fd, c, what = ACT_NONE, lower = 0, upper = 0, rc = 0;
792 int scols_flags = 0;
793 char *type = NULL;
794 char *device = NULL; /* pointer to argv[], ie: /dev/sda1 */
795 char *wholedisk = NULL; /* allocated, ie: /dev/sda */
796 char *outarg = NULL;
797 dev_t disk_devno = 0, part_devno = 0;
798 unsigned int sector_size = 0;
799
800 enum {
801 OPT_LIST_TYPES = CHAR_MAX + 1,
802 OPT_OUTPUT_ALL
803 };
804 static const struct option long_opts[] = {
805 { "bytes", no_argument, NULL, 'b' },
806 { "noheadings", no_argument, NULL, 'g' },
807 { "raw", no_argument, NULL, 'r' },
808 { "list", no_argument, NULL, 'l' },
809 { "show", no_argument, NULL, 's' },
810 { "add", no_argument, NULL, 'a' },
811 { "delete", no_argument, NULL, 'd' },
812 { "update", no_argument, NULL, 'u' },
813 { "type", required_argument, NULL, 't' },
814 { "list-types", no_argument, NULL, OPT_LIST_TYPES },
815 { "nr", required_argument, NULL, 'n' },
816 { "output", required_argument, NULL, 'o' },
817 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
818 { "pairs", no_argument, NULL, 'P' },
819 { "sector-size",required_argument, NULL, 'S' },
820 { "help", no_argument, NULL, 'h' },
821 { "version", no_argument, NULL, 'V' },
822 { "verbose", no_argument, NULL, 'v' },
823 { NULL, 0, NULL, 0 }
824 };
825
826 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
827 { 'P','a','d','l','r','s','u' },
828 { 0 }
829 };
830 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
831
832 setlocale(LC_ALL, "");
833 bindtextdomain(PACKAGE, LOCALEDIR);
834 textdomain(PACKAGE);
835 atexit(close_stdout);
836
837 while ((c = getopt_long(argc, argv,
838 "abdglrsuvn:t:o:PS:hV", long_opts, NULL)) != -1) {
839
840 err_exclusive_options(c, long_opts, excl, excl_st);
841
842 switch(c) {
843 case 'a':
844 what = ACT_ADD;
845 break;
846 case 'b':
847 partx_flags |= FL_BYTES;
848 break;
849 case 'd':
850 what = ACT_DELETE;
851 break;
852 case 'g':
853 scols_flags |= PARTX_NOHEADINGS;
854 break;
855 case 'l':
856 what = ACT_LIST;
857 break;
858 case 'n':
859 if (parse_range(optarg, &lower, &upper, 0))
860 errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
861 break;
862 case 'o':
863 outarg = optarg;
864 break;
865 case OPT_OUTPUT_ALL:
866 for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++)
867 columns[ncolumns] = ncolumns;
868 break;
869 case 'P':
870 scols_flags |= PARTX_EXPORT;
871 what = ACT_SHOW;
872 break;
873 case 'r':
874 scols_flags |= PARTX_RAW;
875 what = ACT_SHOW;
876 break;
877 case 's':
878 what = ACT_SHOW;
879 break;
880 case 'S':
881 sector_size = strtou32_or_err(optarg, _("invalid sector size argument"));
882 break;
883 case 't':
884 type = optarg;
885 break;
886 case 'u':
887 what = ACT_UPD;
888 break;
889 case 'v':
890 verbose = 1;
891 break;
892 case OPT_LIST_TYPES:
893 {
894 size_t idx = 0;
895 const char *name = NULL;
896
897 while (blkid_partitions_get_name(idx++, &name) == 0)
898 puts(name);
899 return EXIT_SUCCESS;
900 }
901 case 'h':
902 usage();
903 case 'V':
904 printf(UTIL_LINUX_VERSION);
905 return 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 wholedisk = xstrdup(lc.device);
1017 } else if (!S_ISBLK(x.st_mode))
1018 errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk);
1019 }
1020 if ((fd = open(wholedisk, O_RDONLY)) == -1)
1021 err(EXIT_FAILURE, _("cannot open %s"), wholedisk);
1022
1023 if (what == ACT_DELETE)
1024 rc = del_parts(fd, wholedisk, disk_devno, lower, upper);
1025 else {
1026 blkid_probe pr = blkid_new_probe();
1027 blkid_partlist ls = NULL;
1028
1029 if (!pr || blkid_probe_set_device(pr, fd, 0, 0))
1030 warnx(_("%s: failed to initialize blkid prober"),
1031 wholedisk);
1032 else {
1033 if (sector_size)
1034 blkid_probe_set_sectorsize(pr, sector_size);
1035
1036 ls = get_partlist(pr, wholedisk, type);
1037 }
1038
1039 if (ls) {
1040 switch (what) {
1041 case ACT_SHOW:
1042 rc = show_parts(ls, scols_flags, lower, upper);
1043 break;
1044 case ACT_LIST:
1045 rc = list_parts(ls, lower, upper);
1046 break;
1047 case ACT_ADD:
1048 rc = add_parts(fd, wholedisk, ls, lower, upper);
1049 break;
1050 case ACT_UPD:
1051 rc = upd_parts(fd, wholedisk, disk_devno, ls, lower, upper);
1052 break;
1053 case ACT_NONE:
1054 break;
1055 default:
1056 abort();
1057 }
1058 } else
1059 rc = 1;
1060
1061 blkid_free_probe(pr);
1062 }
1063
1064 if (loopdev)
1065 loopcxt_deinit(&lc);
1066
1067 if (close_fd(fd) != 0)
1068 err(EXIT_FAILURE, _("write failed"));
1069
1070 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
1071 }