Skip to content

Commit

Permalink
vmimpl: refactor VM type registration
Browse files Browse the repository at this point in the history
Pass Type struct directly during registration.
This allows to add additional optional parameters to VM types
without changing all VM implementations.
We we will need to add SupportsSnapshots flag and one flag to resolve #5028.
With this change it will be possible to add "SupportsSnapshots: true"
to just one VM type implemenetation.
  • Loading branch information
dvyukov committed Jul 23, 2024
1 parent e50e8da commit 53c2e8a
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 21 deletions.
4 changes: 3 additions & 1 deletion vm/adb/adb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
)

func init() {
vmimpl.Register("adb", ctor, false)
vmimpl.Register("adb", vmimpl.Type{
Ctor: ctor,
})
}

type Device struct {
Expand Down
5 changes: 4 additions & 1 deletion vm/bhyve/bhyve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
)

func init() {
vmimpl.Register("bhyve", ctor, true)
vmimpl.Register("bhyve", vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
5 changes: 4 additions & 1 deletion vm/cuttlefish/cuttlefish.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const (
)

func init() {
vmimpl.Register("cuttlefish", ctor, true)
vmimpl.Register("cuttlefish", vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Pool struct {
Expand Down
5 changes: 4 additions & 1 deletion vm/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import (
)

func init() {
vmimpl.Register("gce", ctor, true)
vmimpl.Register("gce", vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
5 changes: 4 additions & 1 deletion vm/gvisor/gvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
)

func init() {
vmimpl.Register(targets.GVisor, ctor, true)
vmimpl.Register(targets.GVisor, vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
4 changes: 3 additions & 1 deletion vm/isolated/isolated.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
const pstoreConsoleFile = "/sys/fs/pstore/console-ramoops-0"

func init() {
vmimpl.Register("isolated", ctor, false)
vmimpl.Register("isolated", vmimpl.Type{
Ctor: ctor,
})
}

type Config struct {
Expand Down
7 changes: 3 additions & 4 deletions vm/proxyapp/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ func makeDefaultParams() *proxyAppParams {
}

func init() {
vmimpl.Register(
"proxyapp",
func(env *vmimpl.Env) (vmimpl.Pool, error) {
vmimpl.Register("proxyapp", vmimpl.Type{
Ctor: func(env *vmimpl.Env) (vmimpl.Pool, error) {
return ctor(makeDefaultParams(), env)
},
false)
})
}

// Package configuration VARs are mostly needed for tests.
Expand Down
5 changes: 4 additions & 1 deletion vm/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (

func init() {
var _ vmimpl.Infoer = (*instance)(nil)
vmimpl.Register("qemu", ctor, true)
vmimpl.Register("qemu", vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
5 changes: 4 additions & 1 deletion vm/starnix/starnix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (

func init() {
var _ vmimpl.Infoer = (*instance)(nil)
vmimpl.Register(targets.Starnix, ctor, true)
vmimpl.Register(targets.Starnix, vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
4 changes: 3 additions & 1 deletion vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func init() {
ctor := func(env *vmimpl.Env) (vmimpl.Pool, error) {
return &testPool{}, nil
}
vmimpl.Register("test", ctor, false)
vmimpl.Register("test", vmimpl.Type{
Ctor: ctor,
})
}

type Test struct {
Expand Down
11 changes: 5 additions & 6 deletions vm/vmimpl/vmimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ func (err InfraError) InfraError() (string, []byte) {
}

// Register registers a new VM type within the package.
func Register(typ string, ctor ctorFunc, allowsOvercommit bool) {
Types[typ] = Type{
Ctor: ctor,
Overcommit: allowsOvercommit,
}
func Register(typ string, desc Type) {
Types[typ] = desc
}

type Type struct {
Ctor ctorFunc
Ctor ctorFunc
// It's possible to create out-of-thin-air instances of this type.
// Out-of-thin-air instances are used by syz-ci for image testing, patch testing, bisection, etc.
Overcommit bool
}

Expand Down
5 changes: 4 additions & 1 deletion vm/vmm/vmm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
var vmctlStatusRegex = regexp.MustCompile(`^\s+([0-9]+)\b.*\brunning`)

func init() {
vmimpl.Register("vmm", ctor, true)
vmimpl.Register("vmm", vmimpl.Type{
Ctor: ctor,
Overcommit: true,
})
}

type Config struct {
Expand Down
4 changes: 3 additions & 1 deletion vm/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

func init() {
vmimpl.Register("vmware", ctor, false)
vmimpl.Register("vmware", vmimpl.Type{
Ctor: ctor,
})
}

type Config struct {
Expand Down

0 comments on commit 53c2e8a

Please sign in to comment.