]> sourceware.org Git - lvm2.git/blame - tools/pvscan.c
Obtain VG list from lvmetad before relying on get_vgs, otherwise PVs in VGs
[lvm2.git] / tools / pvscan.c
CommitLineData
9e300c84 1/*
6606c3ae 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
be684599 3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
9e300c84 4 *
6606c3ae 5 * This file is part of LVM2.
9e300c84 6 *
6606c3ae
AK
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
be684599 9 * of the GNU Lesser General Public License v.2.1.
9e300c84 10 *
be684599 11 * You should have received a copy of the GNU Lesser General Public License
6606c3ae
AK
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
9e300c84
AK
14 */
15
16#include "tools.h"
17
dae08226
PR
18#include "lvmetad.h"
19#include "lvmcache.h"
20
9e300c84
AK
21int pv_max_name_len = 0;
22int vg_max_name_len = 0;
23
8ef2b021 24static void _pvscan_display_single(struct cmd_context *cmd,
72b2cb61 25 struct physical_volume *pv,
08f1ddea 26 void *handle __attribute__((unused)))
5a52dca9 27{
08f1ddea 28 char uuid[64] __attribute__((aligned(8)));
72b2cb61 29 unsigned vg_name_len = 0;
5a52dca9 30
5a52dca9
AK
31 char pv_tmp_name[NAME_LEN] = { 0, };
32 char vg_tmp_name[NAME_LEN] = { 0, };
33 char vg_name_this[NAME_LEN] = { 0, };
34
35 /* short listing? */
36 if (arg_count(cmd, short_ARG) > 0) {
1b8de4cb 37 log_print("%s", pv_dev_name(pv));
5a52dca9
AK
38 return;
39 }
40
41 if (arg_count(cmd, verbose_ARG) > 1) {
42 /* FIXME As per pv_display! Drop through for now. */
43 /* pv_show(pv); */
44
45 /* FIXME - Moved to Volume Group structure */
46 /* log_print("System Id %s", pv->vg->system_id); */
47
48 /* log_print(" "); */
49 /* return; */
50 }
51
52 memset(pv_tmp_name, 0, sizeof(pv_tmp_name));
53
ff77bb1a 54 vg_name_len = strlen(pv_vg_name(pv)) + 1;
5a52dca9
AK
55
56 if (arg_count(cmd, uuid_ARG)) {
57 if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
58 stack;
59 return;
60 }
61
62 sprintf(pv_tmp_name, "%-*s with UUID %s",
1b8de4cb 63 pv_max_name_len - 2, pv_dev_name(pv), uuid);
5a52dca9 64 } else {
1b8de4cb 65 sprintf(pv_tmp_name, "%s", pv_dev_name(pv));
5a52dca9
AK
66 }
67
9c1dbeb3 68 if (is_orphan(pv)) {
5a52dca9
AK
69 log_print("PV %-*s %-*s %s [%s]",
70 pv_max_name_len, pv_tmp_name,
71 vg_max_name_len, " ",
72 pv->fmt ? pv->fmt->name : " ",
ff77bb1a 73 display_size(cmd, pv_size(pv)));
5a52dca9
AK
74 return;
75 }
76
ff77bb1a
DW
77 if (pv_status(pv) & EXPORTED_VG) {
78 strncpy(vg_name_this, pv_vg_name(pv), vg_name_len);
5a52dca9
AK
79 log_print("PV %-*s is in exported VG %s "
80 "[%s / %s free]",
81 pv_max_name_len, pv_tmp_name,
4c64ed4c 82 vg_name_this,
ff77bb1a
DW
83 display_size(cmd, (uint64_t) pv_pe_count(pv) *
84 pv_pe_size(pv)),
85 display_size(cmd, (uint64_t) (pv_pe_count(pv) -
86 pv_pe_alloc_count(pv))
87 * pv_pe_size(pv)));
5a52dca9
AK
88 return;
89 }
90
ff77bb1a 91 sprintf(vg_tmp_name, "%s", pv_vg_name(pv));
30bab85b
AK
92 log_print("PV %-*s VG %-*s %s [%s / %s free]", pv_max_name_len,
93 pv_tmp_name, vg_max_name_len, vg_tmp_name,
94 pv->fmt ? pv->fmt->name : " ",
67cdbd7e 95 display_size(cmd, (uint64_t) pv_pe_count(pv) *
ff77bb1a 96 pv_pe_size(pv)),
67cdbd7e 97 display_size(cmd, (uint64_t) (pv_pe_count(pv) -
ff77bb1a
DW
98 pv_pe_alloc_count(pv)) *
99 pv_pe_size(pv)));
5a52dca9
AK
100}
101
d06f64dd
AK
102static int _pvscan_lvmetad(struct cmd_context *cmd, int argc, char **argv)
103{
104 int ret = ECMD_PROCESSED;
105
106 if (!argc) {
d742cdf3 107 log_error("List of Physical Volumes to be cached by the lvmetad daemon required.");
d06f64dd
AK
108 return EINVALID_CMD_LINE;
109 }
110
111 if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_READ)) {
112 log_error("Unable to obtain global lock.");
113 return ECMD_FAILED;
114 }
115
116 log_verbose("Using physical volume(s) on command line");
117 while (argc--) {
118 if (!pvscan_lvmetad_single(cmd, *argv++)) {
119 ret = ECMD_FAILED;
120 break;
121 }
122 if (sigint_caught())
123 break;
124 }
125
126 unlock_vg(cmd, VG_GLOBAL);
127
128 return ret;
129}
130
dae08226 131int pvscan(struct cmd_context *cmd, int argc, char **argv)
9e300c84
AK
132{
133 int new_pvs_found = 0;
134 int pvs_found = 0;
9e300c84 135
2c44337b 136 struct dm_list *pvslist;
b6c041d7 137 struct pv_list *pvl;
9e300c84
AK
138 struct physical_volume *pv;
139
ee1f91bf
AK
140 uint64_t size_total = 0;
141 uint64_t size_new = 0;
9e300c84
AK
142
143 int len = 0;
144 pv_max_name_len = 0;
145 vg_max_name_len = 0;
146
d742cdf3 147 if (arg_count(cmd, cache_ARG))
d06f64dd 148 return _pvscan_lvmetad(cmd, argc, argv);
dae08226 149
6fda126d 150 if (arg_count(cmd, novolumegroup_ARG) && arg_count(cmd, exported_ARG)) {
b6c041d7 151 log_error("Options -e and -n are incompatible");
ee1f91bf 152 return EINVALID_CMD_LINE;
9e300c84
AK
153 }
154
113f2a01 155 if (arg_count(cmd, exported_ARG) || arg_count(cmd, novolumegroup_ARG))
e7ddf416 156 log_warn("WARNING: only considering physical volumes %s",
6fda126d 157 arg_count(cmd, exported_ARG) ?
9e300c84
AK
158 "of exported volume group(s)" : "in no volume group");
159
e935d217
AK
160 if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE)) {
161 log_error("Unable to obtain global lock.");
162 return ECMD_FAILED;
163 }
164
60274aba 165 persistent_filter_wipe(cmd->filter);
6eb44b50 166 lvmcache_destroy(cmd, 1);
5a52dca9 167
59a5361f
AK
168 /* populate lvmcache */
169 if (!lvmetad_vg_list_to_lvmcache(cmd))
170 stack;
171
d2393d23 172 log_verbose("Walking through all physical volumes");
e935d217
AK
173 if (!(pvslist = get_pvs(cmd))) {
174 unlock_vg(cmd, VG_GLOBAL);
651ff9b3 175 stack;
ee1f91bf 176 return ECMD_FAILED;
e935d217 177 }
9e300c84
AK
178
179 /* eliminate exported/new if required */
2c44337b 180 dm_list_iterate_items(pvl, pvslist) {
cc282870 181 pv = pvl->pv;
9e300c84 182
25b73380 183 if ((arg_count(cmd, exported_ARG)
59a5361f
AK
184 && !(pv_status(pv) & EXPORTED_VG)) ||
185 (arg_count(cmd, novolumegroup_ARG) && (!is_orphan(pv)))) {
2c44337b 186 dm_list_del(&pvl->list);
9e300c84
AK
187 continue;
188 }
189
190 /* Also check for MD use? */
191/*******
192 if (MAJOR(pv_create_kdev_t(pv[p]->pv_name)) != MD_MAJOR) {
258db3ad 193 log_warn
9e300c84
AK
194 ("WARNING: physical volume \"%s\" belongs to a meta device",
195 pv[p]->pv_name);
196 }
197 if (MAJOR(pv[p]->pv_dev) != MD_MAJOR)
198 continue;
199********/
200 pvs_found++;
25d42d50 201
9c1dbeb3 202 if (is_orphan(pv)) {
9e300c84 203 new_pvs_found++;
ff77bb1a
DW
204 size_new += pv_size(pv);
205 size_total += pv_size(pv);
1b9fcf48 206 } else
1641fdf9 207 size_total += (uint64_t) pv_pe_count(pv) * pv_pe_size(pv);
9e300c84
AK
208 }
209
210 /* find maximum pv name length */
211 pv_max_name_len = vg_max_name_len = 0;
2c44337b 212 dm_list_iterate_items(pvl, pvslist) {
f2b7349e 213 pv = pvl->pv;
1b8de4cb 214 len = strlen(pv_dev_name(pv));
9e300c84
AK
215 if (pv_max_name_len < len)
216 pv_max_name_len = len;
ff77bb1a 217 len = strlen(pv_vg_name(pv));
9e300c84
AK
218 if (vg_max_name_len < len)
219 vg_max_name_len = len;
220 }
221 pv_max_name_len += 2;
222 vg_max_name_len += 2;
223
84f48499 224 dm_list_iterate_items(pvl, pvslist) {
dae08226
PR
225 _pvscan_display_single(cmd, pvl->pv, NULL);
226 free_pv_fid(pvl->pv);
84f48499 227 }
9e300c84
AK
228
229 if (!pvs_found) {
230 log_print("No matching physical volumes found");
e935d217 231 unlock_vg(cmd, VG_GLOBAL);
cfb7bfc7 232 return ECMD_PROCESSED;
9e300c84
AK
233 }
234
b6c041d7 235 log_print("Total: %d [%s] / in use: %d [%s] / in no VG: %d [%s]",
9e300c84 236 pvs_found,
72b2cb61 237 display_size(cmd, size_total),
9e300c84 238 pvs_found - new_pvs_found,
72b2cb61
AK
239 display_size(cmd, (size_total - size_new)),
240 new_pvs_found, display_size(cmd, size_new));
9e300c84 241
e935d217
AK
242 unlock_vg(cmd, VG_GLOBAL);
243
cfb7bfc7 244 return ECMD_PROCESSED;
9e300c84 245}
This page took 0.12254 seconds and 5 git commands to generate.