~psusi/ubuntu/precise/multipath-tools/fix-dmraid

« back to all changes in this revision

Viewing changes to .pc/0009-fix-delim.patch/kpartx/kpartx.c

  • Committer: Colin Watson
  • Date: 2012-01-27 11:24:50 UTC
  • mfrom: (41.1.1 multipath-tools)
  • Revision ID: cjwatson@canonical.com-20120127112450-77j6wqc8ui8e14bb
Tags: 0.4.9-3ubuntu2
mergeĀ lp:~psusi/ubuntu/precise/multipath-tools/dmraid-fix-gpt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Source: copy of util-linux' partx partx.c
 
3
 *
 
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
 
8
 */
 
9
 
 
10
/*
 
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.
 
14
 *
 
15
 * Read wholedisk and add all partitions:
 
16
 *      kpartx [-a|-d|-l] [-v] wholedisk
 
17
 *
 
18
 * aeb, 2000-03-21
 
19
 * cva, 2002-10-26
 
20
 */
 
21
 
 
22
#include <stdio.h>
 
23
#include <fcntl.h>
 
24
#include <errno.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <unistd.h>
 
28
#include <stdint.h>
 
29
#include <sys/stat.h>
 
30
#include <sys/types.h>
 
31
#include <ctype.h>
 
32
#include <libdevmapper.h>
 
33
 
 
34
#include "devmapper.h"
 
35
#include "crc32.h"
 
36
#include "lopart.h"
 
37
#include "kpartx.h"
 
38
 
 
39
#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
 
40
 
 
41
#define READ_SIZE       1024
 
42
#define MAXTYPES        64
 
43
#define MAXSLICES       256
 
44
#define DM_TARGET       "linear"
 
45
#define LO_NAME_SIZE    64
 
46
#define PARTNAME_SIZE   128
 
47
#define DELIM_SIZE      8
 
48
 
 
49
struct slice slices[MAXSLICES];
 
50
 
 
51
enum action { LIST, ADD, DELETE };
 
52
 
 
53
struct pt {
 
54
        char *type;
 
55
        ptreader *fn;
 
56
} pts[MAXTYPES];
 
57
 
 
58
int ptct = 0;
 
59
 
 
60
static void
 
61
addpts(char *t, ptreader f)
 
62
{
 
63
        if (ptct >= MAXTYPES) {
 
64
                fprintf(stderr, "addpts: too many types\n");
 
65
                exit(1);
 
66
        }
 
67
        pts[ptct].type = t;
 
68
        pts[ptct].fn = f;
 
69
        ptct++;
 
70
}
 
71
 
 
72
static void
 
73
initpts(void)
 
74
{
 
75
        addpts("gpt", read_gpt_pt);
 
76
        addpts("dos", read_dos_pt);
 
77
        addpts("bsd", read_bsd_pt);
 
78
        addpts("solaris", read_solaris_pt);
 
79
        addpts("unixware", read_unixware_pt);
 
80
        addpts("dasd", read_dasd_pt);
 
81
        addpts("mac", read_mac_pt);
 
82
        addpts("sun", read_sun_pt);
 
83
}
 
84
 
 
85
static char short_opts[] = "rladgvp:t:s";
 
86
 
 
87
/* Used in gpt.c */
 
88
int force_gpt=0;
 
89
 
 
90
static int
 
91
usage(void) {
 
92
        printf("usage : kpartx [-a|-d|-l] [-v] wholedisk\n");
 
93
        printf("\t-a add partition devmappings\n");
 
94
        printf("\t-r devmappings will be readonly\n");
 
95
        printf("\t-d del partition devmappings\n");
 
96
        printf("\t-l list partitions devmappings that would be added by -a\n");
 
97
        printf("\t-p set device name-partition number delimiter\n");
 
98
        printf("\t-g force GUID partition table (GPT)\n");
 
99
        printf("\t-v verbose\n");
 
100
        printf("\t-s sync mode. Don't return until the partitions are created\n");
 
101
        return 1;
 
102
}
 
103
 
 
104
static void
 
105
set_delimiter (char * device, char * delimiter)
 
106
{
 
107
        char * p = device;
 
108
 
 
109
        while (*(p++) != 0x0)
 
110
                continue;
 
111
 
 
112
        if (isdigit(*(p - 2)))
 
113
                *delimiter = 'p';
 
114
}
 
115
 
 
116
static void
 
117
strip_slash (char * device)
 
118
{
 
119
        char * p = device;
 
120
 
 
121
        while (*(p++) != 0x0) {
 
122
 
 
123
                if (*p == '/')
 
124
                        *p = '!';
 
125
        }
 
126
}
 
127
 
 
128
static int
 
129
find_devname_offset (char * device)
 
130
{
 
131
        char *p, *q = NULL;
 
132
 
 
133
        p = device;
 
134
 
 
135
        while (*p++)
 
136
                if (*p == '/')
 
137
                        q = p;
 
138
 
 
139
        return (int)(q - device) + 1;
 
140
}
 
141
 
 
142
static char *
 
143
get_hotplug_device(void)
 
144
{
 
145
        unsigned int major, minor, off, len;
 
146
        const char *mapname;
 
147
        char *devname = NULL;
 
148
        char *device = NULL;
 
149
        char *var = NULL;
 
150
        struct stat buf;
 
151
 
 
152
        var = getenv("ACTION");
 
153
 
 
154
        if (!var || strcmp(var, "add"))
 
155
                return NULL;
 
156
 
 
157
        /* Get dm mapname for hotpluged device. */
 
158
        if (!(devname = getenv("DEVNAME")))
 
159
                return NULL;
 
160
 
 
161
        if (stat(devname, &buf))
 
162
                return NULL;
 
163
 
 
164
        major = (unsigned int)MAJOR(buf.st_rdev);
 
165
        minor = (unsigned int)MINOR(buf.st_rdev);
 
166
 
 
167
        if (!(mapname = dm_mapname(major, minor))) /* Not dm device. */
 
168
                return NULL;
 
169
 
 
170
        off = find_devname_offset(devname);
 
171
        len = strlen(mapname);
 
172
 
 
173
        /* Dirname + mapname + \0 */
 
174
        if (!(device = (char *)malloc(sizeof(char) * (off + len + 1))))
 
175
                return NULL;
 
176
 
 
177
        /* Create new device name. */
 
178
        snprintf(device, off + 1, "%s", devname);
 
179
        snprintf(device + off, len + 1, "%s", mapname);
 
180
 
 
181
        if (strlen(device) != (off + len))
 
182
                return NULL;
 
183
 
 
184
        return device;
 
185
}
 
186
 
 
187
int
 
188
main(int argc, char **argv){
 
189
        int fd, i, j, m, n, op, off, arg, c, d, ro=0;
 
190
        struct slice all;
 
191
        struct pt *ptp;
 
192
        enum action what = LIST;
 
193
        char *type, *diskdevice, *device, *progname;
 
194
        int verbose = 0;
 
195
        char partname[PARTNAME_SIZE], params[PARTNAME_SIZE + 16];
 
196
        char * loopdev = NULL;
 
197
        char * delim = NULL;
 
198
        char *uuid = NULL;
 
199
        char *mapname = NULL;
 
200
        int loopro = 0;
 
201
        int hotplug = 0;
 
202
        int loopcreated = 0;
 
203
        int sync = 0;
 
204
        struct stat buf;
 
205
        uint32_t cookie = 0;
 
206
 
 
207
        initpts();
 
208
        init_crc32();
 
209
 
 
210
        type = device = diskdevice = NULL;
 
211
        memset(&all, 0, sizeof(all));
 
212
        memset(&partname, 0, sizeof(partname));
 
213
 
 
214
        /* Check whether hotplug mode. */
 
215
        progname = strrchr(argv[0], '/');
 
216
 
 
217
        if (!progname)
 
218
                progname = argv[0];
 
219
        else
 
220
                progname++;
 
221
 
 
222
        if (!strcmp(progname, "kpartx.dev")) { /* Hotplug mode */
 
223
                hotplug = 1;
 
224
 
 
225
                /* Setup for original kpartx variables */
 
226
                if (!(device = get_hotplug_device()))
 
227
                        exit(1);
 
228
 
 
229
                diskdevice = device;
 
230
                what = ADD;
 
231
        } else if (argc < 2) {
 
232
                usage();
 
233
                exit(1);
 
234
        }
 
235
 
 
236
        while ((arg = getopt(argc, argv, short_opts)) != EOF) switch(arg) {
 
237
                case 'r':
 
238
                        ro=1;
 
239
                        break;
 
240
                case 'g':
 
241
                        force_gpt=1;
 
242
                        break;
 
243
                case 't':
 
244
                        type = optarg;
 
245
                        break;
 
246
                case 'v':
 
247
                        verbose = 1;
 
248
                        break;
 
249
                case 'p':
 
250
                        delim = optarg;
 
251
                        break;
 
252
                case 'l':
 
253
                        what = LIST;
 
254
                        break;
 
255
                case 'a':
 
256
                        what = ADD;
 
257
                        break;
 
258
                case 'd':
 
259
                        what = DELETE;
 
260
                        break;
 
261
                case 's':
 
262
                        sync = 1;
 
263
                        break;
 
264
                default:
 
265
                        usage();
 
266
                        exit(1);
 
267
        }
 
268
 
 
269
        if (!sync)
 
270
                dm_udev_set_sync_support(0);
 
271
 
 
272
        if (dm_prereq(DM_TARGET, 0, 0, 0) && (what == ADD || what == DELETE)) {
 
273
                fprintf(stderr, "device mapper prerequisites not met\n");
 
274
                exit(1);
 
275
        }
 
276
 
 
277
        if (hotplug) {
 
278
                /* already got [disk]device */
 
279
        } else if (optind == argc-2) {
 
280
                device = argv[optind];
 
281
                diskdevice = argv[optind+1];
 
282
        } else if (optind == argc-1) {
 
283
                diskdevice = device = argv[optind];
 
284
        } else {
 
285
                usage();
 
286
                exit(1);
 
287
        }
 
288
 
 
289
        if (stat(device, &buf)) {
 
290
                printf("failed to stat() %s\n", device);
 
291
                exit (1);
 
292
        }
 
293
 
 
294
        if (S_ISREG (buf.st_mode)) {
 
295
                /* already looped file ? */
 
296
                loopdev = find_loop_by_file(device);
 
297
 
 
298
                if (!loopdev && what == DELETE)
 
299
                        exit (0);
 
300
 
 
301
                if (!loopdev) {
 
302
                        loopdev = find_unused_loop_device();
 
303
 
 
304
                        if (set_loop(loopdev, device, 0, &loopro)) {
 
305
                                fprintf(stderr, "can't set up loop\n");
 
306
                                exit (1);
 
307
                        }
 
308
                        loopcreated = 1;
 
309
                }
 
310
                device = loopdev;
 
311
        }
 
312
 
 
313
        if (delim == NULL) {
 
314
                delim = malloc(DELIM_SIZE);
 
315
                memset(delim, 0, DELIM_SIZE);
 
316
                set_delimiter(device, delim);
 
317
        }
 
318
 
 
319
        off = find_devname_offset(device);
 
320
 
 
321
        if (!loopdev) {
 
322
                uuid = dm_mapuuid((unsigned int)MAJOR(buf.st_rdev),
 
323
                                  (unsigned int)MINOR(buf.st_rdev));
 
324
                mapname = dm_mapname((unsigned int)MAJOR(buf.st_rdev),
 
325
                                     (unsigned int)MINOR(buf.st_rdev));
 
326
        }
 
327
 
 
328
        if (!uuid)
 
329
                uuid = device + off;
 
330
 
 
331
        if (!mapname)
 
332
                mapname = device + off;
 
333
 
 
334
        fd = open(device, O_RDONLY);
 
335
 
 
336
        if (fd == -1) {
 
337
                perror(device);
 
338
                exit(1);
 
339
        }
 
340
 
 
341
        /* add/remove partitions to the kernel devmapper tables */
 
342
        int r = 0;
 
343
        for (i = 0; i < ptct; i++) {
 
344
                ptp = &pts[i];
 
345
 
 
346
                if (type && strcmp(type, ptp->type))
 
347
                        continue;
 
348
 
 
349
                /* here we get partitions */
 
350
                n = ptp->fn(fd, all, slices, SIZE(slices));
 
351
 
 
352
#ifdef DEBUG
 
353
                if (n >= 0)
 
354
                        printf("%s: %d slices\n", ptp->type, n);
 
355
#endif
 
356
 
 
357
                if (n > 0)
 
358
                        close(fd);
 
359
                else
 
360
                        continue;
 
361
 
 
362
                switch(what) {
 
363
                case LIST:
 
364
                        for (j = 0, c = 0, m = 0; j < n; j++) {
 
365
                                if (slices[j].size == 0)
 
366
                                        continue;
 
367
                                if (slices[j].container > 0) {
 
368
                                        c++;
 
369
                                        continue;
 
370
                                }
 
371
 
 
372
                                slices[j].minor = m++;
 
373
 
 
374
                                printf("%s%s%d : 0 %" PRIu64 " %s %" PRIu64"\n",
 
375
                                       mapname, delim, j+1,
 
376
                                       slices[j].size, device,
 
377
                                       slices[j].start);
 
378
                        }
 
379
                        /* Loop to resolve contained slices */
 
380
                        d = c;
 
381
                        while (c) {
 
382
                                for (j = 0; j < n; j++) {
 
383
                                        uint64_t start;
 
384
                                        int k = slices[j].container - 1;
 
385
 
 
386
                                        if (slices[j].size == 0)
 
387
                                                continue;
 
388
                                        if (slices[j].minor > 0)
 
389
                                                continue;
 
390
                                        if (slices[j].container == 0)
 
391
                                                continue;
 
392
                                        slices[j].minor = m++;
 
393
 
 
394
                                        start = slices[j].start - slices[k].start;
 
395
                                        printf("%s%s%d : 0 %" PRIu64 " /dev/dm-%d %" PRIu64 "\n",
 
396
                                               mapname, delim, j+1,
 
397
                                               slices[j].size,
 
398
                                               slices[k].minor, start);
 
399
                                        c--;
 
400
                                }
 
401
                                /* Terminate loop if nothing more to resolve */
 
402
                                if (d == c)
 
403
                                        break;
 
404
                        }
 
405
 
 
406
                        if (loopcreated && S_ISREG (buf.st_mode)) {
 
407
                                if (del_loop(device)) {
 
408
                                        if (verbose)
 
409
                                                printf("can't del loop : %s\n",
 
410
                                                        device);
 
411
                                        exit(1);
 
412
                                }
 
413
                                printf("loop deleted : %s\n", device);
 
414
                        }
 
415
                        break;
 
416
 
 
417
                case DELETE:
 
418
                        for (j = n-1; j >= 0; j--) {
 
419
                                if (safe_sprintf(partname, "%s%s%d",
 
420
                                             mapname, delim, j+1)) {
 
421
                                        fprintf(stderr, "partname too small\n");
 
422
                                        exit(1);
 
423
                                }
 
424
                                strip_slash(partname);
 
425
 
 
426
                                if (!slices[j].size || !dm_map_present(partname))
 
427
                                        continue;
 
428
 
 
429
                                if (!dm_simplecmd(DM_DEVICE_REMOVE, partname,
 
430
                                                  0, &cookie)) {
 
431
                                        r++;
 
432
                                        continue;
 
433
                                }
 
434
                                if (verbose)
 
435
                                        printf("del devmap : %s\n", partname);
 
436
                        }
 
437
 
 
438
                        if (S_ISREG (buf.st_mode)) {
 
439
                                if (del_loop(device)) {
 
440
                                        if (verbose)
 
441
                                                printf("can't del loop : %s\n",
 
442
                                                        device);
 
443
                                        exit(1);
 
444
                                }
 
445
                                printf("loop deleted : %s\n", device);
 
446
                        }
 
447
                        break;
 
448
 
 
449
                case ADD:
 
450
                        for (j = 0, c = 0; j < n; j++) {
 
451
                                if (slices[j].size == 0)
 
452
                                        continue;
 
453
 
 
454
                                /* Skip all contained slices */
 
455
                                if (slices[j].container > 0) {
 
456
                                        c++;
 
457
                                        continue;
 
458
                                }
 
459
 
 
460
                                if (safe_sprintf(partname, "%s%s%d",
 
461
                                             mapname, delim, j+1)) {
 
462
                                        fprintf(stderr, "partname too small\n");
 
463
                                        exit(1);
 
464
                                }
 
465
                                strip_slash(partname);
 
466
 
 
467
                                if (safe_sprintf(params, "%s %" PRIu64 ,
 
468
                                                 device, slices[j].start)) {
 
469
                                        fprintf(stderr, "params too small\n");
 
470
                                        exit(1);
 
471
                                }
 
472
 
 
473
                                op = (dm_map_present(partname) ?
 
474
                                        DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
 
475
 
 
476
                                if (!dm_addmap(op, partname, DM_TARGET, params,
 
477
                                               slices[j].size, ro, uuid, j+1,
 
478
                                               buf.st_mode & 0777, buf.st_uid,
 
479
                                               buf.st_gid, &cookie)) {
 
480
                                        fprintf(stderr, "create/reload failed on %s\n",
 
481
                                                partname);
 
482
                                        r++;
 
483
                                }
 
484
                                if (op == DM_DEVICE_RELOAD &&
 
485
                                    !dm_simplecmd(DM_DEVICE_RESUME, partname,
 
486
                                                  1, &cookie)) {
 
487
                                        fprintf(stderr, "resume failed on %s\n",
 
488
                                                partname);
 
489
                                        r++;
 
490
                                }
 
491
                                dm_devn(partname, &slices[j].major,
 
492
                                        &slices[j].minor);
 
493
 
 
494
                                if (verbose)
 
495
                                        printf("add map %s (%d:%d): 0 %" PRIu64 " %s %s\n",
 
496
                                               partname, slices[j].major,
 
497
                                               slices[j].minor, slices[j].size,
 
498
                                               DM_TARGET, params);
 
499
                        }
 
500
                        /* Loop to resolve contained slices */
 
501
                        d = c;
 
502
                        while (c) {
 
503
                                for (j = 0; j < n; j++) {
 
504
                                        uint64_t start;
 
505
                                        int k = slices[j].container - 1;
 
506
 
 
507
                                        if (slices[j].size == 0)
 
508
                                                continue;
 
509
 
 
510
                                        /* Skip all existing slices */
 
511
                                        if (slices[j].minor > 0)
 
512
                                                continue;
 
513
 
 
514
                                        /* Skip all simple slices */
 
515
                                        if (slices[j].container == 0)
 
516
                                                continue;
 
517
 
 
518
                                        /* Check container slice */
 
519
                                        if (slices[k].size == 0)
 
520
                                                fprintf(stderr, "Invalid slice %d\n",
 
521
                                                        k);
 
522
 
 
523
                                        if (safe_sprintf(partname, "%s%s%d",
 
524
                                                         mapname, delim, j+1)) {
 
525
                                                fprintf(stderr, "partname too small\n");
 
526
                                                exit(1);
 
527
                                        }
 
528
                                        strip_slash(partname);
 
529
 
 
530
                                        start = slices[j].start - slices[k].start;
 
531
                                        if (safe_sprintf(params, "%d:%d %" PRIu64,
 
532
                                                         slices[k].major,
 
533
                                                         slices[k].minor,
 
534
                                                         start)) {
 
535
                                                fprintf(stderr, "params too small\n");
 
536
                                                exit(1);
 
537
                                        }
 
538
 
 
539
                                        op = (dm_map_present(partname) ?
 
540
                                              DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
 
541
 
 
542
                                        dm_addmap(op, partname, DM_TARGET, params,
 
543
                                                  slices[j].size, ro, uuid, j+1,
 
544
                                                  buf.st_mode & 0777,
 
545
                                                  buf.st_uid, buf.st_gid,
 
546
                                                  &cookie);
 
547
 
 
548
                                        if (op == DM_DEVICE_RELOAD)
 
549
                                                dm_simplecmd(DM_DEVICE_RESUME,
 
550
                                                             partname, 1,
 
551
                                                             &cookie);
 
552
 
 
553
                                        dm_devn(partname, &slices[j].major,
 
554
                                                &slices[j].minor);
 
555
 
 
556
                                        if (verbose)
 
557
                                                printf("add map %s : 0 %" PRIu64 " %s %s\n",
 
558
                                                       partname, slices[j].size,
 
559
                                                       DM_TARGET, params);
 
560
                                        c--;
 
561
                                }
 
562
                                /* Terminate loop */
 
563
                                if (d == c)
 
564
                                        break;
 
565
                        }
 
566
                        break;
 
567
 
 
568
                default:
 
569
                        break;
 
570
 
 
571
                }
 
572
                if (n > 0)
 
573
                        break;
 
574
        }
 
575
        dm_udev_wait(cookie);
 
576
        dm_lib_release();
 
577
        dm_lib_exit();
 
578
 
 
579
        return r;
 
580
}
 
581
 
 
582
void *
 
583
xmalloc (size_t size) {
 
584
        void *t;
 
585
 
 
586
        if (size == 0)
 
587
                return NULL;
 
588
 
 
589
        t = malloc (size);
 
590
 
 
591
        if (t == NULL) {
 
592
                fprintf(stderr, "Out of memory\n");
 
593
                exit(1);
 
594
        }
 
595
 
 
596
        return t;
 
597
}
 
598
 
 
599
/*
 
600
 * sseek: seek to specified sector
 
601
 */
 
602
 
 
603
static int
 
604
sseek(int fd, unsigned int secnr) {
 
605
        off64_t in, out;
 
606
        in = ((off64_t) secnr << 9);
 
607
        out = 1;
 
608
 
 
609
        if ((out = lseek64(fd, in, SEEK_SET)) != in)
 
610
        {
 
611
                fprintf(stderr, "llseek error\n");
 
612
                return -1;
 
613
        }
 
614
        return 0;
 
615
}
 
616
 
 
617
static
 
618
struct block {
 
619
        unsigned int secnr;
 
620
        char *block;
 
621
        struct block *next;
 
622
} *blockhead;
 
623
 
 
624
char *
 
625
getblock (int fd, unsigned int secnr) {
 
626
        struct block *bp;
 
627
 
 
628
        for (bp = blockhead; bp; bp = bp->next)
 
629
 
 
630
                if (bp->secnr == secnr)
 
631
                        return bp->block;
 
632
 
 
633
        if (sseek(fd, secnr))
 
634
                return NULL;
 
635
 
 
636
        bp = xmalloc(sizeof(struct block));
 
637
        bp->secnr = secnr;
 
638
        bp->next = blockhead;
 
639
        blockhead = bp;
 
640
        bp->block = (char *) xmalloc(READ_SIZE);
 
641
 
 
642
        if (read(fd, bp->block, READ_SIZE) != READ_SIZE) {
 
643
                fprintf(stderr, "read error, sector %d\n", secnr);
 
644
                bp->block = NULL;
 
645
        }
 
646
 
 
647
        return bp->block;
 
648
}