2 * Source: copy of util-linux' partx partx.c
4 * Copyrights of the original file applies
5 * Copyright (c) 2004, 2005 Christophe Varoqui
6 * Copyright (c) 2005 Kiyoshi Ueda
7 * Copyright (c) 2005 Lars Soltau
11 * Given a block device and a partition table type,
12 * try to parse the partition table, and list the
13 * contents. Optionally add or remove partitions.
15 * Read wholedisk and add all partitions:
16 * kpartx [-a|-d|-l] [-v] wholedisk
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
33 #include <libdevmapper.h>
35 #include "devmapper.h"
40 #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
42 #define READ_SIZE 1024
45 #define DM_TARGET "linear"
46 #define LO_NAME_SIZE 64
47 #define PARTNAME_SIZE 128
50 struct slice slices[MAXSLICES];
52 enum action { LIST, ADD, DELETE, UPDATE };
63 addpts(char *t, ptreader f)
65 if (ptct >= MAXTYPES) {
66 fprintf(stderr, "addpts: too many types\n");
77 addpts("gpt", read_gpt_pt);
78 addpts("dos", read_dos_pt);
79 addpts("bsd", read_bsd_pt);
80 addpts("solaris", read_solaris_pt);
81 addpts("unixware", read_unixware_pt);
82 addpts("dasd", read_dasd_pt);
83 addpts("mac", read_mac_pt);
84 addpts("sun", read_sun_pt);
85 addpts("ps3", read_ps3_pt);
88 static char short_opts[] = "rladfgvp:t:su";
97 printf("usage : kpartx [-a|-d|-l] [-f] [-v] wholedisk\n");
98 printf("\t-a add partition devmappings\n");
99 printf("\t-r devmappings will be readonly\n");
100 printf("\t-d del partition devmappings\n");
101 printf("\t-u update partition devmappings\n");
102 printf("\t-l list partitions devmappings that would be added by -a\n");
103 printf("\t-p set device name-partition number delimiter\n");
104 printf("\t-g force GUID partition table (GPT)\n");
105 printf("\t-f force devmap create\n");
106 printf("\t-v verbose\n");
107 printf("\t-s sync mode. Don't return until the partitions are created\n");
112 set_delimiter (char * device, char * delimiter)
116 while (*(p++) != 0x0)
119 if (isdigit(*(p - 2)))
124 strip_slash (char * device)
128 while (*(p++) != 0x0) {
136 find_devname_offset (char * device)
146 return (int)(q - device) + 1;
150 get_hotplug_device(void)
152 unsigned int major, minor, off, len;
154 char *devname = NULL;
159 var = getenv("ACTION");
161 if (!var || strcmp(var, "add"))
164 /* Get dm mapname for hotpluged device. */
165 if (!(devname = getenv("DEVNAME")))
168 if (stat(devname, &buf))
171 major = major(buf.st_rdev);
172 minor = minor(buf.st_rdev);
174 if (!(mapname = dm_mapname(major, minor))) /* Not dm device. */
177 off = find_devname_offset(devname);
178 len = strlen(mapname);
180 /* Dirname + mapname + \0 */
181 if (!(device = (char *)malloc(sizeof(char) * (off + len + 1)))) {
186 /* Create new device name. */
187 snprintf(device, off + 1, "%s", devname);
188 snprintf(device + off, len + 1, "%s", mapname);
190 if (strlen(device) != (off + len)) {
200 check_uuid(char *uuid, char *part_uuid, char **err_msg) {
201 char *map_uuid = strchr(part_uuid, '-');
202 if (!map_uuid || strncmp(part_uuid, "part", 4) != 0) {
203 *err_msg = "not a kpartx partition";
207 if (strcmp(uuid, map_uuid) != 0) {
208 *err_msg = "a partition of a different device";
215 main(int argc, char **argv){
216 int i, j, m, n, op, off, arg, c, d, ro=0;
220 enum action what = LIST;
221 char *type, *diskdevice, *device, *progname;
223 char partname[PARTNAME_SIZE], params[PARTNAME_SIZE + 16];
224 char * loopdev = NULL;
227 char *mapname = NULL;
235 type = device = diskdevice = NULL;
236 memset(&all, 0, sizeof(all));
237 memset(&partname, 0, sizeof(partname));
239 /* Check whether hotplug mode. */
240 progname = strrchr(argv[0], '/');
247 if (!strcmp(progname, "kpartx.dev")) { /* Hotplug mode */
250 /* Setup for original kpartx variables */
251 if (!(device = get_hotplug_device()))
256 } else if (argc < 2) {
261 while ((arg = getopt(argc, argv, short_opts)) != EOF)
301 #ifdef LIBDM_API_COOKIE
303 dm_udev_set_sync_support(0);
305 dm_udev_set_sync_support(1);
308 if (dm_prereq(DM_TARGET, 0, 0, 0) && (what == ADD || what == DELETE || what == UPDATE)) {
309 fprintf(stderr, "device mapper prerequisites not met\n");
314 /* already got [disk]device */
315 } else if (optind == argc-2) {
316 device = argv[optind];
317 diskdevice = argv[optind+1];
318 } else if (optind == argc-1) {
319 diskdevice = device = argv[optind];
325 if (stat(device, &buf)) {
326 printf("failed to stat() %s\n", device);
330 if (S_ISREG (buf.st_mode)) {
331 /* already looped file ? */
332 loopdev = find_loop_by_file(device);
334 if (!loopdev && what == DELETE)
338 loopdev = find_unused_loop_device();
340 if (set_loop(loopdev, device, 0, &ro)) {
341 fprintf(stderr, "can't set up loop\n");
348 if (stat(device, &buf)) {
349 printf("failed to stat() %s\n", device);
354 off = find_devname_offset(device);
357 uuid = dm_mapuuid(major(buf.st_rdev), minor(buf.st_rdev));
358 mapname = dm_mapname(major(buf.st_rdev), minor(buf.st_rdev));
365 mapname = device + off;
366 else if (!force_devmap &&
367 dm_no_partitions(major(buf.st_rdev), minor(buf.st_rdev))) {
368 /* Feature 'no_partitions' is set, return */
373 delim = malloc(DELIM_SIZE);
374 memset(delim, 0, DELIM_SIZE);
375 set_delimiter(mapname, delim);
378 fd = open(device, O_RDONLY);
385 /* add/remove partitions to the kernel devmapper tables */
387 for (i = 0; i < ptct; i++) {
390 if (type && strcmp(type, ptp->type))
393 /* here we get partitions */
394 n = ptp->fn(fd, all, slices, SIZE(slices));
398 printf("%s: %d slices\n", ptp->type, n);
410 for (j = 0, c = 0, m = 0; j < n; j++) {
411 if (slices[j].size == 0)
413 if (slices[j].container > 0) {
418 slices[j].minor = m++;
420 printf("%s%s%d : 0 %" PRIu64 " %s %" PRIu64"\n",
422 slices[j].size, device,
425 /* Loop to resolve contained slices */
428 for (j = 0; j < n; j++) {
430 int k = slices[j].container - 1;
432 if (slices[j].size == 0)
434 if (slices[j].minor > 0)
436 if (slices[j].container == 0)
438 slices[j].minor = m++;
440 start = slices[j].start - slices[k].start;
441 printf("%s%s%d : 0 %" PRIu64 " /dev/dm-%d %" PRIu64 "\n",
444 slices[k].minor, start);
447 /* Terminate loop if nothing more to resolve */
455 for (j = MAXSLICES-1; j >= 0; j--) {
456 char *part_uuid, *reason;
458 if (safe_sprintf(partname, "%s%s%d",
459 mapname, delim, j+1)) {
460 fprintf(stderr, "partname too small\n");
463 strip_slash(partname);
465 if (!dm_map_present(partname, &part_uuid))
468 if (part_uuid && uuid) {
469 if (check_uuid(uuid, part_uuid, &reason) != 0) {
470 fprintf(stderr, "%s is %s. Not removing\n", partname, reason);
477 if (!dm_simplecmd(DM_DEVICE_REMOVE, partname,
483 printf("del devmap : %s\n", partname);
487 if (del_loop(loopdev)) {
489 printf("can't del loop : %s\n",
493 printf("loop deleted : %s\n", loopdev);
499 /* ADD and UPDATE share the same code that adds new partitions. */
500 for (j = 0, c = 0; j < n; j++) {
501 char *part_uuid, *reason;
503 if (slices[j].size == 0)
506 /* Skip all contained slices */
507 if (slices[j].container > 0) {
512 if (safe_sprintf(partname, "%s%s%d",
513 mapname, delim, j+1)) {
514 fprintf(stderr, "partname too small\n");
517 strip_slash(partname);
519 if (safe_sprintf(params, "%d:%d %" PRIu64 ,
520 major(buf.st_rdev), minor(buf.st_rdev), slices[j].start)) {
521 fprintf(stderr, "params too small\n");
525 op = (dm_map_present(partname, &part_uuid) ?
526 DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
528 if (part_uuid && uuid) {
529 if (check_uuid(uuid, part_uuid, &reason) != 0) {
530 fprintf(stderr, "%s is already in use, and %s\n", partname, reason);
538 if (!dm_addmap(op, partname, DM_TARGET, params,
539 slices[j].size, ro, uuid, j+1,
540 buf.st_mode & 0777, buf.st_uid,
542 fprintf(stderr, "create/reload failed on %s\n",
547 if (op == DM_DEVICE_RELOAD &&
548 !dm_simplecmd(DM_DEVICE_RESUME, partname,
549 1, MPATH_UDEV_RELOAD_FLAG)) {
550 fprintf(stderr, "resume failed on %s\n",
556 dm_devn(partname, &slices[j].major,
560 printf("add map %s (%d:%d): 0 %" PRIu64 " %s %s\n",
561 partname, slices[j].major,
562 slices[j].minor, slices[j].size,
565 /* Loop to resolve contained slices */
568 for (j = 0; j < n; j++) {
569 char *part_uuid, *reason;
570 int k = slices[j].container - 1;
572 if (slices[j].size == 0)
575 /* Skip all existing slices */
576 if (slices[j].minor > 0)
579 /* Skip all simple slices */
580 if (slices[j].container == 0)
583 /* Check container slice */
584 if (slices[k].size == 0)
585 fprintf(stderr, "Invalid slice %d\n",
588 if (safe_sprintf(partname, "%s%s%d",
589 mapname, delim, j+1)) {
590 fprintf(stderr, "partname too small\n");
593 strip_slash(partname);
595 if (safe_sprintf(params, "%d:%d %" PRIu64,
596 major(buf.st_rdev), minor(buf.st_rdev),
598 fprintf(stderr, "params too small\n");
602 op = (dm_map_present(partname,
604 DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
606 if (part_uuid && uuid) {
607 if (check_uuid(uuid, part_uuid, &reason) != 0) {
608 fprintf(stderr, "%s is already in use, and %s\n", partname, reason);
615 dm_addmap(op, partname, DM_TARGET, params,
616 slices[j].size, ro, uuid, j+1,
618 buf.st_uid, buf.st_gid);
620 if (op == DM_DEVICE_RELOAD)
621 dm_simplecmd(DM_DEVICE_RESUME,
623 MPATH_UDEV_RELOAD_FLAG);
624 dm_devn(partname, &slices[j].major,
628 printf("add map %s (%d:%d): 0 %" PRIu64 " %s %s\n",
629 partname, slices[j].major, slices[j].minor, slices[j].size,
639 /* Skip code that removes devmappings for deleted partitions */
643 for (j = MAXSLICES-1; j >= 0; j--) {
644 char *part_uuid, *reason;
645 if (safe_sprintf(partname, "%s%s%d",
646 mapname, delim, j+1)) {
647 fprintf(stderr, "partname too small\n");
650 strip_slash(partname);
652 if (slices[j].size ||
653 !dm_map_present(partname, &part_uuid))
656 if (part_uuid && uuid) {
657 if (check_uuid(uuid, part_uuid, &reason) != 0) {
658 fprintf(stderr, "%s is %s. Not removing\n", partname, reason);
665 if (!dm_simplecmd(DM_DEVICE_REMOVE,
671 printf("del devmap : %s\n", partname);
681 if (what == LIST && loopcreated && S_ISREG (buf.st_mode)) {
684 if (del_loop(device)) {
686 printf("can't del loop : %s\n",
690 printf("loop deleted : %s\n", device);
700 xmalloc (size_t size) {
709 fprintf(stderr, "Out of memory\n");
717 * sseek: seek to specified sector
721 sseek(int fd, unsigned int secnr) {
723 in = ((off64_t) secnr << 9);
726 if ((out = lseek64(fd, in, SEEK_SET)) != in)
728 fprintf(stderr, "llseek error\n");
742 getblock (int fd, unsigned int secnr) {
745 for (bp = blockhead; bp; bp = bp->next)
747 if (bp->secnr == secnr)
750 if (sseek(fd, secnr))
753 bp = xmalloc(sizeof(struct block));
755 bp->next = blockhead;
757 bp->block = (char *) xmalloc(READ_SIZE);
759 if (read(fd, bp->block, READ_SIZE) != READ_SIZE) {
760 fprintf(stderr, "read error, sector %d\n", secnr);
768 get_sector_size(int filedes)
770 int rc, sector_size = 512;
772 rc = ioctl(filedes, BLKSSZGET, §or_size);