Seamless integration of virtual machine and host for easier development.
Posted by Lori about 1 year agoShould I use the ruby installation that comes with your mac? Or maybe I should be using MacPorts so I have some isolation and control of my development environment? How do I keep my environment similar to whats being used in production? Do I have to reinstall MacOS if I screw things up?
Its a common problem for Web developers using a mac. You have this wonderful operating system with many of the tools you need... but you still have to figure out how to get everything installed and working before you can get any work done.
Even worse is when you have to test your software on multiple environments. The app runs in Rails 2.0, but what about 2.1? For some reason installing Rails 2.1 while an app is configured for 2.0 causes it to randomly pick bits from both versions to use.
Seamless integration of virtual machine and host for easier development.
My solution to this problem is to use a virtual machine for my development environment. This has some cool advantages like letting you switch between different environments. Its a little harder to get going initially, but once its configured its great!
Pick your virtualization
I personally prefer VMWare Fusion 2 as I've had many issues with Parallels in the past with slow networking and file operations.
- VMware Fusion 2 - commercial
- Parallels 4 - commercial
- Q/QEmu - opensource
- VirtualBox - mostly opensource
Install a guest OS
I personally prefer ubuntu for this. I am currently using the ubuntu 8.10 server install dvd for my virtual machines.
Configure NFS sharing
(Server) On your mac
Create or edit the file /etc/exports:
/Users -maproot=root -alldirs -network 192.168.227.0 -mask 255.255.255.0
This will share your /Users directory only to the network 192.168.227.0, which is private between your machine and virtual machines if you are using vmware. If you are using a different virtualization software, you will need to figure out which subnet to be sharing to.
(Client) On your guest OS
Make sure you have any packages you need for nfs client usage. For ubuntu you will need to do:
sudo apt-get install portmap nfs-common
Create the directory /u
sudo mkdir /u
Add to the bottom of your /etc/fstab
# Mount the /Users share to /u on the guest
192.168.227.1:/Users /u nfs defaults 0 0
Now you will need to modify your guest OS uid and gid to match your macs.
If you run id on both the mac and your guest os, you can see which uid and gid your users are using.
On my Mac, my user's id is 501, and it belongs to the staff(20) group. On my linux guest, my id is 1000, it belongs to a group named after the user(1000), and also belongs to the admin(114) group.
I've modified my guest os as follows:
old /etc/group
dialout:x:20:lori
admin:x:114:lori
lori:x:1000:
new /etc/group
dialout:x:1000:lori
admin:x:20:lori
lori:x:114:lori
old /etc/passwd
lori:x:1000:1000:Lori Holden,,,:/home/lori:/bin/bash
new /etc/passwd
lori:x:501:20:Lori Holden,,,:/home/lori:/bin/bash
Change ownership of your home directory.
sudo chown 501:20 $HOME
Finally, mount your /u directory.
sudo mount /u
Start coding!
You can now browse to /u/YourUser and do anything you would normally do during development. Edit files locally on your mac, and run your mongrel processes on the virtual machine.
(optional) Change your home directory
You can change your guest os home directory to be the same as your mac fairly easily.
Edit /etc/passwd and change the bit that says /home/username to point to your directory in /u/MacUser
old /etc/passwd
lori:x:501:20:Lori Holden,,,:/home/lori:/bin/bash
new /etc/passwd
lori:x:501:20:Lori Holden,,,:/u/Lori:/bin/bash