/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tools.h"
#include "lv_alloc.h"
static int _remove_pv(struct volume_group *vg, struct pv_list *pvl, int silent)
{
char uuid[64] __attribute((aligned(8)));
if (vg->pv_count == 1) {
log_error("Volume Groups must always contain at least one PV");
return 0;
}
if (!id_write_format(&pvl->pv->id, uuid, sizeof(uuid)))
return_0;
log_verbose("Removing PV with UUID %s from VG %s", uuid, vg->name);
if (pvl->pv->pe_alloc_count) {
if (!silent)
log_error("LVs still present on PV with UUID %s: "
"Can't remove from VG %s", uuid, vg->name);
return 0;
}
if (first && !test_mode() &&
!deactivate_lv(cmd, snap_seg->origin)) {
log_error("Failed to deactivate LV %s",
snap_seg->origin->name);
return 0;
}
*list_unsafe = 1; /* May remove caller's lvht! */
if (!vg_remove_snapshot(cow))
return_0;
log_verbose("Removing LV %s from VG %s", cow->name,
lv->vg->name);
if (!lv_remove(cow))
return_0;
first = 0;
}
/*
* If LV is active, replace it with error segment
* and add to list of LVs to be removed later.
* Doesn't apply to snapshots/origins yet - they're already deactivated.
*/
/*
* If the LV is a part of mirror segment,
* the mirrored LV also should be cleaned up.
* Clean-up is currently done by caller (_make_vg_consistent()).
*/
if ((lv_info(cmd, lv, &info, 0, 0) && info.exists) ||
find_mirror_seg(first_seg(lv))) {
if (!replace_lv_with_error_segment(lv))
return_0;
static int _consolidate_vg(struct cmd_context *cmd, struct volume_group *vg)
{
struct pv_list *pvl;
struct lv_list *lvl;
int r = 1;
dm_list_iterate_items(lvl, &vg->lvs)
if (lvl->lv->status & PARTIAL_LV) {
log_warn("WARNING: Partial LV %s needs to be repaired "
"or removed. ", lvl->lv->name);
r = 0;
}
if (!r) {
cmd->handles_missing_pvs = 1;
log_warn("WARNING: There are still partial LVs in VG %s.", vg->name);
log_warn("To remove them unconditionally use: vgreduce --removemissing --force.");
log_warn("Proceeding to remove empty missing PVs.");
}
dm_list_iterate_items(pvl, &vg->pvs) {
if (pvl->pv->dev && !(pvl->pv->status & MISSING_PV))
continue;
if (r && !_remove_pv(vg, pvl, 0))
return_0;
}
/* Are any segments of this LV on missing PVs? */
dm_list_iterate_items(seg, &lv->segments) {
for (s = 0; s < seg->area_count; s++) {
if (seg_type(seg, s) != AREA_PV)
continue;
/* FIXME Also check for segs on deleted LVs (incl pvmove) */
if (!only_mirror_images_found) {
log_error("Aborting because --mirrorsonly was specified.");
return 0;
}
/*
* Remove missing PVs. FIXME: This duplicates _consolidate_vg above,
* but we cannot use that right now, since the LV removal code in this
* function leaves the VG in a "somewhat inconsistent" state and
* _consolidate_vg doesn't like that -- specifically, mirrors are fixed
* up *after* the PVs are removed. All this should be gradually
* superseded by lvconvert --repair.
*/
dm_list_iterate_safe(pvh, pvht, &vg->pvs) {
pvl = dm_list_item(pvh, struct pv_list);
if (pvl->pv->dev)
continue;
if (!_remove_pv(vg, pvl, 0))
return_0;
}
/* FIXME Recovery. For now people must clean up by hand. */
if (!dm_list_empty(&lvs_changed)) {
if (!vg_write(vg)) {
log_error("Failed to write out a consistent VG for %s",
vg->name);
return 0;
}
if (!test_mode()) {
/* Suspend lvs_changed */
if (!suspend_lvs(cmd, &lvs_changed)) {
stack;
vg_revert(vg);
return 0;
}
}
if (!vg_commit(vg)) {
log_error("Failed to commit consistent VG for %s",
vg->name);
vg_revert(vg);
return 0;
}
if (!test_mode()) {
if (!resume_lvs(cmd, &lvs_changed)) {
log_error("Failed to resume LVs using error segments.");
return 0;
}
}
lvs_changed_altered:
/* Remove lost mirror images from mirrors */
dm_list_iterate_items(lvl, &vg->lvs) {
mirrored_seg_altered:
mirrored_seg = first_seg(lvl->lv);
if (!seg_is_mirrored(mirrored_seg))
continue;
for (s = 0; s < mirrored_seg->area_count; s++) {
dm_list_iterate_items_safe(lvl2, lvlt, &lvs_changed) {
if (seg_type(mirrored_seg, s) != AREA_LV ||
lvl2->lv != seg_lv(mirrored_seg, s))
continue;
dm_list_del(&lvl2->list);
if (!shift_mirror_images(mirrored_seg, s))
return_0;
mimages--; /* FIXME Assumes uniqueness */
}
}
if (mirrored_seg->log_lv) {
dm_list_iterate_items(seg, &mirrored_seg->log_lv->segments) {
/* FIXME: The second test shouldn't be required */
if ((seg->segtype ==
get_segtype_from_string(vg->cmd, "error"))) {
log_print("The log device for %s/%s has failed.",
vg->name, mirrored_seg->lv->name);
remove_log = 1;
break;
}
if (!strcmp(seg->segtype->name, "error")) {
log_print("Log device for %s/%s has failed.",
vg->name, mirrored_seg->lv->name);
remove_log = 1;
break;
}
}
}
if ((mimages != mirrored_seg->area_count) || remove_log){
if (!reconfigure_mirror_images(mirrored_seg, mimages,
NULL, remove_log))
return_0;
if (!vg_write(vg)) {
log_error("Failed to write out updated "
"VG for %s", vg->name);
return 0;
}
if (!vg_commit(vg)) {
log_error("Failed to commit updated VG "
"for %s", vg->name);
vg_revert(vg);
return 0;
}
/* mirrored LV no longer has valid mimages.
* So add it to lvs_changed for removal.
* For this LV may be an area of other mirror,
* restart the loop. */
if (!mimages) {
if (!_remove_lv(cmd, lvl->lv,
&list_unsafe, &lvs_changed))
return_0;
goto lvs_changed_altered;
}
/* As a result of reconfigure_mirror_images(),
* first_seg(lv) may now be different seg.
* e.g. a temporary layer might be removed.
* So check the mirrored_seg again. */
goto mirrored_seg_altered;
}
}
if (!deactivate_lv(cmd, lvl->lv)) {
log_error("Failed to deactivate LV %s",
lvl->lv->name);
/*
* We failed to deactivate.
* Probably because this was a mirror log.
* Don't try to lv_remove it.
* Continue work on others.
*/
dm_list_del(&lvl->list);
}
}
}
if (!vg_split_mdas(cmd, vg, orphan_vg) || !vg->pv_count) {
log_error("Cannot remove final metadata area on \"%s\" from \"%s\"",
name, vg->name);
goto bad;
}
if (!vg_write(vg) || !vg_commit(vg)) {
log_error("Removal of physical volume \"%s\" from "
"\"%s\" failed", name, vg->name);
goto bad;
}
if (!pv_write(cmd, pv, NULL, INT64_C(-1))) {
log_error("Failed to clear metadata from physical "
"volume \"%s\" "
"after removal from \"%s\"", name, vg->name);
goto bad;
}
backup(vg);
log_print("Removed \"%s\" from volume group \"%s\"", name, vg->name);
r = ECMD_PROCESSED;
bad:
unlock_and_release_vg(cmd, orphan_vg, VG_ORPHANS);
return r;
}
int vgreduce(struct cmd_context *cmd, int argc, char **argv)
{
struct volume_group *vg;
char *vg_name;
int ret = ECMD_FAILED;
int fixed = 1;
int repairing = arg_count(cmd, removemissing_ARG);
int saved_ignore_suspended_devices = ignore_suspended_devices();
if (!argc && !repairing) {
log_error("Please give volume group name and "
"physical volume paths");
return EINVALID_CMD_LINE;
}
if (!argc && repairing) {
log_error("Please give volume group name");
return EINVALID_CMD_LINE;
}
/* FIXME We want to allow read-only VGs to be changed here? */
if (vg_read_error(vg) && vg_read_error(vg) != FAILED_READ_ONLY
&& !arg_count(cmd, removemissing_ARG))
goto_out;
if (repairing) {
if (!vg_read_error(vg) && !vg_missing_pv_count(vg)) {
log_error("Volume group \"%s\" is already consistent",
vg_name);
ret = ECMD_PROCESSED;
goto out;
}
vg_release(vg);
log_verbose("Trying to open VG %s for recovery...", vg_name);
/* FIXME: Pass private struct through to all these functions */
/* and update in batch here? */
ret = process_each_pv(cmd, argc, argv, vg, READ_FOR_UPDATE, 0, NULL,
_vgreduce_single);