File: source.go

package info (click to toggle)
podman 5.4.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,124 kB
  • sloc: sh: 6,119; perl: 2,710; python: 2,258; ansic: 1,556; makefile: 1,022; xml: 121; ruby: 42; awk: 12; csh: 8
file content (116 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
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
package ocipull

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"

	"github.com/containers/image/v5/manifest"
	"github.com/containers/image/v5/oci/layout"
	"github.com/containers/image/v5/types"
	"github.com/opencontainers/go-digest"
	specV1 "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/sirupsen/logrus"
)

func GetLocalBlob(ctx context.Context, path string) (*types.BlobInfo, error) {
	ociRef, err := layout.ParseReference(path)
	if err != nil {
		return nil, err
	}
	img, err := ociRef.NewImage(ctx, &types.SystemContext{})
	if err != nil {
		return nil, err
	}

	b, _, err := img.Manifest(ctx)
	if err != nil {
		return nil, err
	}

	localManifest := specV1.Manifest{}
	if err := json.Unmarshal(b, &localManifest); err != nil {
		return nil, err
	}
	blobs := img.LayerInfos()
	if err != nil {
		return nil, err
	}
	if len(blobs) != 1 {
		return nil, errors.New("invalid disk image")
	}
	fmt.Println(blobs[0].Digest.Hex())
	return &blobs[0], nil
}

func GetDiskArtifactReference(ctx context.Context, imgSrc types.ImageSource, opts *DiskArtifactOpts) (digest.Digest, error) {
	rawMannyFest, mannyType, err := imgSrc.GetManifest(ctx, nil)
	if err != nil {
		return "", err
	}

	if !manifest.MIMETypeIsMultiImage(mannyType) { // if not true
		return "", fmt.Errorf("wrong manifest type for disk artifact: %s", mannyType)
	}

	mannyFestList, err := manifest.ListFromBlob(rawMannyFest, mannyType)
	if err != nil {
		return "", fmt.Errorf("failed to parse manifest list from blob: %q", err)
	}

	var (
		artifactDigest digest.Digest
	)
	for _, d := range mannyFestList.Instances() {
		bar, err := mannyFestList.Instance(d)
		if err != nil {
			return "", err
		}
		val, ok := bar.ReadOnly.Annotations["disktype"]
		if !ok { // quick exit, no type match
			continue
		}
		// wrong arch
		if bar.ReadOnly.Platform.Architecture != opts.arch {
			continue
		}
		// wrong os
		if bar.ReadOnly.Platform.OS != opts.os {
			continue
		}
		// wrong disktype
		if val != opts.diskType {
			continue
		}

		// ok, we have a match
		artifactDigest = d
		logrus.Debugf("found image in digest: %q", artifactDigest.String())
		break
	}
	if artifactDigest == "" {
		return "", fmt.Errorf("no valid disk artifact found")
	}
	v1RawMannyfest, _, err := imgSrc.GetManifest(ctx, &artifactDigest)
	if err != nil {
		return "", err
	}
	v1MannyFest := specV1.Manifest{}
	if err := json.Unmarshal(v1RawMannyfest, &v1MannyFest); err != nil {
		return "", err
	}
	if layerLen := len(v1MannyFest.Layers); layerLen > 1 {
		return "", fmt.Errorf("podman-machine images should only have 1 layer: %d found", layerLen)
	}

	// podman-machine-images should have a original file name
	// stored in the annotations under org.opencontainers.image.title
	// i.e. fedora-coreos-39.20240128.2.2-qemu.x86_64.qcow2.xz
	originalFileName, ok := v1MannyFest.Layers[0].Annotations[specV1.AnnotationTitle]
	if !ok {
		return "", fmt.Errorf("unable to determine original artifact name: missing required annotation '%s'", specV1.AnnotationTitle)
	}
	logrus.Debugf("original artifact file name: %s", originalFileName)
	return artifactDigest, err
}