iPhone syncing and Linux

Note: installing iPhone updates through VirtualBox is not a good idea. Make sure you have a backup method to restore your phone before diving into this.

I love my iPhone, but I hate that I can’t sync it natively inside Linux. I was never able to get iTunes working running under Wine, so I’ve been forced to create a virtual machine running WinXP to sync it. I don’t like the idea of a VM running in the background, wasting system resources when it’s not being used. I wanted a better solution.

With the help of udev and VirtualBox command-line tools, I was able to come up with something better. Now when I’m ready for bed I plug my iPhone in, VirtualBox automatically boots the VM, and it syncs with iTunes. When I unplug the phone, Windows shuts down automatically. The only time the VM is using system resources is while I’m sleeping, so I don’t care.

I think this is as good as it gets with the iPhone on Linux and the only compromise is the time invested to set it up.

To do this, you’ll first need to install the full version of Sun’s VirtualBox, it includes USB features that are required. It’s free for personal use. Create a new VM with a bare Windows install on it. I didn’t install any additional updates and I disabled Windows Update, since it’s behind a NAT and I won’t be doing anything other than the sync on it. I also disabled a bunch of unnecessary services and ran Bootvis to minimize the VM boot time. Then I mapped a network drive to my music folder (on the host system) so iTunes will have access to my entire music collection. I logged in with my iTunes account and resubscribed to all my podcasts. At this point it’s not a bad idea to make a copy of the VM in it’s pristine state.

VirtualBox Settings

Shut down Windows. In VirtualBox, right-click the VM and select Preferences. You should tell it to use VT-x/AMD-V (General > Advanced) if you have a relatively modern processor that supports virtualization extensions; it’s not a requirement but will improve performance. The settings you will need are under USB. Check “Enable USB Controller” and “Enable USB 2.0 EHCI Controller”, the click the plus icon to add a filter. While your iPhone is attached to the system, you should see it listed in the dropdown (Apple Inc. iPhone), select it and click OK. This is required so your VM can see the device. You might also want to configure a sound device (PulseAudio) or iTunes will complain.

Now we get to the fun stuff, tweaking the host Linux system and writing BASH scripts. First, let’s create a log file that anyone can write to in case we need to troubleshoot:

$ sudo touch /var/log/iphone.log

$ sudo chmod 777 /var/log/iphone.log

Now, we need to get the internal ID of the VM you created.

$ VBoxManage list vms

You should see the name of the new VM followed by it’s UUID in parenthesis. Copy that UUID, we’ll need it for our first script:

iphone_attach.sh

The two export lines tell the script which X Windows session to attach to. The final command uses the VBoxManage tools to start your VM, log the output, and do it under your own user account (since this will run as root). Obviously you’ll need to change “nathan” to your account name for each instance. You should also replace the UUID with your own. The remove script is not all too different:

iphone_remove.sh

Again, the export commands tell it which X session to use and we use VBoxManage to simulate an ACPI shutdown. You’ll need to replace the UUID with your own and change the user name and home folder path. Make sure these scripts work before you move on to the udev stuff.

grab the serial number

Note, this section was originally created using 0810 (Intrepid) but it broke in Jaunty (0904). I have another way to configure this so it works in Jaunty at the end of this post (it may also work in Interpid).

Now we need to tell Linux to run the above scripts when the device is connected/disconnected from the USB bus. First we need to find the serial number of your iPhone so we can tell udev what to look for.

$ lsusb | grep Apple

This displays the USB bus and device numbers for your phone, note the two 3-digit numbers and plug them into the next command.

Change 004 to the bus number you got in the previous command and change 002 to your device number. This digs into the attributes of your iPhone and displays the serial number which we’ll need later on. Note that you can remove the grep command to view all available attributes of your phone if you’re curious.

udev stuff

Go to the udev rules directory and create a new iphone rule, use a lower number like 98 so it gets processed after the other rules.

The first line here describes what to look for when a device is attached and what to do when it finds your iPhone. The ACTION, SUBSYTEM, and ATTR declarations tell it to watch for a USB device being added with the serial number of your iPhone. SYMLINK tells it to create an entry on the dev file system for it (/dev/iphone); through trial and error I discovered that this is required so we have something to look for when the phone is disconnected. The RUN section tells it to run the iphone_attach.sh script.

The second line is a bit different and maybe not as intuitive. It is watching for the dev file of your iphone to disappear (/dev/iphone) which happens as soon as you unplug it. Then it runs the iphone_remove.sh script. That’s it, you’re done.

other stuff

One annoyance I ran into was Nautilus trying to automount the phone as a camera. I was unable to tell it to ignore the iPhone, so I just turned off automount completely in Nautilus using gconf-editor.

$ gconf-editor

(uncheck: apps > nautilus > preferences > media_automount)

Another idea I had to use the suspend/resume functionality instead of stopping/starting the VM. However, I found that on resume the VM wouldn’t properly sync with the phone until Windows rebooted, even after using the VBoxManage commands to disconnect/reconnect the phone.

In the future, I’d like to look into reducing the XP install size with something like nLite and using dropbox to move the VM to the cloud. The ultimate goal would be to have the ability to sync my phone from home, work, or my laptop while travelling. I have some ideas on the way to approach this but it’s not a burning need at the moment. It would be really cool though.

Jaunty

Unfortunately I noticed the iphone_remove.sh script was no longer triggering after I upgraded to Jaunty. So I figured out another way to configure udev. First, we need to come up with another identifier to use in the udev rule:

udevadm monitor –environment

While that command is running in the background, plug in your phone, one of the lines should look like this:

PRODUCT=5ac/1292/1

Copy this line. I’m not sure if the same ID applies to the original and 3G phones, so run this for yourself. Then we can create the udev rule.

vi /etc/udev/rules.d/98-iphone.rules

You may need to change the Product ID here if it differs from mine, and change the path to your script:

Awesome solution! Finally putting that copy of Vista that came with my machine to use.

One question though – I turned on auto-login, and added iTunes to the startup folder with the “auto-sync” feature turned on.. but how are you getting iTunes to read the networked music folder and maintain an up-to-date library automatically?

Thanks!

n8wood7.9.09 / 8am

notorious, you can use Virtual Box’s Shared Folders feature to map a folder from your host to the guest.

W Alief10.23.09 / 4am

Nice. The scripts work fine when I run them manually, but when I try it automatically with udev, iphone.log shows ‘unknown application’ for both actions.

Thank you, I have just been searching for information approximately this subject for ages and yours is the best I’ve discovered till now. However, what in regards to the bottom line? Are you positive concerning the supply?|What i don’t realize is in reality how you’re not really a lot more well-appreciated than you may be now. You’re so intelligent.

The Lexi-Comp databases cover every topic in the healthcare industry and are available in modules to suit your needs,
from individual fields to a comprehensive package. Mobile lovers are appreciating the
i – Phones as they are the best mobile phones with outstanding features.
Video chat on 3G and 4G networks may even be a reality, which is currently usable solely on Wi – Fi.