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