spiegel_podman/pkg/machine/oci.go
Brent Baude ea4775ec9e Consume OCI images for machine image
allow podman machine to extract its disk image from an oci registry or
oci-dir locally.  for now, the image must be relatively inflexible. it
must have 1 layer.  the layer must possess one image. so a dockerfile
like:

FROM scratch
COPY ./myimage.xz /myimage.xz

when using an oci dir, the directory structure must adhere to the
typical directory structure of a an oci image (with one layer).

── blobs
│   └── sha256
│       ├── 53735773573b3853bb1cae16dd21061beb416239ceb78d4ef1f2a0609f7e843b
│       ├── 80577866ec13c041693e17de61444b4696137623803c3d87f92e4f28a1f4e87b
│       └── af57637ac1ab12f833e3cfa886027cc9834a755a437d0e1cf48b5d4778af7a4e
├── index.json
└── oci-layout

in order to identify this new input, you must use a transport/schema to
differentiate from current podman machine init --image-path behavior. we
will support `oci-dir://` and `docker://` as transports.

when using the docker transport, you can only use an empty transport for
input.  for example, `podman machine init --image-path docker://`.  A
fully quailified image name will be supported in the next iteration.

the transport absent anything means, i want to pull the default fcos
image stored in a registry.  podman will determine its current version
and then look for its correlating manifest.  in this default use case,
it would look for:

quay.io/libpod/podman-machine-images:<version>

that manifest would then point to specific images that contain the
correct arch and provider disk image. i.e.

quay.io/libpod/podman-machine-images:4.6-qcow2

this PR does not enable something like
docker://quay.io/mycorp/myimage:latest yet.

names, addresses, andf schema/transports are all subject to change. the
plan is to keep this all undocumented until things firm up.

[NO NEW TESTS NEEDED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
2023-11-02 10:23:14 -05:00

136 lines
2.8 KiB
Go

package machine
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/containers/image/v5/types"
"github.com/containers/image/v5/pkg/compression"
"github.com/containers/storage/pkg/archive"
"github.com/sirupsen/logrus"
"github.com/blang/semver/v4"
"github.com/containers/podman/v4/version"
)
// quay.io/libpod/podman-machine-images:4.6
const (
diskImages = "podman-machine-images"
registry = "quay.io"
repo = "libpod"
)
type OSVersion struct {
*semver.Version
}
type Disker interface {
Pull() error
Decompress(compressedFile *VMFile) (*VMFile, error)
DiskEndpoint() string
Unpack() (*VMFile, error)
}
type OCIOpts struct {
Scheme *OCIKind
Dir *string
}
type OCIKind string
var (
OCIDir OCIKind = "oci-dir"
OCIRegistry OCIKind = "docker"
OCIUnknown OCIKind = "unknown"
)
func (o OCIKind) String() string {
switch o {
case OCIDir:
return string(OCIDir)
case OCIRegistry:
return string(OCIRegistry)
}
return string(OCIUnknown)
}
func (o OCIKind) IsOCIDir() bool {
return o == OCIDir
}
func StripOCIReference(input string) string {
return strings.TrimPrefix(input, "docker://")
}
func getVersion() *OSVersion {
v := version.Version
// OVERRIDES FOR DEV ONLY
v.Minor = 6
v.Pre = nil
// OVERRIDES FOR DEV ONLY
return &OSVersion{&v}
}
func (o *OSVersion) majorMinor() string {
return fmt.Sprintf("%d.%d", o.Major, o.Minor)
}
func (o *OSVersion) diskImage(diskFlavor ImageFormat) string {
return fmt.Sprintf("%s/%s/%s:%s-%s", registry, repo, diskImages, o.majorMinor(), diskFlavor.string())
}
func unpackOCIDir(ociTb, machineImageDir string) (*VMFile, error) {
imageFileName, err := findTarComponent(ociTb)
if err != nil {
return nil, err
}
unpackedFileName := filepath.Join(machineImageDir, imageFileName)
f, err := os.Open(ociTb)
if err != nil {
return nil, err
}
defer func() {
if err := f.Close(); err != nil {
logrus.Error(err)
}
}()
uncompressedReader, _, err := compression.AutoDecompress(f)
if err != nil {
return nil, err
}
defer func() {
if err := uncompressedReader.Close(); err != nil {
logrus.Error(err)
}
}()
logrus.Debugf("untarring %q to %q", ociTb, machineImageDir)
if err := archive.Untar(uncompressedReader, machineImageDir, &archive.TarOptions{
NoLchown: true,
}); err != nil {
return nil, err
}
return NewMachineFile(unpackedFileName, nil)
}
func localOCIDiskImageDir(blobDirPath string, localBlob *types.BlobInfo) string {
return filepath.Join(blobDirPath, "blobs", "sha256", localBlob.Digest.Hex())
}
func finalFQImagePathName(vmName, imageName string) string {
// imageName here is fully qualified. we need to break
// it apart and add the vmname
baseDir, filename := filepath.Split(imageName)
return filepath.Join(baseDir, fmt.Sprintf("%s-%s", vmName, filename))
}