Skip to content

Commit

Permalink
vm: add support for custom disk images
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Dec 15, 2024
1 parent f829db1 commit 5335a69
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func init() {
startCmd.Flags().StringVarP(&startCmdArgs.Arch, "arch", "a", defaultArch, "architecture (aarch64, x86_64)")
startCmd.Flags().BoolVarP(&startCmdArgs.Flags.Foreground, "foreground", "f", false, "Keep colima in the foreground")
startCmd.Flags().StringVar(&startCmdArgs.Hostname, "hostname", "", "custom hostname for the virtual machine")
startCmd.Flags().StringVarP(&startCmdArgs.DiskImage, "disk-image", "i", "", "local path or URL to a custom disk image")

// host IP addresses
startCmd.Flags().BoolVar(&startCmdArgs.Network.HostAddresses, "network-host-addresses", false, "support port forwarding to specific host IP addresses")
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Config struct {
VMType string `yaml:"vmType,omitempty"`
VZRosetta bool `yaml:"rosetta,omitempty"`
NestedVirtualization bool `yaml:"nestedVirtualization,omitempty"`
DiskImage string `yaml:"diskImage,omitempty"`

// volume mounts
Mounts []Mount `yaml:"mounts,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions embedded/defaults/colima.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ sshPort: 0
# Default: []
mounts: []

# Specify a custom disk image for the virtual machine.
# When not specified, Colima downloads the disk image from Github at
# https://github.com/abiosoft/colima-core/releases.
# A custom disk image (local disk path or URL) can be specified to override the behaviour.
#
# Default: ""
diskImage: ""

# Environment variables for the virtual machine.
#
# EXAMPLE
Expand Down
15 changes: 15 additions & 0 deletions environment/vm/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,26 @@ func (l limaVM) Arch() environment.Arch {
func (l *limaVM) downloadDiskImage(ctx context.Context, conf config.Config) error {
log := l.Logger(ctx)

// use a previously cached image
if image, ok := limautil.ImageCached(l.limaConf.Arch, conf.Runtime); ok {
l.limaConf.Images = []limaconfig.File{image}
return nil
}

// use a user specified disk image
if conf.DiskImage != "" {
log.Infoln("using specified disk image ...")
image, err := limautil.Image(l.limaConf.Arch, conf.Runtime)
if err != nil {
return fmt.Errorf("error getting disk image details: %w", err)
}
log.Warnf("disk image must be identical to '%s'", image.Location)
image.Location = conf.DiskImage
l.limaConf.Images = []limaconfig.File{image}
return nil
}

// download image
log.Infoln("downloading disk image ...")
image, err := limautil.DownloadImage(l.limaConf.Arch, conf.Runtime)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions environment/vm/lima/limautil/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func findImage(arch environment.Arch, runtime string) (f limaconfig.File, err er
return img, nil
}

// Image returns the details of the disk image to download for the arch and runtime.
func Image(arch environment.Arch, runtime string) (limaconfig.File, error) {
return findImage(arch, runtime)
}

// DownloadImage downloads the image for arch and runtime.
func DownloadImage(arch environment.Arch, runtime string) (f limaconfig.File, err error) {
img, err := findImage(arch, runtime)
Expand Down

0 comments on commit 5335a69

Please sign in to comment.