Mounting Windows 7 shared folders easily in an Ubuntu VM.

I’m currently running Ubuntu 10.04 in VMware and it’s nice to have access to files from my host Windows 7 install. I’ve always had problems with VMware tools and so I often just mount SMB shares manually in Ubuntu.

To facilitate that, I wrote the following script, which you can download here.

Make sure that your Ubuntu install is able to see your host. My VM has a network adapter configured to use NAT, so it can always see the host at a fixed IP.

You can set your sharing options for folders in Windows by:

  1. Right click the folder you’d like to share.
  2. Select the sharing tab.
  3. Click on Share… and make sure the user you specify in the script has permissions, then click on Share.
  4. Click on Advanced Sharing… and set the path for your shared folder. You need this path to change the configuration options in the script.
  5. Hit apply and close.

You will also need to make a mount point for your share. I use /media/winshare.

To use this, just drop it somewhere into your $PATH where sudo can see it.

And here’s the script (download):

#!/bin/bash

######################################################## DESCRIPTION
# winshare
# by Benry Yip
# 
# A bash script to quickly mount/unmount a Windows shared folder.
#
# Just edit the configuration options and use
#     winshare mount
# and
#     winshare umount
# to mount and umount your Windows shared folder, respectively.
#
# Inspired by:
# http://mytechnotes.wordpress.com/2007/05/21/windows-shared-folder-from-ubuntu/
#
# Since this was stolen from mytechnotes, feel free to steal this
# and modify it as you see fit!

###################################################### CONFIGURATION
# Edit this! #
##############

# User on Windows machine.
user="Benry Yip"

# Path of the shared folder.
shared_path="//192.168.204.1/benryyip"

# Directory on local computer to mount shared folder to.
mount_path="/media/winshare"

############################################################## SETUP
# First we have to check if we are root.
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root." 1>&2
    exit 1
fi

# Get the name of this script.
script_name=`basename $0`

# Get the "Total #" line from ls.
ls_output=`ls -l $mount_path | head -n 1`

# If there are files in $ls_output, then we know the share is
# mounted.
if [[ "$ls_output" != "total 0" ]]
then
    mounted="true"
else
    unmounted="true"
fi

########################################################## FUNCTIONS
display_usage()
{
    # If there are no arguments, display whether our shared folder
    # is mounted.
    if [[ -n "$mounted" ]]; then
        echo "$mount_path IS mounted."
        echo
        echo "Use the following command:"
        echo "    $script_name umount"
        echo "to unmount your Windows shared folder."
        echo

    else
        echo "$mount_path is NOT mounted."
        echo
        echo "Use the following command:"
        echo "    $script_name mount"
        echo "to mount your Windows shared folder."
        echo
    fi
}

############################################################# SCRIPT
# Check if we have any arguments.
if [[ -n "$1" ]]; then

    # If there are arguments, do the right thing!
    # Got "mount" command.
    if [[ "$1" == "mount" ]]; then

        if [[ -n "$mounted" ]]; then
            display_usage
        else
            echo "Mounting $mount_path..."
            rc=`mount -t smbfs -o username="$user" "$shared_path" "$mount_path"`

            if [[ -n "$rc" ]]; then
                echo "Error: $rc"
            else
                echo "$shared_path mounted at $mount_path."
            fi
        fi

    # Got umount command.
    elif [[ "$1" == "umount"
         || "$1" == "unmount" ]]; then

        if [[ -n "$unmounted" ]]; then
            display_usage
        else
            echo "Unmounting $mount_path..."
            rc=`umount $mount_path`

            if [[ -n "$rc" ]]; then
                echo "Error: $rc"
            else
                echo "Unmounted $mount_path."
            fi
        fi

    # Got some unrecognized command.
    else
        display_usage
    fi

# We don't have any arguments...
else
    # so display the usage text.
    display_usage
fi

Feel free to drop me a line with any modifications/improvements at me@benryyip.com.