File: lvm.c

package info (click to toggle)
autopartkit 1.07
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,060 kB
  • ctags: 198
  • sloc: ansic: 2,489; sh: 843; makefile: 103
file content (607 lines) | stat: -rw-r--r-- 13,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*

   lvm.c - Part of autopartkit, a module to partition devices
                  for debian-installer.

   Author - Petter Reinholdtsen

   Copyright (C) 2003  Petter Reinholdtsen <pere@hungry.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#define _GNU_SOURCE /* for asprintf() in <stdio.h> */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "autopartkit.h"

typedef enum {
    FALSE = 0,
    TRUE = 1
} BOOLEAN;

static const char *VGSCAN = "/sbin/vgscan";

static BOOLEAN
lvm_isinstalled(void)
{
    struct stat statbuf;
    static int isinstalled = -1;

    if (isinstalled != -1)
      return isinstalled;

    if (0 != stat("/etc/uselvm", &statbuf) || ! S_ISREG(statbuf.st_mode))
    {
        autopartkit_log(1, "Missing /etc/uselvm, no LVM support enabled.");
        isinstalled = FALSE;
        return FALSE;
    }

    /* Is /proc/lvm a directory? */
    if ( 0 != stat("/proc/lvm", &statbuf) || ! S_ISDIR(statbuf.st_mode) )
    {
        autopartkit_error(0, "Missing /proc/lvm/, no LVM support available.");
        isinstalled = FALSE;
        return FALSE;
    }
  
    /* Is /bin/vgscan available? */
    if (0 != stat(VGSCAN, &statbuf) || ! S_ISREG(statbuf.st_mode))
    {
        autopartkit_error(0, "Missing %s, no LVM support available.", VGSCAN);
        isinstalled = FALSE;
        return FALSE;
    }

    isinstalled = TRUE;
    return TRUE;
}

/*
 * Return true = 1 if the volume group exist, and false = 0 if it doesn't.
 */
static int
vg_exists(const char *vgname)
{
    struct stat statbuf;
    char *devpath = NULL;
    int retval = FALSE;

    if (NULL == vgname)
        return FALSE;

    if ( -1 == asprintf(&devpath, "/proc/lvm/VGs/%s", vgname) )
        autopartkit_error(0, "Unable to allocate string for vg '%s'", vgname);
    else
    {
        if (0 == stat(devpath, &statbuf) && S_ISDIR(statbuf.st_mode) )
	    retval = TRUE;
        free(devpath);
    }
    return retval;
}

int
lvm_init(void)
{
    int retval = -1;
    if ( ! lvm_isinstalled())
        return retval;

    if (0 == (retval = system("vgscan >> /var/log/messages 2>&1")))
	retval = 0;
    else
        autopartkit_log(2, "Executing vgscan returned error code %d\n",retval);

    return retval;
}

int
lvm_init_dev(const char *devpath)
{
    char * cmd = NULL;
    int retval = -1;

    if ( ! lvm_isinstalled())
        return -1;

    autopartkit_log(1, "Initializing LVM pv '%s'\n",
                    devpath ? devpath : "(null)");

    asprintf(&cmd, "pvcreate %s >> /var/log/messages 2>&1", devpath);
    if (cmd)
    {
        retval = system(cmd);
        free(cmd);
    }
    return retval;
}

/*
 * Create new volumegroup if it do not exist, and add new physical
 * device (devpath) to the voluemgroup if it exists.
 */
int
lvm_volumegroup_add_dev(const char *vgname, const char *devpath)
{
    int retval;
    char *progname = NULL;
    char * cmd = NULL;

    if ( ! lvm_isinstalled())
        return -1;

    autopartkit_log(1, "Adding LVM pv '%s' to vg '%s'\n",
                    devpath ? devpath : "(null)",
                    vgname ? vgname : "(null)");

    /* Call vgscan first, it seem to be required for vgcreate to work. */
    if (0 != (retval = system("vgscan >> /var/log/messages 2>&1")))
        autopartkit_log(2, "Executing vgscan returned error code %d\n",retval);

    if (vg_exists(vgname))
        progname = "vgextend";
    else
        progname = "vgcreate";

    asprintf(&cmd, "%s %s %s >> /var/log/messages 2>&1", progname,
             vgname, devpath);

    retval = -1;
    if (cmd)
    {
	retval = system(cmd);
        free(cmd);
    }
    return retval;
}

/*
 * Create Logical Volume in given volume group, and return device path
 * if successfull.  Return NULL on failure.  The string is malloc()ed,
 * and must be free()d by the caller.
 */
char *
lvm_create_logicalvolume(const char *vgname, const char *lvname,
                       unsigned int mbsize)
{
    char *cmd = NULL;
    char *devpath = NULL;
    int retval = -1;

    if ( ! lvm_isinstalled())
        return NULL;

    autopartkit_log(1, "Creating LVM lv '%s' on vg '%s' (size=%d MiB)\n",
                    lvname ? lvname : "(null)",
                    vgname ? vgname : "(null)",
                    mbsize);

    asprintf(&cmd, "lvcreate -n%s -L%d %s >> /var/log/messages 2>&1",
             lvname, mbsize, vgname);
    if (cmd)
    {
	retval = system(cmd);
	free(cmd);
	cmd=NULL;
    }

    if (0 == retval)
    {
        asprintf(&devpath, "/dev/%s/%s", vgname, lvname);
        return devpath;
    }
    else 
        return NULL;
}

/* Extract vgname, lvname and fstype from "lvm:tjener_vg:home0_lv:default". */
int
lvm_split_fstype(const char *str, int separator, int elemcount,
		 char *elements[])
{
    int elemnum;
    const char *curp = str;
    const char *nextp;

    for (elemnum = 0 ; elemnum < elemcount; elemnum++)
    {
	nextp = strchr(curp, separator);
	if (NULL == nextp)  /* Last element */
	  {
	    elements[elemnum] = strdup(curp);
	    autopartkit_log(3, "Found last '%s'\n", elements[elemnum]);
	    return 0;
	  }
	elements[elemnum] = strndup(curp, nextp - curp);
	autopartkit_log(3, "Found '%s'\n", elements[elemnum]);
	curp = nextp + 1;
    }
    
    return 0;
}

char *
lvm_lv_add(void *stack, const char *fstype, unsigned int mbminsize,
	   unsigned int mbmaxsize)
{
    /* Create LVM logical volume with given FS.  Assuming LVM volume
       group is already created. */
    char *info[4]; /* 0='lvm', 1=vgname, 2=lvname, 3=fstype */

    /* Extract vgname, lvname and fstype from
       "lvm:tjener_vg:home0_lv:default". */
    if (0 != lvm_split_fstype(fstype, ':', 4, info))
    {
        autopartkit_log(0, "  Failed to parse '%s'\n", fstype);
	return NULL;
    }
    if (0 == strcmp("default", info[3]))
    {
        char *s = strdup(DEFAULT_FS);
	if (!s)
	    return NULL;
	else
	{
	    free(info[3]);
	    info[3] = s;
	}
    }
    autopartkit_log(2, "  Stacking LVM lv %s on vg %s "
		    "fstype %s\n", info[1], info[2], info[3]);
    /* Store vgname, lvname and size in stack */
    if (0 == lvm_lv_stack_push(stack, info[1], info[2], info[3],
			       mbminsize, mbmaxsize))
    {
        char buf[1024];
	snprintf(buf, sizeof(buf), "%s:%s", info[1], info[2]);
	return strdup(buf);
	
    }
    return NULL;
}

/* (andread@linpro.no) */
int
lvm_get_free_space_list(char *vgname, struct disk_info_t *spaceinfo)
{
    FILE *vgdisplay = NULL;
    char buf[160];
    char *command = NULL;

    spaceinfo->path = strdup(vgname);
    spaceinfo->capacity = (PedSector)0;
    spaceinfo->freespace = (PedSector)0;
    memset(&(spaceinfo->geom), 0, sizeof(spaceinfo->geom));
    autopartkit_log(2, "  Locating free space on volumegroup %s\n", vgname);

    asprintf(&command, "/sbin/vgdisplay -c %s 2>&1", vgname);
    autopartkit_log(2, "  Running command: %s\n", command);
    vgdisplay = popen(command, "r");
    if (! vgdisplay ){
        autopartkit_log(0, "Failed to run vgdisplay\n");
	free(spaceinfo);
	free(command);
	return 0;
    }
    if (fgets(buf, 160, vgdisplay) != NULL){
	char *token = NULL;
	int i = 0;
	int pe_size = 0;
	int free_pe = 0;
	int vg_size = 0;

        autopartkit_log(3, "  Vgdisplay: %s", buf);
	token = strtok(buf, ":");
	if (token) {
	  while (token){
	    i++;
	    /*
	      We expect the tokenized output fields to be like this:
	      12 - size of volume group in kilobytes
	      13 - physical extent size
	      16 - free number of physical extents for this volume group
	    */
	    /* printf("DEBUG (%i): %s\n", i, token); */
	    if (i == 12) vg_size = atoi(token);
	    else if (i == 13) pe_size = atoi(token);
	    else if (i == 16)free_pe = atoi(token);
	    token = strtok(NULL, ":");
	  }
	} 
	else {
	  /* No tokens */
	  free(spaceinfo);
	  free(command);
	  return 0;
	}
	/* FIXME: Some problems with the data from vgdisplay -c. Using
           field 12 for now instead */
	/*
	spaceinfo->capacity = 
	  (PedSector)(MiB_TO_BLOCKS(pe_size * free_pe / (1024) ));
	spaceinfo->freespace = 
	  (PedSector)(MiB_TO_BLOCKS(pe_size * free_pe / (1024) ));
	*/
	spaceinfo->capacity = 
	  (PedSector)(MiB_TO_BLOCKS(vg_size / (1024) ));
	spaceinfo->freespace = 
	  (PedSector)(MiB_TO_BLOCKS(vg_size / (1024) ));
    }
    else {
        autopartkit_log(0, "Failed to find vg size\n");
        free(spaceinfo);
	free(command);
        return 0;
    }
    free(command);
    return 1;
}

/* LVM stack operations */

/* vg-stack (andread@linpro.no) */

struct lvm_vg_info {
    struct lvm_vg_info *next;
    char *vgname;
};

void *
lvm_vg_stack_new()
{
    struct lvm_vg_info *head;
    head = malloc(sizeof(*head));
    if (NULL != head)
      {
          head->next = head;
	  head->vgname = NULL;
      }
    return head;
}

int
lvm_vg_stack_isempty(void *stack)
{
    struct lvm_vg_info *head = stack;
    return head->next == head;
}

int
lvm_vg_stack_push(void *stack, const char *vgname)
{
    struct lvm_vg_info *head = stack;
    struct lvm_vg_info *elem;

    elem = malloc(sizeof(*elem));
    if (NULL == elem)
        return -1;
    elem->vgname = strdup(vgname);

    elem->next = head->next;
    head->next = elem;

    return 0;
}

int
lvm_vg_stack_pop(void *stack, char **vgname)
{
    struct lvm_vg_info *head = stack;
    struct lvm_vg_info *elem;

    elem = head->next;

    if (elem == head)
        return -1;

    head->next = elem->next;

    *vgname = elem->vgname;

    free(elem);
  
    return 0;
}

int
lvm_vg_stack_delete(void *stack)
{
    char *vgname;
    while ( ! lvm_vg_stack_isempty(stack) )
    {
	lvm_vg_stack_pop(stack, &vgname);
	free(vgname);
    }
    free(stack);
    return 0;
}

/* --- */

struct lvm_pv_info {
    struct lvm_pv_info *next;
    char *vgname;
    char *devpath;
};

void *
lvm_pv_stack_new()
{
    struct lvm_pv_info *head;
    head = malloc(sizeof(*head));
    if (NULL != head)
    {
        head->next = head;
	head->vgname = NULL;
	head->devpath = NULL;
    }
    return head;
}
int
lvm_pv_stack_isempty(void *stack)
{
    struct lvm_pv_info *head = stack;
    return head->next == head;
}
int
lvm_pv_stack_push(void *stack, const char *vgname, const char *devpath)
{
    struct lvm_pv_info *head = stack;
    struct lvm_pv_info *elem;

    elem = malloc(sizeof(*elem));
    if (NULL == elem)
        return -1;
    elem->vgname = strdup(vgname);
    elem->devpath = strdup(devpath);

    elem->next = head->next;
    head->next = elem;

    return 0;
}
int
lvm_pv_stack_pop(void *stack, char **vgname, char **devpath)
{
    struct lvm_pv_info *head = stack;
    struct lvm_pv_info *elem;

    elem = head->next;

    if (elem == head)
        return -1;

    head->next = elem->next;

    *vgname = elem->vgname;
    *devpath = elem->devpath;

    free(elem);
  
    return 0;
}
int
lvm_pv_stack_delete(void *stack)
{
    char *vgname;
    char *devpath;
    while ( ! lvm_pv_stack_isempty(stack) )
    {
	lvm_pv_stack_pop(stack, &vgname, &devpath);
	free(vgname);
	free(devpath);
    }
    free(stack);
    return 0;
}

struct lvm_lv_info { /* Store vgname, lvname and mbsize in list */
    struct lvm_lv_info *next;
    char *vgname;
    char *lvname;
    char *fstype;
    unsigned int mbminsize;
    unsigned int mbmaxsize;
};

void *
lvm_lv_stack_new()
{
    struct lvm_lv_info *head;
    head = malloc(sizeof(*head));
    if (NULL != head)
    {
	head->next = head;
	head->vgname = NULL;
	head->lvname = NULL;
	head->mbminsize = 0;
	head->mbmaxsize = 0;
    }
    return head;
}
int
lvm_lv_stack_isempty(void *stack)
{
    struct lvm_lv_info *head = stack;
    return head->next == head;
}
int
lvm_lv_stack_push(void *stack, const char *vgname, const char *lvname,
		  const char *fstype, unsigned int mbminsize,
		  unsigned int mbmaxsize)
{
    struct lvm_lv_info *head = stack;
    struct lvm_lv_info *elem;

    elem = malloc(sizeof(*elem));
    if (NULL == elem)
        return -1;
    elem->vgname = strdup(vgname);
    elem->lvname = strdup(lvname);
    elem->fstype = strdup(fstype);
    elem->mbminsize = mbminsize;
    elem->mbmaxsize = mbmaxsize;

    elem->next = head->next;
    head->next = elem;

    return 0;
}
int
lvm_lv_stack_pop(void *stack, char **vgname, char **lvname, char **fstype,
		 unsigned int *mbminsize, unsigned int *mbmaxsize)
{
    struct lvm_lv_info *head = stack;
    struct lvm_lv_info *elem;

    elem = head->next;

    if (elem == head)
        return -1;

    head->next = elem->next;

    *vgname = elem->vgname;
    *lvname = elem->lvname;
    *fstype = elem->fstype;
    *mbminsize = elem->mbminsize;
    *mbmaxsize = elem->mbmaxsize;

    free(elem);
  
    return 0;
}
int
lvm_lv_stack_delete(void *stack)
{
    char *vgname;
    char *lvname;
    while ( ! lvm_lv_stack_isempty(stack) )
    {
	lvm_pv_stack_pop(stack, &vgname, &lvname);
	free(vgname);
	free(lvname);
    }
    free(stack);
    return 0;
}