diff --git a/command/mount.go b/command/mount.go index 0eb28c0..771092f 100644 --- a/command/mount.go +++ b/command/mount.go @@ -27,7 +27,7 @@ import ( // mountCmd represents the mount command var mountCmd = &cobra.Command{ Use: "mount", - Short: "Mount local filesystem inside the VM", + Short: "Mount/Unmount local filesystem inside the VM", Long: `Mount local filesystem to a directory inside the VM, if the guest directory is not specified, then /vermin is used @@ -45,23 +45,33 @@ $ vermin mount vm_01 ~/Downloads To mount the ~/MyHtmlProject directory to /var/www/html inside the VM: $ vermin mount vm_01 ~/MyHtmlProject:/var/www/html + +To unmount previous mounted directories: +$ vermin mount -r vm_01 `, Run: func(cmd *cobra.Command, args []string) { - vmName := args[0] - path := args[1] + vmName := normalizeVmName(args[0]) remove, _ := cmd.Flags().GetBool("remove") - p := strings.Split(path, ":") - hostPath := p[0] - checkFilePath(hostPath) + var err error + if remove { + err = vms.Unmount(vmName) + } else { + path := args[1] + + p := strings.Split(path, ":") + hostPath := p[0] + checkFilePath(hostPath) - guestPath := "/vermin" - if len(p) > 1 { - guestPath = p[1] + guestPath := "/vermin" + if len(p) > 1 { + guestPath = p[1] + } + + err = vms.Mount(vmName, hostPath, guestPath) } - err := vms.Mount(vmName, hostPath, guestPath, remove) if err != nil { fmt.Println(err) os.Exit(1) @@ -71,7 +81,8 @@ $ vermin mount vm_01 ~/MyHtmlProject:/var/www/html if len(args) < 1 { return errors.New("vm required") } - if len(args) < 2 { + remove, _ := cmd.Flags().GetBool("remove") + if !remove && len(args) < 2 { return errors.New("path required") } return nil diff --git a/hypervisor/virtualbox/virtualbox.go b/hypervisor/virtualbox/virtualbox.go index 33b502d..3b1a71d 100644 --- a/hypervisor/virtualbox/virtualbox.go +++ b/hypervisor/virtualbox/virtualbox.go @@ -151,6 +151,7 @@ func (*virtualbox) RemoveMounts(vmName, ipAddr string) error { for i := range transientMounts { mountName := transientMounts[i] + fmt.Println(mountName) if _, err = vboxManage("sharedfolder", "remove", vmName, "--name", mountName, "--transient").Call(); err != nil { return err } diff --git a/vms/mount.go b/vms/mount.go index 8d840b5..c3eac64 100644 --- a/vms/mount.go +++ b/vms/mount.go @@ -11,7 +11,7 @@ var ( mountHeader = fmt.Sprintf(mountFormat, "HOST DIR", "GUEST DIR") ) -func Mount(vmName, hostPath, guestPath string, remove bool) error { +func Unmount(vmName string) error { ipAddr, err := ip.Find(vmName, false) if err != nil { return err @@ -21,10 +21,17 @@ func Mount(vmName, hostPath, guestPath string, remove bool) error { return err } - if remove { - if err = hypervisor.RemoveMounts(vmName, ipAddr); err != nil { - return err - } + return hypervisor.RemoveMounts(vmName, ipAddr) +} + +func Mount(vmName, hostPath, guestPath string) error { + ipAddr, err := ip.Find(vmName, false) + if err != nil { + return err + } + + if err := checkRunningVM(vmName); err != nil { + return err } return hypervisor.AddMount(vmName, ipAddr, hostPath, guestPath)