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

Handle live migration between QEMU versions #1775

Merged
merged 4 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/config_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,12 @@ The instance generation UUID changes whenever the instance's place in time moves
It is globally unique across all servers and projects.
```

```{config:option} volatile.vm.definition instance-volatile
:shortdesc: "QEMU VM definition name (used for migration between versions)"
:type: "string"

```

```{config:option} volatile.vsock_id instance-volatile
:shortdesc: "Instance `vsock ID` used as of last start"
:type: "string"
Expand Down
7 changes: 7 additions & 0 deletions internal/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,13 @@ var InstanceConfigKeysVM = map[string]func(value string) error{
// shortdesc: Whether to regenerate VM NVRAM the next time the instance starts
"volatile.apply_nvram": validate.Optional(validate.IsBool),

// gendoc:generate(entity=instance, group=volatile, key=volatile.vm.definition)
//
// ---
// type: string
// shortdesc: QEMU VM definition name (used for migration between versions)
"volatile.vm.definition": validate.Optional(validate.IsAny),

// gendoc:generate(entity=instance, group=volatile, key=volatile.vsock_id)
//
// ---
Expand Down
29 changes: 26 additions & 3 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,8 +1572,14 @@ func (d *qemu) start(stateful bool, op *operationlock.InstanceOperation) error {
cpuType += "," + strings.Join(cpuExtensions, ",")
}

// Provide machine definition when restoring state.
var machineDefinition string
if stateful {
machineDefinition = d.localConfig["volatile.vm.definition"]
}

// Generate the QEMU configuration.
monHooks, err := d.generateQemuConfig(cpuInfo, mountInfo, qemuBus, vsockFD, devConfs, &fdFiles)
monHooks, err := d.generateQemuConfig(machineDefinition, cpuInfo, mountInfo, qemuBus, vsockFD, devConfs, &fdFiles)
if err != nil {
op.Done(err)
return err
Expand Down Expand Up @@ -1824,6 +1830,23 @@ func (d *qemu) start(stateful bool, op *operationlock.InstanceOperation) error {
return err
}

// Record the QEMU machine definition.
if !stateful {
definition, err := monitor.MachineDefinition()
if err != nil {
op.Done(err)
return err
}

err = d.VolatileSet(map[string]string{
"volatile.vm.definition": definition,
})
if err != nil {
op.Done(err)
return err
}
}

// Don't allow the monitor to trigger a disconnection shutdown event until cleanly started so that the
// onStop hook isn't triggered prematurely (as this function's reverter will clean up on failure to start).
monitor.SetOnDisconnectEvent(false)
Expand Down Expand Up @@ -3351,11 +3374,11 @@ func (d *qemu) isWindows() bool {
}

// generateQemuConfig generates the QEMU configuration.
func (d *qemu) generateQemuConfig(cpuInfo *cpuTopology, mountInfo *storagePools.MountInfo, busName string, vsockFD int, devConfs []*deviceConfig.RunConfig, fdFiles *[]*os.File) ([]monitorHook, error) {
func (d *qemu) generateQemuConfig(machineDefinition string, cpuInfo *cpuTopology, mountInfo *storagePools.MountInfo, busName string, vsockFD int, devConfs []*deviceConfig.RunConfig, fdFiles *[]*os.File) ([]monitorHook, error) {
var monHooks []monitorHook

isWindows := d.isWindows()
conf := qemuBase(&qemuBaseOpts{d.Architecture(), util.IsTrue(d.expandedConfig["security.iommu"])})
conf := qemuBase(&qemuBaseOpts{d.Architecture(), util.IsTrue(d.expandedConfig["security.iommu"]), machineDefinition})

err := d.addCPUMemoryConfig(&conf, cpuInfo)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/server/instance/drivers/driver_qemu_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func qemuMachineType(architecture int) string {
type qemuBaseOpts struct {
architecture int
iommu bool
definition string
}

func qemuBase(opts *qemuBaseOpts) []cfg.Section {
Expand All @@ -66,6 +67,10 @@ func qemuBase(opts *qemuBaseOpts) []cfg.Section {
capLargeDecr = "off"
}

if opts.definition != "" {
machineType = opts.definition
}

sections := []cfg.Section{{
Name: "machine",
Comment: "Machine",
Expand Down
25 changes: 25 additions & 0 deletions internal/server/instance/drivers/qmp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ func (m *Monitor) Status() (string, error) {
return resp.Return.Status, nil
}

// MachineDefinition returns the current QEMU machine definition name.
func (m *Monitor) MachineDefinition() (string, error) {
// Prepare the request.
var req struct {
Path string `json:"path"`
Property string `json:"property"`
}

req.Path = "/machine"
req.Property = "type"

// Prepare the response.
var resp struct {
Return string `json:"return"`
}

// Query the machine.
err := m.Run("qom-get", req, &resp)
if err != nil {
return "", err
}

return strings.TrimSuffix(resp.Return, "-machine"), nil
}

// SendFile adds a new file descriptor to the QMP fd table associated to name.
func (m *Monitor) SendFile(name string, file *os.File) error {
// Check if disconnected.
Expand Down
7 changes: 7 additions & 0 deletions internal/server/metadata/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,13 @@
"type": "string"
}
},
{
"volatile.vm.definition": {
"longdesc": "",
"shortdesc": "QEMU VM definition name (used for migration between versions)",
"type": "string"
}
},
{
"volatile.vsock_id": {
"longdesc": "",
Expand Down