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

qemu: allow specifying the machine type #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions spread/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ type System struct {

Priority OptionalInt
Manual bool

// Type of machine emulated, only for qemu. Defaults to "pc", which is
// the QEMU default for x86_64
MachineType string `yaml:"machine-type"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid backend specific options in System, if possible we try to find a generic term that could be reused across various backends. MachineType (or just Machine) is actually not bad as it potentially maps to many different backends, e.g. in linode or google it could map to the machine type like: e2-standard-2. Three is an existing Plan in project.go that is currently used for this.

Which makes me wonder if could consolidate plan and machine somehow, I see some optons:

  1. add new machine and only use in qemu
  2. use plan in the qemu backend and map that to machine type
  3. add new machine and use it in linode/google instead of plan (deprecate plan)
  4. ... something I forgot?

Each has different tradeoffs so input from @niemeyer on this would be best.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mvo5, modifying the code to use the existing plan variable sounds like a good idea to me as it's currently not in use by the qemu backend. If you and @niemeyer are happy with that I will adjust the PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented here: #169

I'll keep this PR open in case we want to go in a different direction

}

func (system *System) String() string { return system.Backend + ":" + system.Name }
Expand Down
8 changes: 8 additions & 0 deletions spread/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func biosPath(biosName string) (string, error) {
return "", fmt.Errorf("cannot find bios path for %q", biosName)
}

func machineType(system *System) string {
if system.MachineType == "" {
return "pc"
}
return system.MachineType
}

func qemuCmd(system *System, path string, mem, port int) (*exec.Cmd, error) {
serial := fmt.Sprintf("telnet:127.0.0.1:%d,server,nowait", port+100)
monitor := fmt.Sprintf("telnet:127.0.0.1:%d,server,nowait", port+200)
Expand All @@ -133,6 +140,7 @@ func qemuCmd(system *System, path string, mem, port int) (*exec.Cmd, error) {
"-net", fwd,
"-serial", serial,
"-monitor", monitor,
"-M", machineType(system),
path)
if os.Getenv("SPREAD_QEMU_GUI") != "1" {
cmd.Args = append([]string{cmd.Args[0], "-nographic"}, cmd.Args[1:]...)
Expand Down
33 changes: 33 additions & 0 deletions spread/qemu_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spread_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -91,3 +92,35 @@ func (s *qemuSuite) TestQemuCmdWithEfi(c *C) {
c.Check(strings.Contains(s, ":-bios:/usr/share/OVMF/OVMF_CODE.fd:"), Equals, tc.UseBiosQemuOption)
}
}

func (s *qemuSuite) TestQemuMachineTypes(c *C) {
imageName := "ubuntu-20.06-64"

restore := makeMockQemuImg(c, imageName)
defer restore()

tests := []struct {
MachineType string
ExpectedMachineType string
}{
// Make sure the default is "pc"
{"", "pc"},
// Make sure the specified machine type is used
{"q35", "q35"},
}

for _, tc := range tests {
ms := &spread.System{
Name: "some-name",
Image: imageName,
Backend: "qemu",
MachineType: tc.MachineType,
}
cmd, err := spread.QemuCmd(ms, "/path/to/image", 512, 9999)
c.Assert(err, IsNil)

s := strings.Join(cmd.Args, " ")
// Make sure the QEMU command specifies the given machine type, e.g. "-M pc"
c.Assert(s, Matches, fmt.Sprintf("^.*-M %s.*$", tc.ExpectedMachineType))
}
}