/
packer_sif.go
84 lines (72 loc) · 2.47 KB
/
packer_sif.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
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package sources
import (
"context"
"fmt"
"io"
"github.com/sylabs/singularity/v4/internal/pkg/image/unpacker"
"github.com/sylabs/singularity/v4/pkg/build/types"
"github.com/sylabs/singularity/v4/pkg/image"
"github.com/sylabs/singularity/v4/pkg/sylog"
)
// SIFPacker holds the locations of where to pack from and to.
type SIFPacker struct {
srcFile string
b *types.Bundle
img *image.Image
}
// Pack puts relevant objects in a Bundle.
func (p *SIFPacker) Pack(context.Context) (*types.Bundle, error) {
err := unpackSIF(p.b, p.img)
if err != nil {
sylog.Errorf("unpackSIF failed: %s", err)
return nil, err
}
return p.b, nil
}
// unpackSIF parses through the sif file and places each component
// in the sandbox. First pass just assumes a single system partition,
// later passes will handle more complex sif files.
func unpackSIF(b *types.Bundle, img *image.Image) (err error) {
part, err := img.GetRootFsPartition()
if err != nil {
return fmt.Errorf("while getting root filesystem in %s: %s", img.Name, err)
}
switch part.Type {
case image.SQUASHFS:
// create a reader for rootfs partition
reader, err := image.NewPartitionReader(img, "", 0)
if err != nil {
return fmt.Errorf("could not extract root filesystem: %s", err)
}
s := unpacker.NewSquashfs(false)
// extract root filesystem
if err := s.ExtractAll(reader, b.RootfsPath); err != nil {
return fmt.Errorf("root filesystem extraction failed: %s", err)
}
case image.EXT3:
// extract ext3 partition by mounting
sylog.Debugf("Ext3 partition detected, mounting to extract.")
if err := unpackExt3(b, img); err != nil {
return fmt.Errorf("while copying partition data to bundle: %v", err)
}
default:
return fmt.Errorf("unrecognized partition format")
}
ociReader, err := image.NewSectionReader(img, image.SIFDescOCIConfigJSON, -1)
if err == image.ErrNoSection {
sylog.Debugf("No %s section found", image.SIFDescOCIConfigJSON)
} else if err != nil {
return fmt.Errorf("could not get OCI config section reader: %v", err)
} else {
ociConfig, err := io.ReadAll(ociReader)
if err != nil {
return fmt.Errorf("could not read OCI config: %v", err)
}
b.JSONObjects[image.SIFDescOCIConfigJSON] = ociConfig
}
return nil
}