forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
193 lines (159 loc) · 4.87 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// template implements base vm factory with vm templating.
package template
import (
"context"
"fmt"
"os"
"syscall"
"time"
pb "github.com/kata-containers/runtime/protocols/cache"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/base"
"github.com/sirupsen/logrus"
)
type template struct {
statePath string
config vc.VMConfig
}
var templateWaitForAgent = 2 * time.Second
var templateLog = logrus.WithField("source", "virtcontainers/factory")
// Fetch finds and returns a pre-built template factory.
// TODO: save template metadata and fetch from storage.
func Fetch(config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
t := &template{templatePath, config}
err := t.checkTemplateVM()
if err != nil {
return nil, err
}
return t, nil
}
// New creates a new VM template factory.
func New(ctx context.Context, config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
t := &template{templatePath, config}
err := t.checkTemplateVM()
if err == nil {
return nil, fmt.Errorf("There is already a VM template in %s", templatePath)
}
err = t.prepareTemplateFiles()
if err != nil {
return nil, err
}
defer func() {
if err != nil {
t.close()
}
}()
err = t.createTemplateVM(ctx)
if err != nil {
return nil, err
}
return t, nil
}
// Config returns template factory's configuration.
func (t *template) Config() vc.VMConfig {
return t.config
}
// GetBaseVM creates a new paused VM from the template VM.
func (t *template) GetBaseVM(ctx context.Context, config vc.VMConfig) (*vc.VM, error) {
return t.createFromTemplateVM(ctx, config)
}
// CloseFactory cleans up the template VM.
func (t *template) CloseFactory(ctx context.Context) {
t.close()
}
// GetVMStatus is not supported
func (t *template) GetVMStatus() []*pb.GrpcVMStatus {
panic("ERROR: package template does not support GetVMStatus")
}
func (t *template) close() {
if err := syscall.Unmount(t.statePath, syscall.MNT_DETACH); err != nil {
t.Logger().WithError(err).Errorf("failed to unmount %s", t.statePath)
}
if err := os.RemoveAll(t.statePath); err != nil {
t.Logger().WithError(err).Errorf("failed to remove %s", t.statePath)
}
}
func (t *template) prepareTemplateFiles() error {
// create and mount tmpfs for the shared memory file
err := os.MkdirAll(t.statePath, 0700)
if err != nil {
return err
}
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+templateDeviceStateSize)
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()
return nil
}
func (t *template) createTemplateVM(ctx context.Context) error {
// create the template vm
config := t.config
config.HypervisorConfig.BootToBeTemplate = true
config.HypervisorConfig.BootFromTemplate = false
config.HypervisorConfig.MemoryPath = t.statePath + "/memory"
config.HypervisorConfig.DevicesStatePath = t.statePath + "/state"
vm, err := vc.NewVM(ctx, config)
if err != nil {
return err
}
defer vm.Stop()
if err = vm.Disconnect(); err != nil {
return err
}
// Sleep a bit to let the agent grpc server clean up
// When we close connection to the agent, it needs sometime to cleanup
// and restart listening on the communication( serial or vsock) port.
// That time can be saved if we sleep a bit to wait for the agent to
// come around and start listening again. The sleep is only done when
// creating new vm templates and saves time for every new vm that are
// created from template, so it worth the invest.
time.Sleep(templateWaitForAgent)
if err = vm.Pause(); err != nil {
return err
}
if err = vm.Save(); err != nil {
return err
}
return nil
}
func (t *template) createFromTemplateVM(ctx context.Context, c vc.VMConfig) (*vc.VM, error) {
config := t.config
config.HypervisorConfig.BootToBeTemplate = false
config.HypervisorConfig.BootFromTemplate = true
config.HypervisorConfig.MemoryPath = t.statePath + "/memory"
config.HypervisorConfig.DevicesStatePath = t.statePath + "/state"
config.ProxyType = c.ProxyType
config.ProxyConfig = c.ProxyConfig
return vc.NewVM(ctx, config)
}
func (t *template) checkTemplateVM() error {
_, err := os.Stat(t.statePath + "/memory")
if err != nil {
return err
}
_, err = os.Stat(t.statePath + "/state")
return err
}
// Logger returns a logrus logger appropriate for logging template messages
func (t *template) Logger() *logrus.Entry {
return templateLog.WithFields(logrus.Fields{
"subsystem": "template",
})
}
// SetLogger sets the logger for the factory template.
func SetLogger(ctx context.Context, logger logrus.FieldLogger) {
fields := templateLog.Data
templateLog = logger.WithFields(fields)
}