VirtualBox: Configuring Static IPs for VMs

06.14.2012 00:57 by kbeckman | Comments

Late last year, I wrote about a hack I came up with for configuring a VirtualBox guest OS with DHCP and ensuring it always got the same IP address. After a new project and some new insight, I’ve figured out a new way that deprecates all of the information in that post. Under that configuration, I assumed that the only way to get a static IP address for a Windows guest OS was to join it to the corporate domain – I was wrong. In the steps that follow, I’ll explain how to set up VirtualBox networking and the network settings for Windows and Ubuntu guests to support static IPs on your VMs. This will make it possible for you to connect from VM-to-VM (provided they’re both running) and for your host OS (in this case Mac OSX) to connect to each VM via static IP.

 

Step 1: VirtualBox Network Settings

To enable static IP addresses on your VirtualBox VMs, you’ll first need to setup a Host-Only network. The host-only network is a virtual network provided by VirtualBox that is visible only to the Host OS and its virtual machines. VirtualBox should setup a default host-only network during installation, but if it doesn’t you can add one pretty easily. Go to VirtualBox’s application menu and select Preferences – Network. Here you’ll be able to add a host-only network if it doesn’t exist… If desired, you can also configure multiple host-only networks allowing you to create various scenarios where VMs are networked together or fenced-off from each other.

 

virtualbox networking

VirtualBox Host-Only Networks

 

Below are my VirtualBox network settings. As you’ll notice, I’ve enabled the DHCP server on the host-only network albeit unnecessary. I just wanted to show that you can have it enabled and still assign your VMs static IP addresses.

 

host-only adapter          host-only dhcp

VirtualBox Host-Only Network Adapter and DHCP Server Settings

 

Step 2: VirtualBox VM Network Adapter Settings

In this scenario, your VMs will need two network adapters – a NAT adapter for Internet connectivity; and a Host-Only adapter for the network we configured in Step 1. When you create your new VM settings, VirtualBox should add a NAT adapter by default so you shouldn’t have to do anything here.

 

vm nat adapter

Virtual Machine Settings: NAT Network Adapter

 

Enable the host-only adapter by clicking the Enable Network Adapter checkbox on the Adapter 2 tab. Select Host-Only Adapter from the Attached To combo box and select the host-only network you created in Step 1 in the Name combo box. If you only have one host-only network, this will already be selected… Please note this is a single VM configuration. You’ll have to add the host-only adapter to all of your participating VMs.

 

vm host-only adapter

Virtual Machine Settings: Host-Only Network Adapter

 

Step 3 (Windows): Static IP Configuration (Host-Only Network)

Setting up a static IP on a Windows machine is fairly straightforward as the IPV4 settings dialog should be familiar to almost everyone… Below I’ve configured the Default Gateway and Preferred DNS Server boxes to contain the host-only network adapter address configured in Step 1 (192.168.56.1). I’ve assigned a static IP similar to the default gateway incrementing the address’s last number by one. There is no configuration necessary for the NAT network adapter. Below you’ll also notice a screen shot of my Windows network properties. For reference purposes only, I renamed the machine’s networks to reflect the names of the VirtualBox network adapters.

 

windows ip settings          windows network settings

Windows IPV4 Configuration Settings

 

Step 3 (Ubuntu): Static IP Configuration (Host-Only Network)

Configuring a static IP in Ubuntu (I’m using the latest version available, 12.04) is just as straightforward as the Windows configuration. Again, you’ll only need to configure the host-only network adapter – the settings are exactly the same as they were for the Windows machine except for the IP address. Both the Default Gateway and Preferred DNS Server should be set to the host-only network adapter address configured in Step 1 (192.168.56.1). Just choose your static IP and you’re finished. As before, for reference purposes I’ve renamed each of my Ubuntu networks to match the VirtualBox network adapter name.

 

ubuntu ipv4 settings          ubuntu network settings

Ubuntu IPV4 Configuration Settings

 

All in all, the static IP configuration is really easy. Nothing more to say on this topic…

‘You Gotta Have a Fiddler in the Band’

12.21.2011 08:18 by kbeckman | Comments

Due to popular request, I wanted to add a quick post with instructions on using Fiddler as a web proxy allowing you to capture all your browser traffic from Firefox on OSX (or any other platform for that matter). I posted a while back on how to setup a Windows SQL Server virtual machine using VirtualBox as the DB server for our Rails project. Now I’m going to take that a step further and put the VM to more good use. Anyone who has tried to run Wireshark to filter HTTP traffic will appreciate this…

 

Fiddler Setup

Unfortunately, Fiddler is a Windows-only installation and won’t run natively on OSX... So after installing it on your Windows VM, configure the following to allow remote connections. This will expose a service on port 8888 on the VM allowing you to setup a Firefox proxy on your host OS.

 

Fiddler

 

Firefox Setup on OSX

I like using Firefox over Chrome on OSX for capturing HTTP traffic because Firefox allows you to configure a proxy for browser traffic only. The Chrome configuration forces you to proxy your system’s network connection. It’s not a big deal, but if you proxy traffic from your network connection rather than just your browser, you might catch a lot of unwanted traffic from other connected applications. I guess it just depends where you want to do your filtering…

 

It’s a simple configuration in Firefox. Just go to the Advanced tab of your Firefox preferences and click the Settings button under the Connection section. Edit the Manual proxy configuration to point at your Fiddler service running on the Windows VM. A quick note here if you didn’t read my VirtualBox setup post. I have two network connections configured on the VM – a NAT network adapter for regular Internet traffic and a Host-Only adapter for database traffic, etc. The IP configured below in the Manual proxy configuration is the Host-Only adapter’s IP address.

 

Firefox1   Firefox2

 

And that’s it! Happy Fiddling!

Just a quick reminder to un-proxy your Firefox traffic when you’re done because the active Fiddler service is required. Your Internet connection will be disabled as soon as you shut down Fiddler if you don’t.

VirtualBox: Getting Around an Absence of Domain

11.06.2011 00:56 by kbeckman | Comments

UPDATE: The contents of this post are a hack for static IP addresses… I’ve posted a better way here.

 

A while back, I wrote about the benefits of using cross-platform tools (when available) for software development. The Bottom Line: it allows developers to more easily make the transition to other platforms and new languages. The practice enables developers to use tools they are already familiar with without having to assume the overhead of learning a completely new set of tools along with the new platform or language. So in the spirit of drinking my own Kool-Aid, I’ve recently made the transition from VMWare Fusion to VirtualBox to power my back end SQL Server VM required for our Rails project. Following are the setup details in case anyone else out there is looking to do the same…

 

A Little Development Environment Background…

My current client is a typical large enterprise with almost all-things Microsoft. Recently, things have changed a bit with our R&D team because we’ve introduced Ruby / Rails development on Mac Book Pro laptops running OSX Snow Leopard. As is the case with many slow-to-adopt IT departments, our Mac Book development machines are either not required or are not allowed to participate in the corporate domain. It seems to me to be either a fear of the unknown or an unwillingness to support something outside of the status quo – I think that answer will change depending on who ask. Reasons aside, local development must mimic the production environment as close as possible to catch any configuration surprises early in the development process.

 

Step 1: Server Internet Connectivity

Internet connectivity is the easy part… Unless you use some weird, custom setup during your VM build-out, VirtualBox will add a default NAT network adapter to ensure the VM has an active Internet connection. No need for further configuration here.

 

NAT adapter

Virtual Machine Settings: NAT Network Adapter

 

Step 2: Pseudo-Static IP Setup to Replace NetBIOS

As I mentioned earlier in this post, our development machines are not part of the corporate domain. The same is true with our development VMs even though they are Windows servers. Joining a server or development machine to the corporate domain is the only way I know of to be able to assign it a static IP address. It’s also the only way I know of to use NetBIOS to connect to the machine using it’s assigned machine name rather than using a static IP address.

 

Absence of a corporate domain is obviously a problem here… To work around it, we need some VirtualBox customization. I found a solution in adding a second VM network adapter: a Host-Only Network Adapter. It is possible to setup your own local domain using the Windows Server 2008 R2 DNS Server Role. But for a single-server environment, I didn’t want the hassle of setting up and maintaining my own local domain. So I set up the Host-Only adapter to use DHCP. You’ll see from the screenshots below that my DHCP settings provide for a relatively wide range of IP address settings, but the nice thing about VirtualBox’s DHCP server is that it will always assign the first sequentially available IP address to every VM you bring online. In other words, my single VM instance always gets the IP address listed in my DHCP server’s “Lower Address Bound” setting. Bingo! I always get the following IP address for my SQL Server VM: 192.198.56.2.

 

To change VirtualBox’s DHCP server settings, go to “Preferences” in the application settings. From the “Network” tab, use the edit button (screwdriver icon) to modify the network settings for the host-only network.

 

preferences

VirtualBox Host-Only Network Listing

 

The DHCP Server will probably not be enabled by default, so be sure to configure it accordingly.

 

network adapter settings          dhcp server settings

VirtualBox Host-Only Network Adapter and DHCP Server Settings

 

Now you’re done with the VirtualBox server settings. The last part of the configuration is to add a second host-only network adapter to the SQL Server VM. After adding the second network adapter, be sure to assign it to the same virtual network that was configured for DHCP access. If you’ve only configured a single VirtualBox host-only network, you don’t need to worry about assignment as it will default correctly.

 

Host-Only adapter

Virtual Machine Settings: Host-Only Network Adapter

 

Problem Solved!

And that’s it. You’re now ready for server and database connectivity via IP address resolution. Below is a screenshot of the VM’s IP configuration. Notice the dual network adapters.

 

ipconfig

Virtual Machine IP Configuration

 

References

Configure DiffMerge for Your Git DiffTool

07.08.2011 22:57 by kbeckman | Comments

A while ago I wrote about the importance of choosing cross-platform development tools when available. As a consultant, being able to work proficiently with multiple platforms provides flexibility in the projects you can choose from. The ability to take a familiar tool with you from one platform to another saves you from having to ramp up on an alternative you’re not used to. It also adds value for your clients allowing you to concentrate precious time on ramping up on the more important domain knowledge specific to the project.

 

DiffMerge is an excellent diff tool that works on all platforms and one I’ve recently adopted as my primary diff tool. Git is easily the best source control system under the sun and also works across platforms. Here I’ll show you how to use them in tandem across Windows, Mac OS X and Ubuntu Linux. Please keep in mind that the actual DiffMerge and Git installation locations may differ on your system requiring a slight tweak or two to the commands below.

 

Mac OS X

Configuring Git to use DiffMerge in Mac OS X is probably the easiest configuration of the three platforms. After installing Git and DiffMerge, you’ll need to run the following Git configuration commands to get up and running.

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "/Applications/DiffMerge.app/Contents/MacOS/diffmerge --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"
git config --global mergetool.keepBackup false

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/Applications/DiffMerge.app/Contents/MacOS/diffmerge \$LOCAL \$REMOTE"

 

Ubuntu Linux

Configuring Git to use DiffMerge on Ubuntu Linux is very similar to the Mac OS X configuration. The commands are the same here except for the DiffMerge installation location.

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "/usr/bin/diffmerge --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"
git config --global mergetool.keepBackup false

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/usr/bin/diffmerge \$LOCAL \$REMOTE"

 

Windows

The windows configuration is the most difficult of the three configurations. I wouldn’t even bother trying to set this up using the Git configuration commands through the Git Bash command prompt. You’ll save yourself A LOT of time by just hacking your .gitconfig file manually to point to the external Git scripts required to launch DiffMerge. Here’s what you need to do…

 

1) Create a directory to hold your external Git command scripts – this can be anywhere. I use the ../cmd directory that is created when you install Git on Windows – C:\Program Files (x86)\Git\cmd.

2) Add your Git external command directory to your PATH System Environment Variable.

Right Click ‘Computer’ and choose “Properties” from the context menu.

Click “Advanced System Settings”.

Click the “Environment Variables” button to launch the necessary modal dialog.

 

image

 

3) Drop the following scripts into your Git external commands directory.

git-difftool-diffmerge-wrapper.sh

# place this file in the Windows Git installation directory /cmd folder
# be sure to add the ../cmd folder to the Path environment variable

# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode

"C:/Program Files (x86)/SourceGear/DiffMerge/DiffMerge.exe" "$1" "$2" | cat

 

git-mergetool-diffmerge-wrapper.sh

# place this file in the Windows Git installation directory /cmd folder
# be sure to add the ../cmd folder to the Path environment variable

# passing the following parameters to mergetool:
# local base remote merge_result

"C:/Program Files (x86)/SourceGear/DiffMerge/DiffMerge.exe" "$1" "$2" "$3" --result="$4" --title1="Mine" --title2="Merge" --title3="Theirs"

 

4) Edit your global .gitconfig file to include the necessary difftool and mergetool commands.

[merge]
    tool = diffmerge
[diff]
    tool = diffmerge
[mergetool]
    keepBackup = false
[mergetool "diffmerge"]
    cmd = git-mergetool-diffmerge-wrapper.sh "$LOCAL" "$BASE" "$REMOTE" "$MERGED"
[difftool "diffmerge"]
    cmd = git-difftool-diffmerge-wrapper.sh "$LOCAL" "$REMOTE"

 

Launch DiffMerge

git mergetool
git difftool

 

Resources

http://adventuresincoding.com/2010/04/how-to-setup-git-to-use-diffmerge

http://support.sourcegear.com/viewtopic.php?f=33&t=13195

http://www.davesquared.net/2009/02/setting-up-diff-and-merge-tools-for-git.html

http://www.davesquared.net/2009/05/setting-up-git-difftool-on-windows.html

Common Git Usage

06.28.2011 09:18 by kbeckman | Comments

Before you read much farther here, I just want to let you know that this post is mostly for my own personal use as a consolidation of scattered Git notes I’ve been keeping around… It’s my Git crib sheet describing some how-to’s and the why’s behind them. I’ll probably be updating this post on occasion as I find or figure out new Git goodies. Hopefully C4SC readers can find a gem or two in here.

 

Create a New Local Branch

The following command creates a new local branch in your Git repository. The workflow first switches to the local master branch and pulls all of the latest remote changes. The --rebase argument tells Git not to merge the remote master branch into your local master branch, but to replay all of the most recent individual remote commits on local master instead. Git will replay all of the commits in succession since your local master’s HEAD commit rather than forcing you to perform a single commit for the whole merge. The checkout -b command tells Git to create a new local branch and then check it out.

git checkout master
git pull --rebase
git checkout -b <new-branch-name>

Pull Remote Changes Locally

Most of the commands here look very similar to the example above, but we’re assuming an existing child branch here (off master) rather than needing to create one. The additional step here is that after updating master, we update the child branch as well. After switching to the child branch, calling git rebase master replays all of the commits to master (since your child branch’s HEAD commit) onto your child branch. Just as in the example before, the rebase command forces Git to avoid a single merge commit in favor of replaying all of the individual commits in master onto the child branch.

git checkout master
git pull --rebase
git checkout <child-branch>
git rebase master

 

Merge Child Branch Into Master (and Push It to Origin Master)

Before merging a child branch into master, you’ll want to repeat all 4 commands in “Pull Remote Changes Locally” prior to the merge to ensure you have the latest remote changes before merging any code. If you don’t do this, Git will force you to do it anyway… One more reminder – after getting all the remote changes and performing your merge, run your unit test suite locally to make sure nothing is broken. If you don’t do this, you deserve whatever hazing you get if you break the build.

A quick note here (because this is different than the last two steps)… When merging a child branch into master, since you updated your child branch by rebasing from master in the previous step “Pull Remote Changes Locally”, your child branch changes are fast-forwarded onto the master branch HEAD commit. This ensures that only the changes you performed on the child branch are committed to master just as if they had been done on master rather than the child branch. This keeps all of your local branch information from showing up in your remote repository (git push origin master). Why? I may have performed LOTS of commits and applied LOTS of refactorings to my child branch to get it ready to merge – I may have even used several local branches to accomplish the task. I don’t want to sei all of the local branch information in the remote repository. This is beneficial for the overall upkeep of the project because other developers may have local branches named the same as yours causing serious conflicts if all the local branch information appeared in the remote repository. This makes everyone’s job easier to integrate local changes when pulling remote changes.

git checkout master
git merge <child-branch>
git push origin master

 

Undelete a Deleted [Staged] File and Roll It Back

As far as Git is concerned, this is one of the easiest operations to perform. However, if you’re used to a nice right-click context menu option in Visual Studio or some other IDE, it might be one of the hardest to remember… In your head you’re saying “Undo Changes” or “Revert File”, but Git requires a two-step approach. The workflow here is to unstage the deleted file and then get the latest repository version by checking it out.

git reset HEAD /some-directory/file.txt
git checkout /some-directory/file.txt

 

Rename / Move a File

The following command is a shortcut. If you chose not to do it this way, you’ll end up with a few more calls. This prevents you from having to add the new file (along with tracking it) and remove the old file in separate commands. If you haven’t yet begun tracking the file you want to move, just remove the git mv command in favor of just mv since Git doesn’t know about it yet.

git mv <file-name> /new-directory/new-file-name

 

Resources

Pro Git -- Scott Chacon (ProGit.org)

Git for Subversion Users – Derrick Bailey (Code-Magazine)