Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new config hook #1577

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2681,3 +2681,7 @@ It introduces the new `--force` flag for connecting to the instance console.
## `network_ovn_state_addresses`

This adds extra fields to the OVN network state struct for the IPv4 and IPv6 addresses used on the uplink.

## `qemu_scriptlet_config`

This extends the QEMU scriptlet feature by allowing to modify QEMU configuration before a VM starts, and passing information about the instance to the scriptlet.
15 changes: 11 additions & 4 deletions doc/reference/instance_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,17 @@ Those take a JSON encoded list of QMP commands to run.

The hooks correspond to:

- `early` is run prior to any device having been added by Incus through QMP
- `pre-start` is run following Incus having added all its devices by prior to the VM being started
- `post-start` is run immediately following the VM starting up
- `early`, run prior to any device having been added by Incus through QMP, after QEMU has started
- `pre-start`, run following Incus having added all its devices but prior to the VM being started
- `post-start`, run immediately following the VM starting up

### Advanced use
For anyone needing dynamic QMP interactions, for example to retrieve the
current value of some objects before modifying or generating new
objects, it's also possible to attach to those same hooks using a
scriptlet.

This is done through `raw.qemu.scriptlet`. The scriptlet must define the `qemu_hook(stage)` function.
This is done through `raw.qemu.scriptlet`. The scriptlet must define the `qemu_hook(instance, stage)` function. The `instance` arguments is an object representing the VM, whose attributes are those of the `api.Instance` struct. The `stage` argument is the name of the hook (`config`, `early`, `pre-start` or `post-start`), with `config` being run before starting QEMU, and the other hooks defined above.

The following commands are exposed to that scriptlet:

Expand All @@ -360,6 +361,10 @@ The following commands are exposed to that scriptlet:
- `log_error` will log an `ERROR` message
- `run_qmp` will run an arbitrary QMP command (JSON) and return its output
- `run_command` will run the specified command with an optional list of arguments and return its output
- `get_qemu_cmdline` will return the list of command-line arguments passed to QEMU
- `set_qemu_cmdline` will set them
- `get_qemu_conf` will return the QEMU configuration file as a dictionary
- `set_qemu_conf` will set it from a dictionary

Additionally the following alias commands (internally use `run_command`) are also available to simplify scripts:

Expand All @@ -378,6 +383,8 @@ Additionally the following alias commands (internally use `run_command`) are als
- `qom_list`
- `qom_set`

The functions allowing to change QEMU configuration can only be run during the `config` hook. In parallel, the functions running QMP commands cannot be run during the `config` hook.

(instance-options-security)=
## Security policies

Expand Down
14 changes: 14 additions & 0 deletions internal/server/instance/drivers/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cfg

// Entry holds single QEMU configuration Key-Value pairs.
type Entry struct {
Key string
Value string
}

// Section holds QEMU configuration sections.
type Section struct {
Name string
Comment string
Entries []Entry
}
Loading
Loading