[lxc-devel] [lxd/master] lxd/vm: Move to separate devices

stgraber on Github lxc-bot at linuxcontainers.org
Tue Jun 9 23:15:23 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 387 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200609/da258251/attachment.bin>
-------------- next part --------------
From 8e2947a16839968fa728b0b8aa1fb8f90e4d7ffa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 9 Jun 2020 19:14:45 -0400
Subject: [PATCH] lxd/vm: Move to separate devices
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

One template per device type.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/instance/drivers/driver_qemu.go           |  54 +++++++-
 lxd/instance/drivers/driver_qemu_templates.go | 118 ++++++++++--------
 2 files changed, 114 insertions(+), 58 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index b4f19fd9d7..e67a45950e 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -1552,15 +1552,34 @@ func (vm *qemu) generateQemuConfigFile(devConfs []*deviceConfig.RunConfig, fdFil
 	var sb *strings.Builder = &strings.Builder{}
 
 	err := qemuBase.Execute(sb, map[string]interface{}{
-		"architecture":     vm.architectureName,
-		"ringbufSizeBytes": qmp.RingbufSize,
-		"spicePath":        vm.spicePath(),
+		"architecture": vm.architectureName,
+		"spicePath":    vm.spicePath(),
 	})
 	if err != nil {
 		return "", err
 	}
 
 	// Now add the dynamic parts of the config.
+	err = vm.addSerialConfig(sb)
+	if err != nil {
+		return "", err
+	}
+
+	err = vm.addSCSIConfig(sb)
+	if err != nil {
+		return "", err
+	}
+
+	err = vm.addBalloonConfig(sb)
+	if err != nil {
+		return "", err
+	}
+
+	err = vm.addRNGConfig(sb)
+	if err != nil {
+		return "", err
+	}
+
 	err = vm.addMemoryConfig(sb)
 	if err != nil {
 		return "", err
@@ -1688,6 +1707,35 @@ func (vm *qemu) addVsockConfig(sb *strings.Builder) error {
 	})
 }
 
+// addSerialConfig adds the qemu config required for setting up the host->VM vsock socket.
+func (vm *qemu) addSerialConfig(sb *strings.Builder) error {
+	return qemuSerial.Execute(sb, map[string]interface{}{
+		"architecture":     vm.architectureName,
+		"ringbufSizeBytes": qmp.RingbufSize,
+	})
+}
+
+// addSCSIConfig adds the qemu config required for setting up the host->VM vsock socket.
+func (vm *qemu) addSCSIConfig(sb *strings.Builder) error {
+	return qemuSCSI.Execute(sb, map[string]interface{}{
+		"architecture": vm.architectureName,
+	})
+}
+
+// addBalloonConfig adds the qemu config required for setting up the host->VM vsock socket.
+func (vm *qemu) addBalloonConfig(sb *strings.Builder) error {
+	return qemuBalloon.Execute(sb, map[string]interface{}{
+		"architecture": vm.architectureName,
+	})
+}
+
+// addRNGConfig adds the qemu config required for setting up the host->VM vsock socket.
+func (vm *qemu) addRNGConfig(sb *strings.Builder) error {
+	return qemuRNG.Execute(sb, map[string]interface{}{
+		"architecture": vm.architectureName,
+	})
+}
+
 // addVGAConfig adds the qemu config required for setting up the host->VM vsock socket.
 func (vm *qemu) addVGAConfig(sb *strings.Builder, chassisIndex int) error {
 	return qemuVGA.Execute(sb, map[string]interface{}{
diff --git a/lxd/instance/drivers/driver_qemu_templates.go b/lxd/instance/drivers/driver_qemu_templates.go
index 69e80ac8cc..5e2a540c39 100644
--- a/lxd/instance/drivers/driver_qemu_templates.go
+++ b/lxd/instance/drivers/driver_qemu_templates.go
@@ -41,6 +41,50 @@ value = "1"
 [boot-opts]
 strict = "on"
 
+# Console
+[chardev "console"]
+backend = "pty"
+
+# Graphical console
+[spice]
+unix = "on"
+addr = "{{.spicePath}}"
+disable-ticketing = "on"
+`))
+
+var qemuMemory = template.Must(template.New("qemuMemory").Parse(`
+# Memory
+[memory]
+size = "{{.memSizeBytes}}B"
+`))
+
+var qemuVsock = template.Must(template.New("qemuVsock").Parse(`
+# Vsock
+{{if eq .architecture "x86_64" "aarch64" -}}
+[device "qemu_pcie4"]
+driver = "pcie-root-port"
+port = "0x13"
+chassis = "4"
+bus = "pcie.0"
+addr = "0x2.0x3"
+{{- end }}
+
+[device]
+guest-cid = "{{.vsockID}}"
+{{if ne .architecture "s390x" -}}
+driver = "vhost-vsock-pci"
+{{if eq .architecture "ppc64le" -}}
+bus = "pci.0"
+{{else -}}
+bus = "qemu_pcie4"
+addr = "0x0"
+{{end -}}
+{{- else}}
+driver = "vhost-vsock-ccw"
+{{- end}}
+`))
+
+var qemuSerial = template.Must(template.New("qemuSerial").Parse(`
 # LXD serial identifier
 [device]
 driver = "virtio-serial"
@@ -53,7 +97,9 @@ chardev = "vserial"
 [chardev "vserial"]
 backend = "ringbuf"
 size = "{{.ringbufSizeBytes}}B"
+`))
 
+var qemuSCSI = template.Must(template.New("qemuSCSI").Parse(`
 # SCSI controller
 {{- if eq .architecture "x86_64" "aarch64" }}
 [device "qemu_pcie1"]
@@ -77,30 +123,9 @@ addr = "0x0"
 {{- else}}
 driver = "virtio-scsi-ccw"
 {{- end}}
+`))
 
-# Balloon driver
-{{- if eq .architecture "x86_64" "aarch64" }}
-[device "qemu_pcie2"]
-driver = "pcie-root-port"
-port = "0x11"
-chassis = "2"
-bus = "pcie.0"
-addr = "0x2.0x1"
-{{- end }}
-
-[device "qemu_ballon"]
-{{- if ne .architecture "s390x"}}
-driver = "virtio-balloon-pci"
-{{- if eq .architecture "ppc64le" }}
-bus = "pci.0"
-{{- else}}
-bus = "qemu_pcie2"
-addr = "0x0"
-{{- end}}
-{{- else}}
-driver = "virtio-balloon-ccw"
-{{- end}}
-
+var qemuRNG = template.Must(template.New("qemuRNG").Parse(`
 # Random number generator
 [object "qemu_rng"]
 qom-type = "rng-random"
@@ -128,47 +153,30 @@ addr = "0x0"
 {{- else}}
 driver = "virtio-rng-ccw"
 {{- end}}
-
-# Console
-[chardev "console"]
-backend = "pty"
-
-# Graphical console
-[spice]
-unix = "on"
-addr = "{{.spicePath}}"
-disable-ticketing = "on"
-`))
-
-var qemuMemory = template.Must(template.New("qemuMemory").Parse(`
-# Memory
-[memory]
-size = "{{.memSizeBytes}}B"
 `))
 
-var qemuVsock = template.Must(template.New("qemuVsock").Parse(`
-# Vsock
-{{if eq .architecture "x86_64" "aarch64" -}}
-[device "qemu_pcie4"]
+var qemuBalloon = template.Must(template.New("qemuBalloon").Parse(`
+# Balloon driver
+{{- if eq .architecture "x86_64" "aarch64" }}
+[device "qemu_pcie2"]
 driver = "pcie-root-port"
-port = "0x13"
-chassis = "4"
+port = "0x11"
+chassis = "2"
 bus = "pcie.0"
-addr = "0x2.0x3"
+addr = "0x2.0x1"
 {{- end }}
 
-[device]
-guest-cid = "{{.vsockID}}"
-{{if ne .architecture "s390x" -}}
-driver = "vhost-vsock-pci"
-{{if eq .architecture "ppc64le" -}}
+[device "qemu_ballon"]
+{{- if ne .architecture "s390x"}}
+driver = "virtio-balloon-pci"
+{{- if eq .architecture "ppc64le" }}
 bus = "pci.0"
-{{else -}}
-bus = "qemu_pcie4"
+{{- else}}
+bus = "qemu_pcie2"
 addr = "0x0"
-{{end -}}
+{{- end}}
 {{- else}}
-driver = "vhost-vsock-ccw"
+driver = "virtio-balloon-ccw"
 {{- end}}
 `))
 


More information about the lxc-devel mailing list