@@ -6,6 +6,7 @@ package imagecustomizerlib
6
6
import (
7
7
"fmt"
8
8
"os"
9
+ "path"
9
10
"path/filepath"
10
11
"regexp"
11
12
"slices"
@@ -102,12 +103,14 @@ func outputArtifacts(items []imagecustomizerapi.OutputArtifactsItemType,
102
103
signedName := replaceSuffix (entry .Name (), ".unsigned.efi" , ".signed.efi" )
103
104
source := "./" + signedName
104
105
unsignedSource := "./" + entry .Name ()
105
- destinationName := replaceSuffix (entry .Name (), ".unsigned.efi" , ".efi" )
106
+ // ToDo: use default unsigned name for UKI.
107
+ //
108
+ // destinationName := replaceSuffix(entry.Name(), ".unsigned.efi", ".efi")
106
109
107
110
outputArtifactsMetadata = append (outputArtifactsMetadata , imagecustomizerapi.InjectArtifactMetadata {
108
111
Partition : partition ,
109
112
Source : source ,
110
- Destination : filepath .Join ("/" , UkiOutputDir , destinationName ),
113
+ Destination : filepath .Join ("/" , UkiOutputDir , entry . Name () ),
111
114
UnsignedSource : unsignedSource ,
112
115
})
113
116
}
@@ -282,6 +285,48 @@ func InjectFiles(buildDir string, baseConfigPath string, inputImageFile string,
282
285
return err
283
286
}
284
287
288
+ // ToDo: prepare COSI image converting.
289
+ //
290
+ rootfsPartition , err := findRootfsPartition (diskPartitions , buildDir )
291
+ if err != nil {
292
+ return fmt .Errorf ("failed to find rootfs partition:\n %w" , err )
293
+ }
294
+
295
+ fstabEntries , osRelease , err := readFstabEntriesFromRootfs (rootfsPartition , diskPartitions , buildDir )
296
+ if err != nil {
297
+ return fmt .Errorf ("failed to read fstab entries from rootfs partition:\n %w" , err )
298
+ }
299
+
300
+ _ , partUuidToFstabEntry , baseImageVerityMetadata , err := fstabEntriesToMountPoints (fstabEntries , diskPartitions , buildDir )
301
+ if err != nil {
302
+ return fmt .Errorf ("failed to find mount info for fstab file entries:\n %w" , err )
303
+ }
304
+
305
+ logger .Log .Debugf ("Resolved partUuidToFstabEntry map:" )
306
+ for partuuid , entry := range partUuidToFstabEntry {
307
+ logger .Log .Debugf (" %s → target=%s, source=%s, fsType=%s, options=%s, fsOptions=%s, vfsOptions=%d, freq=%d, passno=%d" ,
308
+ partuuid , entry .Target , entry .Source , entry .FsType , entry .Options , entry .FsOptions , entry .VfsOptions , entry .Freq , entry .PassNo )
309
+ }
310
+
311
+ logger .Log .Debugf ("Resolved base image verity metadata:" )
312
+ for _ , v := range baseImageVerityMetadata {
313
+ logger .Log .Debugf (" name=%s, rootHash=%s" , v .name , v .rootHash )
314
+ logger .Log .Debugf (" dataPartition: idType=%s, id=%s" , v .dataDeviceMountIdType , v .dataPartUuid )
315
+ logger .Log .Debugf (" hashPartition: idType=%s, id=%s" , v .hashDeviceMountIdType , v .hashPartUuid )
316
+ logger .Log .Debugf (" corruptionOption=%s, hashSignaturePath=%s" , v .corruptionOption , v .hashSignaturePath )
317
+ }
318
+
319
+ logger .Log .Debugf ("OSRelease=%s" , osRelease )
320
+
321
+ outputImageBase := strings .TrimSuffix (filepath .Base (outputImageFile ), filepath .Ext (outputImageFile ))
322
+
323
+ imageUuid , imageUuidStr , err := createUuid ()
324
+ if err != nil {
325
+ return err
326
+ }
327
+
328
+ // ToDo: ABOVE are preparing COSI image converting.
329
+ //
285
330
partitionsToMountpoints := make (map [imagecustomizerapi.InjectFilePartition ]string )
286
331
var mountedPartitions []* safemount.Mount
287
332
@@ -323,12 +368,63 @@ func InjectFiles(buildDir string, baseConfigPath string, inputImageFile string,
323
368
return err
324
369
}
325
370
326
- err = convertImageFile (rawImageFile , outputImageFile , detectedImageFormat )
327
- if err != nil {
328
- return fmt .Errorf ("failed to convert customized raw image to output format:\n %w" , err )
371
+ if detectedImageFormat == imagecustomizerapi .ImageFormatTypeCosi {
372
+ err := convertToCosiWhenInject (buildDirAbs , rawImageFile , outputImageBase , imageUuid , imageUuidStr , outputImageFile ,
373
+ baseImageVerityMetadata , partUuidToFstabEntry , osRelease )
374
+ if err != nil {
375
+ return err
376
+ }
377
+ } else {
378
+ err = convertImageFile (rawImageFile , outputImageFile , detectedImageFormat )
379
+ if err != nil {
380
+ return fmt .Errorf ("failed to convert customized raw image to output format:\n %w" , err )
381
+ }
329
382
}
330
383
331
384
logger .Log .Infof ("Success!" )
332
385
333
386
return nil
334
387
}
388
+
389
+ func convertToCosiWhenInject (buildDirAbs string , rawImageFile string , outputImageBase string ,
390
+ imageUuid [UuidSize ]byte , imageUuidStr string , outputImageFile string , baseImageVerityMetadata []verityDeviceMetadata ,
391
+ partUuidToFstabEntry map [string ]diskutils.FstabEntry , osRelease string ,
392
+ ) error {
393
+ logger .Log .Infof ("Extracting partition files" )
394
+ outputDir := filepath .Join (buildDirAbs , "cosiimages" )
395
+ err := os .MkdirAll (outputDir , os .ModePerm )
396
+ if err != nil {
397
+ return fmt .Errorf ("failed to create folder %s:\n %w" , outputDir , err )
398
+ }
399
+ defer os .Remove (outputDir )
400
+
401
+ imageLoopback , err := safeloopback .NewLoopback (rawImageFile )
402
+ if err != nil {
403
+ return err
404
+ }
405
+ defer imageLoopback .Close ()
406
+
407
+ partitionMetadataOutput , err := extractPartitions (imageLoopback .DevicePath (), outputDir , outputImageBase ,
408
+ "raw-zst" , imageUuid )
409
+ if err != nil {
410
+ return err
411
+ }
412
+ for _ , partition := range partitionMetadataOutput {
413
+ defer os .Remove (path .Join (outputDir , partition .PartitionFilename ))
414
+ }
415
+
416
+ err = buildCosiFile (outputDir , outputImageFile , partitionMetadataOutput , baseImageVerityMetadata ,
417
+ partUuidToFstabEntry , imageUuidStr , osRelease )
418
+ if err != nil {
419
+ return fmt .Errorf ("failed to build COSI file:\n %w" , err )
420
+ }
421
+
422
+ logger .Log .Infof ("Successfully converted to COSI: %s" , outputImageFile )
423
+
424
+ err = imageLoopback .CleanClose ()
425
+ if err != nil {
426
+ return err
427
+ }
428
+
429
+ return nil
430
+ }
0 commit comments