mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
nodedev: implement virNodeDevice(Get|Set)Autostart()
Implement autostart functionality for mediated devices. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
c6607a25b9
commit
dc60f91530
@ -154,6 +154,7 @@ struct _virNodeDevCapMdev {
|
|||||||
virMediatedDeviceAttr **attributes;
|
virMediatedDeviceAttr **attributes;
|
||||||
size_t nattributes;
|
size_t nattributes;
|
||||||
char *parent_addr;
|
char *parent_addr;
|
||||||
|
bool autostart;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct _virNodeDevCapPCIDev virNodeDevCapPCIDev;
|
typedef struct _virNodeDevCapPCIDev virNodeDevCapPCIDev;
|
||||||
|
@ -41,6 +41,7 @@ struct _virNodeDeviceObj {
|
|||||||
used by testdriver */
|
used by testdriver */
|
||||||
bool active;
|
bool active;
|
||||||
bool persistent;
|
bool persistent;
|
||||||
|
bool autostart;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _virNodeDeviceObjList {
|
struct _virNodeDeviceObjList {
|
||||||
@ -1034,6 +1035,21 @@ virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
virNodeDeviceObjIsAutostart(virNodeDeviceObj *obj)
|
||||||
|
{
|
||||||
|
return obj->autostart;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
virNodeDeviceObjSetAutostart(virNodeDeviceObj *obj,
|
||||||
|
bool autostart)
|
||||||
|
{
|
||||||
|
obj->autostart = autostart;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
typedef struct _PredicateHelperData PredicateHelperData;
|
typedef struct _PredicateHelperData PredicateHelperData;
|
||||||
struct _PredicateHelperData {
|
struct _PredicateHelperData {
|
||||||
virNodeDeviceObjListPredicate predicate;
|
virNodeDeviceObjListPredicate predicate;
|
||||||
|
@ -137,6 +137,12 @@ virNodeDeviceObjIsPersistent(virNodeDeviceObj *obj);
|
|||||||
void
|
void
|
||||||
virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
|
virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
|
||||||
bool persistent);
|
bool persistent);
|
||||||
|
bool
|
||||||
|
virNodeDeviceObjIsAutostart(virNodeDeviceObj *obj);
|
||||||
|
|
||||||
|
void
|
||||||
|
virNodeDeviceObjSetAutostart(virNodeDeviceObj *obj,
|
||||||
|
bool autostart);
|
||||||
|
|
||||||
typedef bool (*virNodeDeviceObjListPredicate)(virNodeDeviceObj *obj,
|
typedef bool (*virNodeDeviceObjListPredicate)(virNodeDeviceObj *obj,
|
||||||
const void *opaque);
|
const void *opaque);
|
||||||
|
@ -1289,6 +1289,7 @@ virNetworkPortDefSaveStatus;
|
|||||||
virNodeDeviceObjEndAPI;
|
virNodeDeviceObjEndAPI;
|
||||||
virNodeDeviceObjGetDef;
|
virNodeDeviceObjGetDef;
|
||||||
virNodeDeviceObjIsActive;
|
virNodeDeviceObjIsActive;
|
||||||
|
virNodeDeviceObjIsAutostart;
|
||||||
virNodeDeviceObjIsPersistent;
|
virNodeDeviceObjIsPersistent;
|
||||||
virNodeDeviceObjListAssignDef;
|
virNodeDeviceObjListAssignDef;
|
||||||
virNodeDeviceObjListExport;
|
virNodeDeviceObjListExport;
|
||||||
@ -1306,6 +1307,7 @@ virNodeDeviceObjListNumOfDevices;
|
|||||||
virNodeDeviceObjListRemove;
|
virNodeDeviceObjListRemove;
|
||||||
virNodeDeviceObjListRemoveLocked;
|
virNodeDeviceObjListRemoveLocked;
|
||||||
virNodeDeviceObjSetActive;
|
virNodeDeviceObjSetActive;
|
||||||
|
virNodeDeviceObjSetAutostart;
|
||||||
virNodeDeviceObjSetPersistent;
|
virNodeDeviceObjSetPersistent;
|
||||||
|
|
||||||
|
|
||||||
|
@ -621,11 +621,12 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDef *def, char **buf)
|
|||||||
size_t i;
|
size_t i;
|
||||||
virNodeDevCapMdev *mdev = &def->caps->data.mdev;
|
virNodeDevCapMdev *mdev = &def->caps->data.mdev;
|
||||||
g_autoptr(virJSONValue) json = virJSONValueNewObject();
|
g_autoptr(virJSONValue) json = virJSONValueNewObject();
|
||||||
|
const char *startval = mdev->autostart ? "auto" : "manual";
|
||||||
|
|
||||||
if (virJSONValueObjectAppendString(json, "mdev_type", mdev->type) < 0)
|
if (virJSONValueObjectAppendString(json, "mdev_type", mdev->type) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (virJSONValueObjectAppendString(json, "start", "manual") < 0)
|
if (virJSONValueObjectAppendString(json, "start", startval) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (mdev->attributes) {
|
if (mdev->attributes) {
|
||||||
@ -1014,6 +1015,46 @@ virMdevctlStart(virNodeDeviceDef *def)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* gets a virCommand object that executes a mdevctl command to set the
|
||||||
|
* 'autostart' property of the device to the specified value
|
||||||
|
*/
|
||||||
|
virCommand*
|
||||||
|
nodeDeviceGetMdevctlSetAutostartCommand(virNodeDeviceDef *def,
|
||||||
|
bool autostart,
|
||||||
|
char **errmsg)
|
||||||
|
{
|
||||||
|
virCommand *cmd = virCommandNewArgList(MDEVCTL,
|
||||||
|
"modify",
|
||||||
|
"--uuid",
|
||||||
|
def->caps->data.mdev.uuid,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (autostart)
|
||||||
|
virCommandAddArg(cmd, "--auto");
|
||||||
|
else
|
||||||
|
virCommandAddArg(cmd, "--manual");
|
||||||
|
|
||||||
|
virCommandSetErrorBuffer(cmd, errmsg);
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
virMdevctlSetAutostart(virNodeDeviceDef *def, bool autostart, char **errmsg)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
g_autoptr(virCommand) cmd = NULL;
|
||||||
|
|
||||||
|
cmd = nodeDeviceGetMdevctlSetAutostartCommand(def, autostart, errmsg);
|
||||||
|
|
||||||
|
if (virCommandRun(cmd, &status) < 0 || status != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
virCommand*
|
virCommand*
|
||||||
nodeDeviceGetMdevctlListCommand(bool defined,
|
nodeDeviceGetMdevctlListCommand(bool defined,
|
||||||
char **output,
|
char **output,
|
||||||
@ -1066,6 +1107,7 @@ nodeDeviceParseMdevctlChildDevice(const char *parent,
|
|||||||
virJSONValue *attrs;
|
virJSONValue *attrs;
|
||||||
g_autoptr(virNodeDeviceDef) child = g_new0(virNodeDeviceDef, 1);
|
g_autoptr(virNodeDeviceDef) child = g_new0(virNodeDeviceDef, 1);
|
||||||
virNodeDeviceObj *parent_obj;
|
virNodeDeviceObj *parent_obj;
|
||||||
|
const char *start = NULL;
|
||||||
|
|
||||||
/* the child object should have a single key equal to its uuid.
|
/* the child object should have a single key equal to its uuid.
|
||||||
* The value is an object describing the properties of the mdev */
|
* The value is an object describing the properties of the mdev */
|
||||||
@ -1094,6 +1136,8 @@ nodeDeviceParseMdevctlChildDevice(const char *parent,
|
|||||||
mdev->parent_addr = g_strdup(parent);
|
mdev->parent_addr = g_strdup(parent);
|
||||||
mdev->type =
|
mdev->type =
|
||||||
g_strdup(virJSONValueObjectGetString(props, "mdev_type"));
|
g_strdup(virJSONValueObjectGetString(props, "mdev_type"));
|
||||||
|
start = virJSONValueObjectGetString(props, "start");
|
||||||
|
mdev->autostart = STREQ_NULLABLE(start, "auto");
|
||||||
|
|
||||||
attrs = virJSONValueObjectGet(props, "attrs");
|
attrs = virJSONValueObjectGet(props, "attrs");
|
||||||
|
|
||||||
@ -1346,6 +1390,7 @@ nodeDeviceUpdateMediatedDevice(virNodeDeviceDef *def)
|
|||||||
|
|
||||||
/* all devices returned by virMdevctlListDefined() are persistent */
|
/* all devices returned by virMdevctlListDefined() are persistent */
|
||||||
virNodeDeviceObjSetPersistent(obj, true);
|
virNodeDeviceObjSetPersistent(obj, true);
|
||||||
|
virNodeDeviceObjSetAutostart(obj, def->caps->data.mdev.autostart);
|
||||||
|
|
||||||
if (!defined)
|
if (!defined)
|
||||||
event = virNodeDeviceEventLifecycleNew(name,
|
event = virNodeDeviceEventLifecycleNew(name,
|
||||||
@ -1649,10 +1694,12 @@ removeMissingPersistentMdev(virNodeDeviceObj *obj,
|
|||||||
|
|
||||||
/* The device is active, but no longer defined by mdevctl. Keep the device
|
/* The device is active, but no longer defined by mdevctl. Keep the device
|
||||||
* in the list, but mark it as non-persistent */
|
* in the list, but mark it as non-persistent */
|
||||||
if (virNodeDeviceObjIsActive(obj))
|
if (virNodeDeviceObjIsActive(obj)) {
|
||||||
|
virNodeDeviceObjSetAutostart(obj, false);
|
||||||
virNodeDeviceObjSetPersistent(obj, false);
|
virNodeDeviceObjSetPersistent(obj, false);
|
||||||
else
|
} else {
|
||||||
remove = true;
|
remove = true;
|
||||||
|
}
|
||||||
|
|
||||||
virObjectEventStateQueue(driver->nodeDeviceEventState, event);
|
virObjectEventStateQueue(driver->nodeDeviceEventState, event);
|
||||||
|
|
||||||
@ -1762,6 +1809,92 @@ nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst,
|
|||||||
if (virMediatedDeviceAttrsCopy(dstmdev, srcmdev))
|
if (virMediatedDeviceAttrsCopy(dstmdev, srcmdev))
|
||||||
ret = true;
|
ret = true;
|
||||||
|
|
||||||
|
if (dstmdev->autostart != srcmdev->autostart) {
|
||||||
|
ret = true;
|
||||||
|
dstmdev->autostart = srcmdev->autostart;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
nodeDeviceSetAutostart(virNodeDevice *device,
|
||||||
|
int autostart)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
virNodeDeviceObj *obj = NULL;
|
||||||
|
virNodeDeviceDef *def = NULL;
|
||||||
|
|
||||||
|
if (nodeDeviceInitWait() < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
||||||
|
return -1;
|
||||||
|
def = virNodeDeviceObjGetDef(obj);
|
||||||
|
|
||||||
|
if (virNodeDeviceSetAutostartEnsureACL(device->conn, def) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) {
|
||||||
|
if (!virNodeDeviceObjIsPersistent(obj)) {
|
||||||
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
||||||
|
"%s", _("cannot set autostart for transient device"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (autostart != virNodeDeviceObjIsAutostart(obj)) {
|
||||||
|
g_autofree char *errmsg = NULL;
|
||||||
|
|
||||||
|
if (virMdevctlSetAutostart(def, autostart, &errmsg) < 0) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("Unable to set autostart on '%s': %s"),
|
||||||
|
def->name,
|
||||||
|
errmsg && errmsg[0] != '\0' ? errmsg : _("Unknown Error"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
/* Due to mdevctl performance issues, it may take several seconds
|
||||||
|
* to re-query mdevctl for the defined devices. Because the mdevctl
|
||||||
|
* command returned without an error status, assume it was
|
||||||
|
* successful and set the object status directly here rather than
|
||||||
|
* waiting for the next query */
|
||||||
|
virNodeDeviceObjSetAutostart(obj, autostart);
|
||||||
|
}
|
||||||
|
ret = 0;
|
||||||
|
} else {
|
||||||
|
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
|
||||||
|
_("Unsupported device type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
virNodeDeviceObjEndAPI(&obj);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
nodeDeviceGetAutostart(virNodeDevice *device,
|
||||||
|
int *autostart)
|
||||||
|
{
|
||||||
|
virNodeDeviceObj *obj = NULL;
|
||||||
|
virNodeDeviceDef *def = NULL;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (nodeDeviceInitWait() < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
||||||
|
return -1;
|
||||||
|
def = virNodeDeviceObjGetDef(obj);
|
||||||
|
|
||||||
|
if (virNodeDeviceGetAutostartEnsureACL(device->conn, def) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
*autostart = virNodeDeviceObjIsAutostart(obj);
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
virNodeDeviceObjEndAPI(&obj);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,3 +177,16 @@ int nodeDeviceDefPostParse(virNodeDeviceDef *def,
|
|||||||
|
|
||||||
int nodeDeviceDefValidate(virNodeDeviceDef *def,
|
int nodeDeviceDefValidate(virNodeDeviceDef *def,
|
||||||
void *opaque);
|
void *opaque);
|
||||||
|
|
||||||
|
int
|
||||||
|
nodeDeviceSetAutostart(virNodeDevice *dev,
|
||||||
|
int autostart);
|
||||||
|
|
||||||
|
int
|
||||||
|
nodeDeviceGetAutostart(virNodeDevice *dev,
|
||||||
|
int *autostart);
|
||||||
|
|
||||||
|
virCommand*
|
||||||
|
nodeDeviceGetMdevctlSetAutostartCommand(virNodeDeviceDef *def,
|
||||||
|
bool autostart,
|
||||||
|
char **errmsg);
|
||||||
|
@ -1504,6 +1504,8 @@ udevAddOneDevice(struct udev_device *device)
|
|||||||
bool new_device = true;
|
bool new_device = true;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
bool was_persistent = false;
|
bool was_persistent = false;
|
||||||
|
bool autostart = false;
|
||||||
|
bool is_mdev;
|
||||||
|
|
||||||
def = g_new0(virNodeDeviceDef, 1);
|
def = g_new0(virNodeDeviceDef, 1);
|
||||||
|
|
||||||
@ -1525,12 +1527,16 @@ udevAddOneDevice(struct udev_device *device)
|
|||||||
if (udevSetParent(device, def) != 0)
|
if (udevSetParent(device, def) != 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
|
is_mdev = def->caps->data.type == VIR_NODE_DEV_CAP_MDEV;
|
||||||
|
|
||||||
if ((obj = virNodeDeviceObjListFindByName(driver->devs, def->name))) {
|
if ((obj = virNodeDeviceObjListFindByName(driver->devs, def->name))) {
|
||||||
objdef = virNodeDeviceObjGetDef(obj);
|
objdef = virNodeDeviceObjGetDef(obj);
|
||||||
|
|
||||||
if (def->caps->data.type == VIR_NODE_DEV_CAP_MDEV)
|
if (is_mdev)
|
||||||
nodeDeviceDefCopyFromMdevctl(def, objdef);
|
nodeDeviceDefCopyFromMdevctl(def, objdef);
|
||||||
was_persistent = virNodeDeviceObjIsPersistent(obj);
|
was_persistent = virNodeDeviceObjIsPersistent(obj);
|
||||||
|
autostart = virNodeDeviceObjIsAutostart(obj);
|
||||||
|
|
||||||
/* If the device was defined by mdevctl and was never instantiated, it
|
/* If the device was defined by mdevctl and was never instantiated, it
|
||||||
* won't have a sysfs path. We need to emit a CREATED event... */
|
* won't have a sysfs path. We need to emit a CREATED event... */
|
||||||
new_device = (objdef->sysfs_path == NULL);
|
new_device = (objdef->sysfs_path == NULL);
|
||||||
@ -1543,6 +1549,7 @@ udevAddOneDevice(struct udev_device *device)
|
|||||||
if (!(obj = virNodeDeviceObjListAssignDef(driver->devs, def)))
|
if (!(obj = virNodeDeviceObjListAssignDef(driver->devs, def)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
virNodeDeviceObjSetPersistent(obj, was_persistent);
|
virNodeDeviceObjSetPersistent(obj, was_persistent);
|
||||||
|
virNodeDeviceObjSetAutostart(obj, autostart);
|
||||||
objdef = virNodeDeviceObjGetDef(obj);
|
objdef = virNodeDeviceObjGetDef(obj);
|
||||||
|
|
||||||
if (new_device)
|
if (new_device)
|
||||||
@ -1946,6 +1953,7 @@ udevSetupSystemDev(void)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
virNodeDeviceObjSetActive(obj, true);
|
virNodeDeviceObjSetActive(obj, true);
|
||||||
|
virNodeDeviceObjSetAutostart(obj, true);
|
||||||
|
|
||||||
virNodeDeviceObjEndAPI(&obj);
|
virNodeDeviceObjEndAPI(&obj);
|
||||||
|
|
||||||
@ -2350,6 +2358,8 @@ static virNodeDeviceDriver udevNodeDeviceDriver = {
|
|||||||
.nodeDeviceDefineXML = nodeDeviceDefineXML, /* 7.3.0 */
|
.nodeDeviceDefineXML = nodeDeviceDefineXML, /* 7.3.0 */
|
||||||
.nodeDeviceUndefine = nodeDeviceUndefine, /* 7.3.0 */
|
.nodeDeviceUndefine = nodeDeviceUndefine, /* 7.3.0 */
|
||||||
.nodeDeviceCreate = nodeDeviceCreate, /* 7.3.0 */
|
.nodeDeviceCreate = nodeDeviceCreate, /* 7.3.0 */
|
||||||
|
.nodeDeviceSetAutostart = nodeDeviceSetAutostart, /* 7.8.0 */
|
||||||
|
.nodeDeviceGetAutostart = nodeDeviceGetAutostart, /* 7.8.0 */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user