1*56a34939Shaad /*	$NetBSD: lvmdiskscan.c,v 1.1.1.1 2008/12/22 00:19:05 haad Exp $	*/
2*56a34939Shaad 
3*56a34939Shaad /*
4*56a34939Shaad  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
5*56a34939Shaad  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6*56a34939Shaad  *
7*56a34939Shaad  * This file is part of LVM2.
8*56a34939Shaad  *
9*56a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
10*56a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
11*56a34939Shaad  * of the GNU Lesser General Public License v.2.1.
12*56a34939Shaad  *
13*56a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
14*56a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
15*56a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16*56a34939Shaad  */
17*56a34939Shaad 
18*56a34939Shaad /*
19*56a34939Shaad  * Changelog
20*56a34939Shaad  *
21*56a34939Shaad  *   05/02/2002 - First drop [HM]
22*56a34939Shaad  */
23*56a34939Shaad 
24*56a34939Shaad #include "tools.h"
25*56a34939Shaad 
26*56a34939Shaad int disks_found;
27*56a34939Shaad int parts_found;
28*56a34939Shaad int pv_disks_found;
29*56a34939Shaad int pv_parts_found;
30*56a34939Shaad int max_len;
31*56a34939Shaad 
_get_max_dev_name_len(struct dev_filter * filter)32*56a34939Shaad static int _get_max_dev_name_len(struct dev_filter *filter)
33*56a34939Shaad {
34*56a34939Shaad 	int len = 0;
35*56a34939Shaad 	int maxlen = 0;
36*56a34939Shaad 	struct dev_iter *iter;
37*56a34939Shaad 	struct device *dev;
38*56a34939Shaad 
39*56a34939Shaad 	if (!(iter = dev_iter_create(filter, 1))) {
40*56a34939Shaad 		log_error("dev_iter_create failed");
41*56a34939Shaad 		return 0;
42*56a34939Shaad 	}
43*56a34939Shaad 
44*56a34939Shaad 	/* Do scan */
45*56a34939Shaad 	for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter)) {
46*56a34939Shaad 		len = strlen(dev_name(dev));
47*56a34939Shaad 		if (len > maxlen)
48*56a34939Shaad 			maxlen = len;
49*56a34939Shaad 	}
50*56a34939Shaad 	dev_iter_destroy(iter);
51*56a34939Shaad 
52*56a34939Shaad 	return maxlen;
53*56a34939Shaad }
54*56a34939Shaad 
_count(struct device * dev,int * disks,int * parts)55*56a34939Shaad static void _count(struct device *dev, int *disks, int *parts)
56*56a34939Shaad {
57*56a34939Shaad 	int c = dev_name(dev)[strlen(dev_name(dev)) - 1];
58*56a34939Shaad 
59*56a34939Shaad 	if (!isdigit(c))
60*56a34939Shaad 		(*disks)++;
61*56a34939Shaad 	else
62*56a34939Shaad 		(*parts)++;
63*56a34939Shaad }
64*56a34939Shaad 
_print(struct cmd_context * cmd,const struct device * dev,uint64_t size,const char * what)65*56a34939Shaad static void _print(struct cmd_context *cmd, const struct device *dev,
66*56a34939Shaad 		   uint64_t size, const char *what)
67*56a34939Shaad {
68*56a34939Shaad 	log_print("%-*s [%15s] %s", max_len, dev_name(dev),
69*56a34939Shaad 		  display_size(cmd, size), what ? : "");
70*56a34939Shaad }
71*56a34939Shaad 
_check_device(struct cmd_context * cmd,struct device * dev)72*56a34939Shaad static int _check_device(struct cmd_context *cmd, struct device *dev)
73*56a34939Shaad {
74*56a34939Shaad 	char buffer;
75*56a34939Shaad 	uint64_t size;
76*56a34939Shaad 
77*56a34939Shaad 	if (!dev_open(dev)) {
78*56a34939Shaad 		return 0;
79*56a34939Shaad 	}
80*56a34939Shaad 	if (!dev_read(dev, UINT64_C(0), (size_t) 1, &buffer)) {
81*56a34939Shaad 		dev_close(dev);
82*56a34939Shaad 		return 0;
83*56a34939Shaad 	}
84*56a34939Shaad 	if (!dev_get_size(dev, &size)) {
85*56a34939Shaad 		log_error("Couldn't get size of \"%s\"", dev_name(dev));
86*56a34939Shaad 	}
87*56a34939Shaad 	_print(cmd, dev, size, NULL);
88*56a34939Shaad 	_count(dev, &disks_found, &parts_found);
89*56a34939Shaad 	if (!dev_close(dev)) {
90*56a34939Shaad 		log_error("dev_close on \"%s\" failed", dev_name(dev));
91*56a34939Shaad 		return 0;
92*56a34939Shaad 	}
93*56a34939Shaad 	return 1;
94*56a34939Shaad }
95*56a34939Shaad 
lvmdiskscan(struct cmd_context * cmd,int argc __attribute ((unused)),char ** argv __attribute ((unused)))96*56a34939Shaad int lvmdiskscan(struct cmd_context *cmd, int argc __attribute((unused)),
97*56a34939Shaad 		char **argv __attribute((unused)))
98*56a34939Shaad {
99*56a34939Shaad 	uint64_t size;
100*56a34939Shaad 	struct dev_iter *iter;
101*56a34939Shaad 	struct device *dev;
102*56a34939Shaad 	struct label *label;
103*56a34939Shaad 
104*56a34939Shaad 	/* initialise these here to avoid problems with the lvm shell */
105*56a34939Shaad 	disks_found = 0;
106*56a34939Shaad 	parts_found = 0;
107*56a34939Shaad 	pv_disks_found = 0;
108*56a34939Shaad 	pv_parts_found = 0;
109*56a34939Shaad 
110*56a34939Shaad 	if (arg_count(cmd, lvmpartition_ARG))
111*56a34939Shaad 		log_warn("WARNING: only considering LVM devices");
112*56a34939Shaad 
113*56a34939Shaad 	max_len = _get_max_dev_name_len(cmd->filter);
114*56a34939Shaad 
115*56a34939Shaad 	if (!(iter = dev_iter_create(cmd->filter, 0))) {
116*56a34939Shaad 		log_error("dev_iter_create failed");
117*56a34939Shaad 		return ECMD_FAILED;
118*56a34939Shaad 	}
119*56a34939Shaad 
120*56a34939Shaad 	/* Do scan */
121*56a34939Shaad 	for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter)) {
122*56a34939Shaad 		/* Try if it is a PV first */
123*56a34939Shaad 		if ((label_read(dev, &label, UINT64_C(0)))) {
124*56a34939Shaad 			if (!dev_get_size(dev, &size)) {
125*56a34939Shaad 				log_error("Couldn't get size of \"%s\"",
126*56a34939Shaad 					  dev_name(dev));
127*56a34939Shaad 				continue;
128*56a34939Shaad 			}
129*56a34939Shaad 			_print(cmd, dev, size, "LVM physical volume");
130*56a34939Shaad 			_count(dev, &pv_disks_found, &pv_parts_found);
131*56a34939Shaad 			continue;
132*56a34939Shaad 		}
133*56a34939Shaad 		/* If user just wants PVs we are done */
134*56a34939Shaad 		if (arg_count(cmd, lvmpartition_ARG))
135*56a34939Shaad 			continue;
136*56a34939Shaad 
137*56a34939Shaad 		/* What other device is it? */
138*56a34939Shaad 		if (!_check_device(cmd, dev))
139*56a34939Shaad 			continue;
140*56a34939Shaad 	}
141*56a34939Shaad 	dev_iter_destroy(iter);
142*56a34939Shaad 
143*56a34939Shaad 	/* Display totals */
144*56a34939Shaad 	if (!arg_count(cmd, lvmpartition_ARG)) {
145*56a34939Shaad 		log_print("%d disk%s",
146*56a34939Shaad 			  disks_found, disks_found == 1 ? "" : "s");
147*56a34939Shaad 		log_print("%d partition%s",
148*56a34939Shaad 			  parts_found, parts_found == 1 ? "" : "s");
149*56a34939Shaad 	}
150*56a34939Shaad 	log_print("%d LVM physical volume whole disk%s",
151*56a34939Shaad 		  pv_disks_found, pv_disks_found == 1 ? "" : "s");
152*56a34939Shaad 	log_print("%d LVM physical volume%s",
153*56a34939Shaad 		  pv_parts_found, pv_parts_found == 1 ? "" : "s");
154*56a34939Shaad 
155*56a34939Shaad 	return ECMD_PROCESSED;
156*56a34939Shaad }
157