-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcosicommon.go
More file actions
343 lines (288 loc) · 9.18 KB
/
cosicommon.go
File metadata and controls
343 lines (288 loc) · 9.18 KB
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
338
339
340
341
342
343
package imagecustomizerlib
import (
"archive/tar"
"crypto/sha512"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/microsoft/azurelinux/toolkit/tools/imagegen/diskutils"
"github.com/microsoft/azurelinux/toolkit/tools/internal/logger"
"github.com/microsoft/azurelinux/toolkit/tools/internal/safeloopback"
"github.com/microsoft/azurelinux/toolkit/tools/internal/shell"
)
type ImageBuildData struct {
Source string
KnownInfo outputPartitionMetadata
Metadata *FileSystem
VeritySource string
}
func convertToCosi(ic *ImageCustomizerParameters) error {
logger.Log.Infof("Extracting partition files")
outputDir := filepath.Join(ic.buildDirAbs, "cosiimages")
err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create folder %s:\n%w", outputDir, err)
}
defer os.Remove(outputDir)
imageLoopback, err := safeloopback.NewLoopback(ic.rawImageFile)
if err != nil {
return err
}
defer imageLoopback.Close()
partitionMetadataOutput, err := extractPartitions(imageLoopback.DevicePath(), outputDir, ic.outputImageBase,
"raw-zst", ic.imageUuid)
if err != nil {
return err
}
for _, partition := range partitionMetadataOutput {
defer os.Remove(path.Join(outputDir, partition.PartitionFilename))
}
err = buildCosiFile(outputDir, ic.outputImageFile, partitionMetadataOutput, ic.verityMetadata,
ic.partUuidToFstabEntry, ic.imageUuidStr, ic.osRelease, ic.osPackages)
if err != nil {
return fmt.Errorf("failed to build COSI file:\n%w", err)
}
logger.Log.Infof("Successfully converted to COSI: %s", ic.outputImageFile)
err = imageLoopback.CleanClose()
if err != nil {
return err
}
return nil
}
func buildCosiFile(sourceDir string, outputFile string, partitions []outputPartitionMetadata,
verityMetadata []verityDeviceMetadata, partUuidToFstabEntry map[string]diskutils.FstabEntry,
imageUuidStr string, osRelease string, osPackages []OsPackage,
) error {
// Pre-compute a map for quick lookup of partition metadata by UUID
partUuidToMetadata := make(map[string]outputPartitionMetadata)
for _, partition := range partitions {
partUuidToMetadata[partition.PartUuid] = partition
}
// Pre-compute a set of verity hash UUIDs for quick lookup
verityHashUuids := make(map[string]struct{})
for _, verity := range verityMetadata {
verityHashUuids[verity.hashPartUuid] = struct{}{}
}
imageData := []ImageBuildData{}
for _, partition := range partitions {
// Skip verity hash partitions as their metadata will be assigned to the corresponding data partitions
if _, isVerityHash := verityHashUuids[partition.PartUuid]; isVerityHash {
continue
}
// Skip partitions that are unmounted or have no filesystem type
fstabEntry, hasMount := partUuidToFstabEntry[partition.PartUuid]
if !hasMount || fstabEntry.Target == "" || partition.FileSystemType == "" {
continue
}
metadataImage := FileSystem{
Image: ImageFile{
Path: path.Join("images", partition.PartitionFilename),
UncompressedSize: partition.UncompressedSize,
},
PartType: partition.PartitionTypeUuid,
MountPoint: partUuidToFstabEntry[partition.PartUuid].Target,
FsType: partition.FileSystemType,
FsUuid: partition.Uuid,
}
imageDataEntry := ImageBuildData{
Source: path.Join(sourceDir, partition.PartitionFilename),
Metadata: &metadataImage,
KnownInfo: partition,
}
// Add Verity metadata if the partition has a matching entry in verityMetadata
for _, verity := range verityMetadata {
if partition.PartUuid == verity.dataPartUuid {
hashPartition, exists := partUuidToMetadata[verity.hashPartUuid]
if !exists {
return fmt.Errorf("missing metadata for hash partition UUID:\n%s", verity.hashPartUuid)
}
metadataImage.Verity = &VerityConfig{
Roothash: verity.rootHash,
Image: ImageFile{
Path: path.Join("images", hashPartition.PartitionFilename),
UncompressedSize: hashPartition.UncompressedSize,
},
}
veritySourcePath := path.Join(sourceDir, hashPartition.PartitionFilename)
imageDataEntry.VeritySource = veritySourcePath
break
}
}
imageData = append(imageData, imageDataEntry)
}
// Populate metadata for each image
for i := range imageData {
err := populateMetadata(&imageData[i])
if err != nil {
return fmt.Errorf("failed to populate metadata for %s:\n%w", imageData[i].Source, err)
}
logger.Log.Infof("Populated metadata for image %s", imageData[i].Source)
}
metadata := MetadataJson{
Version: "1.0",
OsArch: getArchitectureForCosi(),
Id: imageUuidStr,
Images: make([]FileSystem, len(imageData)),
OsRelease: osRelease,
OsPackages: osPackages,
}
// Copy updated metadata
for i, data := range imageData {
metadata.Images[i] = *data.Metadata
}
// Marshal metadata.json
metadataJson, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal metadata:\n%w", err)
}
// Create COSI file
cosiFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create COSI file:\n%w", err)
}
defer cosiFile.Close()
tw := tar.NewWriter(cosiFile)
defer tw.Close()
tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: "metadata.json",
Size: int64(len(metadataJson)),
Mode: 0o400,
Format: tar.FormatPAX,
})
tw.Write(metadataJson)
for _, data := range imageData {
if err := addToCosi(data, tw); err != nil {
return fmt.Errorf("failed to add %s to COSI:\n%w", data.Source, err)
}
}
logger.Log.Infof("Finished building COSI: %s", outputFile)
return nil
}
func addToCosi(data ImageBuildData, tw *tar.Writer) error {
err := addFileToCosi(tw, data.Source, data.Metadata.Image)
if err != nil {
return fmt.Errorf("failed to add image file to COSI:\n%w", err)
}
if data.VeritySource != "" && data.Metadata.Verity != nil {
err := addFileToCosi(tw, data.VeritySource, data.Metadata.Verity.Image)
if err != nil {
return fmt.Errorf("failed to add verity file to COSI:\n%w", err)
}
}
return nil
}
func addFileToCosi(tw *tar.Writer, source string, image ImageFile) error {
file, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open file :\n%w", err)
}
defer file.Close()
err = tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: image.Path,
Size: int64(image.CompressedSize),
Mode: 0o400,
Format: tar.FormatPAX,
})
if err != nil {
return fmt.Errorf("failed to write tar header for file '%s':\n%w", image.Path, err)
}
_, err = io.Copy(tw, file)
if err != nil {
return fmt.Errorf("failed to write image '%s' to COSI:\n%w", image.Path, err)
}
return nil
}
func sha384sum(path string) (string, error) {
sha384 := sha512.New384()
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
if _, err := io.Copy(sha384, file); err != nil {
return "", err
}
return fmt.Sprintf("%x", sha384.Sum(nil)), nil
}
func populateImageFile(source string, imageFile *ImageFile) error {
stat, err := os.Stat(source)
if err != nil {
return fmt.Errorf("failed to stat %s:\n%w", source, err)
}
if stat.IsDir() {
return fmt.Errorf("%s is a directory", source)
}
imageFile.CompressedSize = uint64(stat.Size())
sha384, err := sha384sum(source)
if err != nil {
return fmt.Errorf("failed to calculate sha384 of %s:\n%w", source, err)
}
imageFile.Sha384 = sha384
return nil
}
// Enriches the image metadata with size and checksum
func populateMetadata(data *ImageBuildData) error {
if err := populateImageFile(data.Source, &data.Metadata.Image); err != nil {
return fmt.Errorf("failed to populate metadata:\n%w", err)
}
if err := populateVerityMetadata(data.VeritySource, data.Metadata.Verity); err != nil {
return fmt.Errorf("failed to populate verity metadata:\n%w", err)
}
return nil
}
func populateVerityMetadata(source string, verity *VerityConfig) error {
if source == "" && verity == nil {
return nil
}
if source == "" || verity == nil {
return fmt.Errorf("verity source and verity metadata must be both defined or both undefined")
}
if err := populateImageFile(source, &verity.Image); err != nil {
return fmt.Errorf("failed to populate verity image metadata:\n%w", err)
}
return nil
}
func getArchitectureForCosi() string {
if runtime.GOARCH == "amd64" {
return "x86_64"
}
return runtime.GOARCH
}
func getAllPackagesFromChroot(imageConnection *ImageConnection) ([]OsPackage, error) {
var out string
err := imageConnection.Chroot().UnsafeRun(func() error {
var err error
out, _, err = shell.Execute(
"rpm", "-qa", "--queryformat", "%{NAME} %{VERSION} %{RELEASE} %{ARCH}\n",
)
if err != nil {
return fmt.Errorf("failed to list installed RPMs:\n%w", err)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to get RPM output from chroot:\n%w", err)
}
lines := strings.Split(strings.TrimSpace(out), "\n")
var packages []OsPackage
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) != 4 {
return nil, fmt.Errorf("malformed RPM line encountered while parsing installed RPMs for COSI: %q", line)
}
packages = append(packages, OsPackage{
Name: parts[0],
Version: parts[1],
Release: parts[2],
Arch: parts[3],
})
}
return packages, nil
}