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