Skip to content

Commit

Permalink
separate mounts from unmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Dec 9, 2020
1 parent 4708903 commit 67f1265
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
33 changes: 22 additions & 11 deletions command/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions hypervisor/virtualbox/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 12 additions & 5 deletions vms/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 67f1265

Please sign in to comment.