forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vc.go
336 lines (279 loc) · 10.4 KB
/
vc.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package store
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"github.com/kata-containers/runtime/virtcontainers/device/api"
"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/device/drivers"
"github.com/kata-containers/runtime/virtcontainers/types"
)
// VCStorePrefix is only used for tests to config a temp store dir
var VCStorePrefix = ""
// VCStore is a virtcontainers specific Store.
// Virtcontainers typically needs a configuration Store for
// storing permanent items across reboots.
// It also needs a state Store for storing states and other run-time
// related items. Those should not survive a reboot.
//
// VCStore simply dispatches items into the right Store.
type VCStore struct {
config, state, uuid *Store
}
func (s *VCStore) itemToStore(item Item) *Store {
switch item {
case Configuration:
return s.config
case State, Network, Hypervisor, Agent, Process, Lock, Mounts, Devices, DeviceIDs:
return s.state
case UUID:
return s.uuid
}
return s.state
}
// NewVCStore creates a virtcontainers specific Store.
func NewVCStore(ctx context.Context, configRoot, stateRoot string) (*VCStore, error) {
config, err := New(ctx, configRoot)
if err != nil {
fmt.Printf("config root %s\n", configRoot)
return nil, err
}
state, err := New(ctx, stateRoot)
if err != nil {
return nil, err
}
uuid, err := New(ctx, VCStoreUUIDPath())
if err != nil {
return nil, err
}
return &VCStore{
config: config,
state: state,
uuid: uuid,
}, nil
}
// NewVCSandboxStore creates a virtcontainers sandbox Store, with filesystem backend.
func NewVCSandboxStore(ctx context.Context, sandboxID string) (*VCStore, error) {
if sandboxID == "" {
return nil, fmt.Errorf("sandbox ID can not be empty")
}
return NewVCStore(ctx,
SandboxConfigurationRoot(sandboxID),
SandboxRuntimeRoot(sandboxID),
)
}
// NewVCContainerStore creates a virtcontainers container Store, with filesystem backend.
func NewVCContainerStore(ctx context.Context, sandboxID, containerID string) (*VCStore, error) {
if sandboxID == "" {
return nil, fmt.Errorf("sandbox ID can not be empty")
}
if containerID == "" {
return nil, fmt.Errorf("container ID can not be empty")
}
return NewVCStore(ctx,
ContainerConfigurationRoot(sandboxID, containerID),
ContainerRuntimeRoot(sandboxID, containerID),
)
}
// Store stores a virtcontainers item into the right Store.
func (s *VCStore) Store(item Item, data interface{}) error {
return s.itemToStore(item).Store(item, data)
}
// Load loads a virtcontainers item from the right Store.
func (s *VCStore) Load(item Item, data interface{}) error {
return s.itemToStore(item).Load(item, data)
}
// Delete deletes all artifacts created by a VCStore.
// Both config and state Stores are also removed from the manager.
func (s *VCStore) Delete() error {
if err := s.config.Delete(); err != nil {
return err
}
if err := s.state.Delete(); err != nil {
return err
}
return nil
}
// LoadState loads an returns a virtcontainer state
func (s *VCStore) LoadState() (types.SandboxState, error) {
var state types.SandboxState
if err := s.state.Load(State, &state); err != nil {
return types.SandboxState{}, err
}
return state, nil
}
// LoadContainerState loads an returns a virtcontainer state
func (s *VCStore) LoadContainerState() (types.ContainerState, error) {
var state types.ContainerState
if err := s.state.Load(State, &state); err != nil {
return types.ContainerState{}, err
}
return state, nil
}
// TypedDevice is used as an intermediate representation for marshalling
// and unmarshalling Device implementations.
type TypedDevice struct {
Type string
// Data is assigned the Device object.
// This being declared as RawMessage prevents it from being marshalled/unmarshalled.
// We do that explicitly depending on Type.
Data json.RawMessage
}
// StoreDevices stores a virtcontainers devices slice.
// The Device slice is first marshalled into a TypedDevice
// one to include the type of the Device objects.
func (s *VCStore) StoreDevices(devices []api.Device) error {
var typedDevices []TypedDevice
for _, d := range devices {
tempJSON, _ := json.Marshal(d)
typedDevice := TypedDevice{
Type: string(d.DeviceType()),
Data: tempJSON,
}
typedDevices = append(typedDevices, typedDevice)
}
return s.state.Store(Devices, typedDevices)
}
// LoadDevices loads an returns a virtcontainer devices slice.
// We need a custom unmarshalling routine for translating TypedDevices
// into api.Devices based on their type.
func (s *VCStore) LoadDevices() ([]api.Device, error) {
var typedDevices []TypedDevice
var devices []api.Device
if err := s.state.Load(Devices, &typedDevices); err != nil {
return []api.Device{}, err
}
for _, d := range typedDevices {
switch d.Type {
case string(config.DeviceVFIO):
// TODO: remove dependency of drivers package
var device drivers.VFIODevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
case string(config.DeviceBlock):
// TODO: remove dependency of drivers package
var device drivers.BlockDevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
case string(config.DeviceGeneric):
// TODO: remove dependency of drivers package
var device drivers.GenericDevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
default:
return []api.Device{}, fmt.Errorf("Unknown device type, could not unmarshal")
}
}
return devices, nil
}
// Raw creates a raw item in the virtcontainer state Store. A raw
// item is a custom one, not defined through the Item enum, and that
// the caller needs to handle directly.
// Typically this is used to create a custom virtcontainers file.
// For example the Firecracker code uses this API to create temp
// files under the sandbox state root path, and uses them as block
// driver backend placeholder.
func (s *VCStore) Raw(id string) (string, error) {
return s.state.Raw(id)
}
// Lock takes an exclusive lock on the virtcontainers state Lock item.
func (s *VCStore) Lock() (string, error) {
return s.state.ItemLock(Lock, true)
}
// RLock takes a shared lock on the virtcontainers state Lock item.
func (s *VCStore) RLock() (string, error) {
return s.state.ItemLock(Lock, false)
}
// Unlock unlocks the virtcontainers state Lock item.
func (s *VCStore) Unlock(token string) error {
return s.state.ItemUnlock(Lock, token)
}
// Utilities for virtcontainers
// SandboxConfigurationRoot returns a virtcontainers sandbox configuration root URL.
// This will hold across host reboot persistent data about a sandbox configuration.
// It should look like file:///var/lib/vc/sbs/<sandboxID>/
// Or for rootless: file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/
func SandboxConfigurationRoot(id string) string {
return filesystemScheme + "://" + SandboxConfigurationRootPath(id)
}
// SandboxConfigurationRootPath returns a virtcontainers sandbox configuration root path.
func SandboxConfigurationRootPath(id string) string {
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id)
}
// SandboxConfigurationItemPath returns a virtcontainers sandbox configuration item path.
func SandboxConfigurationItemPath(id string, item Item) (string, error) {
if id == "" {
return "", fmt.Errorf("Empty sandbox ID")
}
itemFile, err := itemToFile(item)
if err != nil {
return "", err
}
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id, itemFile), nil
}
// VCStoreUUIDPath returns a virtcontainers runtime uuid URL.
func VCStoreUUIDPath() string {
return filesystemScheme + "://" + filepath.Join(VCStorePrefix, VMUUIDStoragePath())
}
// SandboxRuntimeRoot returns a virtcontainers sandbox runtime root URL.
// This will hold data related to a sandbox run-time state that will not
// be persistent across host reboots.
// It should look like file:///run/vc/sbs/<sandboxID>/
// or if rootless: file://<rootlessDir>/run/vc/sbs/<sandboxID>/
func SandboxRuntimeRoot(id string) string {
return filesystemScheme + "://" + SandboxRuntimeRootPath(id)
}
// SandboxRuntimeRootPath returns a virtcontainers sandbox runtime root path.
func SandboxRuntimeRootPath(id string) string {
return filepath.Join(VCStorePrefix, RunStoragePath(), id)
}
// SandboxRuntimeItemPath returns a virtcontainers sandbox runtime item path.
func SandboxRuntimeItemPath(id string, item Item) (string, error) {
if id == "" {
return "", fmt.Errorf("Empty sandbox ID")
}
itemFile, err := itemToFile(item)
if err != nil {
return "", err
}
return filepath.Join(RunStoragePath(), id, itemFile), nil
}
// ContainerConfigurationRoot returns a virtcontainers container configuration root URL.
// This will hold across host reboot persistent data about a container configuration.
// It should look like file:///var/lib/vc/sbs/<sandboxID>/<containerID>
// Or if rootless file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/<containerID>
func ContainerConfigurationRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + ContainerConfigurationRootPath(sandboxID, containerID)
}
// ContainerConfigurationRootPath returns a virtcontainers container configuration root path.
func ContainerConfigurationRootPath(sandboxID, containerID string) string {
return filepath.Join(VCStorePrefix, ConfigStoragePath(), sandboxID, containerID)
}
// ContainerRuntimeRoot returns a virtcontainers container runtime root URL.
// This will hold data related to a container run-time state that will not
// be persistent across host reboots.
// It should look like file:///run/vc/sbs/<sandboxID>/<containerID>/
// Or for rootless file://<rootlessDir>/run/vc/sbs/<sandboxID>/<containerID>/
func ContainerRuntimeRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + ContainerRuntimeRootPath(sandboxID, containerID)
}
// ContainerRuntimeRootPath returns a virtcontainers container runtime root path.
func ContainerRuntimeRootPath(sandboxID, containerID string) string {
return filepath.Join(VCStorePrefix, RunStoragePath(), sandboxID, containerID)
}
// VCSandboxStoreExists returns true if a sandbox store already exists.
func VCSandboxStoreExists(ctx context.Context, sandboxID string) bool {
s := stores.findStore(SandboxConfigurationRoot(sandboxID))
return s != nil
}