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.]
7 * aeb, 2000-03-21 -- sah is 42 now
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>
24 #include <libsmartcols.h>
27 #include "pathnames.h"
35 #include "closestream.h"
38 /* this is the default upper limit, could be modified by --nr */
39 #define SLICES_MAX 256
41 /* basic table settings */
44 PARTX_NOHEADINGS
= (1 << 1),
45 PARTX_EXPORT
= (1 << 2),
48 /* all the columns (-o option) */
62 #define ACT_ERROR "--{add,delete,show,list,raw,pairs}"
78 const char *name
; /* header */
79 double whint
; /* width hint (N < 1 is in percent of termwidth) */
80 int flags
; /* SCOLS_FL_* */
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)")},
98 #define NCOLS ARRAY_SIZE(infos)
100 /* array with IDs of enabled columns */
101 static int columns
[NCOLS
];
102 static size_t ncolumns
;
105 static int partx_flags
;
106 static struct loopdev_cxt lc
;
109 static void assoc_loopdev(const char *fname
)
113 if (loopcxt_init(&lc
, 0))
114 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
116 rc
= loopcxt_find_unused(&lc
);
118 err(EXIT_FAILURE
, _("%s: failed to find unused loop device"),
122 printf(_("Trying to use '%s' for the loop device\n"),
123 loopcxt_get_device(&lc
));
125 if (loopcxt_set_backing_file(&lc
, fname
))
126 err(EXIT_FAILURE
, _("%s: failed to set backing file"), fname
);
128 rc
= loopcxt_setup_device(&lc
);
131 err(EXIT_FAILURE
, _("%s: failed to set up loop device"), fname
);
136 static inline int get_column_id(int num
)
138 assert(ARRAY_SIZE(columns
) == NCOLS
);
139 assert((size_t)num
< ncolumns
);
140 assert(columns
[num
] < (int) NCOLS
);
144 static inline struct colinfo
*get_column_info(int num
)
146 return &infos
[ get_column_id(num
) ];
149 static int column_name_to_id(const char *name
, size_t namesz
)
155 for (i
= 0; i
< NCOLS
; i
++) {
156 const char *cn
= infos
[i
].name
;
158 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
161 warnx(_("unknown column: %s"), name
);
166 * Given a partition return the corresponding partition number.
168 * Note that this function tries to use sysfs, otherwise it assumes that the
169 * last characters are always numeric (sda1, sdc20, etc).
171 static int get_partno_from_device(char *partition
, dev_t devno
)
175 char *p
, *end
= NULL
;
180 struct sysfs_cxt cxt
;
183 if (sysfs_init(&cxt
, devno
, NULL
))
186 rc
= sysfs_read_int(&cxt
, "partition", &partno
);
193 sz
= strlen(partition
);
194 p
= partition
+ sz
- 1;
196 if (!isdigit((unsigned char) *p
))
199 while (isdigit((unsigned char) *(p
- 1))) p
--;
202 partno
= strtol(p
, &end
, 10);
203 if (errno
|| !end
|| *end
|| p
== end
)
208 errx(EXIT_FAILURE
, _("%s: failed to get partition number"), partition
);
211 static int get_max_partno(const char *disk
, dev_t devno
)
213 char path
[PATH_MAX
], *parent
, *dirname
= NULL
;
219 if (!devno
&& !stat(disk
, &st
))
223 parent
= strrchr(disk
, '/');
228 snprintf(path
, sizeof(path
), _PATH_SYS_DEVBLOCK
"/%d:%d/",
229 major(devno
), minor(devno
));
235 dirname
= xstrdup(path
);
237 while ((d
= readdir(dir
))) {
240 if (!strcmp(d
->d_name
, ".") ||
241 !strcmp(d
->d_name
, ".."))
243 #ifdef _DIRENT_HAVE_D_TYPE
244 if (d
->d_type
!= DT_DIR
&& d
->d_type
!= DT_UNKNOWN
)
247 if (strncmp(parent
, d
->d_name
, strlen(parent
)))
249 snprintf(path
, sizeof(path
), "%s/partition", d
->d_name
);
251 fd
= openat(dirfd(dir
), path
, O_RDONLY
);
254 FILE *f
= fdopen(fd
, "r");
256 if (fscanf(f
, "%d", &x
) == 1 && x
> partno
)
270 static int recount_range_by_pt(blkid_partlist ls
, int *lower
, int *upper
)
272 int n
= 0, i
, nparts
= blkid_partlist_numof_partitions(ls
);
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
);
281 *lower
= n
+ *lower
+ 1;
283 *upper
= n
+ *upper
+ 1;
285 if (*lower
> *upper
&& *upper
!= 0) {
286 warnx(_("specified range <%d:%d> does not make sense"), *lower
, *upper
);
290 printf(_("range recount: max partno=%d, lower=%d, upper=%d\n"), n
, *lower
, *upper
);
294 static void del_parts_warnx(const char *device
, int first
, int last
)
297 warnx(_("%s: error deleting partition %d"), device
, first
);
299 warnx(_("%s: error deleting partitions %d-%d"),
300 device
, first
, last
);
303 static int del_parts(int fd
, const char *device
, dev_t devno
,
304 int lower
, int upper
)
306 int rc
= 0, i
, errfirst
= 0, errlast
= 0;
311 /* recount range by information in /sys */
314 if (!upper
|| lower
< 0 || upper
< 0) {
315 int n
= get_max_partno(device
, devno
);
319 upper
= n
+ upper
+ 1;
321 lower
= n
+ lower
+ 1;
324 warnx(_("specified range <%d:%d> "
325 "does not make sense"), lower
, upper
);
329 for (i
= lower
; i
<= upper
; i
++) {
330 rc
= partx_del_partition(fd
, i
);
333 printf(_("%s: partition #%d removed\n"), device
, i
);
335 } else if (errno
== ENXIO
) {
337 printf(_("%s: partition #%d doesn't exist\n"), device
, i
);
342 warn(_("%s: deleting partition #%d failed"), device
, i
);
344 errlast
= errfirst
= i
;
345 else if (errlast
+ 1 == i
)
348 del_parts_warnx(device
, errfirst
, errlast
);
349 errlast
= errfirst
= i
;
354 del_parts_warnx(device
, errfirst
, errlast
);
359 static void add_parts_warnx(const char *device
, int first
, int last
)
362 warnx(_("%s: error adding partition %d"), device
, first
);
364 warnx(_("%s: error adding partitions %d-%d"),
365 device
, first
, last
);
368 static int add_parts(int fd
, const char *device
,
369 blkid_partlist ls
, int lower
, int upper
)
371 int i
, nparts
, rc
, errfirst
= 0, errlast
= 0;
377 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
381 nparts
= blkid_partlist_numof_partitions(ls
);
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
;
388 if (lower
&& n
< lower
)
390 if (upper
&& n
> upper
)
393 start
= blkid_partition_get_start(par
);
394 size
= blkid_partition_get_size(par
);
396 if (blkid_partition_is_extended(par
))
398 * Let's follow the Linux kernel and reduce
399 * DOS extended partition to 1 or 2 sectors.
401 size
= min(size
, (uintmax_t) 2);
403 if (partx_add_partition(fd
, n
, start
, size
) == 0) {
405 printf(_("%s: partition #%d added\n"), device
, n
);
410 warn(_("%s: adding partition #%d failed"), device
, n
);
412 errlast
= errfirst
= n
;
413 else if (errlast
+ 1 == n
)
416 add_parts_warnx(device
, errfirst
, errlast
);
417 errlast
= errfirst
= n
;
422 add_parts_warnx(device
, errfirst
, errlast
);
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
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
);
434 if (n
< lower
|| n
> upper
)
435 partx_del_partition(fd
, n
);
442 static void upd_parts_warnx(const char *device
, int first
, int last
)
445 warnx(_("%s: error updating partition %d"), device
, first
);
447 warnx(_("%s: error updating partitions %d-%d"),
448 device
, first
, last
);
451 static int upd_parts(int fd
, const char *device
, dev_t devno
,
452 blkid_partlist ls
, int lower
, int upper
)
454 int n
, nparts
, rc
= 0, errfirst
= 0, errlast
= 0, err
;
456 uintmax_t start
, size
;
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
);
467 if (!upper
|| lower
< 0 || upper
< 0) {
468 n
= get_max_partno(device
, devno
);
470 upper
= n
> nparts
? n
: nparts
;
472 upper
= n
+ upper
+ 1;
474 lower
= n
+ lower
+ 1;
477 warnx(_("specified range <%d:%d> "
478 "does not make sense"), lower
, upper
);
482 for (n
= lower
; n
<= upper
; n
++) {
483 par
= blkid_partlist_get_partition_by_partno(ls
, n
);
486 warn(_("%s: no partition #%d"), device
, n
);
490 start
= blkid_partition_get_start(par
);
491 size
= blkid_partition_get_size(par
);
492 if (blkid_partition_is_extended(par
))
494 * Let's follow the Linux kernel and reduce
495 * DOS extended partition to 1 or 2 sectors.
497 size
= min(size
, (uintmax_t) 2);
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
)
505 err
= partx_resize_partition(fd
, n
, start
, size
);
507 printf(_("%s: partition #%d resized\n"), device
, n
);
511 if (err
== 0 && partx_add_partition(fd
, n
, start
, size
) == 0) {
513 printf(_("%s: partition #%d added\n"), device
, n
);
521 warn(_("%s: updating partition #%d failed"), device
, n
);
523 errlast
= errfirst
= n
;
524 else if (errlast
+ 1 == n
)
527 upd_parts_warnx(device
, errfirst
, errlast
);
528 errlast
= errfirst
= n
;
533 upd_parts_warnx(device
, errfirst
, errlast
);
537 static int list_parts(blkid_partlist ls
, int lower
, int upper
)
543 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
547 nparts
= blkid_partlist_numof_partitions(ls
);
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
;
554 if (lower
&& n
< lower
)
556 if (upper
&& n
> upper
)
559 start
= blkid_partition_get_start(par
);
560 size
= blkid_partition_get_size(par
);
562 printf(P_("#%2d: %9ju-%9ju (%9ju sector, %6ju MB)\n",
563 "#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n",
565 n
, start
, start
+ size
-1,
566 size
, (size
<< 9) / 1000000);
571 static int add_scols_line(struct libscols_table
*table
, blkid_partition par
)
573 struct libscols_line
*line
;
579 line
= scols_table_new_line(table
, NULL
);
581 warn(_("failed to allocate output line"));
585 for (i
= 0; (size_t)i
< ncolumns
; i
++) {
586 char *str
= NULL
; /* allocated string */
587 const char *cstr
= NULL
; /* foreign string */
589 switch (get_column_id(i
)) {
591 xasprintf(&str
, "%d", blkid_partition_get_partno(par
));
594 xasprintf(&str
, "%ju", blkid_partition_get_start(par
));
597 xasprintf(&str
, "%ju",
598 blkid_partition_get_start(par
) +
599 blkid_partition_get_size(par
) - 1);
602 xasprintf(&str
, "%ju", blkid_partition_get_size(par
));
605 if (partx_flags
& FL_BYTES
)
606 xasprintf(&str
, "%ju", (uintmax_t)
607 blkid_partition_get_size(par
) << 9);
609 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
,
610 blkid_partition_get_size(par
) << 9);
613 cstr
= blkid_partition_get_name(par
);
616 cstr
= blkid_partition_get_uuid(par
);
619 if (blkid_partition_get_type_string(par
))
620 cstr
= blkid_partition_get_type_string(par
);
622 xasprintf(&str
, "0x%x",
623 blkid_partition_get_type(par
));
626 xasprintf(&str
, "0x%llx", blkid_partition_get_flags(par
));
630 blkid_parttable tab
= blkid_partition_get_table(par
);
632 cstr
= blkid_parttable_get_type(tab
);
640 rc
= scols_line_set_data(line
, i
, cstr
);
642 rc
= scols_line_refer_data(line
, i
, str
);
644 warn(_("failed to add output data"));
652 static int show_parts(blkid_partlist ls
, int scols_flags
, int lower
, int upper
)
655 struct libscols_table
*table
;
660 nparts
= blkid_partlist_numof_partitions(ls
);
665 table
= scols_new_table();
667 warn(_("failed to allocate output table"));
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
));
674 for (i
= 0; (size_t)i
< ncolumns
; i
++) {
675 struct colinfo
*col
= get_column_info(i
);
677 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
)) {
678 warnx(_("failed to allocate output column"));
683 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
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
);
691 if (lower
&& n
< lower
)
693 if (upper
&& n
> upper
)
696 rc
= add_scols_line(table
, par
);
702 scols_print_table(table
);
704 scols_unref_table(table
);
708 static blkid_partlist
get_partlist(blkid_probe pr
,
709 const char *device
, char *type
)
718 char *name
[] = { type
, NULL
};
720 if (blkid_probe_filter_partitions_type(pr
,
721 BLKID_FLTR_ONLYIN
, name
)) {
722 warnx(_("failed to initialize blkid "
723 "filter for '%s'"), type
);
728 ls
= blkid_probe_get_partitions(pr
);
730 warnx(_("%s: failed to read partition table"), device
);
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
));
739 if (!blkid_partlist_numof_partitions(ls
))
740 printf(_("%s: partition table with no partitions"), device
);
746 static void __attribute__((__noreturn__
)) usage(void)
751 fputs(USAGE_HEADER
, out
);
753 _(" %s [-a|-d|-s|-u] [--nr <n:m> | <partition>] <disk>\n"),
754 program_invocation_short_name
);
756 fputs(USAGE_SEPARATOR
, out
);
757 fputs(_("Tell the kernel about the presence and numbering of partitions.\n"), out
);
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
);
776 fputs(USAGE_SEPARATOR
, out
);
777 printf(USAGE_HELP_OPTIONS(22));
779 fputs(USAGE_COLUMNS
, out
);
780 for (i
= 0; i
< NCOLS
; i
++)
781 fprintf(out
, " %10s %s\n", infos
[i
].name
, _(infos
[i
].help
));
783 printf(USAGE_MAN_TAIL("partx(8)"));
788 int main(int argc
, char **argv
)
790 int fd
, c
, what
= ACT_NONE
, lower
= 0, upper
= 0, rc
= 0;
793 char *device
= NULL
; /* pointer to argv[], ie: /dev/sda1 */
794 char *wholedisk
= NULL
; /* allocated, ie: /dev/sda */
796 dev_t disk_devno
= 0, part_devno
= 0;
797 unsigned int sector_size
= 0;
800 OPT_LIST_TYPES
= CHAR_MAX
+ 1,
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' },
825 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
826 { 'P','a','d','l','r','s','u' },
829 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
831 setlocale(LC_ALL
, "");
832 bindtextdomain(PACKAGE
, LOCALEDIR
);
834 atexit(close_stdout
);
836 while ((c
= getopt_long(argc
, argv
,
837 "abdglrsuvn:t:o:PS:hV", long_opts
, NULL
)) != -1) {
839 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
846 partx_flags
|= FL_BYTES
;
852 scols_flags
|= PARTX_NOHEADINGS
;
858 if (parse_range(optarg
, &lower
, &upper
, 0))
859 errx(EXIT_FAILURE
, _("failed to parse --nr <M-N> range"));
865 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
866 columns
[ncolumns
] = ncolumns
;
869 scols_flags
|= PARTX_EXPORT
;
873 scols_flags
|= PARTX_RAW
;
880 sector_size
= strtou32_or_err(optarg
, _("invalid sector size argument"));
894 const char *name
= NULL
;
896 while (blkid_partitions_get_name(idx
++, &name
) == 0)
903 printf(UTIL_LINUX_VERSION
);
906 errtryhelp(EXIT_FAILURE
);
910 if (what
== ACT_NONE
)
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
;
924 if (what
== ACT_SHOW
&& outarg
&&
925 string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
926 &ncolumns
, column_name_to_id
) < 0)
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.
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
939 device
= argv
[optind
];
941 if (strcmp(device
, "-") == 0) {
943 wholedisk
= xstrdup(argv
[optind
+ 1]);
945 device
= argv
[optind
];
946 wholedisk
= xstrdup(argv
[optind
+ 1]);
948 if (device
&& wholedisk
&& !startswith(device
, wholedisk
))
949 errx(EXIT_FAILURE
, _("partition and disk name do not match"));
951 } else if (optind
== argc
- 1) {
952 /* passed only one arg (ie: /dev/sda3 or /dev/sda) */
955 device
= argv
[optind
];
957 if (stat(device
, &sb
))
958 err(EXIT_FAILURE
, _("stat of %s failed"), device
);
960 part_devno
= sb
.st_rdev
;
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
);
968 wholedisk
= xstrdup(device
);
969 disk_devno
= part_devno
;
974 warnx(_("bad usage"));
975 errtryhelp(EXIT_FAILURE
);
977 if (device
&& (upper
|| lower
))
978 errx(EXIT_FAILURE
, _("--nr and <partition> are mutually exclusive"));
983 /* use partno from given partition instead of --nr range, e.g:
986 * partx -d --nr 3 /dev/sda
990 if (!part_devno
&& !stat(device
, &sb
))
991 part_devno
= sb
.st_rdev
;
993 lower
= upper
= get_partno_from_device(device
, part_devno
);
997 printf(_("partition: %s, disk: %s, lower: %d, upper: %d\n"),
998 device
? device
: "none", wholedisk
, lower
, upper
);
1000 if (what
== ACT_ADD
|| what
== ACT_DELETE
) {
1003 if (stat(wholedisk
, &x
))
1004 errx(EXIT_FAILURE
, "%s", wholedisk
);
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"),
1011 if (!loopmod_supports_partscan())
1012 errx(EXIT_FAILURE
, _("%s: partitioned loop devices unsupported"),
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
);
1019 if ((fd
= open(wholedisk
, O_RDONLY
)) == -1)
1020 err(EXIT_FAILURE
, _("cannot open %s"), wholedisk
);
1022 if (what
== ACT_DELETE
)
1023 rc
= del_parts(fd
, wholedisk
, disk_devno
, lower
, upper
);
1025 blkid_probe pr
= blkid_new_probe();
1026 blkid_partlist ls
= NULL
;
1028 if (!pr
|| blkid_probe_set_device(pr
, fd
, 0, 0))
1029 warnx(_("%s: failed to initialize blkid prober"),
1033 blkid_probe_set_sectorsize(pr
, sector_size
);
1035 ls
= get_partlist(pr
, wholedisk
, type
);
1041 rc
= show_parts(ls
, scols_flags
, lower
, upper
);
1044 rc
= list_parts(ls
, lower
, upper
);
1047 rc
= add_parts(fd
, wholedisk
, ls
, lower
, upper
);
1050 rc
= upd_parts(fd
, wholedisk
, disk_devno
, ls
, lower
, upper
);
1060 blkid_free_probe(pr
);
1064 loopcxt_deinit(&lc
);
1066 if (close_fd(fd
) != 0)
1067 err(EXIT_FAILURE
, _("write failed"));
1069 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;