-
Notifications
You must be signed in to change notification settings - Fork 63
/
ociimage.go
266 lines (215 loc) · 6.03 KB
/
ociimage.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package initrd
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"kraftkit.sh/log"
"github.com/anchore/stereoscope"
scfile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/filetree/filenode"
"github.com/cavaliergopher/cpio"
"github.com/containers/image/v5/copy"
ociarchive "github.com/containers/image/v5/oci/archive"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
)
type ociimage struct {
imageName string
opts InitrdOptions
args []string
ref types.ImageReference
env []string
files []string
}
// NewFromOCIImage creates a new initrd from a remote container image.
func NewFromOCIImage(ctx context.Context, path string, opts ...InitrdOption) (Initrd, error) {
if !strings.Contains("://", path) {
path = fmt.Sprintf("docker://%s", path)
}
ref, err := alltransports.ParseImageName(path)
if err != nil {
return nil, err
}
initrd := ociimage{
imageName: path,
ref: ref,
}
for _, opt := range opts {
if err := opt(&initrd.opts); err != nil {
return nil, err
}
}
return &initrd, nil
}
// Build implements Initrd.
func (initrd *ociimage) Build(ctx context.Context) (string, error) {
sysCtx := &types.SystemContext{
OSChoice: "linux",
}
if initrd.opts.arch != "" {
if initrd.opts.arch == "x86_64" {
sysCtx.ArchitectureChoice = "amd64"
} else {
sysCtx.ArchitectureChoice = initrd.opts.arch
}
}
policy := &signature.Policy{
Default: []signature.PolicyRequirement{
signature.NewPRInsecureAcceptAnything(),
},
}
policyCtx, err := signature.NewPolicyContext(policy)
if err != nil {
return "", fmt.Errorf("failed to generate default policy context: %w", err)
}
defer func() {
_ = policyCtx.Destroy()
}()
img, err := initrd.ref.NewImage(ctx, sysCtx)
if err != nil {
return "", err
}
defer func() {
_ = img.Close()
}()
ociImage, err := img.OCIConfig(ctx)
if err != nil {
return "", err
}
initrd.args = ociImage.Config.Entrypoint
initrd.args = append(initrd.args, ociImage.Config.Cmd...)
initrd.env = ociImage.Config.Env
if initrd.opts.output == "" {
fi, err := os.CreateTemp("", "")
if err != nil {
return "", err
}
initrd.opts.output = fi.Name()
}
// Create a temporary directory to output the image to
outputDir, err := os.MkdirTemp("", "")
if err != nil {
return "", fmt.Errorf("could not make temporary directory: %w", err)
}
defer func() {
_ = os.RemoveAll(outputDir)
}()
ociTarballFile := filepath.Join(outputDir, "oci.tar.gz")
dest, err := ociarchive.NewReference(ociTarballFile, "")
if err != nil {
return "", fmt.Errorf("invalid destination name %s: %v", dest, err)
}
opts := copy.Options{
ReportWriter: log.G(ctx).Writer(),
DestinationCtx: sysCtx,
SourceCtx: sysCtx,
}
if _, err = copy.Image(ctx, policyCtx, dest, initrd.ref, &opts); err != nil {
return "", fmt.Errorf("failed to copy image: %w", err)
}
image, err := stereoscope.GetImage(ctx, ociTarballFile)
if err != nil {
return "", fmt.Errorf("could not load image: %w", err)
}
f, err := os.OpenFile(initrd.opts.output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return "", fmt.Errorf("could not open initramfs file: %w", err)
}
defer func() {
_ = f.Close()
}()
writer := cpio.NewWriter(f)
defer func() {
_ = writer.Close()
}()
if err := image.SquashedTree().Walk(func(path scfile.Path, f filenode.FileNode) error {
if f.Reference == nil {
log.G(ctx).
WithField("path", path).
Warn("skipping: no reference")
return nil
}
info, err := image.FileCatalog.Get(*f.Reference)
if err != nil {
return err
}
internal := fmt.Sprintf(".%s", path)
if f.FileType == scfile.TypeDirectory {
if err := writer.WriteHeader(&cpio.Header{
Name: internal,
Mode: cpio.FileMode(info.Mode().Perm()) | cpio.TypeDir,
}); err != nil {
return fmt.Errorf("could not write CPIO header: %w", err)
}
return nil
}
initrd.files = append(initrd.files, internal)
log.G(ctx).
WithField("file", path).
Trace("archiving")
var data []byte
header := &cpio.Header{
Name: internal,
Mode: cpio.FileMode(info.Mode().Perm()),
ModTime: info.ModTime(),
Size: info.Size(),
}
// Populate platform specific information
populateCPIO(info, header)
// No other file types are part of the archive. As a result we only check
// whether the path is the same as the reference path which indicates
// whether the entry is a symbolic link.
if f.FileType == scfile.TypeSymLink {
header.Mode |= cpio.TypeSymlink
header.Linkname = info.Path
header.Size = int64(len(info.Path))
data = []byte(info.Path)
} else {
reader, err := image.OpenPathFromSquash(path)
if err != nil {
return fmt.Errorf("could not open file: %w", err)
}
header.Mode |= cpio.TypeReg
header.Size = info.Size()
data, err = io.ReadAll(reader)
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
}
if err := writer.WriteHeader(header); err != nil {
return fmt.Errorf("writing cpio header for %q: %w", internal, err)
}
if _, err := writer.Write(data); err != nil {
return fmt.Errorf("could not write CPIO data for %s: %w", internal, err)
}
return nil
}, nil); err != nil {
return "", fmt.Errorf("could not walk image: %w", err)
}
if initrd.opts.compress {
if err := compressFiles(initrd.opts.output, writer, f); err != nil {
return "", fmt.Errorf("could not compress files: %w", err)
}
}
return initrd.opts.output, nil
}
// Files implements Initrd.
func (initrd *ociimage) Files() []string {
return initrd.files
}
// Env implements Initrd.
func (initrd *ociimage) Env() []string {
return initrd.env
}
// Args implements Initrd.
func (initrd *ociimage) Args() []string {
return initrd.args
}