Tizen 2.1 base
[external/device-mapper.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         }
60
61         if (arg_count(cmd, restorefile_ARG)) {
62                 pp->restorefile = arg_str_value(cmd, restorefile_ARG, "");
63                 /* The uuid won't already exist */
64                 if (!(vg = backup_read_vg(cmd, NULL, pp->restorefile))) {
65                         log_error("Unable to read volume group from %s",
66                                   pp->restorefile);
67                         return 0;
68                 }
69                 if (!(existing_pvl = find_pv_in_vg_by_uuid(vg, pp->idp))) {
70                         log_error("Can't find uuid %s in backup file %s",
71                                   uuid, pp->restorefile);
72                         return 0;
73                 }
74                 pp->pe_start = pv_pe_start(existing_pvl->pv);
75                 pp->extent_size = pv_pe_size(existing_pvl->pv);
76                 pp->extent_count = pv_pe_count(existing_pvl->pv);
77                 free_vg(vg);
78         }
79
80         if (arg_sign_value(cmd, physicalvolumesize_ARG, 0) == SIGN_MINUS) {
81                 log_error("Physical volume size may not be negative");
82                 return 0;
83         }
84         pp->size = arg_uint64_value(cmd, physicalvolumesize_ARG, UINT64_C(0));
85
86         if (arg_count(cmd, restorefile_ARG) || arg_count(cmd, uuidstr_ARG))
87                 pp->zero = 0;
88         return 1;
89 }
90
91 int pvcreate(struct cmd_context *cmd, int argc, char **argv)
92 {
93         int i;
94         int ret = ECMD_PROCESSED;
95         struct pvcreate_params pp;
96
97         pvcreate_params_set_defaults(&pp);
98
99         if (!pvcreate_restore_params_validate(cmd, argc, argv, &pp)) {
100                 return EINVALID_CMD_LINE;
101         }
102         if (!pvcreate_params_validate(cmd, argc, argv, &pp)) {
103                 return EINVALID_CMD_LINE;
104         }
105
106         for (i = 0; i < argc; i++) {
107                 if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
108                         log_error("Can't get lock for orphan PVs");
109                         return ECMD_FAILED;
110                 }
111
112                 unescape_colons_and_at_signs(argv[i], NULL, NULL);
113
114                 if (!pvcreate_single(cmd, argv[i], &pp)) {
115                         stack;
116                         ret = ECMD_FAILED;
117                 }
118
119                 unlock_vg(cmd, VG_ORPHANS);
120                 if (sigint_caught())
121                         return ret;
122         }
123
124         return ret;
125 }