]> sourceware.org Git - lvm2.git/blob - tools/pvscan.c
The lvmetad client-side integration. Only active when use_lvmetad = 1 is set in
[lvm2.git] / tools / pvscan.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 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
18 #include "lvmetad.h"
19 #include "lvmcache.h"
20
21 int pv_max_name_len = 0;
22 int vg_max_name_len = 0;
23
24 static void _pvscan_display_single(struct cmd_context *cmd,
25 struct physical_volume *pv,
26 void *handle __attribute__((unused)))
27 {
28 char uuid[64] __attribute__((aligned(8)));
29 unsigned vg_name_len = 0;
30
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) {
37 log_print("%s", pv_dev_name(pv));
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
54 vg_name_len = strlen(pv_vg_name(pv)) + 1;
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",
63 pv_max_name_len - 2, pv_dev_name(pv), uuid);
64 } else {
65 sprintf(pv_tmp_name, "%s", pv_dev_name(pv));
66 }
67
68 if (is_orphan(pv)) {
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 : " ",
73 display_size(cmd, pv_size(pv)));
74 return;
75 }
76
77 if (pv_status(pv) & EXPORTED_VG) {
78 strncpy(vg_name_this, pv_vg_name(pv), vg_name_len);
79 log_print("PV %-*s is in exported VG %s "
80 "[%s / %s free]",
81 pv_max_name_len, pv_tmp_name,
82 vg_name_this,
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)));
88 return;
89 }
90
91 sprintf(vg_tmp_name, "%s", pv_vg_name(pv));
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 : " ",
95 display_size(cmd, (uint64_t) pv_pe_count(pv) *
96 pv_pe_size(pv)),
97 display_size(cmd, (uint64_t) (pv_pe_count(pv) -
98 pv_pe_alloc_count(pv)) *
99 pv_pe_size(pv)));
100 }
101
102 int pvscan(struct cmd_context *cmd, int argc, char **argv)
103 {
104 int new_pvs_found = 0;
105 int pvs_found = 0;
106
107 struct dm_list *pvslist;
108 struct pv_list *pvl;
109 struct physical_volume *pv;
110
111 uint64_t size_total = 0;
112 uint64_t size_new = 0;
113
114 int len = 0;
115 pv_max_name_len = 0;
116 vg_max_name_len = 0;
117
118 if (arg_count(cmd, lvmetad_ARG)) {
119 if (!pvscan_lvmetad(cmd, argc, argv))
120 return ECMD_FAILED;
121 return ECMD_PROCESSED;
122 }
123
124 if (arg_count(cmd, novolumegroup_ARG) && arg_count(cmd, exported_ARG)) {
125 log_error("Options -e and -n are incompatible");
126 return EINVALID_CMD_LINE;
127 }
128
129 if (arg_count(cmd, exported_ARG) || arg_count(cmd, novolumegroup_ARG))
130 log_warn("WARNING: only considering physical volumes %s",
131 arg_count(cmd, exported_ARG) ?
132 "of exported volume group(s)" : "in no volume group");
133
134 if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE)) {
135 log_error("Unable to obtain global lock.");
136 return ECMD_FAILED;
137 }
138
139 persistent_filter_wipe(cmd->filter);
140 lvmcache_destroy(cmd, 1);
141
142 log_verbose("Walking through all physical volumes");
143 if (!(pvslist = get_pvs(cmd))) {
144 unlock_vg(cmd, VG_GLOBAL);
145 stack;
146 return ECMD_FAILED;
147 }
148
149 /* eliminate exported/new if required */
150 dm_list_iterate_items(pvl, pvslist) {
151 pv = pvl->pv;
152
153 if ((arg_count(cmd, exported_ARG)
154 && !(pv_status(pv) & EXPORTED_VG))
155 || (arg_count(cmd, novolumegroup_ARG) && (!is_orphan(pv)))) {
156 dm_list_del(&pvl->list);
157 continue;
158 }
159
160 /* Also check for MD use? */
161 /*******
162 if (MAJOR(pv_create_kdev_t(pv[p]->pv_name)) != MD_MAJOR) {
163 log_warn
164 ("WARNING: physical volume \"%s\" belongs to a meta device",
165 pv[p]->pv_name);
166 }
167 if (MAJOR(pv[p]->pv_dev) != MD_MAJOR)
168 continue;
169 ********/
170 pvs_found++;
171
172 if (is_orphan(pv)) {
173 new_pvs_found++;
174 size_new += pv_size(pv);
175 size_total += pv_size(pv);
176 } else
177 size_total += (uint64_t) pv_pe_count(pv) * pv_pe_size(pv);
178 }
179
180 /* find maximum pv name length */
181 pv_max_name_len = vg_max_name_len = 0;
182 dm_list_iterate_items(pvl, pvslist) {
183 pv = pvl->pv;
184 len = strlen(pv_dev_name(pv));
185 if (pv_max_name_len < len)
186 pv_max_name_len = len;
187 len = strlen(pv_vg_name(pv));
188 if (vg_max_name_len < len)
189 vg_max_name_len = len;
190 }
191 pv_max_name_len += 2;
192 vg_max_name_len += 2;
193
194 dm_list_iterate_items(pvl, pvslist) {
195 _pvscan_display_single(cmd, pvl->pv, NULL);
196 free_pv_fid(pvl->pv);
197 }
198
199 if (!pvs_found) {
200 log_print("No matching physical volumes found");
201 unlock_vg(cmd, VG_GLOBAL);
202 return ECMD_PROCESSED;
203 }
204
205 log_print("Total: %d [%s] / in use: %d [%s] / in no VG: %d [%s]",
206 pvs_found,
207 display_size(cmd, size_total),
208 pvs_found - new_pvs_found,
209 display_size(cmd, (size_total - size_new)),
210 new_pvs_found, display_size(cmd, size_new));
211
212 unlock_vg(cmd, VG_GLOBAL);
213
214 return ECMD_PROCESSED;
215 }
This page took 0.045701 seconds and 6 git commands to generate.