Alright, the title is a little bit grand. In any case, let it be known that I pretty much hate darwinports and fink, as something always seems to go wrong when I try to use them. [Although, they are very necessary for development in OSX — and I am certain I won’t be able to get by without them]

In this tutorial I will briefly explain how to tightly integrate VirtualBox into OSX as an Ubuntu Host. [This would probably work with other Virtual Machines, but I’ll use VirtualBox as my example because it’s free.

  1. First, Download and install VirtualBox from Virtualbox.org
  2. Second, download and a recent 32-bit ubuntu ISO from ubuntu.org
  3. Install Ubuntu under VirtualBox, remember the name of your virtualbox, you will need it later — there are many tutorials around, and for anyone who wants ubuntu running in virtualbox, this should be pretty straight forward
  4. On ubuntu install some key packages:
    apt-get install ssh smbfs
  5. Also create a .ssh directory inside of your home directory (i.e.
    mkdir ~/.ssh

    )

  6. Shut down your Ubuntu VirtualBox
  7. Open up a Terminal (on your OSX box)
    cd /Applications/VirtualBox.app/Contents/MacOS
  8. Add this script as portfw.rb
    sudo bash # (you'll need to be root to create this script)
    #!/usr/bin/ruby
    #
    if(ARGV.size < 4)
            print "Ussage: ruby portfw.rb    \n"
            exit()
    end
    print "Adding Portforwarding from #{ARGV[2]} to #{ARGV[3]} named (#{ARGV[1]} on VBOX:#{ARGV[0]}\n"
    print `./VBoxManage setextradata "#{ARGV[0]}" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/#{ARGV[1]}/HostPort" #{ARGV[2]}`
    print `./VBoxManage setextradata "#{ARGV[0]}" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/#{ARGV[1]}/GuestPort" #{ARGV[3]}`
    print `./VBoxManage setextradata "#{ARGV[0]}" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/#{ARGV[1]}/Protocol" TCP`
  9. We’ll want to forward some of our local ports to VirtualBox for testing
    exit # to become non root again -- so you can see your virtualbox
    
    # ruby portfw.rb
    ruby portfw.rb ubuntu ssh 2222 22
    # for Rails Testing
    ruby portfw.rb ubuntu rails 3001 3000
    # for HTTP
    ruby portfw.rb ubuntu http 8080 80
  10. You can restart you virtual box now if you want.
  11. Now, let’s make it so we can mount our OSX drive from the virtualbox, without doing anything fancy — yes virtualbox has a virtual-drive, but it is horrible. (or was at the time of this writing). First, let’s add a local IP alias, so we can have a constant pointer to our SMB Share.
    sudo vim /etc/rc.local
    /sbin/ifconfig en0 alias 10.1.1.10 netmask 255.255.255.255
  12. You can run this file by typing:
    source /etc/rc.local
  13. Now you want to make OSX share Samba — this is somewhat insecure, and you should probably tweak your firewall to prevent non-local connections. [I haven’t looked this up yet].
    1. In OSX, goto the sharing preference pane
    2. Check the box marked File Sharing
    3. Add a share (I share my homedirectory /Users/bcx )
    4. In the left panel make sure your User has Read & Write access to the directory also make sure Everyone has “no Access” and no one else has access besides you
    5. Under the [Options..] tab, check the box “Share files and folders using SMB” and “On” for next to your username
    6. Exit out of sharing.
  14. I ran into a few problems with SAMBA and Subversion. Which were causing some file permissions errors, and completely broke SVN over SAMBA. A combination of adding three configuration options to your /etc/smb.conf on OSX fixed the errors I got. Under globals you will need to add
    [Globals]
        force create mode = 0777
        force directory mode = 0777
        delete readonly = yes
        # ....
    

    Once this is done you need to restart samba:

    sudo service smbd stop
    sudo service nmbd stop
    sudo service smbd start
    sudo service nmbd start
    
  15. The next step is to make it easy to log into your Ubuntu install. If you don’t already have one create a file called .bash_profile and .bashrc.
    vim ~/.bash_profile
    #~/.bash_profile
    source ~/.bashrc

    Create an alias for SSHing locally

    vim ~/.bashrc
    #~/.bashrc
    alias sshl='ssh -p 2222 localhost'
  16. If you don’t have a public key, or don’t know what they are, you’ll need to generate one
    # type this in the terminal.  Leave the pass phrase empty, and use the default values
    ssh-keygen
  17. Next copy your SSH key to your local virtual box (this one liner will do it)
    cat ~/.ssh/id_rsa.pub | sshl "cat - >> ~/.ssh/authorized_keys"
  18. Now typing
    sshl

    on the command line will ssh into your virtual ubuntu instance. We are almost there.

  19. log into your virtual box. IF for some reason RSA authentication isn’t working take a look at /etc/ssh_config and make sure that
    RSAAuthentication = yes
    PubkeyAuthentication = yes

    are set

  20. You are almost there, next step — setup samba
    mkdir /mnt/smb
    sudo vim /etc/fstab
    # add the line:
    //10.1.1.10/[osx_username] /mnt/smb smbfs user=[osx_username],uid=[ubuntu_username],password=[osx_password]  0 0
  21. Finally mount your OSX home directory inside of ubuntu
    sudo mount /mnt/smb

    And then make an easy to access symlink to it

    ln -s /mnt/smb ~/osx
  22. That should do it!  Now you can do cool things like edit files inside your home directory using textmate, and run them on ubuntu.
Advertisements