1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-02-24 17:57:48 +03:00

lvmdevices: --deldev using device id

When used with --deviceidtype, --deldev can specify
a device id to remove.
This commit is contained in:
David Teigland 2022-04-06 12:29:26 -05:00
parent 151ce8b276
commit fb7698b0ce
7 changed files with 80 additions and 7 deletions

View File

@ -935,7 +935,7 @@ struct dev_use *get_du_for_devname(struct cmd_context *cmd, const char *devname)
return NULL;
}
static struct dev_use *_get_du_for_device_id(struct cmd_context *cmd, uint16_t idtype, const char *idname)
struct dev_use *get_du_for_device_id(struct cmd_context *cmd, uint16_t idtype, const char *idname)
{
struct dev_use *du;
@ -1128,7 +1128,7 @@ id_done:
du_devname = get_du_for_devname(cmd, dev_name(dev));
/* Is there already an entry using the device_id for this device? */
du_devid = _get_du_for_device_id(cmd, id->idtype, id->idname);
du_devid = get_du_for_device_id(cmd, id->idtype, id->idname);
if (du_dev)
log_debug("device_id_add %s pvid %s matches entry %p dev %s",
@ -1322,7 +1322,7 @@ void device_id_update_vg_uuid(struct cmd_context *cmd, struct volume_group *vg,
memcpy(old_idname+4, old_vgid, ID_LEN);
memcpy(old_idname+4+ID_LEN, &lvl->lv->lvid.id[1], ID_LEN);
if ((du = _get_du_for_device_id(cmd, DEV_ID_TYPE_LVMLV_UUID, old_idname))) {
if ((du = get_du_for_device_id(cmd, DEV_ID_TYPE_LVMLV_UUID, old_idname))) {
log_debug("device_id update %s pvid %s vgid %s to %s",
du->devname ?: ".", du->pvid ?: ".", old_vgid, new_vgid);
memcpy(du->idname+4, new_vgid, ID_LEN);

View File

@ -43,6 +43,7 @@ struct dev_use *get_du_for_devno(struct cmd_context *cmd, dev_t devno);
struct dev_use *get_du_for_dev(struct cmd_context *cmd, struct device *dev);
struct dev_use *get_du_for_pvid(struct cmd_context *cmd, const char *pvid);
struct dev_use *get_du_for_devname(struct cmd_context *cmd, const char *devname);
struct dev_use *get_du_for_device_id(struct cmd_context *cmd, uint16_t idtype, const char *idname);
char *devices_file_version(void);
int devices_file_exists(struct cmd_context *cmd);

View File

@ -85,6 +85,7 @@ is used for dm crypt devices, reported by sysfs.
.IP \[bu] 2
.B md_uuid
is used for md devices, reported by sysfs.
.IP \[bu] 2
.B lvmlv_uuid
is used if a PV is placed on top of an lvm LV, reported by sysfs.
.IP \[bu] 2

View File

@ -135,6 +135,24 @@ lvmdevices --deldev "$LOOP1"
not grep "$LOOP1" $DF
lvmdevices --deldev "$LOOP2"
not grep "$LOOP2" $DF
rm $DF
# deldev using idname
lvmdevices --adddev "$LOOP1"
lvmdevices --adddev "$LOOP2"
vgcreate $vg "$LOOP1" "$LOOP2"
IDNAME1=`pvs "$LOOP1" --noheading -o deviceid | tr -d - | awk '{print $1}'`
IDNAME2=`pvs "$LOOP2" --noheading -o deviceid | tr -d - | awk '{print $1}'`
lvmdevices --deldev "$IDNAME2" --deviceidtype loop_file
not grep "$IDNAME2" $DF
not grep "$LOOP2" $DF
lvmdevices --deldev "$IDNAME1" --deviceidtype loop_file
not grep "$IDNAME1" $DF
not grep "$LOOP1" $DF
lvmdevices --adddev "$LOOP1"
lvmdevices --adddev "$LOOP2"
vgremove $vg
rm $DF
# add/delpvid with default idtype loop_file
lvmdevices --addpvid "$PVID1"
@ -151,6 +169,7 @@ not grep "$PVID1" $DF
lvmdevices --delpvid "$PVID2"
not grep "$LOOP2" $DF
not grep "$PVID2" $DF
rm $DF
# add/deldev with non-default idtype devname
lvmdevices --adddev "$LOOP1" --deviceidtype devname
@ -165,6 +184,7 @@ lvmdevices --deldev "$LOOP1"
not grep "$LOOP1" $DF
lvmdevices --deldev "$LOOP2"
not grep "$LOOP2" $DF
rm $DF
# add/delpvid with non-default idtype devname
lvmdevices --addpvid "$PVID1" --deviceidtype devname
@ -179,6 +199,7 @@ lvmdevices --deldev "$LOOP1"
not grep "$LOOP1" $DF
lvmdevices --deldev "$LOOP2"
not grep "$LOOP2" $DF
rm $DF
# add/deldev when dev is missing, using default idtype
lvmdevices --adddev "$LOOP1"

View File

@ -46,8 +46,12 @@ arg(addtag_ARG, '\0', "addtag", tag_VAL, ARG_GROUPABLE, 0,
arg(adddev_ARG, '\0', "adddev", pv_VAL, 0, 0,
"Add a device to the devices file.\n")
arg(deldev_ARG, '\0', "deldev", pv_VAL, 0, 0,
"Remove a device from the devices file.\n")
arg(deldev_ARG, '\0', "deldev", string_VAL, 0, 0,
"Remove a device from the devices file.\n"
"When used alone, --deldev specifies a device name.\n"
"When used with --deviceidtype, --deldev specifies a device id.\n")
arg(addpvid_ARG, '\0', "addpvid", string_VAL, 0, 0,
"Find a device with the PVID and add the device to the devices file.\n")
arg(delpvid_ARG, '\0', "delpvid", string_VAL, 0, 0,

View File

@ -1434,7 +1434,8 @@ OO: --deviceidtype String
ID: lvmdevices_edit
DESC: Add a device to the devices file.
lvmdevices --deldev PV
lvmdevices --deldev PV|String
OO: --deviceidtype String
ID: lvmdevices_edit
DESC: Remove a device from the devices file.

View File

@ -416,12 +416,15 @@ int lvmdevices(struct cmd_context *cmd, int argc, char **argv)
goto out;
}
if (arg_is_set(cmd, deldev_ARG)) {
if (arg_is_set(cmd, deldev_ARG) && !arg_is_set(cmd, deviceidtype_ARG)) {
const char *devname;
if (!(devname = arg_str_value(cmd, deldev_ARG, NULL)))
goto_bad;
if (strncmp(devname, "/dev/", 5))
log_warn("WARNING: to remove a device by device id, include --deviceidtype.");
/*
* No filter because we always want to allow removing a device
* by name from the devices file.
@ -453,6 +456,48 @@ int lvmdevices(struct cmd_context *cmd, int argc, char **argv)
goto out;
}
/*
* By itself, --deldev <devname> specifies a device name to remove.
* With an id type specified, --deldev specifies a device id to remove:
* --deldev <idname> --deviceidtype <idtype>
*/
if (arg_is_set(cmd, deldev_ARG) && arg_is_set(cmd, deviceidtype_ARG)) {
const char *idtype_str = arg_str_value(cmd, deviceidtype_ARG, NULL);
const char *idname = arg_str_value(cmd, deldev_ARG, NULL);
int idtype;
if (!idtype_str || !idname || !strlen(idname) || !strlen(idtype_str))
goto_bad;
if (!(idtype = idtype_from_str(idtype_str))) {
log_error("Unknown device_id type.");
goto_bad;
}
if (!strncmp(idname, "/dev/", 5))
log_warn("WARNING: to remove a device by name, do not include --deviceidtype.");
if (!(du = get_du_for_device_id(cmd, idtype, idname))) {
log_error("No devices file entry with device id %s %s.", idtype_str, idname);
goto_bad;
}
dev = du->dev;
if (dev && dev_is_used_by_active_lv(cmd, dev, NULL, NULL, NULL, NULL)) {
if (!arg_count(cmd, yes_ARG) &&
yes_no_prompt("Device %s is used by an active LV, continue to remove? ", dev_name(dev)) == 'n') {
log_error("Device not removed.");
goto bad;
}
}
dm_list_del(&du->list);
free_du(du);
device_ids_write(cmd);
goto out;
}
if (arg_is_set(cmd, delpvid_ARG)) {
struct id id;
char pvid[ID_LEN+1] = { 0 };