Skip to content

Commit 2a9a0fd

Browse files
committed
incusd/instance/qemu: Use saved machine definition when restoring running VM
Signed-off-by: Stéphane Graber <[email protected]>
1 parent b181232 commit 2a9a0fd

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

internal/server/instance/drivers/driver_qemu.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,14 @@ func (d *qemu) start(stateful bool, op *operationlock.InstanceOperation) error {
15721572
cpuType += "," + strings.Join(cpuExtensions, ",")
15731573
}
15741574

1575+
// Provide machine definition when restoring state.
1576+
var machineDefinition string
1577+
if stateful {
1578+
machineDefinition = d.localConfig["volatile.vm.definition"]
1579+
}
1580+
15751581
// Generate the QEMU configuration.
1576-
monHooks, err := d.generateQemuConfig(cpuInfo, mountInfo, qemuBus, vsockFD, devConfs, &fdFiles)
1582+
monHooks, err := d.generateQemuConfig(machineDefinition, cpuInfo, mountInfo, qemuBus, vsockFD, devConfs, &fdFiles)
15771583
if err != nil {
15781584
op.Done(err)
15791585
return err
@@ -1824,6 +1830,23 @@ func (d *qemu) start(stateful bool, op *operationlock.InstanceOperation) error {
18241830
return err
18251831
}
18261832

1833+
// Record the QEMU machine definition.
1834+
if !stateful {
1835+
definition, err := monitor.MachineDefinition()
1836+
if err != nil {
1837+
op.Done(err)
1838+
return err
1839+
}
1840+
1841+
err = d.VolatileSet(map[string]string{
1842+
"volatile.vm.definition": definition,
1843+
})
1844+
if err != nil {
1845+
op.Done(err)
1846+
return err
1847+
}
1848+
}
1849+
18271850
// Don't allow the monitor to trigger a disconnection shutdown event until cleanly started so that the
18281851
// onStop hook isn't triggered prematurely (as this function's reverter will clean up on failure to start).
18291852
monitor.SetOnDisconnectEvent(false)
@@ -3351,11 +3374,11 @@ func (d *qemu) isWindows() bool {
33513374
}
33523375

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

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

33603383
err := d.addCPUMemoryConfig(&conf, cpuInfo)
33613384
if err != nil {

internal/server/instance/drivers/driver_qemu_templates.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func qemuMachineType(architecture int) string {
5252
type qemuBaseOpts struct {
5353
architecture int
5454
iommu bool
55+
definition string
5556
}
5657

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

70+
if opts.definition != "" {
71+
machineType = opts.definition
72+
}
73+
6974
sections := []cfg.Section{{
7075
Name: "machine",
7176
Comment: "Machine",

0 commit comments

Comments
 (0)