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
;
183 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
187 rc
= ul_path_read_s32(pc
, &partno
, "partition");
194 sz
= strlen(partition
);
195 p
= partition
+ sz
- 1;
197 if (!isdigit((unsigned char) *p
))
200 while (isdigit((unsigned char) *(p
- 1))) p
--;
203 partno
= strtol(p
, &end
, 10);
204 if (errno
|| !end
|| *end
|| p
== end
)
209 errx(EXIT_FAILURE
, _("%s: failed to get partition number"), partition
);
212 static int get_max_partno(const char *disk
, dev_t devno
)
214 char path
[PATH_MAX
], *parent
, *dirname
= NULL
;
220 if (!devno
&& !stat(disk
, &st
))
224 parent
= strrchr(disk
, '/');
229 snprintf(path
, sizeof(path
), _PATH_SYS_DEVBLOCK
"/%d:%d/",
230 major(devno
), minor(devno
));
236 dirname
= xstrdup(path
);
238 while ((d
= readdir(dir
))) {
241 if (!strcmp(d
->d_name
, ".") ||
242 !strcmp(d
->d_name
, ".."))
244 #ifdef _DIRENT_HAVE_D_TYPE
245 if (d
->d_type
!= DT_DIR
&& d
->d_type
!= DT_UNKNOWN
)
248 if (strncmp(parent
, d
->d_name
, strlen(parent
)) != 0)
250 snprintf(path
, sizeof(path
), "%s/partition", d
->d_name
);
252 fd
= openat(dirfd(dir
), path
, O_RDONLY
);
255 FILE *f
= fdopen(fd
, "r");
257 if (fscanf(f
, "%d", &x
) == 1 && x
> partno
)
271 static int recount_range_by_pt(blkid_partlist ls
, int *lower
, int *upper
)
273 int n
= 0, i
, nparts
= blkid_partlist_numof_partitions(ls
);
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
);
282 *lower
= n
+ *lower
+ 1;
284 *upper
= n
+ *upper
+ 1;
286 if (*lower
> *upper
&& *upper
!= 0) {
287 warnx(_("specified range <%d:%d> does not make sense"), *lower
, *upper
);
291 printf(_("range recount: max partno=%d, lower=%d, upper=%d\n"), n
, *lower
, *upper
);
295 static void del_parts_warnx(const char *device
, int first
, int last
)
298 warnx(_("%s: error deleting partition %d"), device
, first
);
300 warnx(_("%s: error deleting partitions %d-%d"),
301 device
, first
, last
);
304 static int del_parts(int fd
, const char *device
, dev_t devno
,
305 int lower
, int upper
)
307 int rc
= 0, i
, errfirst
= 0, errlast
= 0;
312 /* recount range by information in /sys */
315 if (!upper
|| lower
< 0 || upper
< 0) {
316 int n
= get_max_partno(device
, devno
);
320 upper
= n
+ upper
+ 1;
322 lower
= n
+ lower
+ 1;
325 warnx(_("specified range <%d:%d> "
326 "does not make sense"), lower
, upper
);
330 for (i
= lower
; i
<= upper
; i
++) {
331 if (partx_del_partition(fd
, i
) == 0) {
333 printf(_("%s: partition #%d removed\n"), device
, i
);
337 if (errno
== ENXIO
) {
339 printf(_("%s: partition #%d doesn't exist\n"), device
, i
);
344 warn(_("%s: deleting partition #%d failed"), device
, i
);
346 errlast
= errfirst
= i
;
347 else if (errlast
+ 1 == i
)
350 del_parts_warnx(device
, errfirst
, errlast
);
351 errlast
= errfirst
= i
;
356 del_parts_warnx(device
, errfirst
, errlast
);
361 static void add_parts_warnx(const char *device
, int first
, int last
)
364 warnx(_("%s: error adding partition %d"), device
, first
);
366 warnx(_("%s: error adding partitions %d-%d"),
367 device
, first
, last
);
370 static int add_parts(int fd
, const char *device
,
371 blkid_partlist ls
, int lower
, int upper
)
373 int i
, nparts
, rc
, errfirst
= 0, errlast
= 0;
379 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
383 nparts
= blkid_partlist_numof_partitions(ls
);
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
;
390 if (lower
&& n
< lower
)
392 if (upper
&& n
> upper
)
395 start
= blkid_partition_get_start(par
);
396 size
= blkid_partition_get_size(par
);
398 if (blkid_partition_is_extended(par
))
400 * Let's follow the Linux kernel and reduce
401 * DOS extended partition to 1 or 2 sectors.
403 size
= min(size
, (uintmax_t) 2);
405 if (partx_add_partition(fd
, n
, start
, size
) == 0) {
407 printf(_("%s: partition #%d added\n"), device
, n
);
412 warn(_("%s: adding partition #%d failed"), device
, n
);
414 errlast
= errfirst
= n
;
415 else if (errlast
+ 1 == n
)
418 add_parts_warnx(device
, errfirst
, errlast
);
419 errlast
= errfirst
= n
;
424 add_parts_warnx(device
, errfirst
, errlast
);
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
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
);
436 if (n
< lower
|| n
> upper
)
437 partx_del_partition(fd
, n
);
444 static void upd_parts_warnx(const char *device
, int first
, int last
)
447 warnx(_("%s: error updating partition %d"), device
, first
);
449 warnx(_("%s: error updating partitions %d-%d"),
450 device
, first
, last
);
453 static int upd_parts(int fd
, const char *device
, dev_t devno
,
454 blkid_partlist ls
, int lower
, int upper
)
456 int n
, nparts
, rc
= 0, errfirst
= 0, errlast
= 0, err
;
458 uintmax_t start
, size
;
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
);
469 if (!upper
|| lower
< 0 || upper
< 0) {
470 n
= get_max_partno(device
, devno
);
472 upper
= n
> nparts
? n
: nparts
;
474 upper
= n
+ upper
+ 1;
476 lower
= n
+ lower
+ 1;
479 warnx(_("specified range <%d:%d> "
480 "does not make sense"), lower
, upper
);
484 for (n
= lower
; n
<= upper
; n
++) {
485 par
= blkid_partlist_get_partition_by_partno(ls
, n
);
488 warn(_("%s: no partition #%d"), device
, n
);
492 start
= blkid_partition_get_start(par
);
493 size
= blkid_partition_get_size(par
);
494 if (blkid_partition_is_extended(par
))
496 * Let's follow the Linux kernel and reduce
497 * DOS extended partition to 1 or 2 sectors.
499 size
= min(size
, (uintmax_t) 2);
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
)
507 err
= partx_resize_partition(fd
, n
, start
, size
);
509 printf(_("%s: partition #%d resized\n"), device
, n
);
513 if (err
== 0 && partx_add_partition(fd
, n
, start
, size
) == 0) {
515 printf(_("%s: partition #%d added\n"), device
, n
);
523 warn(_("%s: updating partition #%d failed"), device
, n
);
525 errlast
= errfirst
= n
;
526 else if (errlast
+ 1 == n
)
529 upd_parts_warnx(device
, errfirst
, errlast
);
530 errlast
= errfirst
= n
;
535 upd_parts_warnx(device
, errfirst
, errlast
);
539 static int list_parts(blkid_partlist ls
, int lower
, int upper
)
545 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
549 nparts
= blkid_partlist_numof_partitions(ls
);
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
;
556 if (lower
&& n
< lower
)
558 if (upper
&& n
> upper
)
561 start
= blkid_partition_get_start(par
);
562 size
= blkid_partition_get_size(par
);
564 printf(P_("#%2d: %9ju-%9ju (%9ju sector, %6ju MB)\n",
565 "#%2d: %9ju-%9ju (%9ju sectors, %6ju MB)\n",
567 n
, start
, start
+ size
-1,
568 size
, (size
<< 9) / 1000000);
573 static int add_scols_line(struct libscols_table
*table
, blkid_partition par
)
575 struct libscols_line
*line
;
581 line
= scols_table_new_line(table
, NULL
);
583 warn(_("failed to allocate output line"));
587 for (i
= 0; (size_t)i
< ncolumns
; i
++) {
588 char *str
= NULL
; /* allocated string */
589 const char *cstr
= NULL
; /* foreign string */
591 switch (get_column_id(i
)) {
593 xasprintf(&str
, "%d", blkid_partition_get_partno(par
));
596 xasprintf(&str
, "%ju", blkid_partition_get_start(par
));
599 xasprintf(&str
, "%ju",
600 blkid_partition_get_start(par
) +
601 blkid_partition_get_size(par
) - 1);
604 xasprintf(&str
, "%ju", blkid_partition_get_size(par
));
607 if (partx_flags
& FL_BYTES
)
608 xasprintf(&str
, "%ju", (uintmax_t)
609 blkid_partition_get_size(par
) << 9);
611 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
,
612 blkid_partition_get_size(par
) << 9);
615 cstr
= blkid_partition_get_name(par
);
618 cstr
= blkid_partition_get_uuid(par
);
621 if (blkid_partition_get_type_string(par
))
622 cstr
= blkid_partition_get_type_string(par
);
624 xasprintf(&str
, "0x%x",
625 blkid_partition_get_type(par
));
628 xasprintf(&str
, "0x%llx", blkid_partition_get_flags(par
));
632 blkid_parttable tab
= blkid_partition_get_table(par
);
634 cstr
= blkid_parttable_get_type(tab
);
642 rc
= scols_line_set_data(line
, i
, cstr
);
644 rc
= scols_line_refer_data(line
, i
, str
);
646 warn(_("failed to add output data"));
654 static int show_parts(blkid_partlist ls
, int scols_flags
, int lower
, int upper
)
657 struct libscols_table
*table
;
662 nparts
= blkid_partlist_numof_partitions(ls
);
667 table
= scols_new_table();
669 warn(_("failed to allocate output table"));
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
));
676 for (i
= 0; (size_t)i
< ncolumns
; i
++) {
677 struct colinfo
*col
= get_column_info(i
);
679 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
)) {
680 warnx(_("failed to allocate output column"));
685 rc
= recount_range_by_pt(ls
, &lower
, &upper
);
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
);
693 if (lower
&& n
< lower
)
695 if (upper
&& n
> upper
)
698 rc
= add_scols_line(table
, par
);
704 scols_print_table(table
);
706 scols_unref_table(table
);
710 static blkid_partlist
get_partlist(blkid_probe pr
,
711 const char *device
, char *type
)
720 char *name
[] = { type
, NULL
};
722 if (blkid_probe_filter_partitions_type(pr
,
723 BLKID_FLTR_ONLYIN
, name
)) {
724 warnx(_("failed to initialize blkid "
725 "filter for '%s'"), type
);
730 ls
= blkid_probe_get_partitions(pr
);
732 warnx(_("%s: failed to read partition table"), device
);
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
));
741 if (!blkid_partlist_numof_partitions(ls
))
742 printf(_("%s: partition table with no partitions"), device
);
748 static void __attribute__((__noreturn__
)) usage(void)
753 fputs(USAGE_HEADER
, out
);
755 _(" %s [-a|-d|-s|-u] [--nr <n:m> | <partition>] <disk>\n"),
756 program_invocation_short_name
);
758 fputs(USAGE_SEPARATOR
, out
);
759 fputs(_("Tell the kernel about the presence and numbering of partitions.\n"), out
);
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
);
778 fputs(USAGE_SEPARATOR
, out
);
779 printf(USAGE_HELP_OPTIONS(22));
781 fputs(USAGE_COLUMNS
, out
);
782 for (i
= 0; i
< NCOLS
; i
++)
783 fprintf(out
, " %10s %s\n", infos
[i
].name
, _(infos
[i
].help
));
785 printf(USAGE_MAN_TAIL("partx(8)"));
790 int main(int argc
, char **argv
)
792 int fd
, c
, what
= ACT_NONE
, lower
= 0, upper
= 0, rc
= 0;
795 char *device
= NULL
; /* pointer to argv[], ie: /dev/sda1 */
796 char *wholedisk
= NULL
; /* allocated, ie: /dev/sda */
798 dev_t disk_devno
= 0, part_devno
= 0;
799 unsigned int sector_size
= 0;
802 OPT_LIST_TYPES
= CHAR_MAX
+ 1,
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' },
827 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
828 { 'P','a','d','l','r','s','u' },
831 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
833 setlocale(LC_ALL
, "");
834 bindtextdomain(PACKAGE
, LOCALEDIR
);
836 close_stdout_atexit();
838 while ((c
= getopt_long(argc
, argv
,
839 "abdglrsuvn:t:o:PS:hV", long_opts
, NULL
)) != -1) {
841 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
848 partx_flags
|= FL_BYTES
;
854 scols_flags
|= PARTX_NOHEADINGS
;
860 if (parse_range(optarg
, &lower
, &upper
, 0))
861 errx(EXIT_FAILURE
, _("failed to parse --nr <M-N> range"));
867 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
868 columns
[ncolumns
] = ncolumns
;
871 scols_flags
|= PARTX_EXPORT
;
875 scols_flags
|= PARTX_RAW
;
882 sector_size
= strtou32_or_err(optarg
, _("invalid sector size argument"));
896 const char *name
= NULL
;
898 while (blkid_partitions_get_name(idx
++, &name
) == 0)
905 print_version(EXIT_SUCCESS
);
907 errtryhelp(EXIT_FAILURE
);
911 if (what
== ACT_NONE
)
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
;
925 if (what
== ACT_SHOW
&& outarg
&&
926 string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
927 &ncolumns
, column_name_to_id
) < 0)
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.
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
940 device
= argv
[optind
];
942 if (strcmp(device
, "-") == 0) {
944 wholedisk
= xstrdup(argv
[optind
+ 1]);
946 device
= argv
[optind
];
947 wholedisk
= xstrdup(argv
[optind
+ 1]);
949 if (device
&& wholedisk
&& !startswith(device
, wholedisk
))
950 errx(EXIT_FAILURE
, _("partition and disk name do not match"));
952 } else if (optind
== argc
- 1) {
953 /* passed only one arg (ie: /dev/sda3 or /dev/sda) */
956 device
= argv
[optind
];
958 if (stat(device
, &sb
))
959 err(EXIT_FAILURE
, _("stat of %s failed"), device
);
961 part_devno
= sb
.st_rdev
;
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
);
969 wholedisk
= xstrdup(device
);
970 disk_devno
= part_devno
;
975 warnx(_("bad usage"));
976 errtryhelp(EXIT_FAILURE
);
978 if (device
&& (upper
|| lower
))
979 errx(EXIT_FAILURE
, _("--nr and <partition> are mutually exclusive"));
984 /* use partno from given partition instead of --nr range, e.g:
987 * partx -d --nr 3 /dev/sda
991 if (!part_devno
&& !stat(device
, &sb
))
992 part_devno
= sb
.st_rdev
;
994 lower
= upper
= get_partno_from_device(device
, part_devno
);
998 printf(_("partition: %s, disk: %s, lower: %d, upper: %d\n"),
999 device
? device
: "none", wholedisk
, lower
, upper
);
1001 if (what
== ACT_ADD
|| what
== ACT_DELETE
) {
1004 if (stat(wholedisk
, &x
))
1005 errx(EXIT_FAILURE
, "%s", wholedisk
);
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"),
1012 if (!loopmod_supports_partscan())
1013 errx(EXIT_FAILURE
, _("%s: partitioned loop devices unsupported"),
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
);
1020 if ((fd
= open(wholedisk
, O_RDONLY
)) == -1)
1021 err(EXIT_FAILURE
, _("cannot open %s"), wholedisk
);
1023 if (what
== ACT_DELETE
)
1024 rc
= del_parts(fd
, wholedisk
, disk_devno
, lower
, upper
);
1026 blkid_probe pr
= blkid_new_probe();
1027 blkid_partlist ls
= NULL
;
1029 if (!pr
|| blkid_probe_set_device(pr
, fd
, 0, 0))
1030 warnx(_("%s: failed to initialize blkid prober"),
1034 blkid_probe_set_sectorsize(pr
, sector_size
);
1036 ls
= get_partlist(pr
, wholedisk
, type
);
1042 rc
= show_parts(ls
, scols_flags
, lower
, upper
);
1045 rc
= list_parts(ls
, lower
, upper
);
1048 rc
= add_parts(fd
, wholedisk
, ls
, lower
, upper
);
1051 rc
= upd_parts(fd
, wholedisk
, disk_devno
, ls
, lower
, upper
);
1061 blkid_free_probe(pr
);
1065 loopcxt_deinit(&lc
);
1067 if (close_fd(fd
) != 0)
1068 err(EXIT_FAILURE
, _("write failed"));
1070 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;