-
Notifications
You must be signed in to change notification settings - Fork 5.1k
drivers: Add support for Virtiofs mounts for vfkit and krunkit #21149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+350
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.