Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / tools / pvremove.c
blobb5ffca523d136a0fd189e3224cc42e6dd85b29d2
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
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.
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
18 #include "tools.h"
20 const char _really_wipe[] =
21 "Really WIPE LABELS from physical volume \"%s\" of volume group \"%s\" [y/n]? ";
24 * Decide whether it is "safe" to wipe the labels on this device.
25 * 0 indicates we may not.
27 static int pvremove_check(struct cmd_context *cmd, const char *name)
29 struct physical_volume *pv;
30 struct dm_list mdas;
32 dm_list_init(&mdas);
34 /* FIXME Check partition type is LVM unless --force is given */
36 /* Is there a pv here already? */
37 /* If not, this is an error unless you used -f. */
38 if (!(pv = pv_read(cmd, name, &mdas, NULL, 1, 0))) {
39 if (arg_count(cmd, force_ARG))
40 return 1;
41 log_error("Physical Volume %s not found", name);
42 return 0;
46 * If a PV has no MDAs it may appear to be an
47 * orphan until the metadata is read off
48 * another PV in the same VG. Detecting this
49 * means checking every VG by scanning every
50 * PV on the system.
52 if (is_orphan(pv) && !dm_list_size(&mdas)) {
53 if (!scan_vgs_for_pvs(cmd)) {
54 log_error("Rescan for PVs without metadata areas "
55 "failed.");
56 return 0;
58 if (!(pv = pv_read(cmd, name, NULL, NULL, 1, 0))) {
59 log_error("Failed to read physical volume %s", name);
60 return 0;
64 /* orphan ? */
65 if (is_orphan(pv))
66 return 1;
68 /* Allow partial & exported VGs to be destroyed. */
69 /* we must have -ff to overwrite a non orphan */
70 if (arg_count(cmd, force_ARG) < 2) {
71 log_error("Can't pvremove physical volume \"%s\" of "
72 "volume group \"%s\" without -ff", name, pv_vg_name(pv));
73 return 0;
76 /* prompt */
77 if (!arg_count(cmd, yes_ARG) &&
78 yes_no_prompt(_really_wipe, name, pv_vg_name(pv)) == 'n') {
79 log_print("%s: physical volume label not removed", name);
80 return 0;
83 if (arg_count(cmd, force_ARG)) {
84 log_warn("WARNING: Wiping physical volume label from "
85 "%s%s%s%s", name,
86 !is_orphan(pv) ? " of volume group \"" : "",
87 !is_orphan(pv) ? pv_vg_name(pv) : "",
88 !is_orphan(pv) ? "\"" : "");
91 return 1;
94 static int pvremove_single(struct cmd_context *cmd, const char *pv_name,
95 void *handle __attribute((unused)))
97 struct device *dev;
98 int ret = ECMD_FAILED;
100 if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
101 log_error("Can't get lock for orphan PVs");
102 return ECMD_FAILED;
105 if (!pvremove_check(cmd, pv_name))
106 goto error;
108 if (!(dev = dev_cache_get(pv_name, cmd->filter))) {
109 log_error("%s: Couldn't find device. Check your filters?",
110 pv_name);
111 goto error;
114 if (!dev_test_excl(dev)) {
115 /* FIXME Detect whether device-mapper is still using the device */
116 log_error("Can't open %s exclusively - not removing. "
117 "Mounted filesystem?", dev_name(dev));
118 goto error;
121 /* Wipe existing label(s) */
122 if (!label_remove(dev)) {
123 log_error("Failed to wipe existing label(s) on %s", pv_name);
124 goto error;
127 log_print("Labels on physical volume \"%s\" successfully wiped",
128 pv_name);
130 ret = ECMD_PROCESSED;
132 error:
133 unlock_vg(cmd, VG_ORPHANS);
135 return ret;
138 int pvremove(struct cmd_context *cmd, int argc, char **argv)
140 int i, r;
141 int ret = ECMD_PROCESSED;
143 if (!argc) {
144 log_error("Please enter a physical volume path");
145 return EINVALID_CMD_LINE;
148 if (arg_count(cmd, yes_ARG) && !arg_count(cmd, force_ARG)) {
149 log_error("Option y can only be given with option f");
150 return EINVALID_CMD_LINE;
153 for (i = 0; i < argc; i++) {
154 r = pvremove_single(cmd, argv[i], NULL);
155 if (r > ret)
156 ret = r;
159 return ret;