mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-03 10:37:10 +00:00
sandbox: cleanup sandbox if creation failed
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>
This commit is contained in:
parent
0f6fb5439a
commit
bf1a5ce000
@ -74,6 +74,13 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cleanup sandbox resources in case of any failure
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
s.Delete()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Create the sandbox network
|
// Create the sandbox network
|
||||||
if err = s.createNetwork(); err != nil {
|
if err = s.createNetwork(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +134,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup host cgroups
|
// Setup host cgroups
|
||||||
if err := s.setupCgroups(); err != nil {
|
if err = s.setupCgroups(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -705,3 +705,7 @@ func (fc *firecracker) getThreadIDs() (*threadIDs, error) {
|
|||||||
// of get /machine-config
|
// of get /machine-config
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fc *firecracker) cleanup() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -606,4 +606,5 @@ type hypervisor interface {
|
|||||||
capabilities() capabilities
|
capabilities() capabilities
|
||||||
hypervisorConfig() HypervisorConfig
|
hypervisorConfig() HypervisorConfig
|
||||||
getThreadIDs() (*threadIDs, error)
|
getThreadIDs() (*threadIDs, error)
|
||||||
|
cleanup() error
|
||||||
}
|
}
|
||||||
|
@ -101,3 +101,7 @@ func (m *mockHypervisor) getThreadIDs() (*threadIDs, error) {
|
|||||||
vcpus := []int{os.Getpid()}
|
vcpus := []int{os.Getpid()}
|
||||||
return &threadIDs{vcpus}, nil
|
return &threadIDs{vcpus}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockHypervisor) cleanup() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -533,6 +533,7 @@ func (q *qemu) startSandbox() error {
|
|||||||
q.Logger().WithError(err).Error("After launching Qemu")
|
q.Logger().WithError(err).Error("After launching Qemu")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
q.fds = []*os.File{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
vmPath := filepath.Join(RunVMStoragePath, q.id)
|
vmPath := filepath.Join(RunVMStoragePath, q.id)
|
||||||
@ -1474,3 +1475,17 @@ func (q *qemu) resizeVCPUs(reqVCPUs uint32) (currentVCPUs uint32, newVCPUs uint3
|
|||||||
}
|
}
|
||||||
return currentVCPUs, newVCPUs, nil
|
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
|
||||||
|
}
|
||||||
|
@ -374,3 +374,14 @@ func TestQMPSetupShutdown(t *testing.T) {
|
|||||||
err := q.qmpSetup()
|
err := q.qmpSetup()
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQemuCleanup(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
q := &qemu{
|
||||||
|
config: newQemuConfig(),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := q.cleanup()
|
||||||
|
assert.Nil(err)
|
||||||
|
}
|
||||||
|
@ -1001,6 +1001,10 @@ func (s *Sandbox) Delete() error {
|
|||||||
s.monitor.stop()
|
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)
|
return s.storage.deleteSandboxResources(s.id, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user