mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-03 18:47:03 +00:00
template: clean up template mountpoint on failures
So that we don't leave it behind. Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
parent
76b0c3c7d3
commit
b193f0f3d0
@ -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 {
|
||||
@ -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)
|
||||
}
|
||||
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user