Friday, September 08, 2006

SSH, HTTP And SAMBA Tinkering With Parrallels

So I know what you're thinking . . . you're thinking I have a new intel mac, and I bought a copy of Parallels (offsite) and installed Ubuntu 6.06 Dapper Drake into a virtual machine, but now that I've combined these two majestic operating systems how do I go about sharing files between the two?

Well I'll tell you how. I was hoping that parallels was using something like chroot to partition the drive, mount it with the requested file system and then virtualize the OS in a jail. This is, rather unfortunately not the case. What parallels actually does is creates an 8.4 GB file in the /users/<username>/Library/Parallels/<name of virtual machine>/ folder named <name of virtual machine>.hdd. Crazy.

So there is no way you're going to copy and paste files in and out of the jail locally, not even with the terminal and the old standard cp command. So how do I share or access files in between Mac OS X and Ubuntu? Well there are three ways depending on what you want to share or access.

1. SSH

If you actually want to move files from Ubuntu to OS X, or from OS X to Ubuntu you'll need to do it through SSH's SFTP program. This is actually relatively easy to set up, though my set up will require that you have a router (I think you can get around this by editing the hosts files in Ubuntu and OS X to resolve to the proper IP without any routing, but I havn't tested that).

First you'll need to bring down the active network device. If you are connected to your network via a wired ethernet connection, your primary connection device is most likely eth0, if connected by your airport card it is most likely ath0 (though possibly eth1, and definitely eth1 if you're still running Breezy Badger).

To bring down the wired connection:

shell~$ sudo ifdown eth0

To bring down the wireless connection:

shell~$ sudo ifdown ath0

Then We're going to set Ubuntu up with a static IP. Edit the /etc/network/interfaces file:

shell~$ sudo nano /etc/network/interfaces

For a wired connection, find the section for eth0, should look something like this:

auto eth0
iface eth0 inet dhcp

We need to modify this to give it a static IP:

auto eth0
iface eth0 inet static
# this defines the interface eth0 to have a static IP
# this defines the static IP of the interface
# this defines the default gateway (its the IP of your router)
# this is the subnet mask of the device (probably
# if your setup is so advanced that it isn't you
# then you probably know what this value is anyway
# if your netmask is, your broadcast is the first
# 3 values of the IP address of the router followed by
# 255, X.X.X.255 if its not this, see the above netmask comment

For a wireless interface you'll need to find your wireless section, which should look something like this:

auto ath0
iface ath0 inet dhcp

And change it to have a static IP:

auto ath0
iface ath0 inet static
# This defines the interface ath0 to have a static IP
# This defines the static IP of the interface ath0
# This defines the default gateway of ath0 (Router IP)
# Subnet mask of ath0
# Broadcast address of ath0
wireless-essid BEER
# Name of Wireless Network Connected to
wireless-mode managed
# mode of wireless network connected to
wireless-key H3X1SFUN
# WEP-key for wireless Network connection (or ASCII key)

Then bring the connection back up with the new configuration.

For a wired connection:

shell~$ sudo ifup eth0

For a wireless connection:

shell~$ sudo ifup ath0

To see if your static IP is working properly you'll need to perform a couple of tests. First check to make sure the proper device is representing the ip.

For a wired connection:

shell~$ sudo ifconfig eth0

For a wireless connection:

shell~$ sudo ifconfig ath0

A working device will output the values that you set in the interfaces file for each of the address, netmask and broadcast fields. Next you'll want to ping the router to make sure you're connected to the LAN:

shell~$ ping <router ip>

Then ping a site on the internet to make sure you have proper WAN access (assuming you have WAN access at all):

shell~$ ping

Awesome, now we have a static ip on our ubuntu OS running inside of the parallels. To SSH to the ubuntu OS, you'll next have to install SSH on ubuntu:

shell~$ sudo aptitude install ssh

After the installation, make sure the ssh daemon is up and running:

shell~$ ps aux | grep sshd

At this point you should be able to use SFTP from Mac OS X to Ubuntu, in order to put and get files. To make sure, switch over to your OS X partition and open a terminal:

shell~$ sftp <ubuntu-ip>
sftp-> get
sftp-> put
sftp-> exit

To be thoughrough you should also start up the ssh daemon on OS X. To do this go to System Preferences -> Sharing and turn on personal file sharing. To test this switch back to your Ubuntu OS and run the same commands above only using the OS X ip instead of the ubuntu ip. If you don' know the OS X ip, just switch back to OS X and use ifconfig to get it:

shell~$ ifconfig

That about wraps it up for SSH.

2. HTTP Sharing

For some stuff SSH just doesn't make sense. For example if for some reason you want to listen to your music, or watch your movies from the OS X area in the Ubuntu area, or vice versa, you just don't want to have to copy those large files from one OS to the other, especially with the movies. So for this purpose we're going to set up HTTP servers to stream media from one OS to the other. Incidentally, as a cool side-effect of this you'll be able to stream this to any machine on your LAN, virtual or otherwise. Though this is because anyone on the LAN will be able to stream or download any file that you share via HTTP, and therefore if you want to securely share files, make sure you do it with sftp, not http.

Before we continue, I should point out that I am assuming you are on a LAN for this, and understand that if you stream copyrighted content (even legally aquired) over the WAN you are in violation of several laws, however bogus they may be.

Onward . . . you will most likely be streaming from OS X into Ubuntu, so we'll start there. To turn on your apache server just go to System Preferences -> Sharing and turn on personal web sharing. With one click of a button apache is up and running (though it apache 1.3 unfortunately) and they even poke a hole in the firewall on port 80 and 443 for you as well. Lets all just take a minute here to thank the folks at apple for being so friggin' awesome . . .

The document root for the apache server is: /Library/WebServer/Documents/ and you'll need to put all of your files into this folder in order to serve them. But of course we don't want to take up unnecissary space so just soft link the files or folders you want into the document root.

For example, my movies are in /Volumes/FATMATT/Movies, so . . .

shell~$ ln -s /Volumes/FATMATT/Movies /Library/WebServer/Documents/

Make sure the permissions are ok for the www user to read the files that you want to stream:

shell~$ ls -l /Library/WebServer/Documents/Movies/
shell~$ chmod -R 755 /Library/WebServer/Documents/Movies

Now we need to switch to Ubuntu and use a web browser to browse to the folder that we just set up, found at <ip of OS X>/Movies. If you want to download the files shared over HTTP, you can just browse to the folder and click on the file to download and save it. If you want to stream the files however (if they are media files) you'll need to download and install VLC. To install VLC in Ubuntu you'll first need to edit the /etc/apt/sources.list file:

shell~$ sudo nano /etc/apt/sources.list

And uncomment all of the universal repositories. Then perform and apt update, and install the program:

shell~$ sudo aptitude update
shell~$ sudo aptitude install vlc

To stream with VLC, browse to the shared folder and right click on the file, copy the link location. Then open VLC, go to open file, and paste the link location into the MRL text bar. And now you stream files to your Ubuntu OS wihtout having to take up vital disk space on that 8.4 GB file system.

To share files via HTTP from Ubuntu to OS X we'll need to set up an apache server on the Ubuntu OS. To do this install apache2:

shell~$ sudo aptitude install apache2-mpm-prefork

This will take a little while, but while it goes just sit back and enjoy the glory of apt, and take a moment to thank the developers at debian for allowing us to never have to hand-resolve dependancies ever again. Once this has finished apache should be running, but make sure:

shell~$ ps aux | grep apache2

The document root for apache in ubuntu is /var/www/, so linkng files in will look like this:

shell~$ ln -s /target/directory/ /var/www/

Once you've done that switch to OS X and browse to the folder: <ip address of ubuntu OS>/<folder name>. You can again download the files by clicking on them, or stream them in VLC using the same technique discussed above. To get VLC for mac go to their website (offsite) and download it.


The third option will allow you to read and write files on a samba share, without having to move the file from one to the other. This option will also allow you to copy files in and out of your samba shares on either OS and you'll be able to move files in between the two using this.

Most likely you'll be accessing the files on OS X with Ubuntu, so first we'll go through how to mount a samba share in Ubuntu. First you'll need to initialize the Samba server on OS X. You do this by going to System Preferences -> Sharing, and then you turn on Windows File Sharing. Doing this will prompt you for a user name and password, and OS X will conveniently let you know that the way Samba stores your password is less secure than the way the OS X stores your password, but it is still sufficiently secure for a LAN. Once you've done this your home directory in OS X will be up on a network Samba share, you can verify this in the finder, in the network section.

To mount the share in Dapper Drake go to Places -> Network Servers and then click on the share that has the name of your OS X box. My box is called MINIMATT, so I go to Places -> Network Servers -> MINIMATT to access my Samba share. If you need to configure additional samba shares on OS X, please refer to my previous post Setting Up External Hard Drive Samba Shares On OS X.

There is no real need to configure a samba server on your Ubuntu machine, as you can copy and paste what you need into and out of Ubuntu using the OS X samba server share. However to be thorough I should probably put this information up here as well. Unfortunately I don't have the desire to do this at the moment, so at some point in the future I'll put up a post covering how to set up a samba server in Ubuntu.

Sunday, September 03, 2006

Setting up External Hard Drive Samba Shares on OS X

I have a large USB 2.0 external hard drive (300GB) connected to my brand spanking new mac mini, loaded with files that I wanted to be able to access on my Ubuntu box, which is all the way on the other side of the room. Initially I was just unmounting the volume, unplugging it and carrying it all the way accross the room to my Ubuntu machine. Well after doing this once, my arms got tired, and I thought to myself there must be a better way.

My initial thought on this was to create an alias in my home directory, and then open it up for sharing. So to do this I created an alias of the drive in my home directory (right-click on the folder -> create alias of "FATMATT" -> drag the alias into my home directory) and then allow my home directory to be shared with samba (system preferences -> sharing -> windows sharing). This all seemed to work fine, except that linux (and windows for that matter) doesn't know what the alias is.

Well I have a solution for this I thought, I'll just soft link the directory into my home directory, certainly Ubuntu can understand that. So I opened up my trusty terminal window and went to work:

ln -s /Volumes/FATMATT /Users/matt

I go back to my Ubuntu box and it understands the soft link, and I can go into the folder and browse the contents. When I try to open anything in the samba share however, it won't do it. Well it turns out that since the actual files are ouside of the samba share, and also because OS X prevents sharing USB drives over samba shares, you're unable to open the files despite the fact that you can see what files are on the volume. Though I would recommend using soft links instead of aliases whenever possible, as through this process I discovered that a soft link is actually much faster in file browsing than an alias is (especially when using front row).

So I'm left with my final option, the one I didn't want to go with: configure an extra samba share for the USB drive. As OS X is nice enough to come with samba installed and configured for the home directories, I didn't have to install a samba server on the machine (a task that is quite daunting in Linux), all I had to do was reconfigure samba a bit.

In OS X samba's primary configuration is stored in the /etc folder as the file /etc/smb.conf, to add extra samba shares you need to configure this file by hand, so I went once again to my trusty terminal and opened the file with sudo and nano (that's right I use nano . . . just leave it), backing the file up first of course:

sudo cp /etc/smb.conf /etc/smb.conf.bak
sudo nano /etc/smb.conf

Samba's configuration is quite simple and easy to understand, it is comprised of sections denoted by starting tags of the form [start tag]. To define a share you just think of what you want the share to be called and then create a section with that name, for example my drive is called "FATTMATT" so I create the following section with the tag [FATMATT]:

comment = FATMATT
path = /Volumes/FATMATT/
browsable = yes
read only = yes

The configuration options in the section tell samba the following:

comment = FATMATT

This option gives samba the name of the share as it will be displayed to the samba client.

browsable = yes

This value allows the samba client to browse the contents of the folder.

read only = yes

This disallows the ability to write to the share from the client, to allow the share to be written to from a client merely exchange that line with the following line:

read only = no

Once you've added your section you need to make sure of a few things. Check the ownership and permissions of the USB drive to make sure that the user you will be accessing the share as has read permission to the drive (for read only) or read/write permission on the drive (for not read only)

ls -l /Volumes/

Then finally you'll have to reload samba with the new configuration file

sudo killall -HUP smbd


Saturday, September 02, 2006

Firefox CSS Work Around

We all love firefox, right? If your answer to that question is anything other than yes, then please let the door hit you in the ass on your way offsite. Unfortunately I discovered a CSS bug in firefox 1.0, and 1.5, in both the Linux and Mac versions (I assume it is also a bug in Windows firefox, but havn't had the software or desire to test it). Basically the problem is that sometimes (havn't been able to work out the exact circumstances of the bug) if you create a div with a width of 100%, the div itself actually is 100% wide, but all the attributes of the div only span as far as the content of the div. For example, lets say I created a title bar for a page like so:

<div style="float:left; background-color:#FFFFFF; border-bottom:2px solid #EEEEEE; width:100%">Hello World</div>

The background color and bottom border would only be as wide as the text "Hello World" in the div. Kind of a pain. Well as a simple work around to this problem you can add a span or a div with a blank space in it floating on the opposite side of the div. So for example, the above div has float left, so to rectify the problem we could do this:

<div style="float:left; background-color:#FFFFFF; border-bottom:2px solid #EEEEEE; width:100%">Hello World<span style="float:right;"> </span></div>

<!-- or -->

<div style="float:left; background-color:#FFFFFF; border-bottom:2px solid #EEEEEE; width:100%">Hello World<div style="float:right;"> </div></div>

This CSS bug doesn't happen with IE or Safari - I know, it doesn't make sense, a CSS error in firefox that doesn't happen in IE, but its true, I swear - and hopefully the crack firefox crew will get on fixing it and you won't have to use this fix sometime in the very near future.


Welcome to the Code and the Fury, the blog of irrefutable code and philosophy god, Matthew Story. I'll try to keep this place updated as much as possible, as I'm sure I'll have plenty of fans and followers eagerly awaiting new articles with each passing day. In case you didn't read the "about" section, in which case you're totally daft, I'll use this first post to give you all a brief idea of what this blog is all about.

My two principle interests are web programming and philosophy. I am currently employed as a web-programmer and network administrator in Chicago. I graduated from the University of Chicago in June of 2006 with a degree in Philosophy. I know what you're thinking . . . biggest nerd ever . . . well yes, you're right.

So for the Code part of this blog, you'll find nifty web-app hacks up here, and also the occasional networking hack or awesome bash script. On the administrative side of things you should probably know that if you're looking for windows help, you won't find it here. I worked for a couple months as an OS X systems administrator, and in my current position I work on Debian GNU-Linux. On my LAN at home I am currently running a combination of Ubuntu Linux and OS X, and I love playing with them. So up here you'll get linux and OS X hacks, and maybe if i get around to installing it again, we'll get to some FreeBSD hacks later on.

For the Philosophy part of this blog I will be writing predominantly about modern continental philosophy, mostly Heidegger, but also Husserl, Camus, Sartre. Every once in a while I might reach back and pull out some german ideology, but we'll see where that goes.

As an added treat, I will occasionally combine the two parts of the blog into some sort of . . . superblog. I'm a big proponant of Open Source Software, and I have quite a bit to say on the subject. Because of this also you're free to use all the code snippets that appear on this site under the rules of the LGPL v 2.1 (offsite).