Monday, November 5, 2012

OSX, VirtualBox, NAT and static DHCP



Update Oct, 2014: the guide was slightly modified to support new OSX 10.10 Yosemite. The changes are minor, so I believe it still works for the previous versions, but I didn’t check it.

There's an issue that VirtualBox doesn't allow to configure VMs' static IP-addresses by, for example, their MAC-addresses.

In general the solution is quite simple:
  1. run DHCP server on the host on the VM-ethernet interface (vboxnet0);
  2. allow access from VMs to the external network via NAT.
Step by step solution
en0 - ethernet interface
en1 - WiFi interface
192.168.56.0/24 - internal virtual network for VMs
  1. in VirtualBox disable DHCP-server;
  2. for each VM set network adapter to Host-only;
  3. install (I used brew) dnsmasq;
  4. in dnsmasq conf-file (/usr/local/etc/dnsmasq.conf) configure DHCP-settings;
    • set the property dhcp-leasefile=/usr/local/etc/dnsmasq.leases;
    • interface listen to (interface=vboxnet0);
    • IP-range for dynamic IP-addresses (dhcp-range=...);
    • static IPs for particular MAC-addresses(dhcp-host=...);
  5. configure dnsmasq to start as daemon;
  6. enable port forwarding
    • sudo sysctl -w net.inet.ip.forwarding=1
  7. in /etc/pf.conf add line after nat-anchor "com.apple/*"
    • nat on { en0 en1 } from 192.168.56.0/24 to any -> { (en0) (en1) }
  8. load rules into pf
    • pfctl -F all -f /etc/pf.conf
  9. enable pf with command
    • pfctl -e

Some additional hints

IP-forwarting can be enabled permanently. For this add into /etc/sysctl.conf (create this file if it doesn't exist):
  • net.inet.ip.forwarding=1
To enable pf on boot open file /System/Library/LaunchDaemons/com.apple.pfctl.plist and add -e to ProgramArguments:
        <key>ProgramArguments</key>
        <array>
                <string>pfctl</string>
                <string>-f</string>
                <string>/etc/pf.conf</string>
                <string>-e</string>
        </array>

To run dnsmasq as daemon create file /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist with this content:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.mxcl.dnsmasq</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/sbin/dnsmasq</string>
      <string>--keep-in-foreground</string>
    </array>
    <key>KeepAlive</key>
    <true/>
  </dict>
</plist>

Then register it:
  • sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

The virtual network interface vboxnet0 is created only when you run one of the VMs. To create it on boot I wrote a script and place it in ~/.scripts/vboxnet0.sh:
#!/bin/bash

VBoxManage list hostonlyifs > /dev/null
VBoxManage hostonlyif ipconfig vboxnet0 --ip 192.168.56.1 > /dev/null

Don't forget to execute chmod a+x ~/.scripts/vboxnet0.sh

Then I've created ~/Library/LaunchAgents/virtualbox.vboxnet0.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>virtualbox.vboxnet0</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/Oleg/.scripts/vboxnet0.sh</string>
    </array>
    <key>RunAtLoad</key>
        <true/>
  </dict>
</plist>
*Don't forget to change the username to yours :-)

And registered it:
  • launchctl load ~/Library/LaunchAgents/virtualbox.vboxnet0.plist
Thus dnsmasq doesn't crash with the message that there's no interface vboxnet0.

Tuesday, October 30, 2012

OSX: Russian "Ё" in the PC keyboard layout

The Windows PC keyboard layout is familiar for the most of us. There's one within Lion and Mountain Lion, but there's an issue with the Russian layout - the letter "Ё" is not on it's standard place under "~" button. I was going to write how to create the custom keyboard layout, but there's a lot of manuals about it already. So I've decided to share the my one: Russian - PC - yo.zip
This archive contains two files:
  • Russian - PC - yo.keylayout
  • Russian - PC - yo.icns
Just put them into "/Library/Keyboard Layouts/" directory and reboot. 

Friday, October 26, 2012

Homebrew and GUI applications



Homebrew is a package manager where there are a lot of ports of popular Linux utilities.
It installs all software into /usr/local/bin directory. And to use them by default this directory should be before the standard /usr/bin in the PATH environment variable.
For the Terminal it's simple. Just add into ~/.bash_profile the following line:
  • PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:$PATH
But it doesn't work for GUI applications that use these utilities (e. g. IDE uses Maven and SVN).
To fix this I've changed PATH in the two places.

The first place is /etc/launchd.conf:
  • setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The second place is /etc/paths:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Reboot after this changes are made.
With this changes almost all applications work fine. "Almost" is because there's one specific for my project, it doesn't see SVN 1.7. As workaround I run it from the Terminal. 

Wednesday, October 24, 2012

OSX software for common task








When start using a new operating system, it's painful to do usual tasks, because the software is unknown. For make this process easier I want to publish the list of software I'm using (or used).

Almost all software is free. For the paid software there are free analogues.

Terminal

Nice terminal with a lot of preferences and features. I like splitting windows vertically and saving workspaces.

File Manager 

muCommander
I used it initially. But it's buggy, so I've moved to the next one.

ForkLift2 ($19.99) (AppStore)
I was lucky to get it when it was free. I like how it stores bookmarks for the remote hosts, how it connects to my home NAS with WebDav and several other features.

Text editor

TextWrangler (AppStore)
With default TextEdit it's hard for me to edit XML and properties filed. So I've found this one.

Package manager

Homebrew
The most of Linux utilities are available with "brew install [utility]". For example, "brew install wget" for wget, which isn't available for OSX by default.

VNC-client

VNC Viewer
I like how it scales the remote desktop view on the window size changing;

Screen Sharing (/System/Library/CoreServices/Screen Sharing.app)
I've just found it. The first impression is very good.

OpenVPN client

Tunnelblick
It works. But it replaces the default DNS servers with ones it gets from OpenVPN server. If these servers are down, nothing works :-). So I've moved to the next one.

Viscosity ($9.99)
I didn't notice any problems while using it.

Window manager

Scaling the window to full screen with double-click on its header + several other nice features.

Application launcher

Launching applications with "Control+Space" and first letter of the application.

I haven't used it. Other OSX users say that it's almost the same as Quicksilver. I put it here, because it's available in AppStore.

Notification center

Growl ($3.99) (AppStore)
Managing notifications from different application in the one unified way.

Context manager

ControlPlane
The context is a set of preferences and/or actions that depends on the environment (office, home, etc.). This application allows to change them automatically basing on the rules. For example, it changes my default printer when I at home or at work. Or, when I'm at home it mounts my NAS volumes automatically.

The first note

I've created this blog to share my experience and to get some feedback.