/
containerd.go
337 lines (313 loc) · 11 KB
/
containerd.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
337
package containerd
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/deepfence/vessel/utils"
"github.com/sirupsen/logrus"
"github.com/containerd/containerd"
containerdApi "github.com/containerd/containerd"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
)
// New instantiates a new Containerd runtime object
func New(host string) *Containerd {
return &Containerd{
socketPath: host,
namespaces: getNamespaces(host),
}
}
func getNamespaces(host string) []string {
clientd, err := containerd.New(strings.Replace(host, "unix://", "", 1))
if err != nil {
return nil
}
defer clientd.Close()
namespace_store := clientd.NamespaceService()
list, err := namespace_store.List(context.Background())
if err != nil {
return nil
}
return list
}
// GetSocket is socket getter
func (c Containerd) GetSocket() string {
return c.socketPath
}
// ExtractImage will create the tarball from the containerd image, extracts into dir
// and then skopeo is used to migrate oci layers using the dir to docker v1 layer spec format tar
// and again extracts back to dir
// example:
// skopeo copy oci:///home/ubuntu/img/docker/threatmapper_containerd-dir \
// docker-archive:/home/ubuntu/img/docker/threatmapper_containerd.tar
func (c Containerd) ExtractImage(imageID, imageName, path string) error {
var stderr bytes.Buffer
save := exec.Command("nerdctl", "save", imageName, "--address", c.socketPath)
save.Stderr = &stderr
extract := exec.Command("tar", "xf", "-", "--warning=none", "-C"+path)
extract.Stderr = &stderr
pipe, err := extract.StdinPipe()
if err != nil {
return err
}
save.Stdout = pipe
err = extract.Start()
if err != nil {
return errors.New(stderr.String())
}
err = save.Run()
if err != nil {
return errors.New(stderr.String())
}
err = pipe.Close()
if err != nil {
return err
}
err = extract.Wait()
if err != nil {
return errors.New(stderr.String())
}
err = migrateOCIToDockerV1(path, imageID, "")
if err != nil {
return err
}
return nil
}
// GetImageID returns the image id
func (c Containerd) GetImageID(imageName string) ([]byte, error) {
return exec.Command("nerdctl", "images", "-q", "--no-trunc", imageName, "--address", c.socketPath).Output()
}
// Save just saves image using -o flag
func (c Containerd) Save(imageName, outputParam string) ([]byte, error) {
nerrors := []error{}
for _, ns := range c.namespaces {
res, err := exec.Command("nerdctl", "-n", ns, "save", "--address", c.socketPath, "-o", outputParam, imageName).CombinedOutput()
if err == nil {
return res, nil
}
nerrors = append(nerrors, fmt.Errorf("namespace: %v, err: %v\n", ns, string(res)+err.Error()))
}
return nil, fmt.Errorf("Save failed. errors:\n%v", nerrors)
}
// migrateOCIToDockerV1 migrates OCI image to Docker v1 image tarball
func migrateOCIToDockerV1(path, imageID, tarFilePath string) error {
if tarFilePath == "" {
tarFilePath = path + imageID + ".tar"
}
sourceDir := "oci://" + path
destinationTar := "docker-archive:" + tarFilePath
var stderr bytes.Buffer
// skopeo will convert oci dir into docker v1 tarball
skopeoCopy := exec.Command("/usr/bin/skopeo", "copy", sourceDir, destinationTar)
skopeoCopy.Stderr = &stderr
err := skopeoCopy.Run()
if err != nil {
return fmt.Errorf("failed to migrate OCI to Docker image: %v", stderr)
}
// untar the docker archive
tarxf := exec.Command("tar", "xf", tarFilePath, "--warning=none", "-C"+path)
tarxf.Stderr = &stderr
err = tarxf.Run()
if err != nil {
return fmt.Errorf("failed to migrate OCI to Docker image: %v", err)
}
// delete docker tar, not required
removeTar := exec.Command("rm", tarFilePath)
removeTar.Stderr = &stderr
err = removeTar.Run()
if err != nil {
return fmt.Errorf("failed to delete generated docker-archive: %v", err)
}
return nil
}
// fileuploader specific
func MigrateOCITarToDockerV1Tar(dir, tarName string) error {
logrus.Info("migrating image ...")
var stderr bytes.Buffer
tarPath := path.Join(dir, tarName)
_, err := exec.Command("tar", "xf", tarPath, "--warning=none", "-C"+dir).Output()
if err != nil {
return fmt.Errorf("failed to migrate OCI to Docker image: failed to untar: %v", err)
}
// delete docker tar, not required
removeTar := exec.Command("rm", tarPath)
removeTar.Stderr = &stderr
err = removeTar.Run()
if err != nil {
return fmt.Errorf("failed to delete generated docker-archive: %v", err)
}
//migrate now
sourceDir := "oci://" + dir
destinationTar := "docker-archive:" + tarPath
// var stderr bytes.Buffer
// skopeo will convert oci dir into docker v1 tarball
skopeoCopy := exec.Command("/usr/bin/skopeo", "copy", sourceDir, destinationTar)
skopeoCopy.Stderr = &stderr
err = skopeoCopy.Run()
if err != nil {
return fmt.Errorf("failed to migrate OCI to Docker image: %v", stderr)
}
return nil
}
// ExtractFileSystem Extract the file system from tar of an image by creating a temporary dormant container instance
func (c Containerd) ExtractFileSystem(imageTarPath string, outputTarPath string, imageName string) error {
// create a new client connected to the default socket path for containerd
client, err := containerdApi.New(strings.Replace(c.socketPath, "unix://", "", 1))
if err != nil {
return err
}
defer client.Close()
// create a new context with an "temp" namespace
ctx := namespaces.WithNamespace(context.Background(), "temp")
reader, err := os.Open(imageTarPath)
if err != nil {
logrus.Error("Error while opening image")
return err
}
imgs, err := client.Import(ctx, reader,
containerdApi.WithSkipDigestRef(func(name string) bool { return name != "" }),
containerdApi.WithDigestRef(archive.DigestTranslator(imageName)))
if err != nil {
logrus.Error("Error while Importing image")
return err
}
if len(imgs) == 0 {
logrus.Errorf("No images imported, imageTarPath: %s, outputTarPath: %s, imageName: %s \n", imageTarPath, outputTarPath, imageName)
return errors.New("image not imported from: " + imageTarPath)
}
image, err := client.GetImage(ctx, imgs[0].Name)
if err != nil {
logrus.Error("Error while getting image from client")
return err
}
rand.Seed(time.Now().UnixNano())
containerName := "temp" + fmt.Sprint(rand.Intn(9999))
err = image.Unpack(ctx, "")
if err != nil {
logrus.Error("Error while unpacking image")
return err
}
container, err := client.NewContainer(
ctx,
containerName,
containerdApi.WithImage(image),
containerdApi.WithNewSnapshot("temp"+fmt.Sprint(rand.Intn(9999)), image),
containerdApi.WithNewSpec(oci.WithImageConfig(image)),
)
if err != nil {
logrus.Error("Error while creating container")
return err
}
info, _ := container.Info(ctx)
snapshotter := client.SnapshotService(info.Snapshotter)
mounts, err := snapshotter.Mounts(ctx, info.SnapshotKey)
target := strings.Replace(outputTarPath, ".tar", "", 1) + containerName
_, err = exec.Command("mkdir", target).Output()
if err != nil && !strings.Contains(err.Error(), "exit status 1") {
logrus.Errorf("Error while creating temp target dir %s: %s", target, err.Error())
return err
}
defer func() {
exec.Command("umount", target).Output()
exec.Command("rm", "-rf", target).Output()
}()
_, err = exec.Command("bash", "-c", fmt.Sprintf("mount -t %s %s %s -o %s\n", mounts[0].Type, mounts[0].Source, target, strings.Join(mounts[0].Options, ","))).Output()
if err != nil {
logrus.Error("Error while mounting image on temp target dir")
return err
}
_, err = exec.Command("tar", "-czvf", outputTarPath, "-C", target, ".").Output()
if !utils.CheckTarFileValid(outputTarPath) {
if err != nil {
logrus.Error("Error while packing tar")
return err
}
}
container.Delete(ctx, containerdApi.WithSnapshotCleanup)
client.ImageService().Delete(ctx, imgs[0].Name, images.SynchronousDelete())
return nil
}
// ExtractFileSystemContainer Extract the file system of an existing container to tar
func (c Containerd) ExtractFileSystemContainer(containerId string, namespace string, outputTarPath string) error {
// create a new client connected to the default socket path for containerd
client, err := containerdApi.New(strings.Replace(c.socketPath, "unix://", "", 1))
if err != nil {
return err
}
defer client.Close()
// create a new context with namespace
if len(namespace) == 0 {
namespace = utils.CONTAINERD_K8S_NS
}
ctx := namespaces.WithNamespace(context.Background(), namespace)
container, err := client.LoadContainer(ctx, containerId)
if err != nil {
logrus.Error("Error while getting container")
return err
}
info, _ := container.Info(ctx)
snapshotter := client.SnapshotService(info.Snapshotter)
mounts, err := snapshotter.Mounts(ctx, info.SnapshotKey)
target := strings.Replace(outputTarPath, ".tar", "", 1) + containerId
_, err = exec.Command("mkdir", target).Output()
if err != nil && !strings.Contains(err.Error(), "exit status 1") {
logrus.Errorf("Error while creating temp target dir %s %s \n", target, err.Error())
return err
}
defer func() {
exec.Command("umount", target).Output()
exec.Command("rm", "-rf", target).Output()
}()
var mountStatement = fmt.Sprintf("mount -t %s %s %s -o %s\n", mounts[0].Type, mounts[0].Source, target, strings.Join(mounts[0].Options, ","))
cmd := exec.Command("bash", "-c", mountStatement)
logrus.Infof("mount command: %s", cmd.String())
_, err = cmd.Output()
if err != nil {
mountedHostPath := "/fenced/mnt/host"
logrus.Warnf("error while mounting image on temp target dir %s %s %s \n", mountStatement, " err: ", err.Error())
logrus.Infof("Reattempting mount from %s \n", mountedHostPath)
var containerdTmpDirs = []string{"/tmp", "/var/lib"}
var workDir, upperDir, lowerDir string
for index, option := range mounts[0].Options {
for _, tmpDir := range containerdTmpDirs {
if strings.Contains(option, tmpDir) {
mounts[0].Options[index] = strings.Replace(option, tmpDir, mountedHostPath+tmpDir, -1)
if strings.Index(mounts[0].Options[index], "upperdir") >= 0 {
upperDir = strings.Split(mounts[0].Options[index], "=")[1]
} else if strings.Index(mounts[0].Options[index], "workdir") >= 0 {
workDir = strings.Split(mounts[0].Options[index], "=")[1]
} else if strings.Index(mounts[0].Options[index], "lowerdir") >= 0 {
lowerDir = strings.Split(mounts[0].Options[index], "=")[1]
}
}
}
}
mountStatement = fmt.Sprintf("mount -t %s %s %s -o index=off,lowerdir=%s \n",
mounts[0].Type, mounts[0].Source, target, workDir+":"+upperDir+":"+lowerDir)
cmd := exec.Command("bash", "-c", mountStatement)
logrus.Infof("mount command: %s", cmd.String())
_, err = cmd.Output()
if err != nil {
logrus.Errorf("error while mounting image on temp target dir 2nd attempt %s %s %s \n", mountStatement, " err: ", err.Error())
return err
}
logrus.Info("mount success \n")
}
_, err = exec.Command("tar", "-czvf", outputTarPath, "-C", target, ".").Output()
if !utils.CheckTarFileValid(outputTarPath) {
if err != nil {
logrus.Errorf("Error while packing tar %s %s %s \n", outputTarPath, target, err.Error())
return err
}
}
return nil
}