Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions pkg/drivers/common/virtiofs/virtiofs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Copyright 2025 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package virtiofs

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/docker/machine/libmachine/drivers"
"github.com/google/uuid"
)

// Mount is a directory on the host shared with the guest using virtiofs.
type Mount struct {
// HostPath is an absolute path to existing directory to share with the
// guest via virtiofs protocol. Also called "source" by some tools.
HostPath string

// GuestPath is a path in the guest for mounting the shared directory using
// virtiofs. Also called target or mountpoint by some tools.
GuestPath string

// Tag is a string identifying the shared file system in the guest.
// Generated by minikube.
Tag string
}

// ValidateMountString parses the mount-string flag and validates that the
// specified paths can be used for virtiofs mount. Returns list with one
// validated mount, ready for configuring the driver.
// TODO: Drop when we have a flag supporting multiple mounts.
func ValidateMountString(s string) ([]*Mount, error) {
if s == "" {
return nil, nil
}
return validateMounts([]string{s})
}

func validateMounts(args []string) ([]*Mount, error) {
var mounts []*Mount

seenHost := map[string]*Mount{}
seenGuest := map[string]*Mount{}

for _, s := range args {
mount, err := ParseMount(s)
if err != nil {
return nil, err
}

if err := mount.Validate(); err != nil {
return nil, err
}

if existing, ok := seenHost[mount.HostPath]; ok {
return nil, fmt.Errorf("host path %q is already shared at guest path %q", mount.HostPath, existing.GuestPath)
}
seenHost[mount.HostPath] = mount

if existing, ok := seenGuest[mount.GuestPath]; ok {
return nil, fmt.Errorf("guest path %q is already shared from host path %q", mount.GuestPath, existing.HostPath)
}
seenGuest[mount.GuestPath] = mount

mounts = append(mounts, mount)
}

return mounts, nil
}

// ParseMount parses a string in the format "/host-path:/guest-path" and returns
// a new Mount instance. The mount must be validated before using it to
// configure the driver.
func ParseMount(s string) (*Mount, error) {
pair := strings.SplitN(s, ":", 2)
if len(pair) != 2 {
return nil, fmt.Errorf("invalid virtiofs mount %q: (expected '/host-path:/guest-path')", s)
}

return &Mount{
HostPath: pair[0],
GuestPath: pair[1],
Tag: uuid.NewString(),
}, nil
}

// Validate that the mount can be used for virtiofs device configuration. Both
// host and guest paths must be absolute. Host path must be a directory and must
// not include virtiofs configuration separator (",").
func (m *Mount) Validate() error {
// "," is a --device configuration separator in vfkit and krunkit.
if strings.Contains(m.HostPath, ",") {
return fmt.Errorf("host path %q must not contain ','", m.HostPath)
}

if !filepath.IsAbs(m.HostPath) {
return fmt.Errorf("host path %q is not an absolute path", m.HostPath)
}

if fs, err := os.Stat(m.HostPath); err != nil {
return fmt.Errorf("failed to validate host path %q: %w", m.HostPath, err)
} else if !fs.IsDir() {
return fmt.Errorf("host path %q is not a directory", m.HostPath)
}

if !filepath.IsAbs(m.GuestPath) {
return fmt.Errorf("guest path %q is not an absolute path", m.GuestPath)
}

return nil
}

// SetupMounts connects to the host via SSH, creates the mount directory if
// needed, and mount the virtiofs file system. It should be called by
// driver.Start().
func SetupMounts(d drivers.Driver, mounts []*Mount) error {
var script strings.Builder

script.WriteString("set -e\n")

for _, mount := range mounts {
script.WriteString(fmt.Sprintf("sudo mkdir -p \"%s\"\n", mount.GuestPath))
script.WriteString(fmt.Sprintf("sudo mount -t virtiofs %s \"%s\"\n", mount.Tag, mount.GuestPath))
}

if _, err := drivers.RunSSHCommandFromDriver(d, script.String()); err != nil {
return err
}

return nil
}
146 changes: 146 additions & 0 deletions pkg/drivers/common/virtiofs/virtiofs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright 2025 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package virtiofs_test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/google/uuid"

"k8s.io/minikube/pkg/drivers/common/virtiofs"
)

func TestVirtiofsValidateEmptyMountString(t *testing.T) {
mounts, err := virtiofs.ValidateMountString("")
if err != nil {
t.Fatalf("failed to parse empty mount string: %s", err)
}
if mounts != nil {
t.Fatalf("expected nil mounts, got %v", mounts)
}
}

func TestVirtiofsValidateMountString(t *testing.T) {
hostPath := t.TempDir()
guestPath := "/mnt/models"
mountString := fmt.Sprintf("%s:%s", hostPath, guestPath)

mounts, err := virtiofs.ValidateMountString(mountString)
if err != nil {
t.Fatalf("failed to parse mountString %q: %s", mountString, err)
}
if len(mounts) != 1 {
t.Fatalf("expected a single mount, got %v", mounts)
}

mount := mounts[0]
if mount.HostPath != hostPath {
t.Fatalf("expected host path %q, got %q", hostPath, mount.HostPath)
}
if mount.GuestPath != guestPath {
t.Fatalf("expected guest path %q, got %q", guestPath, mount.GuestPath)
}

tag, err := uuid.Parse(mount.Tag)
if err != nil {
t.Fatalf("failed to parse UUID from mount tag: %s", err)
}
if tag.Version() != 4 {
t.Fatalf("mount tag is not a random UUID")
}

if err := mount.Validate(); err != nil {
t.Fatalf("mount is not valid: %s", err)
}
}

func TestVirtiofsParseInvalidMountString(t *testing.T) {
for _, tt := range []struct {
name string
mountString string
}{
{
name: "empty",
mountString: "",
},
{
name: "guest path is missing",
mountString: "host-path",
},
} {
t.Run(tt.name, func(t *testing.T) {
mount, err := virtiofs.ParseMount(tt.mountString)
if err == nil {
t.Fatalf("invalid mount string %q did not fail to parse", tt.mountString)
}
if mount != nil {
t.Fatalf("expected nil mount for %q, got %v", tt.mountString, mount)
}
})
}
}

func TestVirtiofsValidateInvalidMount(t *testing.T) {
dir := t.TempDir()
missing := filepath.Join(dir, "missing")
file := filepath.Join(dir, "file")

f, err := os.Create(file)
if err != nil {
t.Fatal(err)
}
f.Close()

for _, tt := range []struct {
name string
mountString string
}{
{
name: "host path contains virtiofs config separator",
mountString: "/host,path:/guest-path",
},
{
name: "host path is relative",
mountString: "host-path:/guest-path",
},
{
name: "guest path is relative",
mountString: fmt.Sprintf("%s:guest-path", dir),
},
{
name: "host path is missing",
mountString: fmt.Sprintf("%s:/guest-path", missing),
},
{
name: "host path is not a directory",
mountString: fmt.Sprintf("%s:/guest-path", file),
},
} {
t.Run(tt.name, func(t *testing.T) {
mount, err := virtiofs.ParseMount(tt.mountString)
if err != nil {
t.Fatalf("failed to parse mount string %q: %s", tt.mountString, err)
}
if err := mount.Validate(); err == nil {
t.Fatalf("invalid mount %q did not failed validation", tt.mountString)
}
})
}
}
19 changes: 18 additions & 1 deletion pkg/drivers/krunkit/krunkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/common"
"k8s.io/minikube/pkg/drivers/common/virtiofs"
"k8s.io/minikube/pkg/drivers/common/vmnet"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/firewall"
Expand Down Expand Up @@ -71,6 +72,7 @@ type Driver struct {
CPU int
Memory int
ExtraDisks int
VirtiofsMounts []*virtiofs.Mount
MACAddress string
VmnetHelper vmnet.Helper
}
Expand Down Expand Up @@ -207,8 +209,18 @@ func (d *Driver) Start() error {
}

log.Infof("Waiting for VM to start (ssh -p %d docker@%s)...", d.SSHPort, d.IPAddress)
if err := WaitForTCPWithDelay(fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort), time.Second); err != nil {
return err
}

if len(d.VirtiofsMounts) > 0 {
log.Infof("Setup virtiofs mounts ...")
if err := virtiofs.SetupMounts(d, d.VirtiofsMounts); err != nil {
return err
}
}

return WaitForTCPWithDelay(fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort), time.Second)
return nil
}

// startKrunkit starts the krunkit child process.
Expand All @@ -231,6 +243,11 @@ func (d *Driver) startKrunkit(socketPath string) error {
"--device", fmt.Sprintf("virtio-blk,path=%s", common.ExtraDiskPath(d.BaseDriver, i)))
}

for _, mount := range d.VirtiofsMounts {
args = append(args,
"--device", fmt.Sprintf("virtio-fs,sharedDir=%s,mountTag=%s", mount.HostPath, mount.Tag))
}

log.Debugf("executing: krunkit %s", strings.Join(args, " "))
cmd := exec.Command(driverName, args...)

Expand Down
20 changes: 19 additions & 1 deletion pkg/drivers/vfkit/vfkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/common"
"k8s.io/minikube/pkg/drivers/common/virtiofs"
"k8s.io/minikube/pkg/drivers/common/vmnet"
"k8s.io/minikube/pkg/minikube/detect"
"k8s.io/minikube/pkg/minikube/exit"
Expand Down Expand Up @@ -72,6 +73,7 @@ type Driver struct {
CPU int
Memory int
ExtraDisks int
VirtiofsMounts []*virtiofs.Mount
Network string // "", "nat", "vmnet-shared"
MACAddress string // For network=nat, network=""
VmnetHelper *vmnet.Helper // For network=vmnet-shared
Expand Down Expand Up @@ -250,8 +252,18 @@ func (d *Driver) Start() error {
}

log.Infof("Waiting for VM to start (ssh -p %d docker@%s)...", d.SSHPort, d.IPAddress)
if err := WaitForTCPWithDelay(fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort), time.Second); err != nil {
return err
}

if len(d.VirtiofsMounts) > 0 {
log.Infof("Setup virtiofs mounts ...")
if err := virtiofs.SetupMounts(d, d.VirtiofsMounts); err != nil {
return err
}
}

return WaitForTCPWithDelay(fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort), time.Second)
return nil
}

// startVfkit starts the vfkit child process. If socketPath is not empty, vfkit
Expand Down Expand Up @@ -308,6 +320,12 @@ func (d *Driver) startVfkit(socketPath string) error {
startCmd = append(startCmd,
"--device", fmt.Sprintf("virtio-serial,logFilePath=%s", serialPath))

for _, mount := range d.VirtiofsMounts {
startCmd = append(startCmd,
"--device", fmt.Sprintf("virtio-fs,sharedDir=%s,mountTag=%s", mount.HostPath, mount.Tag))

}

log.Debugf("executing: vfkit %s", strings.Join(startCmd, " "))
os.Remove(d.sockfilePath())
cmd := exec.Command("vfkit", startCmd...)
Expand Down
Loading
Loading