|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package virtiofs |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/docker/machine/libmachine/drivers" |
| 26 | + "github.com/google/uuid" |
| 27 | +) |
| 28 | + |
| 29 | +// Mount is a directory on the host shared with the guest using virtiofs. |
| 30 | +type Mount struct { |
| 31 | + // HostPath is an absolute path to existing directory to share with the |
| 32 | + // guest via virtiofs protocol. Also called "source" by some tools. |
| 33 | + HostPath string |
| 34 | + |
| 35 | + // GuestPath is a path in the guest for mounting the shared directory using |
| 36 | + // virtiofs. Also called target or mountpoint by some tools. |
| 37 | + GuestPath string |
| 38 | + |
| 39 | + // Tag is a string identifying the shared file system in the guest. |
| 40 | + // Generated by minikube. |
| 41 | + Tag string |
| 42 | +} |
| 43 | + |
| 44 | +// ValidateMountString parses the mount-string flag and validates that the |
| 45 | +// specified paths can be used for virtiofs mount. Returns list with one |
| 46 | +// validated mount, ready for configuring the driver. |
| 47 | +// TODO: Drop when we have a flag supporting multiple mounts. |
| 48 | +func ValidateMountString(s string) ([]*Mount, error) { |
| 49 | + if s == "" { |
| 50 | + return nil, nil |
| 51 | + } |
| 52 | + return validateMounts([]string{s}) |
| 53 | +} |
| 54 | + |
| 55 | +func validateMounts(args []string) ([]*Mount, error) { |
| 56 | + var mounts []*Mount |
| 57 | + |
| 58 | + seenHost := map[string]*Mount{} |
| 59 | + seenGuest := map[string]*Mount{} |
| 60 | + |
| 61 | + for _, s := range args { |
| 62 | + mount, err := ParseMount(s) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + if err := mount.Validate(); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + if existing, ok := seenHost[mount.HostPath]; ok { |
| 72 | + return nil, fmt.Errorf("host path %q is already shared at guest path %q", mount.HostPath, existing.GuestPath) |
| 73 | + } |
| 74 | + seenHost[mount.HostPath] = mount |
| 75 | + |
| 76 | + if existing, ok := seenGuest[mount.GuestPath]; ok { |
| 77 | + return nil, fmt.Errorf("guest path %q is already shared from host path %q", mount.GuestPath, existing.HostPath) |
| 78 | + } |
| 79 | + seenGuest[mount.GuestPath] = mount |
| 80 | + |
| 81 | + mounts = append(mounts, mount) |
| 82 | + } |
| 83 | + |
| 84 | + return mounts, nil |
| 85 | +} |
| 86 | + |
| 87 | +// ParseMount parses a string in the format "/host-path:/guest-path" and returns |
| 88 | +// a new Mount instance. The mount must be validated before using it to |
| 89 | +// configure the driver. |
| 90 | +func ParseMount(s string) (*Mount, error) { |
| 91 | + pair := strings.SplitN(s, ":", 2) |
| 92 | + if len(pair) != 2 { |
| 93 | + return nil, fmt.Errorf("invalid virtiofs mount %q: (expected '/host-path:/guest-path')", s) |
| 94 | + } |
| 95 | + |
| 96 | + return &Mount{ |
| 97 | + HostPath: pair[0], |
| 98 | + GuestPath: pair[1], |
| 99 | + Tag: uuid.NewString(), |
| 100 | + }, nil |
| 101 | +} |
| 102 | + |
| 103 | +// Validate that the mount can be used for virtiofs device configuration. Both |
| 104 | +// host and guest paths must be absolute. Host path must be a directory and must |
| 105 | +// not include virtiofs configuration separator (","). |
| 106 | +func (m *Mount) Validate() error { |
| 107 | + // "," is a --device configuration separator in vfkit and krunkit. |
| 108 | + if strings.Contains(m.HostPath, ",") { |
| 109 | + return fmt.Errorf("host path %q must not contain ','", m.HostPath) |
| 110 | + } |
| 111 | + |
| 112 | + if !filepath.IsAbs(m.HostPath) { |
| 113 | + return fmt.Errorf("host path %q is not an absolute path", m.HostPath) |
| 114 | + } |
| 115 | + |
| 116 | + if fs, err := os.Stat(m.HostPath); err != nil { |
| 117 | + return fmt.Errorf("failed to validate host path %q: %w", m.HostPath, err) |
| 118 | + } else if !fs.IsDir() { |
| 119 | + return fmt.Errorf("host path %q is not a directory", m.HostPath) |
| 120 | + } |
| 121 | + |
| 122 | + if !filepath.IsAbs(m.GuestPath) { |
| 123 | + return fmt.Errorf("guest path %q is not an absolute path", m.GuestPath) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// SetupMounts connects to the host via SSH, creates the mount directory if |
| 130 | +// needed, and mount the virtiofs file system. It should be called by |
| 131 | +// driver.Start(). |
| 132 | +func SetupMounts(d drivers.Driver, mounts []*Mount) error { |
| 133 | + var script strings.Builder |
| 134 | + |
| 135 | + script.WriteString("set -e\n") |
| 136 | + |
| 137 | + for _, mount := range mounts { |
| 138 | + script.WriteString(fmt.Sprintf("sudo mkdir -p \"%s\"\n", mount.GuestPath)) |
| 139 | + script.WriteString(fmt.Sprintf("sudo mount -t virtiofs %s \"%s\"\n", mount.Tag, mount.GuestPath)) |
| 140 | + } |
| 141 | + |
| 142 | + if _, err := drivers.RunSSHCommandFromDriver(d, script.String()); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments