forked from kubernetes/minikube
/
mount.go
144 lines (133 loc) · 4.07 KB
/
mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2016 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 cmd
import (
"fmt"
"net"
"os"
"sync"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
cmdUtil "k8s.io/minikube/cmd/util"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/third_party/go9p/ufs"
)
var mountIP string
var isKill bool
// mountCmd represents the mount command
var mountCmd = &cobra.Command{
Use: "mount [flags] MOUNT_DIRECTORY(ex:\"/home\")",
Short: "Mounts the specified directory into minikube",
Long: `Mounts the specified directory into minikube.`,
Run: func(cmd *cobra.Command, args []string) {
if isKill {
if err := cmdUtil.KillMountProcess(); err != nil {
fmt.Println("Errors occurred deleting mount process: ", err)
os.Exit(1)
}
os.Exit(0)
}
if len(args) != 1 {
errText := `Please specify the directory to be mounted:
\tminikube mount HOST_MOUNT_DIRECTORY:VM_MOUNT_DIRECTORY(ex:"/host-home:/vm-home")
`
fmt.Fprintln(os.Stderr, errText)
os.Exit(1)
}
mountString := args[0]
idx := strings.LastIndex(mountString, ":")
if idx == -1 { // no ":" was present
errText := `Mount directory must be in the form:
\tHOST_MOUNT_DIRECTORY:VM_MOUNT_DIRECTORY`
fmt.Fprintln(os.Stderr, errText)
os.Exit(1)
}
hostPath := mountString[:idx]
vmPath := mountString[idx+1:]
if _, err := os.Stat(hostPath); err != nil {
if os.IsNotExist(err) {
errText := fmt.Sprintf("Cannot find directory %s for mount", hostPath)
fmt.Fprintln(os.Stderr, errText)
} else {
errText := fmt.Sprintf("Error accesssing directory %s for mount", hostPath)
fmt.Fprintln(os.Stderr, errText)
}
os.Exit(1)
}
if len(vmPath) == 0 || !strings.HasPrefix(vmPath, "/") {
errText := fmt.Sprintf("The :VM_MOUNT_DIRECTORY must be an absolute path")
fmt.Fprintln(os.Stderr, errText)
os.Exit(1)
}
var debugVal int
if glog.V(1) {
debugVal = 1 // ufs.StartServer takes int debug param
}
api, err := machine.NewAPIClient(clientType)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting client: %s\n", err)
os.Exit(1)
}
defer api.Close()
host, err := api.Load(config.GetMachineName())
if err != nil {
glog.Errorln("Error loading api: ", err)
os.Exit(1)
}
if host.Driver.DriverName() == "none" {
fmt.Println(`'none' driver does not support 'minikube mount' command`)
os.Exit(0)
}
var ip net.IP
if mountIP == "" {
ip, err = cluster.GetVMHostIP(host)
if err != nil {
glog.Errorln("Error getting the host IP address to use from within the VM: ", err)
os.Exit(1)
}
} else {
ip = net.ParseIP(mountIP)
if ip == nil {
glog.Errorln("error parsing the input ip address for mount")
os.Exit(1)
}
}
fmt.Printf("Mounting %s into %s on the minikubeVM\n", hostPath, vmPath)
fmt.Println("This daemon process needs to stay alive for the mount to still be accessible...")
port, err := cmdUtil.GetPort()
if err != nil {
glog.Errorln("Error finding port for mount: ", err)
os.Exit(1)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
ufs.StartServer(net.JoinHostPort(ip.String(), port), debugVal, hostPath)
wg.Done()
}()
err = cluster.MountHost(api, vmPath, ip, port)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
wg.Wait()
},
}
func init() {
mountCmd.Flags().StringVar(&mountIP, "ip", "", "Specify the ip that the mount should be setup on")
mountCmd.Flags().BoolVar(&isKill, "kill", false, "Kill the mount process spawned by minikube start")
RootCmd.AddCommand(mountCmd)
}