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

Commit

Permalink
template: clean up template mountpoint on failures
Browse files Browse the repository at this point in the history
So that we don't leave it behind.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Sep 20, 2018
1 parent 76b0c3c commit b193f0f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions virtcontainers/factory/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func New(ctx context.Context, config vc.VMConfig) base.FactoryBase {
// fallback to direct factory if template is not supported.
return direct.New(ctx, config)
}
defer func() {
if err != nil {
t.close()
}
}()

err = t.createTemplateVM(ctx)
if err != nil {
Expand All @@ -73,6 +78,10 @@ func (t *template) GetBaseVM(ctx context.Context, config vc.VMConfig) (*vc.VM, e

// CloseFactory cleans up the template VM.
func (t *template) CloseFactory(ctx context.Context) {
t.close()
}

func (t *template) close() {
syscall.Unmount(t.statePath, 0)
os.RemoveAll(t.statePath)
}
Expand All @@ -86,10 +95,12 @@ func (t *template) prepareTemplateFiles() error {
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+8)
if err = syscall.Mount("tmpfs", t.statePath, "tmpfs", flags, opts); err != nil {
t.close()
return err
}
f, err := os.Create(t.statePath + "/memory")
if err != nil {
t.close()
return err
}
f.Close()
Expand Down

0 comments on commit b193f0f

Please sign in to comment.