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
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
;
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
))
41 log_error("Physical Volume %s not found", name
);
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
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 "
58 if (!(pv
= pv_read(cmd
, name
, NULL
, NULL
, 1, 0))) {
59 log_error("Failed to read physical volume %s", name
);
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
));
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
);
83 if (arg_count(cmd
, force_ARG
)) {
84 log_warn("WARNING: Wiping physical volume label from "
86 !is_orphan(pv
) ? " of volume group \"" : "",
87 !is_orphan(pv
) ? pv_vg_name(pv
) : "",
88 !is_orphan(pv
) ? "\"" : "");
94 static int pvremove_single(struct cmd_context
*cmd
, const char *pv_name
,
95 void *handle
__attribute((unused
)))
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");
105 if (!pvremove_check(cmd
, pv_name
))
108 if (!(dev
= dev_cache_get(pv_name
, cmd
->filter
))) {
109 log_error("%s: Couldn't find device. Check your filters?",
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
));
121 /* Wipe existing label(s) */
122 if (!label_remove(dev
)) {
123 log_error("Failed to wipe existing label(s) on %s", pv_name
);
127 log_print("Labels on physical volume \"%s\" successfully wiped",
130 ret
= ECMD_PROCESSED
;
133 unlock_vg(cmd
, VG_ORPHANS
);
138 int pvremove(struct cmd_context
*cmd
, int argc
, char **argv
)
141 int ret
= ECMD_PROCESSED
;
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
);