Skip to content

Commit

Permalink
chore: minor fixes (#682)
Browse files Browse the repository at this point in the history
* net: gvproxy: use qemu 7.2 stream

* cli: handle --profile flag for `colima list`
  • Loading branch information
abiosoft authored Apr 2, 2023
1 parent 9971eca commit 65ee3d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
37 changes: 1 addition & 36 deletions cmd/colima/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
Expand All @@ -15,8 +13,6 @@ import (
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/daemon/process/gvproxy"
"github.com/abiosoft/colima/daemon/process/vmnet"
"github.com/sirupsen/logrus"
)

func main() {
Expand All @@ -35,7 +31,6 @@ func qemuWrapper(qemu string) {
}

gvproxyInfo := gvproxy.Info()
vmnetInfo := vmnet.Info()

// check if qemu is meant to run by lima
// decided by -pidfile flag
Expand All @@ -48,51 +43,21 @@ func qemuWrapper(qemu string) {
}

args := os.Args[1:] // forward all args
var extraFiles []*os.File

gvproxyEnabled, _ := strconv.ParseBool(os.Getenv(gvproxy.SubProcessEnvVar))
vmnetEnabled, _ := strconv.ParseBool(os.Getenv(vmnet.SubProcessEnvVar))

if qemuRunning && gvproxyEnabled {
// vmnet should come first as it would be added by Lima and would have the fd 3

// vmnet
if vmnetEnabled {
fd := os.NewFile(3, vmnetInfo.Socket.File())
extraFiles = append(extraFiles, fd)
}

// gvproxy
{
conn, err := net.Dial("unix", gvproxyInfo.Socket.File())
if err != nil {
logrus.Fatal(fmt.Errorf("error connecting to gvproxy socket: %w", err))
}
fd, err := conn.(*net.UnixConn).File()
if err != nil {
logrus.Fatal(fmt.Errorf("error retrieving fd for gvproxy socket: %w", err))
}
extraFiles = append(extraFiles, fd)
}

// gvproxy fd
fd := strconv.Itoa(2 + len(extraFiles))
args = append(args,
"-netdev", "socket,id=vlan,fd="+fd,
"-netdev", "stream,id=vlan,addr.type=unix,addr.path="+gvproxyInfo.Socket.File(),
"-device", "virtio-net-pci,netdev=vlan,mac="+gvproxyInfo.MacAddress,
)
}

cmd := exec.Command(qemu, args...)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

if len(extraFiles) > 0 {
cmd.ExtraFiles = append(cmd.ExtraFiles, extraFiles...)
}

err := cmd.Run()
if err != nil {
if err, ok := err.(*exec.ExitError); ok {
Expand Down
8 changes: 7 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"text/tabwriter"

"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
Expand All @@ -26,7 +27,12 @@ var listCmd = &cobra.Command{
A new instance can be created during 'colima start' by specifying the '--profile' flag.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
instances, err := limautil.Instances()
profile := []string{}
if cmd.Flag("profile").Changed {
profile = append(profile, config.CurrentProfile().ID)
}

instances, err := limautil.Instances(profile...)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions environment/vm/lima/limautil/limautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,15 @@ func getInstance(profileID string) (InstanceInfo, error) {
}

// Instances returns Lima instances created by colima.
func Instances() ([]InstanceInfo, error) {
func Instances(ids ...string) ([]InstanceInfo, error) {
limaIDs := make([]string, len(ids))
for i := range ids {
limaIDs = append(limaIDs, config.Profile(ids[i]).ID)
}
args := append([]string{"list", "--json"}, limaIDs...)

var buf bytes.Buffer
cmd := cli.Command("limactl", "list", "--json")
cmd := cli.Command("limactl", args...)
cmd.Stderr = nil
cmd.Stdout = &buf

Expand Down

0 comments on commit 65ee3d2

Please sign in to comment.