Skip to content

Commit

Permalink
lxd/instance/drivers/qemu: Add function to retrieve the vsock Context ID
Browse files Browse the repository at this point in the history
As a client retrieving the already existing vsock Context ID for a VM is now handled by a call to getVsockID()

Signed-off-by: Julian Pelizäus <[email protected]>
  • Loading branch information
roosterfish committed Jun 28, 2023
1 parent 7a12774 commit b9ee525
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,10 @@ func (d *qemu) getAgentClient() (*http.Client, error) {
return nil, err
}

vsockID := d.vsockID() // Default to using the vsock ID that will be used on next start.

// But if vsock ID from last VM start is present in volatile, then use that.
// This allows a running VM to be recovered after DB record deletion and that agent connection still work
// after the VM's instance ID has changed.
if d.localConfig["volatile.vsock_id"] != "" {
volatileVsockID, err := strconv.ParseUint(d.localConfig["volatile.vsock_id"], 10, 32)
if err == nil {
vsockID = uint32(volatileVsockID)
}
// Existing vsock ID from volatile.
vsockID, err := d.getVsockID()
if err != nil {
return nil, err
}

agent, err := lxdvsock.HTTPClient(vsockID, shared.HTTPSDefaultPort, clientCert, clientKey, agentCert)
Expand Down Expand Up @@ -2943,6 +2937,12 @@ func (d *qemu) generateQemuConfigFile(cpuInfo *cpuTopology, mountInfo *storagePo

cfg = append(cfg, qemuTablet(&tabletOpts)...)

// Existing vsock ID from volatile.
vsockID, err := d.getVsockID()
if err != nil {
return "", nil, err
}

devBus, devAddr, multi = bus.allocate(busFunctionGroupGeneric)
vsockOpts := qemuVsockOpts{
dev: qemuDevOpts{
Expand All @@ -2951,7 +2951,7 @@ func (d *qemu) generateQemuConfigFile(cpuInfo *cpuTopology, mountInfo *storagePo
devAddr: devAddr,
multifunction: multi,
},
vsockID: d.vsockID(),
vsockID: vsockID,
}

cfg = append(cfg, qemuVsock(&vsockOpts)...)
Expand Down Expand Up @@ -7426,6 +7426,21 @@ func (d *qemu) DeviceEventHandler(runConf *deviceConfig.RunConfig) error {
return nil
}

// getVsockID returns the vsock Context ID for the VM.
func (d *qemu) getVsockID() (uint32, error) {
existingVsockID, ok := d.localConfig["volatile.vsock_id"]
if ok {
vsockID, err := strconv.ParseUint(existingVsockID, 10, 32)
if err != nil {
return 0, fmt.Errorf("Failed to parse volatile.vsock_id: %q: %w", existingVsockID, err)
}

return uint32(vsockID), nil
}

return 0, fmt.Errorf("Failed to get vsock Context ID for VM")
}

// vsockID returns the vsock Context ID for the VM.
func (d *qemu) vsockID() uint32 {
// We use the system's own VsockID as the base.
Expand Down

0 comments on commit b9ee525

Please sign in to comment.