-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathartifactsinputoutput.go
More file actions
537 lines (444 loc) · 20.5 KB
/
Copy pathartifactsinputoutput.go
File metadata and controls
537 lines (444 loc) · 20.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package imagecustomizerlib
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"go.opentelemetry.io/otel"
"gopkg.in/yaml.v3"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagegen/diskutils"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/file"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/imageconnection"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/logger"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/randomization"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safeloopback"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safemount"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/sliceutils"
"golang.org/x/sys/unix"
)
var (
// Artifact handling errors
ErrArtifactImageConnection = NewImageCustomizerError("Artifacts:ImageConnection", "failed to connect to image file to output artifacts")
ErrArtifactESPPartitionMount = NewImageCustomizerError("Artifacts:ESPPartitionMount", "failed to mount ESP partition")
ErrArtifactUKIDirectoryRead = NewImageCustomizerError("Artifacts:UKIDirectoryRead", "failed to read UKI directory")
ErrArtifactBinaryCopy = NewImageCustomizerError("Artifacts:BinaryCopy", "failed to copy binary")
ErrArtifactRootHashDump = NewImageCustomizerError("Artifacts:RootHashDump", "failed to dump root hash")
ErrArtifactInjectFilesYamlWrite = NewImageCustomizerError("Artifacts:InjectFilesYamlWrite", "failed to write inject-files.yaml")
ErrArtifactInjectFilesYamlMarshal = NewImageCustomizerError("Artifacts:InjectFilesYamlMarshal", "failed to marshal inject files metadata")
ErrArtifactInvalidInjectFilesConfig = NewImageCustomizerError("Artifacts:InvalidInjectFilesConfig", "invalid inject files config")
ErrArtifactInjectFilesPathResolution = NewImageCustomizerError("Artifacts:InjectFilesPathResolution", "failed to get absolute path of inject-files.yaml")
ErrArtifactInjectFilesImageConnection = NewImageCustomizerError("Artifacts:InjectFilesImageConnection", "failed to connect to image file to inject files")
ErrArtifactInjectFilesPartitionMount = NewImageCustomizerError("Artifacts:InjectFilesPartitionMount", "failed to mount partition for file injection")
ErrArtifactPartitionUnmount = NewImageCustomizerError("Artifacts:PartitionUnmount", "failed to cleanly unmount partition")
ErrArtifactCosiImageInfoCollect = NewImageCustomizerError("Artifacts:CosiImageInfoCollect", "failed to collect OS information for cosi output format")
ErrArtifactCosiImageConversion = NewImageCustomizerError("Artifacts:CosiImageConversion", "failed to convert customized raw image to cosi output format")
ErrArtifactOutputImageConversion = NewImageCustomizerError("Artifacts:OutputImageConversion", "failed to convert customized raw image to output format")
ErrArtifactImageConnectionForExtraction = NewImageCustomizerError("Artifacts:ImageConnectionForExtraction", "failed to connect to image")
ErrArtifactImageConnectionClose = NewImageCustomizerError("Artifacts:ImageConnectionClose", "failed to cleanly close image connection")
ErrArtifactReleaseFileRead = NewImageCustomizerError("Artifacts:ReleaseFileRead", "failed to read release file")
ErrArtifactImageUuidNotFound = NewImageCustomizerError("Artifacts:ImageUuidNotFound", "IMAGE_UUID not found in release file")
ErrArtifactImageUuidParse = NewImageCustomizerError("Artifacts:ImageUuidParse", "failed to parse IMAGE_UUID")
)
var ukiRegex = regexp.MustCompile(`^vmlinuz-.*\.efi$`)
func outputArtifacts(ctx context.Context, items []imagecustomizerapi.OutputArtifactsItemType,
outputDir string, buildDir string, buildImage string, verityMetadata []verityDeviceMetadata,
previewFeatures []imagecustomizerapi.PreviewFeature, distroHandler DistroHandler,
) error {
logger.Log.Infof("Outputting artifacts")
_, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "output_artifacts")
defer span.End()
var outputArtifactsMetadata []imagecustomizerapi.InjectArtifactMetadata
loopback, err := safeloopback.NewLoopback(buildImage)
if err != nil {
return fmt.Errorf("%w:\n%w", ErrArtifactImageConnection, err)
}
defer loopback.Close()
diskPartitions, err := diskutils.GetDiskPartitions(loopback.DevicePath())
if err != nil {
return err
}
systemBootPartition, err := findSystemBootPartition(diskPartitions)
if err != nil {
return err
}
bootPartition, err := findBootPartitionFromEsp(systemBootPartition, diskPartitions, buildDir, distroHandler)
if err != nil {
return err
}
systemBootPartitionTmpDir := filepath.Join(buildDir, tmpEspPartitionDirName)
systemBootPartitionMount, err := safemount.NewMount(systemBootPartition.Path,
systemBootPartitionTmpDir, systemBootPartition.FileSystemType, unix.MS_RDONLY, "", true)
if err != nil {
return fmt.Errorf("%w (partition='%s'):\n%w", ErrArtifactESPPartitionMount, systemBootPartition.Path, err)
}
defer systemBootPartitionMount.Close()
// Detect system architecture
bootConfig, err := distroHandler.GetBootArchConfig()
if err != nil {
return err
}
espInjectFilePartition := imagecustomizerapi.InjectFilePartition{
MountIdType: imagecustomizerapi.MountIdentifierTypePartUuid,
Id: systemBootPartition.PartUuid,
}
bootInjectFilePartition := imagecustomizerapi.InjectFilePartition{
MountIdType: imagecustomizerapi.MountIdentifierTypePartUuid,
Id: bootPartition.PartUuid,
}
// Output UKIs
if slices.Contains(items, imagecustomizerapi.OutputArtifactsItemUkis) {
ukiDir := filepath.Join(systemBootPartitionTmpDir, UkiOutputDir)
dirEntries, err := os.ReadDir(ukiDir)
if err != nil {
return fmt.Errorf("%w (directory='%s'):\n%w", ErrArtifactUKIDirectoryRead, ukiDir, err)
}
// Create subdirectory for UKIs
ukiOutputSubdir := filepath.Join(outputDir, string(imagecustomizerapi.OutputArtifactsItemUkis))
err = os.MkdirAll(ukiOutputSubdir, 0o755)
if err != nil {
return fmt.Errorf("failed to create ukis subdirectory:\n%w", err)
}
for _, entry := range dirEntries {
if !entry.IsDir() && ukiRegex.MatchString(entry.Name()) {
// Copy main UKI file
srcPath := filepath.Join(ukiDir, entry.Name())
destPath := filepath.Join(ukiOutputSubdir, entry.Name())
err := file.Copy(srcPath, destPath)
if err != nil {
return fmt.Errorf("%w (source='%s', destination='%s'):\n%w", ErrArtifactBinaryCopy, srcPath, destPath, err)
}
source := "./" + string(imagecustomizerapi.OutputArtifactsItemUkis) + "/" + entry.Name()
outputArtifactsMetadata = append(outputArtifactsMetadata, imagecustomizerapi.InjectArtifactMetadata{
Partition: espInjectFilePartition,
Source: source,
Destination: filepath.Join("/", UkiOutputDir, entry.Name()),
Type: imagecustomizerapi.OutputArtifactsItemUkis,
})
logger.Log.Debugf("Added UKI file to metadata: %s", entry.Name())
// Check for and copy UKI addon directory (e.g., vmlinuz-6.6.104.2-4.azl3.efi.extra.d/)
addonDirName := entry.Name() + ".extra.d"
addonSrcDir := filepath.Join(ukiDir, addonDirName)
addonDirExists, err := file.DirExists(addonSrcDir)
if err != nil {
return fmt.Errorf("failed to check for UKI addon directory (%s):\n%w", addonSrcDir, err)
}
if addonDirExists {
logger.Log.Debugf("Found UKI addon directory: %s", addonDirName)
addonDestDir := filepath.Join(ukiOutputSubdir, addonDirName)
// Copy the entire addon directory with all its contents
err = file.CopyDir(addonSrcDir, addonDestDir, 0o755, 0o644, nil)
if err != nil {
return fmt.Errorf("failed to copy UKI addon directory (%s):\n%w", addonSrcDir, err)
}
addonEntries, err := os.ReadDir(addonSrcDir)
if err != nil {
return fmt.Errorf("failed to read UKI addon directory (%s):\n%w", addonSrcDir, err)
}
for _, addonEntry := range addonEntries {
if !addonEntry.IsDir() {
addonFileName := addonEntry.Name()
addonSource := "./" + string(imagecustomizerapi.OutputArtifactsItemUkis) + "/" + addonDirName + "/" + addonFileName
addonDestination := filepath.Join("/", UkiOutputDir, addonDirName, addonFileName)
outputArtifactsMetadata = append(outputArtifactsMetadata, imagecustomizerapi.InjectArtifactMetadata{
Partition: espInjectFilePartition,
Source: addonSource,
Destination: addonDestination,
Type: imagecustomizerapi.OutputArtifactsItemUkiAddons,
})
logger.Log.Debugf("Added UKI addon file to metadata: %s", addonFileName)
}
}
} else {
// No addon directory found - UKI with cmdline embedded in the main UKI file
logger.Log.Debugf("No addon directory found for UKI: %s (single UKI file or no cmdline customization)", entry.Name())
}
}
}
}
// Output shim
if slices.Contains(items, imagecustomizerapi.OutputArtifactsItemShim) {
// Create subdirectory for shim
shimOutputSubdir := filepath.Join(outputDir, string(imagecustomizerapi.OutputArtifactsItemShim))
err = os.MkdirAll(shimOutputSubdir, 0o755)
if err != nil {
return fmt.Errorf("failed to create shim subdirectory:\n%w", err)
}
srcPath := filepath.Join(systemBootPartitionTmpDir, bootConfig.espBootBinaryPath)
destPath := filepath.Join(shimOutputSubdir, bootConfig.bootBinary)
err := file.Copy(srcPath, destPath)
if err != nil {
return fmt.Errorf("%w (source='%s', destination='%s'):\n%w", ErrArtifactBinaryCopy, srcPath, destPath, err)
}
source := "./" + string(imagecustomizerapi.OutputArtifactsItemShim) + "/" + bootConfig.bootBinary
outputArtifactsMetadata = append(outputArtifactsMetadata, imagecustomizerapi.InjectArtifactMetadata{
Partition: espInjectFilePartition,
Source: source,
Destination: filepath.Join("/", bootConfig.espBootBinaryPath),
Type: imagecustomizerapi.OutputArtifactsItemShim,
})
logger.Log.Debugf("Added shim file to metadata: %s", bootConfig.bootBinary)
}
// Output bootloader (e.g. GRUBX64.efi)
if slices.Contains(items, imagecustomizerapi.OutputArtifactsItemBootloader) {
bootloaderOutputSubdir := filepath.Join(outputDir, string(imagecustomizerapi.OutputArtifactsItemBootloader))
err = os.MkdirAll(bootloaderOutputSubdir, 0o755)
if err != nil {
return fmt.Errorf("failed to create bootloader subdirectory:\n%w", err)
}
srcPath := filepath.Join(systemBootPartitionTmpDir, bootConfig.espGrubBinaryPath)
destPath := filepath.Join(bootloaderOutputSubdir, bootConfig.grubBinary)
err := file.Copy(srcPath, destPath)
if err != nil {
return fmt.Errorf("%w (source='%s', destination='%s'):\n%w", ErrArtifactBinaryCopy, srcPath, destPath, err)
}
source := "./" + string(imagecustomizerapi.OutputArtifactsItemBootloader) + "/" + bootConfig.grubBinary
outputArtifactsMetadata = append(outputArtifactsMetadata, imagecustomizerapi.InjectArtifactMetadata{
Partition: espInjectFilePartition,
Source: source,
Destination: filepath.Join("/", bootConfig.espGrubBinaryPath),
Type: imagecustomizerapi.OutputArtifactsItemBootloader,
})
logger.Log.Debugf("Added bootloader file to metadata: %s", bootConfig.grubBinary)
}
// Output verity hash
if slices.Contains(items, imagecustomizerapi.OutputArtifactsItemVerityHash) {
// Create subdirectory for verity hashes
verityHashOutputSubdir := filepath.Join(outputDir, string(imagecustomizerapi.OutputArtifactsItemVerityHash))
err = os.MkdirAll(verityHashOutputSubdir, 0o755)
if err != nil {
return fmt.Errorf("failed to create verity-hash subdirectory:\n%w", err)
}
for _, verity := range verityMetadata {
if verity.hashSignaturePath == "" {
continue
}
// Use the exact destination filename (e.g., "usr.hash" if hashSignaturePath is "/boot/usr.hash").
destination := strings.TrimPrefix(verity.hashSignaturePath, "/boot")
hashFile := filepath.Base(destination)
destPath := filepath.Join(verityHashOutputSubdir, hashFile)
err = file.Write(verity.rootHash, destPath)
if err != nil {
return fmt.Errorf("%w (name='%s', path='%s'):\n%w", ErrArtifactRootHashDump, verity.name, destPath, err)
}
source := "./" + string(imagecustomizerapi.OutputArtifactsItemVerityHash) + "/" + hashFile
outputArtifactsMetadata = append(outputArtifactsMetadata, imagecustomizerapi.InjectArtifactMetadata{
Partition: bootInjectFilePartition,
Source: source,
Destination: filepath.Join("/", destination),
Type: imagecustomizerapi.OutputArtifactsItemVerityHash,
})
logger.Log.Debugf("Added verity hash file to metadata: %s", hashFile)
}
}
err = writeInjectFilesYaml(outputArtifactsMetadata, outputDir, previewFeatures)
if err != nil {
return fmt.Errorf("%w (outputDir='%s'):\n%w", ErrArtifactInjectFilesYamlWrite, outputDir, err)
}
err = systemBootPartitionMount.CleanClose()
if err != nil {
return err
}
err = loopback.CleanClose()
if err != nil {
return err
}
return nil
}
func writeInjectFilesYaml(metadata []imagecustomizerapi.InjectArtifactMetadata, outputDir string,
previewFeatures []imagecustomizerapi.PreviewFeature,
) error {
injectPreviewFeatures := []imagecustomizerapi.PreviewFeature{imagecustomizerapi.PreviewFeatureInjectFiles}
yamlStruct := imagecustomizerapi.InjectFilesConfig{
InjectFiles: metadata,
PreviewFeatures: injectPreviewFeatures,
}
yamlBytes, err := yaml.Marshal(&yamlStruct)
if err != nil {
return fmt.Errorf("%w:\n%w", ErrArtifactInjectFilesYamlMarshal, err)
}
outputFilePath := filepath.Join(outputDir, "inject-files.yaml")
if err := os.WriteFile(outputFilePath, yamlBytes, 0o644); err != nil {
return fmt.Errorf("%w (file='%s'):\n%w", ErrArtifactInjectFilesYamlWrite, outputFilePath, err)
}
return nil
}
func InjectFilesWithConfigFile(ctx context.Context, configFile string, options InjectFilesOptions) error {
var config imagecustomizerapi.InjectFilesConfig
err := imagecustomizerapi.UnmarshalYamlFile(configFile, &config)
if err != nil {
return err
}
if err := config.IsValid(); err != nil {
return fmt.Errorf("%w:\n%w", ErrArtifactInvalidInjectFilesConfig, err)
}
if err := options.IsValid(); err != nil {
return err
}
baseConfigPath, _ := filepath.Split(configFile)
absBaseConfigPath, err := filepath.Abs(baseConfigPath)
if err != nil {
return fmt.Errorf("%w (path='%s'):\n%w", ErrArtifactInjectFilesPathResolution, baseConfigPath, err)
}
err = injectFilesWithOptions(ctx, absBaseConfigPath, config.InjectFiles, options)
if err != nil {
return err
}
return nil
}
func injectFilesWithOptions(ctx context.Context, baseConfigPath string,
metadata []imagecustomizerapi.InjectArtifactMetadata, options InjectFilesOptions,
) error {
logger.Log.Debugf("Injecting Files")
ctx, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "inject_files")
defer span.End()
buildDirAbs, err := filepath.Abs(options.BuildDir)
if err != nil {
return err
}
// Ensure build directory exists.
err = os.MkdirAll(buildDirAbs, os.ModePerm)
if err != nil {
return err
}
rawImageFile := filepath.Join(buildDirAbs, BaseImageName)
detectedImageFormat, err := convertImageToRaw(options.InputImageFile, rawImageFile)
if err != nil {
return err
}
if options.OutputImageFormat != "" {
detectedImageFormat = imagecustomizerapi.ImageFormatType(options.OutputImageFormat)
}
outputImageFile := options.OutputImageFile
if outputImageFile == "" {
outputImageFile = options.InputImageFile
}
err = injectFilesIntoImage(options.BuildDir, baseConfigPath, rawImageFile, metadata)
if err != nil {
return err
}
err = convertRawImageToOutputFormat(ctx, buildDirAbs, rawImageFile, detectedImageFormat, outputImageFile,
options.CosiCompressionLevel)
if err != nil {
return err
}
logger.Log.Infof("Success!")
return nil
}
func injectFilesIntoImage(buildDir string, baseConfigPath string, rawImageFile string,
metadata []imagecustomizerapi.InjectArtifactMetadata,
) error {
loopback, err := safeloopback.NewLoopback(rawImageFile)
if err != nil {
return fmt.Errorf("%w:\n%w", ErrArtifactInjectFilesImageConnection, err)
}
defer loopback.Close()
diskPartitions, err := diskutils.GetDiskPartitions(loopback.DevicePath())
if err != nil {
return err
}
partitionsToMountpoints := make(map[imagecustomizerapi.InjectFilePartition]string)
var mountedPartitions []*safemount.Mount
for idx, item := range metadata {
partitionKey := item.Partition
if _, exists := partitionsToMountpoints[partitionKey]; !exists {
partitionsToMountpoints[partitionKey] = filepath.Join(buildDir, fmt.Sprintf("inject-partition-%d", idx))
partition, _, err := findPartition(item.Partition.MountIdType, item.Partition.Id, diskPartitions)
if err != nil {
return err
}
mount, err := safemount.NewMount(partition.Path, partitionsToMountpoints[partitionKey], partition.FileSystemType, 0, "", true)
if err != nil {
return fmt.Errorf("%w (partition='%s'):\n%w", ErrArtifactInjectFilesPartitionMount, partition.Path, err)
}
defer mount.Close()
mountedPartitions = append(mountedPartitions, mount)
}
srcPath := filepath.Join(baseConfigPath, item.Source)
destPath := filepath.Join(partitionsToMountpoints[partitionKey], item.Destination)
err := file.Copy(srcPath, destPath)
if err != nil {
return fmt.Errorf("%w (source='%s', destination='%s'):\n%w", ErrArtifactBinaryCopy, srcPath, destPath, err)
}
}
for _, m := range mountedPartitions {
if err := m.CleanClose(); err != nil {
return fmt.Errorf("%w (target='%s'):\n%w", ErrArtifactPartitionUnmount, m.Target(), err)
}
}
err = loopback.CleanClose()
if err != nil {
return err
}
return nil
}
func prepareImageConversionData(ctx context.Context, rawImageFile string, buildDir string,
chrootDir string,
) ([]fstabEntryPartNum, []verityDeviceMetadata, string,
[]OsPackage, [randomization.UuidSize]byte, string, *CosiBootloader, []string, error,
) {
imageConnection, partitionsLayout, baseImageVerityMetadata, readonlyPartUuids, distroHandler, err :=
connectToExistingImage(ctx, rawImageFile, buildDir, chrootDir, true, true, true, true, nil)
if err != nil {
err = fmt.Errorf("%w:\n%w", ErrArtifactImageConnectionForExtraction, err)
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
defer imageConnection.Close()
osRelease, err := extractOSRelease(imageConnection)
if err != nil {
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
osPackages, cosiBootMetadata, err := collectOSInfoHelper(ctx, buildDir, imageConnection, distroHandler)
if err != nil {
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
imageUuid, imageUuidStr, err := tryExtractImageUUID(imageConnection)
if err != nil {
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
if imageUuidStr == "" {
// File not generated by Image Customizer.
// So, generate a new UUID.
imageUuid, imageUuidStr, err = randomization.CreateUuid()
if err != nil {
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
}
if err := imageConnection.CleanClose(); err != nil {
err = fmt.Errorf("%w:\n%w", ErrArtifactImageConnectionClose, err)
return nil, nil, "", nil, [randomization.UuidSize]byte{}, "", nil, nil, err
}
return partitionsLayout, baseImageVerityMetadata, osRelease, osPackages, imageUuid, imageUuidStr,
cosiBootMetadata, readonlyPartUuids, nil
}
func tryExtractImageUUID(imageConnection *imageconnection.ImageConnection) ([randomization.UuidSize]byte, string, error) {
var emptyUuid [randomization.UuidSize]byte
releasePath := filepath.Join(imageConnection.Chroot().RootDir(), ImageCustomizerReleasePath)
data, err := file.Read(releasePath)
if errors.Is(err, fs.ErrNotExist) {
return emptyUuid, "", nil
}
if err != nil {
return emptyUuid, "", fmt.Errorf("%w (path='%s'):\n%w", ErrArtifactReleaseFileRead, releasePath, err)
}
lines := strings.Split(string(data), "\n")
line, found := sliceutils.FindValueFunc(lines, func(line string) bool {
return strings.HasPrefix(line, "IMAGE_UUID=")
})
if !found {
return emptyUuid, "", fmt.Errorf("%w (path='%s')", ErrArtifactImageUuidNotFound, releasePath)
}
uuidStr := strings.Trim(strings.TrimPrefix(line, "IMAGE_UUID="), `"`)
parsed, err := randomization.ParseUuidString(uuidStr)
if err != nil {
return emptyUuid, "", fmt.Errorf("%w (IMAGE_UUID='%s'):\n%w", ErrArtifactImageUuidParse, uuidStr, err)
}
return parsed, uuidStr, nil
}