.\" mdevctl - Mediated device management utility
.TH mdevctl 8
.SH NAME
mdevctl, lsmdev \- Mediated device management utility
.SH SYNOPSIS
\fBmdevctl\fR {COMMAND} [OPTIONS...]\fR
.SH DESCRIPTION
\fBmdevctl\fR is a utility for managing and persisting devices in the
mediated device device framework of the Linux kernel. Mediated
devices are sub-devices of a parent device (ex. a vGPU) which
can be dynamically created and potentially used by drivers like
vfio-mdev for assignment to virtual machines.
.SH OPTIONS
.PP
The following options are understood:
.PP
\fB--addattr=ATTRIBUTE\fR
.RS 4
Add an attribute \fIATTRIBUTE\fR. Valid for the \fBmodify\fR
command.
.RE
.PP
\fB-a|--auto\fR
.RS 4
Automatically start the device on parent availability. Valid for
\fBdefine\fR and \fBmodify\fR commands.
.RE
.PP
\fB-d|--defined\fR
.RS 4
List all defined devices, even if not active. Valid for the \fBlist\fR
command.
.RE
.PP
\fB--delattr\fR
.RS 4
Delete an attribute entry. Valid for the \fBmodify\fR command.
.RE
.PP
\fB--dumpjson\fR
.RS 4
Dump the configuration for a device in JSON format when filtered to
as single device and used with the \fBlist\fR command. When used
with the \fBtypes\fR command, output machine readable type information.
.RE
.PP
\fB-i|--index=INDEX\fR
.RS 4
Act on the attribute \fIINDEX\fR. Valid for the \fBmodify\fR command.
.RE
.PP
\fB--jsonfile=FILE\fR
.RS 4
Read the configuration for a device from a JSON file \fIFILE\fR.
Valid for the \fBdefine\fR and \fBstart\fR commands.
.RE
.PP
\fB-m|--manual\fR
.RS 4
Do not start a device automatically on parent availability. Valid
for the \fBmodify\fR command.
.RE
.PP
\fB-p|--parent=PARENT\fR
.RS 4
Specify or identify the device by its parent device.
.RE
.PP
\fB-t|--type=TYPE\fR
.RS 4
Specify or identify the device by its type.
.RE
.PP
\fB-u|--uuid=UUID\fR
.RS 4
Specify or identify the device by its UUID.
.RE
.PP
\fB--value=VALUE\fR
.RS 4
Set an attribute to \fIVALUE\fR, in the format accepted by the attribute.
Valid for the \fBmodify\fR command.
.RE
.PP
\fB-v|--verbose\fR
.RS 4
Increase output verbosity, currently only adds attribute output to the
\fBlist\fR command.
.RE
.PP
\fB-V|--version\fR
.RS 4
Print mdevctl version.
.RE
.SH COMMANDS
.PP
The following commands are understood:
.PP
\fBdefine\fR \fIDEVICESPEC\fR
.RS 4
Define a config for an mdev device, identified either by an UUID (if
the device already exists), or by the parent device and either the type
or a JSON configuration file, and, optionally, the UUID. If no UUID is
specified, one is autogenerated and printed. If no file is used,
\fI-a|--auto\fR may be used to specify that the device should be started
automatically.
.RE
.PP
\fBlist\fR
.RS 4
List mdev devices. With no options, currently running devices are listed.
With \fB-d|--defined\fR, previously defined devices are listed.
Can be restricted to list only devices for a given parent or UUID. With
\fB--dumpjson\fR output is provided in machine readable JSON format.
When a UUID is provided and the output results in a single device, the
JSON output format is compatible with the configuration file format.
.RE
.PP
\fBmodify\fR \fIDEVICESPEC\fR
.RS 4
Modify the configuration for an mdev device, identified via its UUID
and optionally its parent.
Type and startup mode (auto or manual) can be modified by this command.
Attributes can be added or deleted. Attributes to be deleted must be
specified by their index; if an attribute is specified without an
index, it is appended at the end of the attribute list.
Running devices are unaffected by this command; changes in the configuration
are applied the next time the device is started.
.RE
.PP
\fBstart\fR \fIDEVICESPEC\fR
.RS 4
Start a mediated device. This command can be used to start either a
previously-defined device or a newly-created transient device.
If the UUID and optional parent argument matches an existing device definition,
then the existing device will be started. It is an error to specify a device
type that conflicts with the existing device definition.
If the UUID argument is omitted or if the specified UUID and parent does not
match an existing device definition, a new transient device will be started.
If the UUID is omitted, a new UUID will be automatically generated. When
starting a new transient device, the parent and device type must be specified.
A \fB--jsonfile\fR may replace the \fB--type\fR specification and also include
additional attributes in JSON format to be applied to the started device.
.RE
.PP
\fBstop\fR \fIDEVICESPEC\fR
.RS 4
Stop an mdev device, specified via its UUID.
.RE
.PP
\fBtypes\fR
.RS 4
List the mdev device types known to the system by parent device. Output
may be limited to a single parent device with the \fB-p|--parent\fR option.
JSON output format is used with the \fB--dumpjson\fR option.
.RE
.PP
\fBundefine\fR \fIDEVICESPEC\fR
.RS 4
Undefine, or remove the configuration for an mdev device, specified by
its UUID and optionally its parent. If a UUID exists for multiple
parents, all of them will be removed unless restricted to a single parent.
Running devices are unaffected by this command.
.RE
.SH "NOTE ON DEVICE SPECIFICATION"
For a given UUID, only one device with that UUID may be running at the
same time. However, it is possible to define multiple devices with the
same UUID under different parent devices. Therefore, it is sometimes
necessary to specify the parent device alongside the UUID to uniquely
identify a device.
.SH "EXIT STATUS"
On success, 0 is returned, a non-zero failure code otherwise.
.SH EXAMPLES
.nf
List running mdev devices:
.EX
# mdevctl list
85006552-1b4b-45ef-ad62-de05be9171df 0000:00:02.0 i915-GVTg_V4_4
83c32df7-d52e-4ec1-9668-1f3c7e4df107 0000:00:02.0 i915-GVTg_V4_8 (defined)
.EE
List defined mdev devices:
.EX
# mdevctl list -d
83c32df7-d52e-4ec1-9668-1f3c7e4df107 0000:00:02.0 i915-GVTg_V4_8 auto
b0a3989f-8138-4d49-b63a-59db28ec8b48 0000:00:02.0 i915-GVTg_V4_8 auto
5cf14a12-a437-4c82-a13f-70e945782d7b 0000:00:02.0 i915-GVTg_V4_4 manual
.EE
List mdev types supported on the host system:
.EX
# mdevctl types
0000:00:02.0
i915-GVTg_V4_2
Available instances: 1
Device API: vfio-pci
Description: low_gm_size: 256MB high_gm_size: 1024MB fence: 4 resolution: 1920x1200 weight: 8
i915-GVTg_V4_1
Available instances: 0
Device API: vfio-pci
Description: low_gm_size: 512MB high_gm_size: 2048MB fence: 4 resolution: 1920x1200 weight: 16
i915-GVTg_V4_8
Available instances: 4
Device API: vfio-pci
Description: low_gm_size: 64MB high_gm_size: 384MB fence: 4 resolution: 1024x768 weight: 2
i915-GVTg_V4_4
Available instances: 3
Device API: vfio-pci
Description: low_gm_size: 128MB high_gm_size: 512MB fence: 4 resolution: 1920x1200 weight: 4
.EE
Modify a defined device from automatic start to manual:
.EX
# mdevctl modify --uuid 83c32df7-d52e-4ec1-9668-1f3c7e4df107 --manual
# mdevctl list -d
83c32df7-d52e-4ec1-9668-1f3c7e4df107 0000:00:02.0 i915-GVTg_V4_8 manual
b0a3989f-8138-4d49-b63a-59db28ec8b48 0000:00:02.0 i915-GVTg_V4_8 auto
5cf14a12-a437-4c82-a13f-70e945782d7b 0000:00:02.0 i915-GVTg_V4_4 manual
.EE
Stop a running mdev device:
.EX
# mdevctl stop -u 83c32df7-d52e-4ec1-9668-1f3c7e4df107
.EE
Start an mdev device that is not defined:
.EX
# uuidgen
6eba5b41-176e-40db-b93e-7f18e04e0b93
# mdevctl start -u 6eba5b41-176e-40db-b93e-7f18e04e0b93 -p 0000:00:02.0 --type i915-GVTg_V4_1
# mdevctl list
85006552-1b4b-45ef-ad62-de05be9171df 0000:00:02.0 i915-GVTg_V4_4
6eba5b41-176e-40db-b93e-7f18e04e0b93 0000:00:02.0 i915-GVTg_V4_1
.EE
Promote the new created mdev to a defined device:
.EX
# mdevctl define --uuid 6eba5b41-176e-40db-b93e-7f18e04e0b93
# mdevctl list -d
83c32df7-d52e-4ec1-9668-1f3c7e4df107 0000:00:02.0 i915-GVTg_V4_8 manual
6eba5b41-176e-40db-b93e-7f18e04e0b93 0000:00:02.0 i915-GVTg_V4_1 manual
b0a3989f-8138-4d49-b63a-59db28ec8b48 0000:00:02.0 i915-GVTg_V4_8 auto
5cf14a12-a437-4c82-a13f-70e945782d7b 0000:00:02.0 i915-GVTg_V4_4 manual
.EE
.SS "ADVANCED EXAMPLES (ATTRIBUTES AND JSON)"
.EX
# mdevctl list -d
783e6dbb-ea0e-411f-94e2-717eaad438bf matrix vfio_ap-passthrough manual
.EE
Add some attributes:
.EX
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_adapter --value=5
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_adapter --value=6
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_domain --value=0xab
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_control_domain --value=0xab
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_domain --value=4
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --addattr=assign_control_domain --value=4
# mdevctl list -dv
783e6dbb-ea0e-411f-94e2-717eaad438bf matrix vfio_ap-passthrough manual
Attrs:
@{0}: {"assign_adapter":"5"}
@{1}: {"assign_adapter":"6"}
@{2}: {"assign_domain":"0xab"}
@{3}: {"assign_control_domain":"0xab"}
@{4}: {"assign_domain":"4"}
@{5}: {"assign_control_domain":"4"}
.EE
Dump the JSON configuration:
.EX
# mdevctl list -d -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --dumpjson
{
"mdev_type": "vfio_ap-passthrough",
"start": "manual",
"attrs": [
{
"assign_adapter": "5"
},
{
"assign_adapter": "6"
},
{
"assign_domain": "0xab"
},
{
"assign_control_domain": "0xab"
},
{
"assign_domain": "4"
},
{
"assign_control_domain": "4"
}
]
}
.EE
Remove some attributes:
.EX
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --delattr --index=5
# mdevctl modify -u 783e6dbb-ea0e-411f-94e2-717eaad438bf --delattr --index=4
# mdevctl list -dv
783e6dbb-ea0e-411f-94e2-717eaad438bf matrix vfio_ap-passthrough manual
Attrs:
@{0}: {"assign_adapter":"5"}
@{1}: {"assign_adapter":"6"}
@{2}: {"assign_domain":"0xab"}
@{3}: {"assign_control_domain":"0xab"}
.EE
Define an mdev device from a file:
.EX
# cat vfio_ap_device.json
{
"mdev_type": "vfio_ap-passthrough",
"start": "manual",
"attrs": [
{
"assign_adapter": "5"
},
{
"assign_domain": "0x47"
},
{
"assign_domain": "0xff"
}
]
}
# mdevctl define -p matrix --jsonfile vfio_ap_device.json
e2e73122-cc39-40ee-89eb-b0a47d334cae
# mdevctl list -dv
783e6dbb-ea0e-411f-94e2-717eaad438bf matrix vfio_ap-passthrough manual
Attrs:
@{0}: {"assign_adapter":"5"}
@{1}: {"assign_adapter":"6"}
@{2}: {"assign_domain":"0xab"}
@{3}: {"assign_control_domain":"0xab"}
e2e73122-cc39-40ee-89eb-b0a47d334cae matrix vfio_ap-passthrough manual
Attrs:
@{0}: {"assign_adapter":"5"}
@{1}: {"assign_domain":"0x47"}
@{2}: {"assign_domain":"0xff"}
.EE
.SH "CONFIGURATION FILE FORMAT"
Configuration files are in JSON. Attributes in \fB"attrs"\fR are optional.
.EX
{
"mdev_type": \fI"TYPE"\fR,
"start": \fI"auto|manual"\fR,
"attrs": [
{
\fI"attribute0"\fR: \fI"VALUE"\fR
},
{
\fI"attribute1"\fR: \fI"VALUE"\fR
}
]
}
.EE
.SH INVOKING EXTERNAL SCRIPTS FOR DEVICE EVENTS
mdevctl supports invoking external scripts to handle additional device
type-specific configurations and to broadcast notifications regarding changes
or updates to a device. These scripts are invoked before, after, and/or during
mdevctl's "primary command execution" (e.g. writing the device configuration
file for define, or activating a device for start).
Essentially, the procedure in mdevctl looks like this:
.RS
.IP "1. command-line parsing & setup"
.IP "2. invoke pre-command call-out"
.IP "3. primary command execution*"
.IP "4. invoke post-command call-out*"
.IP "5. invoke notifier"
.IP "* skipped if step 2 fails."
.RE
.SS EVENT SCRIPTS
A call-out or notification event invokes a script along with a set of
parameters detailing the type of call-out, mdevctl's command execution
progress, and the mediated device. The parameters are as follows:
<CONFIG> | SCRIPT <\fB-t=\fR\fItype\fR \fB-e=\fR\fIevent\fR
\fB-a=\fR\fIaction\fR \fB-s=\fR\fIstate\fR \fB-u=\fR\fIUUID\fR
\fB-p=\fR\fIparent\fR>
.PP
\fBCONFIG\fR
.RS 4
The device's JSON configuration, provided via standard input.
.RE
.PP
\fB-t=\fR\fItype\fR
.RS 4
The device type.
.RE
.PP
\fB-e=\fR\fIevent\fR
.RS 4
Event type of call-out that is invoked. For call-out scripts, this may be
\fBpre\fR, \fBpost\fR, or \fBget\fR. For notification scripts, this will
always be \fBnotify\fR.
.RE
.PP
\fB-a=\fR\fIaction\fR
.RS 4
An action synonymous with an mdevctl command (e.g. define, start).
.RE
.PP
\fB-s=\fR\fIstate\fR
.RS 4
A trinary state of the mdevctl command execution. The possibilities are
\fBnone\fR if the mdevctl command has yet to execute, \fBsuccess\fR
if the mdevctl command completed successfully, or \fBfailure\fR if there
was a problem executing the mdevctl command.
.RE
.PP
\fB-u=\fR\fIUUID\fR
.RS 4
UUID of the mediated device.
.RE
.PP
\fB-p=\fR\fIparent\fR>
.RS 4
Parent of the mediated the device.
.RE
.SS CALL-OUT EVENT SCRIPTS
A call-out event script is invoked during a \fBpre\fR, \fBpost\fR, or \fBget\fR
event. mdevctl will attempt each script stored in the mdevctl callouts
directory until either a script that satisfies the device type is found or all
scripts have been attempted. A device script must check the "TYPE" parameter to
ensure the specified device type is supported, otherwise error code 2 should be
returned. If no script is found for the specified device type, then mdevctl
will carry on as normal.
These scripts are stored in \fI/etc/mdevctl.d/scripts.d/callouts\fR. The same
script is invoked for \fBpre\fR, \fBpost\fR, and \fBget\fR call-out events for
the device type.
.PP
\fBPre-Command\fR
.RS 4
A pre-command call-out event is invoked once prior to primary command execution.
Event type is \fBpre\fR. Status will always be \fBnone\fR.
Any non-zero return code (exempting 2) will prevent mdevctl from performing
the primary command execution and mdevctl will abort early.
A notification event will follow only if an error code (exempting 2) is
observed.
This event is not supported for the \fBlist\fR, \fBtypes\fR, or \fBversion\fR
commands.
.RE
.PP
\fBPost-Command\fR
.RS 4
A post-command call-out event is invoked once after primary command execution.
Event type is \fBpost\fR. Status will be \fBsuccess\fR if mdevctl was able to
finish primary command execution successfully, or \fBfailure\fR otherwise.
The same script used for the pre event is used for the post event.
Any return code is non-disruptive.
A notification event will always follow a post-command call-out.
This event is not supported for the \fBlist\fR, \fBtypes\fR, or \fBversion\fR
commands.
.RE
.PP
\fBGet-attributes\fR
.RS 4
A get event is invoked during a \fBdefine\fR and \fBlist\fR command to
acquire device attributes from an active device. Event type is \fBget\fR. Action
is \fBattributes\fR. Status is \fBnone\fR. Note that, unlike other call-outs
events, \fBget-attributes does not expect a device config on stdin, and an
array of JSON formatted device attributes is returned via stdout\fR.
The same script used for the pre event is used for the get event. If the script
is not designed to support a get event, then the return code is 0.
For \fBdefine\fR, a non-zero return code (exempting 2) will disrupt the define
command entirely.
For \fBlist\fR, any return code is non-disruptive.
A script must return a JSON formatted array of device attributes on standard
output. Example:
.EX
[
{
\fI"attribute0"\fR: \fI"VALUE"\fR
},
{
\fI"attribute1"\fR: \fI"VALUE"\fR
}
]
.EE
.RE
.SS AUTO-START CALL-OUTS
For each device set to start automatically during system boot, mdevctl will
invoke the pre and post events. Action is the string \fBstart\fR.
Return code and notification event behavior is the same as documented for
the pre and post events. Errors reported by a script will disrupt the
auto-start for that particular device and the message will be reported to the
system log before attempting to the next auto-start device.
Note that if a notification script is used to convey information to another
program or daemon during the auto-start procedure, it is not guaranteed that
the program will already be active prior to mdevctl's invocation (e.g. the
auto-start event may occur before the libvirt daemon is activated).
.RE
.SS NOTIFICATION EVENT SCRIPTS
Notification event scripts may be used to signal the state of the mediated
device or the status of an mdevctl command to other programs or loggers. Unlike
call-out scripts, notifier scripts are device-type agnostic.
.PP
\fBNotify\fR
.RS 4
A notification event is invoked once either following a pre-command call-out
failure or after a post-command call-out. Event is \fBnotify\fR. If following a
pre event, then status will be \fBnone\fR. If following a post event, then
status will mirror the value passed to the post-command call-out.
These scripts are stored in \fI/etc/mdevctl.d/scripts.d/notifiers\fR. \fBAll
notification scripts will be invoked during a notification event\fR.
A non-zero return code is ignored.
This event is not supported for the \fBlist\fR, \fBtypes\fR, or \fBversion\fR
commands.
.RE
.SS SCRIPT RETURN VALUES
.RS
.IP "0 if OK,
.IP "1 if an error occurred,
.IP "2 if the script does not support the device type
.RE
.SH FILES
\fI/etc/mdevctl.d/*\fR
Configuration files are in one subdirectory per parent device and named
by UUID.
\fI/etc/mdevctl.d/scripts.d/callouts/*\fR
Scripts for pre/post/get call-out events.
\fI/etc/mdevctl.d/scripts.d/notifiers/*\fR
Scripts for notification call-out events.
.SH "SEE ALSO"
\fBudev\fR(7)
\fBudevadm\fR(8)
\fBdriverctl\fR(8)