]> sourceware.org Git - lvm2.git/blob - tools/pvcreate.c
spacing
[lvm2.git] / tools / pvcreate.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 *
5 * This file is part of LVM2.
6 *
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
10 *
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16 #include "tools.h"
17 #include "metadata-exported.h"
18
19 /*
20 * Intial sanity checking of recovery-related command-line arguments.
21 * These args are: --restorefile, --uuid, and --physicalvolumesize
22 *
23 * Output arguments:
24 * pp: structure allocated by caller, fields written / validated here
25 */
26 static int pvcreate_restore_params_validate(struct cmd_context *cmd,
27 int argc, char **argv,
28 struct pvcreate_params *pp)
29 {
30 const char *uuid = NULL;
31 struct volume_group *vg;
32 struct pv_list *existing_pvl;
33
34 if (arg_count(cmd, restorefile_ARG) && !arg_count(cmd, uuidstr_ARG)) {
35 log_error("--uuid is required with --restorefile");
36 return 0;
37 }
38
39 if (!arg_count(cmd, restorefile_ARG) && arg_count(cmd, uuidstr_ARG)) {
40 if (!arg_count(cmd, norestorefile_ARG) &&
41 find_config_tree_bool(cmd,
42 "devices/require_restorefile_with_uuid",
43 DEFAULT_REQUIRE_RESTOREFILE_WITH_UUID)) {
44 log_error("--restorefile is required with --uuid");
45 return 0;
46 }
47 }
48
49 if (arg_count(cmd, uuidstr_ARG) && argc != 1) {
50 log_error("Can only set uuid on one volume at once");
51 return 0;
52 }
53
54 if (arg_count(cmd, uuidstr_ARG)) {
55 uuid = arg_str_value(cmd, uuidstr_ARG, "");
56 if (!id_read_format(&pp->id, uuid))
57 return 0;
58 pp->idp = &pp->id;
59 lvmcache_seed_infos_from_lvmetad(cmd); /* need to check for UUID dups */
60 }
61
62 if (arg_count(cmd, restorefile_ARG)) {
63 pp->restorefile = arg_str_value(cmd, restorefile_ARG, "");
64 /* The uuid won't already exist */
65 if (!(vg = backup_read_vg(cmd, NULL, pp->restorefile))) {
66 log_error("Unable to read volume group from %s",
67 pp->restorefile);
68 return 0;
69 }
70 if (!(existing_pvl = find_pv_in_vg_by_uuid(vg, pp->idp))) {
71 log_error("Can't find uuid %s in backup file %s",
72 uuid, pp->restorefile);
73 return 0;
74 }
75 pp->pe_start = pv_pe_start(existing_pvl->pv);
76 pp->extent_size = pv_pe_size(existing_pvl->pv);
77 pp->extent_count = pv_pe_count(existing_pvl->pv);
78 release_vg(vg);
79 }
80
81 if (arg_sign_value(cmd, physicalvolumesize_ARG, SIGN_NONE) == SIGN_MINUS) {
82 log_error("Physical volume size may not be negative");
83 return 0;
84 }
85 pp->size = arg_uint64_value(cmd, physicalvolumesize_ARG, UINT64_C(0));
86
87 if (arg_count(cmd, restorefile_ARG) || arg_count(cmd, uuidstr_ARG))
88 pp->zero = 0;
89 return 1;
90 }
91
92 int pvcreate(struct cmd_context *cmd, int argc, char **argv)
93 {
94 int i;
95 int ret = ECMD_PROCESSED;
96 struct pvcreate_params pp;
97 struct physical_volume *pv;
98
99 pvcreate_params_set_defaults(&pp);
100
101 if (!pvcreate_restore_params_validate(cmd, argc, argv, &pp)) {
102 return EINVALID_CMD_LINE;
103 }
104 if (!pvcreate_params_validate(cmd, argc, argv, &pp)) {
105 return EINVALID_CMD_LINE;
106 }
107
108 for (i = 0; i < argc; i++) {
109 if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
110 log_error("Can't get lock for orphan PVs");
111 return ECMD_FAILED;
112 }
113
114 dm_unescape_colons_and_at_signs(argv[i], NULL, NULL);
115
116 if (!(pv = pvcreate_single(cmd, argv[i], &pp, 1))) {
117 stack;
118 ret = ECMD_FAILED;
119 }
120
121 unlock_vg(cmd, VG_ORPHANS);
122 if (sigint_caught())
123 return ret;
124 }
125
126 return ret;
127 }
This page took 0.038919 seconds and 5 git commands to generate.