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
|
/*
* Copyright (C) 2001 Sistina Software
*
* This file is released under the GPL.
*
*/
#include "tools.h"
#include <fcntl.h>
struct lvcreate_params {
/* flags */
int snapshot;
int zero;
int contiguous;
int minor;
char *origin;
char *vg_name;
char *lv_name;
uint32_t stripes;
uint32_t stripe_size;
uint32_t chunk_size;
/* size */
uint32_t extents;
uint64_t size;
uint32_t permission;
uint32_t read_ahead;
int pv_count;
char **pvs;
};
static int _read_name_params(struct lvcreate_params *lp,
struct cmd_context *cmd, int *pargc, char ***pargv)
{
int argc = *pargc;
char **argv = *pargv, *ptr;
if (arg_count(cmd, name_ARG))
lp->lv_name = arg_value(cmd, name_ARG);
if (arg_count(cmd, snapshot_ARG)) {
lp->snapshot = 1;
if (!argc) {
log_err("Please specify a logical volume to act as "
"the snapshot origin.");
return 0;
}
lp->origin = argv[0];
(*pargv)++, (*pargc)--;
if (!(lp->vg_name = extract_vgname(cmd, lp->origin))) {
log_err("The origin name should include the "
"volume group.");
return 0;
}
/* Strip the volume group from the origin */
if ((ptr = strrchr(lp->origin, (int) '/')))
lp->origin = ptr + 1;
} else {
/*
* If VG not on command line, try -n arg and then
* environment.
*/
if (!argc) {
if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
log_err("Please provide a volume group name");
return 0;
}
} else {
if (strrchr(argv[0], '/')) {
log_error("Volume group name expected "
"(no slash)");
return 0;
}
/*
* Ensure lv_name doesn't contain a
* different VG.
*/
if (lp->lv_name && strchr(lp->lv_name, '/')) {
if (!(lp->vg_name =
extract_vgname(cmd, lp->lv_name)))
return 0;
if (strcmp(lp->vg_name, argv[0])) {
log_error("Inconsistent volume group "
"names "
"given: \"%s\" and \"%s\"",
lp->vg_name, argv[0]);
return 0;
}
}
lp->vg_name = argv[0];
(*pargv)++, (*pargc)--;
}
}
if (lp->lv_name && (ptr = strrchr(lp->lv_name, '/')))
lp->lv_name = ptr + 1;
return 1;
}
static int _read_size_params(struct lvcreate_params *lp,
struct cmd_context *cmd, int *pargc, char ***pargv)
{
/*
* There are two mutually exclusive ways of specifying
* the size ...
*/
if (arg_count(cmd, extents_ARG) && arg_count(cmd, size_ARG)) {
log_error("Invalid combination of arguments");
return 0;
}
/*
* ... you must use one of them.
*/
if (arg_count(cmd, size_ARG) + arg_count(cmd, extents_ARG) == 0) {
log_error("Please indicate size using option -l or -L");
return 0;
}
if (arg_count(cmd, extents_ARG))
lp->extents = arg_int_value(cmd, extents_ARG, 0);
/* Size returned in kilobyte units; held in sectors */
if (arg_count(cmd, size_ARG))
lp->size = arg_int64_value(cmd, size_ARG, 0) * 2ull;
return 1;
}
static int _read_stripe_params(struct lvcreate_params *lp,
struct cmd_context *cmd,
int *pargc, char ***pargv)
{
int argc = *pargc;
lp->stripes = 1;
if (arg_count(cmd, stripes_ARG)) {
lp->stripes = arg_int_value(cmd, stripes_ARG, 1);
if (lp->stripes == 1)
log_print("Redundant stripes argument: default is 1");
}
if (arg_count(cmd, stripesize_ARG))
lp->stripe_size = 2 * arg_int_value(cmd, stripesize_ARG, 0);
if (lp->stripes == 1 && lp->stripe_size) {
log_print("Ignoring stripesize argument with single stripe");
lp->stripe_size = 0;
}
if (lp->stripes > 1 && !lp->stripe_size) {
lp->stripe_size = 2 * STRIPE_SIZE_DEFAULT;
log_print("Using default stripesize %dKB", lp->stripe_size / 2);
}
if (argc && argc < lp->stripes) {
log_error("Too few physical volumes on "
"command line for %d-way striping", lp->stripes);
return 0;
}
if (lp->stripes < 1 || lp->stripes > MAX_STRIPES) {
log_error("Number of stripes (%d) must be between %d and %d",
lp->stripes, 1, MAX_STRIPES);
return 0;
}
if (lp->stripes > 1 && (lp->stripe_size < STRIPE_SIZE_MIN ||
lp->stripe_size > STRIPE_SIZE_MAX ||
lp->stripe_size & (lp->stripe_size - 1))) {
log_error("Invalid stripe size %d", lp->stripe_size);
return 0;
}
return 1;
}
static int _read_params(struct lvcreate_params *lp, struct cmd_context *cmd,
int argc, char **argv)
{
memset(lp, 0, sizeof(*lp));
/*
* Check selected options are compatible and set defaults
*/
if (arg_count(cmd, snapshot_ARG)) {
if (arg_count(cmd, zero_ARG)) {
log_error("-s and -Z are incompatible");
return 0;
}
lp->chunk_size = 2 * arg_int_value(cmd, chunksize_ARG, 8);
log_verbose("Setting chunksize to %d sectors.", lp->chunk_size);
} else {
if (arg_count(cmd, chunksize_ARG)) {
log_error("-c is only available with -s");
return 0;
}
}
if (!_read_name_params(lp, cmd, &argc, &argv) ||
!_read_size_params(lp, cmd, &argc, &argv) ||
!_read_stripe_params(lp, cmd, &argc, &argv))
return 0;
/*
* Should we zero the lv.
*/
lp->zero = strcmp(arg_str_value(cmd, zero_ARG, "y"), "n");
/*
* Contiguous ?
*/
lp->contiguous = strcmp(arg_str_value(cmd, contiguous_ARG, "n"), "n");
/*
* Read ahead.
*/
if (arg_count(cmd, readahead_ARG))
lp->read_ahead = arg_int_value(cmd, readahead_ARG, 0);
/*
* Permissions.
*/
if (arg_count(cmd, permission_ARG))
lp->permission = arg_int_value(cmd, permission_ARG, 0);
else
lp->permission = LVM_READ | LVM_WRITE;
lp->minor = arg_int_value(cmd, minor_ARG, -1);
/* Persistent minor */
if (arg_count(cmd, persistent_ARG)) {
if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "y")) {
if (lp->minor == -1) {
log_error("Please specify minor number with "
"--minor when using -My");
return 0;
}
} else {
if (lp->minor != -1) {
log_error("--minor not possible with -Mn");
return 0;
}
}
}
lp->pv_count = argc;
lp->pvs = argv;
return 1;
}
/*
* Volumes may be zeroed to remove old application data.
*/
static int _zero_lv(struct cmd_context *cmd, struct logical_volume *lv)
{
struct device *dev;
char *name;
/*
* FIXME:
* <clausen> also, more than 4k
* <clausen> say, reiserfs puts it's superblock 32k in, IIRC
* <ejt_> k, I'll drop a fixme to that effect
* (I know the device is at least 4k, but not 32k)
*/
if (!(name = pool_alloc(cmd->mem, PATH_MAX))) {
log_error("Name allocation failed - device not zeroed");
return 0;
}
if (lvm_snprintf(name, PATH_MAX, "%s%s/%s", cmd->dev_dir,
lv->vg->name, lv->name) < 0) {
log_error("Name too long - device not zeroed (%s)", lv->name);
return 0;
}
log_verbose("Zeroing start of logical volume \"%s\"", lv->name);
if (!(dev = dev_cache_get(name, NULL))) {
log_error("%s: not found: device not zeroed", name);
return 0;
}
if (!(dev_open(dev, O_WRONLY)))
return 0;
dev_zero(dev, 0, 4096);
dev_close(dev);
return 1;
}
static int _lvcreate(struct cmd_context *cmd, struct lvcreate_params *lp)
{
uint32_t size_rest;
uint32_t status = 0;
alloc_policy_t alloc = ALLOC_NEXT_FREE;
struct volume_group *vg;
struct logical_volume *lv, *org;
struct list *pvh;
if (lp->contiguous)
alloc = ALLOC_CONTIGUOUS;
status |= lp->permission;
/* does VG exist? */
log_verbose("Finding volume group \"%s\"", lp->vg_name);
if (!(vg = vg_read(cmd, lp->vg_name))) {
log_error("Volume group \"%s\" doesn't exist", lp->vg_name);
return 0;
}
if (vg->status & EXPORTED_VG) {
log_error("Volume group \"%s\" is exported", lp->vg_name);
return 0;
}
if (!(vg->status & LVM_WRITE)) {
log_error("Volume group \"%s\" is read-only", lp->vg_name);
return 0;
}
if (lp->lv_name && find_lv_in_vg(vg, lp->lv_name)) {
log_error("Logical volume \"%s\" already exists in "
"volume group \"%s\"", lp->lv_name, lp->vg_name);
return 0;
}
/*
* Create the pv list.
*/
if (lp->pv_count) {
if (!(pvh = create_pv_list(cmd->mem, vg,
lp->pv_count, lp->pvs))) {
stack;
return 0;
}
} else
pvh = &vg->pvs;
if (lp->stripe_size > vg->extent_size) {
log_error("Setting stripe size %d KB to physical extent "
"size %u KB", lp->stripe_size / 2,
vg->extent_size / 2);
lp->stripe_size = vg->extent_size;
}
if (lp->size) {
/* No of 512-byte sectors */
lp->extents = lp->size;
if (lp->extents % vg->extent_size) {
char *s1;
lp->extents += vg->extent_size - lp->extents %
vg->extent_size;
log_print("Rounding up size to full physical "
"extent %s",
(s1 = display_size(lp->extents / 2,
SIZE_SHORT)));
dbg_free(s1);
}
lp->extents /= vg->extent_size;
}
if ((size_rest = lp->extents % lp->stripes)) {
log_print("Rounding size (%d extents) up to stripe boundary "
"size (%d extents)", lp->extents,
lp->extents - size_rest + lp->stripes);
lp->extents = lp->extents - size_rest + lp->stripes;
}
if (lp->snapshot && !(org = find_lv(vg, lp->origin))) {
log_err("Couldn't find origin volume '%s'.", lp->origin);
return 0;
}
/*
* For now all logical volumes are visible.
*/
status |= VISIBLE_LV;
if (!(lv = lv_create(vg->fid, lp->lv_name, status, alloc,
lp->stripes, lp->stripe_size, lp->extents,
vg, pvh))) return 0;
if (lp->read_ahead) {
log_verbose("Setting read ahead sectors");
lv->read_ahead = lp->read_ahead;
}
if (lp->minor >= 0) {
lv->minor = lp->minor;
lv->status |= FIXED_MINOR;
log_verbose("Setting minor number to %d", lv->minor);
}
if (!archive(vg))
return 0;
/* store vg on disk(s) */
if (!vg_write(vg))
return 0;
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_ACTIVATE))
return 0;
if (lp->zero || lp->snapshot)
_zero_lv(cmd, lv);
else
log_print("WARNING: \"%s\" not zeroed", lv->name);
if (lp->snapshot) {
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE)) {
log_err("Couldn't unlock snapshot.");
return 0;
}
if (!lock_vol(cmd, org->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
log_error("Failed to lock origin %s", org->name);
return 0;
}
if (!vg_add_snapshot(org, lv, 1, lp->chunk_size)) {
log_err("Couldn't create snapshot.");
return 0;
}
/* store vg on disk(s) */
if (!vg_write(vg))
return 0;
if (!unlock_lv(cmd, org->lvid.s)) {
log_error("Problem reactivating origin %s", org->name);
return 0;
}
}
backup(vg);
log_print("Logical volume \"%s\" created", lv->name);
/*
* FIXME: as a sanity check we could try reading the
* last block of the device ?
*/
return 1;
}
int lvcreate(struct cmd_context *cmd, int argc, char **argv)
{
int r = ECMD_FAILED;
struct lvcreate_params lp;
memset(&lp, 0, sizeof(lp));
if (!_read_params(&lp, cmd, argc, argv))
return -EINVALID_CMD_LINE;
if (!driver_is_loaded())
return ECMD_FAILED;
if (!lock_vol(cmd, lp.vg_name, LCK_VG_WRITE)) {
log_error("Can't get lock for %s", lp.vg_name);
return 0;
}
if (!_lvcreate(cmd, &lp)) {
stack;
goto out;
}
r = 0;
out:
unlock_vg(cmd, lp.vg_name);
return r;
}
|