Skip to content

Commit

Permalink
Implement CrcImagePuller
Browse files Browse the repository at this point in the history
This will replace the default podman-machine code to get the disk image
for the VM.
  • Loading branch information
cfergeau committed Oct 4, 2024
1 parent 22ebbe7 commit 6b87280
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 11 deletions.
137 changes: 137 additions & 0 deletions pkg/machinedriver/crcimageprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package macadam

import (
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
crcos "github.com/crc-org/crc/v2/pkg/os"
)

type CrcImagePuller struct {
localPath *define.VMFile

Check failure on line 15 in pkg/machinedriver/crcimageprovider.go

View workflow job for this annotation

GitHub Actions / lint

field `localPath` is unused (unused)
sourcePath string
vmType define.VMType
machineConfig *vmconfigs.MachineConfig

Check failure on line 18 in pkg/machinedriver/crcimageprovider.go

View workflow job for this annotation

GitHub Actions / lint

field `machineConfig` is unused (unused)
machineDirs *define.MachineDirs

Check failure on line 19 in pkg/machinedriver/crcimageprovider.go

View workflow job for this annotation

GitHub Actions / lint

field `machineDirs` is unused (unused)
}

func GetHomeDir() string {
homeDir, err := os.UserHomeDir()
if err != nil {
panic("Failed to get homeDir: " + err.Error())
}
return homeDir
}

var (
CrcBaseDir = filepath.Join(GetHomeDir(), ".crc")
MachineBaseDir = CrcBaseDir
MachineCacheDir = filepath.Join(MachineBaseDir, "cache")
MachineInstanceDir = filepath.Join(MachineBaseDir, "machines")
)

func NewCrcImagePuller(vmType define.VMType) (*CrcImagePuller, error) {
crcImage := CrcImagePuller{
vmType: vmType,
}

return &crcImage, nil
}

/* TODO: Might be better to pass an actual URI and to strip the "file://" part from it */
func (puller *CrcImagePuller) SetSourceURI(path string) {
puller.sourcePath = path
}

/*
type MachineDirs struct {
ConfigDir *VMFile
DataDir *VMFile
ImageCacheDir *VMFile
RuntimeDir *VMFile
}
var crcMachineDirs = MachineDirs {
ConfigDir: CrcBaseDir,
DataDir: filepath.Join(MachineInstanceDir, , "crc"),
ImageCacheDir: MachineCacheDir,
RuntimeDir: filepath.Join(MachineInstanceDir, , "crc"),
}
*/

func imageExtension(vmType define.VMType) string {
switch vmType {
case define.QemuVirt:
return ".qcow2"
case define.AppleHvVirt, define.LibKrun:
return ".raw"
case define.HyperVVirt:
return ".vhdx"
case define.WSLVirt:
return ""
default:
return ""
}
}

func (puller *CrcImagePuller) LocalPath() (*define.VMFile, error) {
// filename is bundle specific
return define.NewMachineFile(filepath.Join(MachineInstanceDir, "crc", fmt.Sprintf("crc%s", imageExtension(puller.vmType))), nil)
}

func (puller *CrcImagePuller) Download() error {
//_ = bundle.Get()
// no download yet, reuse crc code
imagePath, err := puller.LocalPath()
if err != nil {
return err
}

slog.Info(fmt.Sprintf("%+v", puller))
slog.Info("file copy", "source", puller.sourcePath, "dest", imagePath.GetPath())
if err := crcos.CopyFile(puller.sourcePath, imagePath.GetPath()); err != nil {
return err
}
/*
if err := unix.Access(imagePath.GetPath(), unix.R_OK|unix.W_OK); err != nil {
return fmt.Errorf("cannot access %s: %w", imagePath.GetPath(), err)
}
*/
fi, err := os.Stat(imagePath.GetPath())
if err != nil {
return fmt.Errorf("cannot get file information for %s: %w", imagePath.GetPath(), err)
}
perms := fi.Mode().Perm()
if !perms.IsRegular() {
return fmt.Errorf("%s must be a regular file", imagePath.GetPath())
}
if (perms & 0600) != 0600 {
return fmt.Errorf("%s is not readable/writable by the user", imagePath.GetPath())
}
slog.Info("all is fine", "imagePath", imagePath.GetPath())

return nil
}

/*
bundleInfo, err := bundle.Use(bundleName)
if err == nil {
logging.Infof("Loading bundle: %s...", bundleName)
return bundleInfo, nil
}
logging.Debugf("Failed to load bundle %s: %v", bundleName, err)
logging.Infof("Downloading bundle: %s...", bundleName)
bundlePath, err = bundle.Download(preset, bundlePath, enableBundleQuayFallback)
if err != nil {
return nil, err
}
logging.Infof("Extracting bundle: %s...", bundleName)
if _, err := bundle.Extract(bundlePath); err != nil {
return nil, err
}
return bundle.Use(bundleName)
*/
18 changes: 7 additions & 11 deletions pkg/machinedriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
provider2 "github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/shim"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
crcos "github.com/crc-org/crc/v2/pkg/os"
"github.com/crc-org/machine/libmachine/drivers"
"github.com/crc-org/machine/libmachine/state"
)
Expand Down Expand Up @@ -166,16 +165,6 @@ func (d *Driver) Create() error {
return err
}

switch d.ImageFormat {
case "qcow2":
// FIXME: the libvirt machine driver uses qemu-img
if err := crcos.CopyFile(d.ImageSourcePath, d.getDiskPath()); err != nil {
return err
}
default:
return fmt.Errorf("%s is an unsupported disk image format", d.ImageFormat)
}

// Check if machine already exists
vmConfig, exists, err := shim.VMExists(d.MachineName, []vmconfigs.VMProvider{d.vmProvider})
if err != nil {
Expand Down Expand Up @@ -206,6 +195,13 @@ func (d *Driver) Create() error {
*/

initOpts := d.initOpts()
crcPuller, err := NewCrcImagePuller(d.vmProvider.VMType())
if err != nil {
return nil
}
crcPuller.SetSourceURI(d.ImageSourcePath)
initOpts.ImagePuller = crcPuller

for idx, vol := range initOpts.Volumes {
initOpts.Volumes[idx] = os.ExpandEnv(vol)
}
Expand Down

0 comments on commit 6b87280

Please sign in to comment.