From cf22f402d80bd05ad00b5895d629d3358e334736 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Mon, 10 Dec 2018 04:09:33 +0100 Subject: [PATCH] virtcontainers: Remove the hypervisor waitSandbox method We always call waitSandbox after we start the VM (startSandbox), so let's simplify the hypervisor interface and integrate waiting for the VM into startSandbox. This makes startSandbox a blocking call, but that is practically the case today. Fixes: #1009 Signed-off-by: Samuel Ortiz --- virtcontainers/fc.go | 16 ++++++---------- virtcontainers/hypervisor.go | 3 +-- virtcontainers/mock_hypervisor.go | 6 +----- virtcontainers/mock_hypervisor_test.go | 10 +--------- virtcontainers/qemu.go | 4 ++-- virtcontainers/sandbox.go | 6 +----- virtcontainers/vm.go | 7 ++----- 7 files changed, 14 insertions(+), 38 deletions(-) diff --git a/virtcontainers/fc.go b/virtcontainers/fc.go index bc5e3bd37..7942da958 100644 --- a/virtcontainers/fc.go +++ b/virtcontainers/fc.go @@ -335,7 +335,7 @@ func (fc *firecracker) fcStartVM() error { // startSandbox will start the hypervisor for the given sandbox. // In the context of firecracker, this will start the hypervisor, // for configuration, but not yet start the actual virtual machine -func (fc *firecracker) startSandbox() error { +func (fc *firecracker) startSandbox(timeout int) error { span, _ := fc.trace("startSandbox") defer span.Finish() @@ -375,7 +375,11 @@ func (fc *firecracker) startSandbox() error { } } - return fc.fcStartVM() + if err := fc.fcStartVM(); err != nil { + return err + } + + return fc.waitVMM(timeout) } func (fc *firecracker) createDiskPool() error { @@ -411,14 +415,6 @@ func (fc *firecracker) createDiskPool() error { return nil } -// waitSandbox will wait for the Sandbox's VM to be up and running. -func (fc *firecracker) waitSandbox(timeout int) error { - span, _ := fc.trace("waitSandbox") - defer span.Finish() - - return fc.waitVMM(timeout) -} - // stopSandbox will stop the Sandbox's VM. func (fc *firecracker) stopSandbox() (err error) { span, _ := fc.trace("stopSandbox") diff --git a/virtcontainers/hypervisor.go b/virtcontainers/hypervisor.go index 0e2289bee..6d5335917 100644 --- a/virtcontainers/hypervisor.go +++ b/virtcontainers/hypervisor.go @@ -591,8 +591,7 @@ func RunningOnVMM(cpuInfoPath string) (bool, error) { // The default hypervisor implementation is Qemu. type hypervisor interface { createSandbox(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, storage resourceStorage) error - startSandbox() error - waitSandbox(timeout int) error + startSandbox(timeout int) error stopSandbox() error pauseSandbox() error saveSandbox() error diff --git a/virtcontainers/mock_hypervisor.go b/virtcontainers/mock_hypervisor.go index d5ca20c2d..f23b3b876 100644 --- a/virtcontainers/mock_hypervisor.go +++ b/virtcontainers/mock_hypervisor.go @@ -30,11 +30,7 @@ func (m *mockHypervisor) createSandbox(ctx context.Context, id string, hyperviso return nil } -func (m *mockHypervisor) startSandbox() error { - return nil -} - -func (m *mockHypervisor) waitSandbox(timeout int) error { +func (m *mockHypervisor) startSandbox(timeout int) error { return nil } diff --git a/virtcontainers/mock_hypervisor_test.go b/virtcontainers/mock_hypervisor_test.go index e6007380f..75fd59c80 100644 --- a/virtcontainers/mock_hypervisor_test.go +++ b/virtcontainers/mock_hypervisor_test.go @@ -47,15 +47,7 @@ func TestMockHypervisorCreateSandbox(t *testing.T) { func TestMockHypervisorStartSandbox(t *testing.T) { var m *mockHypervisor - if err := m.startSandbox(); err != nil { - t.Fatal(err) - } -} - -func TestMockHypervisorWaitSandbox(t *testing.T) { - var m *mockHypervisor - - if err := m.waitSandbox(0); err != nil { + if err := m.startSandbox(vmStartTimeout); err != nil { t.Fatal(err) } } diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index 97e9857c9..59d9dbbb1 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -534,7 +534,7 @@ func (q *qemu) createSandbox(ctx context.Context, id string, hypervisorConfig *H } // startSandbox will start the Sandbox's VM. -func (q *qemu) startSandbox() error { +func (q *qemu) startSandbox(timeout int) error { span, _ := q.trace("startSandbox") defer span.Finish() @@ -578,7 +578,7 @@ func (q *qemu) startSandbox() error { return fmt.Errorf("%s", strErr) } - return nil + return q.waitSandbox(timeout) } // waitSandbox will wait for the Sandbox's VM to be up and running. diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 90f1ea015..51a3a7f74 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -947,15 +947,11 @@ func (s *Sandbox) startVM() error { return nil } - return s.hypervisor.startSandbox() + return s.hypervisor.startSandbox(vmStartTimeout) }); err != nil { return err } - if err := s.hypervisor.waitSandbox(vmStartTimeout); err != nil { - return err - } - // In case of vm factory, network interfaces are hotplugged // after vm is started. if s.factory != nil { diff --git a/virtcontainers/vm.go b/virtcontainers/vm.go index e22fc1e69..43bffffe6 100644 --- a/virtcontainers/vm.go +++ b/virtcontainers/vm.go @@ -131,10 +131,7 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { } // 3. boot up guest vm - if err = hypervisor.startSandbox(); err != nil { - return nil, err - } - if err = hypervisor.waitSandbox(vmStartTimeout); err != nil { + if err = hypervisor.startSandbox(vmStartTimeout); err != nil { return nil, err } @@ -211,7 +208,7 @@ func (v *VM) Resume() error { // Start kicks off a configured VM. func (v *VM) Start() error { v.logger().Info("start vm") - return v.hypervisor.startSandbox() + return v.hypervisor.startSandbox(vmStartTimeout) } // Disconnect agent and proxy connections to a VM