1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

pv/vgchange --uuid to change (non-random) UUIDs to random values

This commit is contained in:
Alasdair Kergon 2004-01-13 18:42:05 +00:00
parent cc8f6e3dbc
commit 15e6db353d
3 changed files with 101 additions and 52 deletions

View File

@ -315,17 +315,18 @@ xx(lvscan,
xx(pvchange,
"Change attributes of physical volume(s)",
"pvchange\n"
"\t[-a|--all]\n"
"\t[-A|--autobackup y|n]\n"
"\t[-d|--debug]\n"
"\t[-h|--help]\n"
"\t[-t|--test]\n"
"\t[-u|--uuid]\n"
"\t[-x|--allocatable y|n]\n"
"\t[-v|--verbose]\n"
"\t[--version]" "\n"
"\t[-a|--all]\n"
"\t[-t|--test]\n"
"\t[-x|--allocatable y|n]\n"
"\t[PhysicalVolumePath...]\n",
all_ARG, allocatable_ARG, allocation_ARG, autobackup_ARG, test_ARG)
all_ARG, allocatable_ARG, allocation_ARG, autobackup_ARG, test_ARG, uuid_ARG)
xx(pvcreate,
"Initialize physical volume(s) for use by LVM",
@ -531,6 +532,7 @@ xx(vgchange,
"\t[-h|--help] " "\n"
"\t[--ignorelockingfailure]\n"
"\t[-t|--test]" "\n"
"\t[-u|--uuid] " "\n"
"\t[-v|--verbose] " "\n"
"\t[--version]" "\n"
"\t{-a|--available {y|n} |" "\n"
@ -539,7 +541,8 @@ xx(vgchange,
"\t[VolumeGroupName...]\n",
allocation_ARG, autobackup_ARG, available_ARG, ignorelockingfailure_ARG,
logicalvolume_ARG, partial_ARG, resizeable_ARG, resizable_ARG, test_ARG)
logicalvolume_ARG, partial_ARG, resizeable_ARG, resizable_ARG, test_ARG,
uuid_ARG)
xx(vgck,
"Check the consistency of volume group(s)",

View File

@ -33,8 +33,11 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
const char *pv_name = dev_name(pv->dev);
int consistent = 1;
int allocatable =
!strcmp(arg_str_value(cmd, allocatable_ARG, "n"), "y");
int allocatable = 0;
if (arg_count(cmd, allocatable_ARG))
allocatable = !strcmp(arg_str_value(cmd, allocatable_ARG, "n"),
"y");
/* If in a VG, must change using volume group. */
if (*pv->vg_name) {
@ -43,7 +46,7 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
if (!lock_vol(cmd, pv->vg_name, LCK_VG_WRITE)) {
log_error("Can't get lock for %s", pv->vg_name);
return ECMD_FAILED;
return 0;
}
if (!(vg = vg_read(cmd, pv->vg_name, &consistent))) {
@ -56,13 +59,13 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
if (vg->status & EXPORTED_VG) {
unlock_vg(cmd, pv->vg_name);
log_error("Volume group \"%s\" is exported", vg->name);
return ECMD_FAILED;
return 0;
}
if (!(vg->status & LVM_WRITE)) {
unlock_vg(cmd, pv->vg_name);
log_error("Volume group \"%s\" is read-only", vg->name);
return ECMD_FAILED;
return 0;
}
if (!(pvl = find_pv_in_vg(vg, pv_name))) {
@ -72,13 +75,19 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
pv_name, vg->name);
return 0;
}
if (arg_count(cmd, uuid_ARG) && lvs_in_vg_activated(vg)) {
unlock_vg(cmd, pv->vg_name);
log_error("Volume group containing %s has active "
"logical volumes", pv_name);
return 0;
}
pv = pvl->pv;
if (!archive(vg))
return 0;
} else {
if (!lock_vol(cmd, ORPHAN, LCK_VG_WRITE)) {
log_error("Can't get lock for orphans");
return ECMD_FAILED;
return 0;
}
if (!(pv = pv_read(cmd, pv_name, &mdas, &sector))) {
@ -89,43 +98,48 @@ static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
}
if (!*pv->vg_name &&
!(pv->fmt->features & FMT_ORPHAN_ALLOCATABLE)) {
log_error("Allocatability not supported by orphan "
"%s format PV %s", pv->fmt->name, pv_name);
unlock_vg(cmd, ORPHAN);
return 0;
}
/* change allocatability for a PV */
if (allocatable && (pv->status & ALLOCATABLE_PV)) {
log_error("Physical volume \"%s\" is already allocatable",
pv_name);
if (*pv->vg_name)
unlock_vg(cmd, pv->vg_name);
else
if (arg_count(cmd, allocatable_ARG)) {
if (!*pv->vg_name &&
!(pv->fmt->features & FMT_ORPHAN_ALLOCATABLE)) {
log_error("Allocatability not supported by orphan "
"%s format PV %s", pv->fmt->name, pv_name);
unlock_vg(cmd, ORPHAN);
return 1;
}
return 0;
}
if (!allocatable && !(pv->status & ALLOCATABLE_PV)) {
log_error("Physical volume \"%s\" is already unallocatable",
pv_name);
if (*pv->vg_name)
unlock_vg(cmd, pv->vg_name);
else
unlock_vg(cmd, ORPHAN);
return 1;
}
/* change allocatability for a PV */
if (allocatable && (pv->status & ALLOCATABLE_PV)) {
log_error("Physical volume \"%s\" is already "
"allocatable", pv_name);
if (*pv->vg_name)
unlock_vg(cmd, pv->vg_name);
else
unlock_vg(cmd, ORPHAN);
return 1;
}
if (allocatable) {
log_verbose("Setting physical volume \"%s\" allocatable",
pv_name);
pv->status |= ALLOCATABLE_PV;
if (!allocatable && !(pv->status & ALLOCATABLE_PV)) {
log_error("Physical volume \"%s\" is already "
"unallocatable", pv_name);
if (*pv->vg_name)
unlock_vg(cmd, pv->vg_name);
else
unlock_vg(cmd, ORPHAN);
return 1;
}
if (allocatable) {
log_verbose("Setting physical volume \"%s\" "
"allocatable", pv_name);
pv->status |= ALLOCATABLE_PV;
} else {
log_verbose("Setting physical volume \"%s\" NOT "
"allocatable", pv_name);
pv->status &= ~ALLOCATABLE_PV;
}
} else {
log_verbose("Setting physical volume \"%s\" NOT allocatable",
pv_name);
pv->status &= ~ALLOCATABLE_PV;
/* --uuid: Change PV ID randomly */
id_create(&pv->id);
}
log_verbose("Updating physical volume \"%s\"", pv_name);
@ -168,8 +182,9 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv)
list_init(&mdas);
if (arg_count(cmd, allocatable_ARG) == 0) {
log_error("Please give the x option");
if (arg_count(cmd, allocatable_ARG) +
+ arg_count(cmd, uuid_ARG) != 1) {
log_error("Please give exactly one option of -x or --uuid");
return EINVALID_CMD_LINE;
}
@ -211,8 +226,8 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv)
log_print("%d physical volume%s changed / %d physical volume%s "
"not changed",
done, done > 1 ? "s" : "",
total - done, total - done > 1 ? "s" : "");
done, done == 1 ? "" : "s",
total - done, (total - done) == 1 ? "" : "s");
return (total == done) ? ECMD_PROCESSED : ECMD_FAILED;
}

View File

@ -163,6 +163,34 @@ static int _vgchange_logicalvolume(struct cmd_context *cmd,
return ECMD_PROCESSED;
}
static int _vgchange_uuid(struct cmd_context *cmd, struct volume_group *vg)
{
struct lv_list *lvl;
if (lvs_in_vg_activated(vg)) {
log_error("Volume group has active logical volumes");
return ECMD_FAILED;
}
if (!archive(vg))
return ECMD_FAILED;
id_create(&vg->id);
list_iterate_items(lvl, &vg->lvs) {
memcpy(&lvl->lv->lvid, &vg->id, sizeof(vg->id));
}
if (!vg_write(vg) || !vg_commit(vg))
return ECMD_FAILED;
backup(vg);
log_print("Volume group \"%s\" successfully changed", vg->name);
return ECMD_PROCESSED;
}
static int vgchange_single(struct cmd_context *cmd, const char *vg_name,
struct volume_group *vg, int consistent,
void *handle)
@ -201,6 +229,9 @@ static int vgchange_single(struct cmd_context *cmd, const char *vg_name,
else if (arg_count(cmd, logicalvolume_ARG))
r = _vgchange_logicalvolume(cmd, vg);
else if (arg_count(cmd, uuid_ARG))
r = _vgchange_uuid(cmd, vg);
return r;
}
@ -208,14 +239,14 @@ int vgchange(struct cmd_context *cmd, int argc, char **argv)
{
if (!
(arg_count(cmd, available_ARG) + arg_count(cmd, logicalvolume_ARG) +
arg_count(cmd, resizeable_ARG))) {
log_error("One of -a, -l or -x options required");
arg_count(cmd, resizeable_ARG) + arg_count(cmd, uuid_ARG))) {
log_error("One of -a, -l, --uuid or -x options required");
return EINVALID_CMD_LINE;
}
if (arg_count(cmd, available_ARG) + arg_count(cmd, logicalvolume_ARG) +
arg_count(cmd, resizeable_ARG) > 1) {
log_error("Only one of -a, -l or -x options allowed");
arg_count(cmd, resizeable_ARG) + arg_count(cmd, uuid_ARG) > 1) {
log_error("Only one of -a, -l, --uuid or -x options allowed");
return EINVALID_CMD_LINE;
}