Windows on Mac Tips

2015-03-01 in osx, vmware

I work in what is primarily a .NET shop, but we also do a lot of development using other technologies. Since I prefer to have a rounded skill set, and working in JS and Ruby is painful on Windows, my work machine is a MacBook Pro. I've collected a set of scripts and tricks to make this process as smooth as possible. I virtualize our standard Windows development machine through VMWare Fusion, but the tips should apply to other solutions, such as Parallels.

Mapping shared folders

VMWare Fusion dictates where shared directories live on the guest machine. Since our site's JavaScript is buried deep inside a solution, I needed a way to map arbitrary folders on the guest to arbitrary folders on the host. This way, I could use Windows only for serving the site and making changes to the .NET code.

The solution I prefer is to use the sharing feature built into Windows, which is implemented as SMB shares, and to mount them manually in OS X. There are two sides to this process: sharing the folder in Windows, and connecting to the folder in OS X. To share the folder in Windows:

  1. Select 'Properties' in the context menu for the folder you want to share
  2. Under 'Sharing', choose 'Advanced Sharing...'
  3. In the dialog that appears, choose 'Share this folder'
  4. Click 'Permissions'. In this dialog, add your current account with 'Full Control', and remove 'Everyone'.
  5. Hit 'OK', 'OK', and 'Close' to apply all changes

Mounting shared folders manually

To map a share via OS X's Finder interface:

  1. From Finder's top bar, choose 'Go' -> 'Connect to Server...' (or hit cmd-k)
  2. Fill in 'Server Address' with the share address of the folder (given by windows). It should look like smb://WINDOWS-HOSTNAME/share-name.
  3. Hit 'Connect', and give your credentials when prompted

Mounting shared folders from the command line

The above works, but has two big disadvantages: it is difficult to automate, and you can't choose which folder to mount your share to. I got around these two problems with a script that allows you to connect to any shared file.

It mounts your shares to ~/smb/share-name as configured, but can be changed by changing the variable SHAREDIR in the script. You also need to configure WINDOWSHOST with your Windows username and hostname.

The script can be used by executing ./connect.sh share-name, and when it's done, you'll be inside the target directory. It enumerates every file in the share by default to populate the SMB cache - I've found this gives me a large boost in speed when subsequently loading the folder in an editor.

#!/bin/sh
WINDOWSHOST = "//user@guest-hostname" 
SHAREDIR = "~/smb"

# make sure the mount dir on host exists
if [ ! -d "$SHAREDIR/$1" ]; then
  mkdir "$SHAREDIR/$1"
fi

# if there's something in the mount, because either share is active or files
#  are hanging around, cancel the mount process
if find "$SHAREDIR/$1" -maxdepth 0 -empty | read v; then
  rm -rf "$SHAREDIR/$1"
  mkdir "$SHAREDIR/$1"
  mount -t smbfs "$WINDOWSHOST/$1" "$SHAREDIR/$1"
fi

# move into the mount and enumerate all files to fill the cache
cd "$SHAREDIR/$1"
ls -laR > /dev/null

Performance

This is more specific to VMware Fusion. It's important to only assign up to as many physical cores as your machine has to your guest - assigning hyperthreaded cores will severely degrade performance for both the guest and the host.

In addition, try to leave a fair amount of memory available to your host - OS X loves to have as much memory as possible, for file caching and other things, and giving it too little will result in heavy swapping.