forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkata-volume.go
167 lines (151 loc) · 4.09 KB
/
kata-volume.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
// Copyright (c) 2022 Databricks Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"encoding/json"
"fmt"
"net/url"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
volume "github.com/kata-containers/kata-containers/src/runtime/pkg/direct-volume"
"github.com/kata-containers/kata-containers/src/runtime/pkg/utils/shimclient"
"github.com/urfave/cli"
)
var volumeSubCmds = []cli.Command{
addCommand,
removeCommand,
statsCommand,
resizeCommand,
}
var (
mountInfo string
volumePath string
size uint64
)
var kataVolumeCommand = cli.Command{
Name: "direct-volume",
Usage: "directly assign a volume to Kata Containers to manage",
Subcommands: volumeSubCmds,
Action: func(context *cli.Context) {
cli.ShowSubcommandHelp(context)
},
}
var addCommand = cli.Command{
Name: "add",
Usage: "add a direct assigned block volume device to the Kata Containers runtime",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
cli.StringFlag{
Name: "mount-info",
Usage: "the mount info for the Kata Containers runtime to manage the volume",
Destination: &mountInfo,
},
},
Action: func(c *cli.Context) error {
if err := volume.Add(volumePath, mountInfo); err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
},
}
var removeCommand = cli.Command{
Name: "remove",
Usage: "remove a direct assigned block volume device from the Kata Containers runtime",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
},
Action: func(c *cli.Context) error {
if err := volume.Remove(volumePath); err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
},
}
var statsCommand = cli.Command{
Name: "stats",
Usage: "get the filesystem stat of a direct assigned volume",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
},
Action: func(c *cli.Context) error {
stats, err := Stats(volumePath)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println(string(stats))
return nil
},
}
var resizeCommand = cli.Command{
Name: "resize",
Usage: "resize a direct assigned block volume",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
cli.Uint64Flag{
Name: "size",
Usage: "the new size of the volume",
Destination: &size,
},
},
Action: func(c *cli.Context) error {
if err := Resize(volumePath, size); err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
},
}
// Stats retrieves the filesystem stats of the direct volume inside the guest.
func Stats(volumePath string) ([]byte, error) {
sandboxId, err := volume.GetSandboxIdForVolume(volumePath)
if err != nil {
return nil, err
}
volumeMountInfo, err := volume.VolumeMountInfo(volumePath)
if err != nil {
return nil, err
}
urlSafeDevicePath := url.PathEscape(volumeMountInfo.Device)
body, err := shimclient.DoGet(sandboxId, defaultTimeout,
fmt.Sprintf("%s?%s=%s", containerdshim.DirectVolumeStatUrl, containerdshim.DirectVolumePathKey, urlSafeDevicePath))
if err != nil {
return nil, err
}
return body, nil
}
// Resize resizes a direct volume inside the guest.
func Resize(volumePath string, size uint64) error {
sandboxId, err := volume.GetSandboxIdForVolume(volumePath)
if err != nil {
return err
}
volumeMountInfo, err := volume.VolumeMountInfo(volumePath)
if err != nil {
return err
}
resizeReq := containerdshim.ResizeRequest{
VolumePath: volumeMountInfo.Device,
Size: size,
}
encoded, err := json.Marshal(resizeReq)
if err != nil {
return err
}
return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, "application/json", encoded)
}