Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
sandbox: cleanup sandbox if creation failed
Browse files Browse the repository at this point in the history
This includes cleaning up the sandbox on disk resources,
and closing open fds when preparing the hypervisor.

Fixes: #1057

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Dec 21, 2018
1 parent 0f6fb54 commit bf1a5ce
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
11 changes: 9 additions & 2 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
return nil, err
}

// cleanup sandbox resources in case of any failure
defer func() {
if err != nil {
s.Delete()
}
}()

// Create the sandbox network
if err = s.createNetwork(); err != nil {
return nil, err
Expand Down Expand Up @@ -112,7 +119,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
}
}()

if err := s.getAndStoreGuestDetails(); err != nil {
if err = s.getAndStoreGuestDetails(); err != nil {
return nil, err
}

Expand All @@ -127,7 +134,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
}

// Setup host cgroups
if err := s.setupCgroups(); err != nil {
if err = s.setupCgroups(); err != nil {
return nil, err
}

Expand Down
4 changes: 4 additions & 0 deletions virtcontainers/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,7 @@ func (fc *firecracker) getThreadIDs() (*threadIDs, error) {
// of get /machine-config
return nil, nil
}

func (fc *firecracker) cleanup() error {
return nil
}
1 change: 1 addition & 0 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,5 @@ type hypervisor interface {
capabilities() capabilities
hypervisorConfig() HypervisorConfig
getThreadIDs() (*threadIDs, error)
cleanup() error
}
4 changes: 4 additions & 0 deletions virtcontainers/mock_hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ func (m *mockHypervisor) getThreadIDs() (*threadIDs, error) {
vcpus := []int{os.Getpid()}
return &threadIDs{vcpus}, nil
}

func (m *mockHypervisor) cleanup() error {
return nil
}
15 changes: 15 additions & 0 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ func (q *qemu) startSandbox() error {
q.Logger().WithError(err).Error("After launching Qemu")
}
}
q.fds = []*os.File{}
}()

vmPath := filepath.Join(RunVMStoragePath, q.id)
Expand Down Expand Up @@ -1474,3 +1475,17 @@ func (q *qemu) resizeVCPUs(reqVCPUs uint32) (currentVCPUs uint32, newVCPUs uint3
}
return currentVCPUs, newVCPUs, nil
}

func (q *qemu) cleanup() error {
span, _ := q.trace("cleanup")
defer span.Finish()

for _, fd := range q.fds {
if err := fd.Close(); err != nil {
q.Logger().WithError(err).Warn("failed closing fd")
}
}
q.fds = []*os.File{}

return nil
}
11 changes: 11 additions & 0 deletions virtcontainers/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,14 @@ func TestQMPSetupShutdown(t *testing.T) {
err := q.qmpSetup()
assert.Nil(err)
}

func TestQemuCleanup(t *testing.T) {
assert := assert.New(t)

q := &qemu{
config: newQemuConfig(),
}

err := q.cleanup()
assert.Nil(err)
}
4 changes: 4 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,10 @@ func (s *Sandbox) Delete() error {
s.monitor.stop()
}

if err := s.hypervisor.cleanup(); err != nil {
s.Logger().WithError(err).Error("failed to cleanup hypervisor")
}

return s.storage.deleteSandboxResources(s.id, nil)
}

Expand Down

0 comments on commit bf1a5ce

Please sign in to comment.