mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-11 12:52:23 +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.
|
// fallback to direct factory if template is not supported.
|
||||||
return direct.New(ctx, config)
|
return direct.New(ctx, config)
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
t.close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
err = t.createTemplateVM(ctx)
|
err = t.createTemplateVM(ctx)
|
||||||
if err != nil {
|
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.
|
// CloseFactory cleans up the template VM.
|
||||||
func (t *template) CloseFactory(ctx context.Context) {
|
func (t *template) CloseFactory(ctx context.Context) {
|
||||||
|
t.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *template) close() {
|
||||||
syscall.Unmount(t.statePath, 0)
|
syscall.Unmount(t.statePath, 0)
|
||||||
os.RemoveAll(t.statePath)
|
os.RemoveAll(t.statePath)
|
||||||
}
|
}
|
||||||
@ -86,10 +95,12 @@ func (t *template) prepareTemplateFiles() error {
|
|||||||
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
|
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
|
||||||
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+8)
|
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+8)
|
||||||
if err = syscall.Mount("tmpfs", t.statePath, "tmpfs", flags, opts); err != nil {
|
if err = syscall.Mount("tmpfs", t.statePath, "tmpfs", flags, opts); err != nil {
|
||||||
|
t.close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f, err := os.Create(t.statePath + "/memory")
|
f, err := os.Create(t.statePath + "/memory")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
t.close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user