xref: /dragonfly/contrib/lvm2/dist/tools/pvscan.c (revision 9348a738)
1 /*	$NetBSD: pvscan.c,v 1.1.1.2 2009/12/02 00:25:54 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 
18 #include "tools.h"
19 
20 int pv_max_name_len = 0;
21 int vg_max_name_len = 0;
22 
23 static void _pvscan_display_single(struct cmd_context *cmd,
24 				   struct physical_volume *pv,
25 				   void *handle __attribute((unused)))
26 {
27 	char uuid[64] __attribute((aligned(8)));
28 	unsigned vg_name_len = 0;
29 
30 	char pv_tmp_name[NAME_LEN] = { 0, };
31 	char vg_tmp_name[NAME_LEN] = { 0, };
32 	char vg_name_this[NAME_LEN] = { 0, };
33 
34 	/* short listing? */
35 	if (arg_count(cmd, short_ARG) > 0) {
36 		log_print("%s", pv_dev_name(pv));
37 		return;
38 	}
39 
40 	if (arg_count(cmd, verbose_ARG) > 1) {
41 		/* FIXME As per pv_display! Drop through for now. */
42 		/* pv_show(pv); */
43 
44 		/* FIXME - Moved to Volume Group structure */
45 		/* log_print("System Id             %s", pv->vg->system_id); */
46 
47 		/* log_print(" "); */
48 		/* return; */
49 	}
50 
51 	memset(pv_tmp_name, 0, sizeof(pv_tmp_name));
52 
53 	vg_name_len = strlen(pv_vg_name(pv)) + 1;
54 
55 	if (arg_count(cmd, uuid_ARG)) {
56 		if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
57 			stack;
58 			return;
59 		}
60 
61 		sprintf(pv_tmp_name, "%-*s with UUID %s",
62 			pv_max_name_len - 2, pv_dev_name(pv), uuid);
63 	} else {
64 		sprintf(pv_tmp_name, "%s", pv_dev_name(pv));
65 	}
66 
67 	if (is_orphan(pv)) {
68 		log_print("PV %-*s    %-*s %s [%s]",
69 			  pv_max_name_len, pv_tmp_name,
70 			  vg_max_name_len, " ",
71 			  pv->fmt ? pv->fmt->name : "    ",
72 			  display_size(cmd, pv_size(pv)));
73 		return;
74 	}
75 
76 	if (pv_status(pv) & EXPORTED_VG) {
77 		strncpy(vg_name_this, pv_vg_name(pv), vg_name_len);
78 		log_print("PV %-*s  is in exported VG %s "
79 			  "[%s / %s free]",
80 			  pv_max_name_len, pv_tmp_name,
81 			  vg_name_this,
82 			  display_size(cmd, (uint64_t) pv_pe_count(pv) *
83 				       pv_pe_size(pv)),
84 			  display_size(cmd, (uint64_t) (pv_pe_count(pv) -
85 						pv_pe_alloc_count(pv))
86 				       * pv_pe_size(pv)));
87 		return;
88 	}
89 
90 	sprintf(vg_tmp_name, "%s", pv_vg_name(pv));
91 	log_print("PV %-*s VG %-*s %s [%s / %s free]", pv_max_name_len,
92 		  pv_tmp_name, vg_max_name_len, vg_tmp_name,
93 		  pv->fmt ? pv->fmt->name : "    ",
94 		  display_size(cmd, (uint64_t) pv_pe_count(pv) *
95 					       pv_pe_size(pv)),
96 		  display_size(cmd, (uint64_t) (pv_pe_count(pv) -
97 						pv_pe_alloc_count(pv)) *
98 					   pv_pe_size(pv)));
99 	return;
100 }
101 
102 int pvscan(struct cmd_context *cmd, int argc __attribute((unused)),
103 	   char **argv __attribute((unused)))
104 {
105 	int new_pvs_found = 0;
106 	int pvs_found = 0;
107 
108 	struct dm_list *pvslist;
109 	struct pv_list *pvl;
110 	struct physical_volume *pv;
111 
112 	uint64_t size_total = 0;
113 	uint64_t size_new = 0;
114 
115 	int len = 0;
116 	pv_max_name_len = 0;
117 	vg_max_name_len = 0;
118 
119 	if (arg_count(cmd, novolumegroup_ARG) && arg_count(cmd, exported_ARG)) {
120 		log_error("Options -e and -n are incompatible");
121 		return EINVALID_CMD_LINE;
122 	}
123 
124 	if (arg_count(cmd, exported_ARG) || arg_count(cmd, novolumegroup_ARG))
125 		log_warn("WARNING: only considering physical volumes %s",
126 			  arg_count(cmd, exported_ARG) ?
127 			  "of exported volume group(s)" : "in no volume group");
128 
129 	if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE)) {
130 		log_error("Unable to obtain global lock.");
131 		return ECMD_FAILED;
132 	}
133 
134 	persistent_filter_wipe(cmd->filter);
135 	lvmcache_destroy(cmd, 1);
136 
137 	log_verbose("Walking through all physical volumes");
138 	if (!(pvslist = get_pvs(cmd))) {
139 		unlock_vg(cmd, VG_GLOBAL);
140 		stack;
141 		return ECMD_FAILED;
142 	}
143 
144 	/* eliminate exported/new if required */
145 	dm_list_iterate_items(pvl, pvslist) {
146 		pv = pvl->pv;
147 
148 		if ((arg_count(cmd, exported_ARG)
149 		     && !(pv_status(pv) & EXPORTED_VG))
150 		    || (arg_count(cmd, novolumegroup_ARG) && (!is_orphan(pv)))) {
151 			dm_list_del(&pvl->list);
152 			continue;
153 		}
154 
155 		/* Also check for MD use? */
156 /*******
157 		if (MAJOR(pv_create_kdev_t(pv[p]->pv_name)) != MD_MAJOR) {
158 			log_print
159 			    ("WARNING: physical volume \"%s\" belongs to a meta device",
160 			     pv[p]->pv_name);
161 		}
162 		if (MAJOR(pv[p]->pv_dev) != MD_MAJOR)
163 			continue;
164 ********/
165 		pvs_found++;
166 
167 		if (is_orphan(pv)) {
168 			new_pvs_found++;
169 			size_new += pv_size(pv);
170 			size_total += pv_size(pv);
171 		} else
172 			size_total += pv_pe_count(pv) * pv_pe_size(pv);
173 	}
174 
175 	/* find maximum pv name length */
176 	pv_max_name_len = vg_max_name_len = 0;
177 	dm_list_iterate_items(pvl, pvslist) {
178 		pv = pvl->pv;
179 		len = strlen(pv_dev_name(pv));
180 		if (pv_max_name_len < len)
181 			pv_max_name_len = len;
182 		len = strlen(pv_vg_name(pv));
183 		if (vg_max_name_len < len)
184 			vg_max_name_len = len;
185 	}
186 	pv_max_name_len += 2;
187 	vg_max_name_len += 2;
188 
189 	dm_list_iterate_items(pvl, pvslist)
190 	    _pvscan_display_single(cmd, pvl->pv, NULL);
191 
192 	if (!pvs_found) {
193 		log_print("No matching physical volumes found");
194 		unlock_vg(cmd, VG_GLOBAL);
195 		return ECMD_PROCESSED;
196 	}
197 
198 	log_print("Total: %d [%s] / in use: %d [%s] / in no VG: %d [%s]",
199 		  pvs_found,
200 		  display_size(cmd, size_total),
201 		  pvs_found - new_pvs_found,
202 		  display_size(cmd, (size_total - size_new)),
203 		  new_pvs_found, display_size(cmd, size_new));
204 
205 	unlock_vg(cmd, VG_GLOBAL);
206 
207 	return ECMD_PROCESSED;
208 }
209