From 71d6e2fbafe724bad7dab94f6c5077eedf639e5c Mon Sep 17 00:00:00 2001 From: Arthur Sengileyev Date: Tue, 23 Jul 2024 16:24:48 +0300 Subject: [PATCH] Add utility to convert VMFile to URL for UNIX sockets This adds generic utility to convert file system path into URL structure. Instead of string manipulation it uses URL parsing and building routines. Appending absolute path to `unix:///` URL out of the box correctly handles URL format on Windows platform, where filepath should be prepended by additional `/` before drive letter. Signed-off-by: Arthur Sengileyev --- pkg/machine/qemu/stubber.go | 7 +++++-- pkg/machine/sockets/sockets.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/machine/qemu/stubber.go b/pkg/machine/qemu/stubber.go index 903d56c285..999ef2f081 100644 --- a/pkg/machine/qemu/stubber.go +++ b/pkg/machine/qemu/stubber.go @@ -9,7 +9,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strconv" "strings" "time" @@ -319,11 +318,15 @@ func (q *QEMUStubber) StartNetworking(mc *vmconfigs.MachineConfig, cmd *gvproxy. if err != nil { return err } + socketURL, err := sockets.ToUnixURL(gvProxySock) + if err != nil { + return err + } // make sure it does not exist before gvproxy is called if err := gvProxySock.Delete(); err != nil { logrus.Error(err) } - cmd.AddQemuSocket(fmt.Sprintf("unix://%s", filepath.ToSlash(gvProxySock.GetPath()))) + cmd.AddQemuSocket(socketURL.String()) return nil } diff --git a/pkg/machine/sockets/sockets.go b/pkg/machine/sockets/sockets.go index 520dcf9099..62c15fc6a1 100644 --- a/pkg/machine/sockets/sockets.go +++ b/pkg/machine/sockets/sockets.go @@ -5,6 +5,7 @@ import ( "bytes" "fmt" "net" + "net/url" "path/filepath" "time" @@ -110,3 +111,17 @@ func WaitForSocketWithBackoffs(maxBackoffs int, backoff time.Duration, socketPat } return fmt.Errorf("unable to connect to %q socket at %q", name, socketPath) } + +// ToUnixURL converts `socketLoc` into URL representation +func ToUnixURL(socketLoc *define.VMFile) (*url.URL, error) { + p := socketLoc.GetPath() + if !filepath.IsAbs(p) { + return nil, fmt.Errorf("socket path must be absolute %q", p) + } + s, err := url.Parse("unix:///") + if err != nil { + return nil, err + } + s = s.JoinPath(filepath.ToSlash(p)) + return s, nil +}