Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2002-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 : : /*
17 : : * Changelog
18 : : *
19 : : * 05/02/2002 - First drop [HM]
20 : : */
21 : :
22 : : #include "tools.h"
23 : :
24 : : int disks_found;
25 : : int parts_found;
26 : : int pv_disks_found;
27 : : int pv_parts_found;
28 : : int max_len;
29 : :
30 : 0 : static int _get_max_dev_name_len(struct dev_filter *filter)
31 : : {
32 : 0 : int len = 0;
33 : 0 : int maxlen = 0;
34 : : struct dev_iter *iter;
35 : : struct device *dev;
36 : :
37 [ # # ]: 0 : if (!(iter = dev_iter_create(filter, 1))) {
38 : 0 : log_error("dev_iter_create failed");
39 : 0 : return 0;
40 : : }
41 : :
42 : : /* Do scan */
43 [ # # ]: 0 : for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter)) {
44 : 0 : len = strlen(dev_name(dev));
45 [ # # ]: 0 : if (len > maxlen)
46 : 0 : maxlen = len;
47 : : }
48 : 0 : dev_iter_destroy(iter);
49 : :
50 : 0 : return maxlen;
51 : : }
52 : :
53 : 0 : static void _count(struct device *dev, int *disks, int *parts)
54 : : {
55 : 0 : int c = dev_name(dev)[strlen(dev_name(dev)) - 1];
56 : :
57 [ # # ]: 0 : if (!isdigit(c))
58 : 0 : (*disks)++;
59 : : else
60 : 0 : (*parts)++;
61 : 0 : }
62 : :
63 : 0 : static void _print(struct cmd_context *cmd, const struct device *dev,
64 : : uint64_t size, const char *what)
65 : : {
66 [ # # ]: 0 : log_print("%-*s [%15s] %s", max_len, dev_name(dev),
67 : : display_size(cmd, size), what ? : "");
68 : 0 : }
69 : :
70 : 0 : static int _check_device(struct cmd_context *cmd, struct device *dev)
71 : : {
72 : : char buffer;
73 : : uint64_t size;
74 : :
75 [ # # ]: 0 : if (!dev_open(dev)) {
76 : 0 : return 0;
77 : : }
78 [ # # ]: 0 : if (!dev_read(dev, UINT64_C(0), (size_t) 1, &buffer)) {
79 : 0 : dev_close(dev);
80 : 0 : return 0;
81 : : }
82 [ # # ]: 0 : if (!dev_get_size(dev, &size)) {
83 : 0 : log_error("Couldn't get size of \"%s\"", dev_name(dev));
84 : : }
85 : 0 : _print(cmd, dev, size, NULL);
86 : 0 : _count(dev, &disks_found, &parts_found);
87 [ # # ]: 0 : if (!dev_close(dev)) {
88 : 0 : log_error("dev_close on \"%s\" failed", dev_name(dev));
89 : 0 : return 0;
90 : : }
91 : 0 : return 1;
92 : : }
93 : :
94 : 0 : int lvmdiskscan(struct cmd_context *cmd, int argc __attribute((unused)),
95 : : char **argv __attribute((unused)))
96 : : {
97 : : uint64_t size;
98 : : struct dev_iter *iter;
99 : : struct device *dev;
100 : : struct label *label;
101 : :
102 : : /* initialise these here to avoid problems with the lvm shell */
103 : 0 : disks_found = 0;
104 : 0 : parts_found = 0;
105 : 0 : pv_disks_found = 0;
106 : 0 : pv_parts_found = 0;
107 : :
108 [ # # ]: 0 : if (arg_count(cmd, lvmpartition_ARG))
109 : 0 : log_warn("WARNING: only considering LVM devices");
110 : :
111 : 0 : max_len = _get_max_dev_name_len(cmd->filter);
112 : :
113 [ # # ]: 0 : if (!(iter = dev_iter_create(cmd->filter, 0))) {
114 : 0 : log_error("dev_iter_create failed");
115 : 0 : return ECMD_FAILED;
116 : : }
117 : :
118 : : /* Do scan */
119 [ # # ]: 0 : for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter)) {
120 : : /* Try if it is a PV first */
121 [ # # ]: 0 : if ((label_read(dev, &label, UINT64_C(0)))) {
122 [ # # ]: 0 : if (!dev_get_size(dev, &size)) {
123 : 0 : log_error("Couldn't get size of \"%s\"",
124 : : dev_name(dev));
125 : 0 : continue;
126 : : }
127 : 0 : _print(cmd, dev, size, "LVM physical volume");
128 : 0 : _count(dev, &pv_disks_found, &pv_parts_found);
129 : 0 : continue;
130 : : }
131 : : /* If user just wants PVs we are done */
132 [ # # ]: 0 : if (arg_count(cmd, lvmpartition_ARG))
133 : 0 : continue;
134 : :
135 : : /* What other device is it? */
136 [ # # ]: 0 : if (!_check_device(cmd, dev))
137 : 0 : continue;
138 : : }
139 : 0 : dev_iter_destroy(iter);
140 : :
141 : : /* Display totals */
142 [ # # ]: 0 : if (!arg_count(cmd, lvmpartition_ARG)) {
143 [ # # ]: 0 : log_print("%d disk%s",
144 : : disks_found, disks_found == 1 ? "" : "s");
145 [ # # ]: 0 : log_print("%d partition%s",
146 : : parts_found, parts_found == 1 ? "" : "s");
147 : : }
148 [ # # ]: 0 : log_print("%d LVM physical volume whole disk%s",
149 : : pv_disks_found, pv_disks_found == 1 ? "" : "s");
150 [ # # ]: 0 : log_print("%d LVM physical volume%s",
151 : : pv_parts_found, pv_parts_found == 1 ? "" : "s");
152 : :
153 : 0 : return ECMD_PROCESSED;
154 : : }
|