Syncing an iPhone with iTunes in a VM

Preface
There is no easy way to sync an iPhone in Linux. There are projects underway to make this happen without jailbreaking, but I haven't tried any. My only real option is to sync to Windows in a VM since I don't run Windows natively anywhere. I did a lot of searching but couldn't find any instructions that worked for me, so once I figured it out I figured I should document what I did.

Prerequisites

 * iPhone 3G[s] (I assume this will work for other generation iPhones and iPods as well, but hasn't been tested)
 * A good working Gentoo system (I'm running 2.6.29-gentoo-r5 kernel at this time)
 * A VT enhanced processor (not needed, but suggested if you're running VMWare or VirtualBox)
 * A legal copy of Windows XP Professional SP3 (others may work, but that's what I tested)
 * Virtualization platform (pick the one you prefer)
 * VMWare Server 2 from portage (I'm running VMware Server 2.0.1 build-156745 at this time)
 * VirtualBox 2 from portage (I'm running VirtualBox 2.2.4 at this time)
 * Content to add to your iPhone (why else would you go through this hassle)

VMWare Server
VMWare Server 2 is now in the unstable tree. I will review the highlights of installing VMWare, but there are more complete guides out there than this. Unless you are running unstable, you will need to unmask vmware-server and vmware-modules.

Next

When I did this, it pulled in app-emulation/vmware-server-2.0.1.156745-r1, app-emulation/vmware-modules-1.0.0.24, sys-fs/fuse-2.7.4. I'm not sure what it's used for but if you don't have FUSE enabled in your kernel, you will have to recompile your kernel to enable this feature.

This can be found in:

Once that's all compiled, you can continue setting up VMWare. You will need to add your username to the vmware group:

You will then need to load a kernel module called "vmci" that VMWare added. If this is the first time you are running VMWare you should load it with modprobe:

To set it so that it's loaded on reboot:

Next, you need to run the setup script. It is pretty safe to accept all of the defaults. You will need to request a free serial number through the VMWare website (if you are installing VMWare for the first time) to complete this process. If you are upgrading from VMWare Server 1, I believe your old serial number will work. Run:

Next, you need to start the VMWare service,

To make VMWare start automatically next time you reboot, run:

VMWare Server 2 now runs as a web service as opposed to VMWare Server 1 which ran under it's own console. To connect, go to https://localhost:8333/ui/ in a web browser. You will have to accept a security exception since it's using a self signed certificate. Login with the username and password you added to the vmware group in the above step. You will now be able to create a new VMWare Guest XP VM. I'm not going to review all the necessary steps for that, but make sure you add a USB interface. Also, make sure you install VMWare Tools into the Guest XP VM. Before you go any further though, complete the section on modifying USBFS.

VMware Workstation
Since VMware Server is going out of support in 2011, I decided to move to VMware Workstation. The downside is you need to pay for this VMware product. I am currently using Version 7. This version also comes with VMware Player 3 which is free and seems to do almost everything Workstation will aside from the more advanced snapshotting. If you have an existing Server VM, you can just import it into Workstation once you have it installed.

I did not install Workstation from portage since there is no port for version 7 yet. I simply downloaded the binary installer from VMware and ran the self installer. Once that is installed, make sure you add yourself to the vmware group:

From there you just run Workstation as an application, start up your VM and sync your phone. The one thing I still haven't gotten working is iOS upgrades. There seems to be something fundamentally incompatible between this process and VMware products.

VirtualBox
Alternatively, you can use VirtualBox instead of VMWare. I chose VMWare mostly because it's what I'm comforable with, but VirtualBox should work as well if you prefer that. One advantage VirtualBox has is the ability to use "Shared Folders" to import music you have on your local machine. There is a good VirtualBox guide on here that goes into the details of how to install, so I will highlight what I used from that.

If you are not running an unstable Gentoo system, you will first have to unmask app-emulation/virtualbox-bin and app-emulation/virtualbox-modules. to do this, add the following to

You will also want to add the following to include the Guest System Tools:

There are some caveats about what version of GCC you are running, so check the VirtualBox tutorial for more information on that. If those notes don't apply, you can then install VirtualBox with the following command:

Once that's done, the next step is to add your username to the "vboxusers" group to allow you to run the application. You can do this with the following command:

You will have to log out and back in for this change to take effect. Once you log back in, you can use the command to verify your username has been added to the appropriate group.

Once that's done, the next step is to load the kernel module. This can be done with the following command:

That will load the module until you reboot. To set it so that vboxdrv is loaded on reboot:

Once all that's done and you complete the section on modifying USBFS, you should be able to type "VirtualBox" on the command line and start up the application. From there you can create a new VM and install XP.

Modifying USBFS
It was at this point that I was getting stuck. I had created a new XP VM and verified that I had USB enabled. I would plug in my iPhone and a KDE window would pop up asking me if I wanted to connect to the camera on my phone. I would cancel that and then try to connect my iPhone under the XP VM. VMWare would think about it for a few seconds and come back with the following error: Remote USB device error: Remote device disconnected: an error occured while sending data.

After doing a lot of searching I pieced together what was going on. It basically comes down to the fact that my VM which I believes runs as a process under my username didn't have sufficient permissions to read/write to the USB filesystem that was being mounted on my iPhone. I found hints in the VirtualBox tutorial on here that pointed me to add the following line to /etc/fstab:

That line essentially changes the group of anything that's plugged into USB from "usb" to "plugdev". In my case "plugdev" is group ID 1008. I think that's pretty uniform for Gentoo, but you should check to be sure

You can see this by looking at the file permissions before and after the change:

I believe you will need to reboot for the changes to take effect though, unless there's a way to restart some service that will rescan these settings.

After that's completed, you also need to add your username to the plugdev group so that you can have write access to the device:

If you don't do this before you reboot as I suggested above, you will need to log out and back in for the change to your group membership to take effect.

you can use the command

to verify your username has been added to the appropriate group.

Automatically Connecting the iPhone in VMWare
At this point, you should be able to boot up your XP VM, connect your iPhone and have VMWare recognize it. You should also be able to select the iPhone from the Devices menu and "Connect" it. The iPhone will not be automatically connected, so you will have to do this every time you sync. This is probably OK in most situations, but I have read that it's difficult/impossible to upgrade the software on the iPhone because it disconnects/reconnects and reboots during this process.

There is a way configure VMWare server to automatically connect to a USB device, so this may get around that issue. I haven't tested this yet since my phone came with 3.0 software already installed, and there are no updates available yet. Here's what you need to do. With the VM powered off, connect your iPhone to your computer. Then type:

Once you are in there, you can search for the iPhone section by typing:

And then up arrow until you get tothe beginning of the section. You should see something like: T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#=  7 Spd=480 MxCh= 0 D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  4 P: Vendor=05ac ProdID=1294 Rev= 0.01 S: Manufacturer=Apple Inc. S:  Product=iPhone S: SerialNumber= C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr=500mA I:* If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver=(none) E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=1250us E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=83(I) Atr=03(Int.) MxPS=  64 Ivl=64ms C: #Ifs= 3 Cfg#= 2 Atr=c0 MxPwr=500mA I: If#= 0 Alt= 0 #EPs= 0 Cls=01(audio) Sub=01 Prot=00 Driver= I: If#= 1 Alt= 0 #EPs= 0 Cls=01(audio) Sub=02 Prot=00 Driver= I: If#= 1 Alt= 1 #EPs= 1 Cls=01(audio) Sub=02 Prot=00 Driver= E: Ad=81(I) Atr=01(Isoc) MxPS= 192 Ivl=1ms I: If#= 2 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=00 Driver= E: Ad=83(I) Atr=03(Int.) MxPS=  64 Ivl=125us C: #Ifs= 2 Cfg#= 3 Atr=c0 MxPwr=500mA I: If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver= E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=1250us E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=83(I) Atr=03(Int.) MxPS=  64 Ivl=64ms I: If#= 1 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=fe Prot=02 Driver= E: Ad=04(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=85(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms C: #Ifs= 3 Cfg#= 4 Atr=c0 MxPwr=500mA I: If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver= E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=1250us E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=83(I) Atr=03(Int.) MxPS=  64 Ivl=64ms I: If#= 1 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=fe Prot=02 Driver= E: Ad=04(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=85(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms I: If#= 2 Alt= 0 #EPs= 0 Cls=ff(vend.) Sub=fd Prot=01 Driver= I: If#= 2 Alt= 1 #EPs= 2 Cls=ff(vend.) Sub=fd Prot=01 Driver= E: Ad=86(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

You'll be focusing your attention on the 3rd line down containing the Vendor and ProdID values. In my case they are 05ac and 1294 respectively. I would guess the Vendor ID is probably common across all Apple products, but the ProdID may vary with different models.

Now you need to edit the configuration file for your VM to allow that specific device to automount. You need to edit the following file: /var/lib/vmware/Virtual Machines//.vmx and add the following line:

That line basically says auto connect a USB device with a vendor ID 0x5ac and a ProdID of 0x1294 when VMWare sees it. As you can see, this takes the Vendor and ProdID values captured above and represents them in hexidecimal. I found references to this in VMWare Support Documentation. Now boot up the VM with the iPhone connected or connect the phone after the VM is up, and your iPhone should be automatically detected. One thing I have noticed is VMWare makes it appear that you haven't connected the device yet even though the VM sees and connects the iPhone.

Automatically Connecting the iPhone in VirtualBox
The process of setting up auto connect under VirtualBox is much easier. With the VM powered off and your iPhone plugged in, start the VirtualBox console:

Click on the Details tab for your VM and scroll down to the USB section. Click on USB to bring up the dialog box. Verify that USB 2.0 is enabled and create a new device filter by clicking on the + icon. Select your iPhone from the list and click OK. When you power up the VM from now on, your iPhone should auto connect just as in VMWare.

NOTE: Creating a filter with this method will cause issues when trying to update your phone within VirtualBox. During a part of the update process, the phone is rebooted into DFU mode and identifies itself as a different device. This is easily managed by creating a blank filter and adding "Apple Inc." to the "Manufacturer" field of the filter.

Adding Content
There are several ways to add content into your VM. The goal here is to get your music, movies, etc. into iTunes, so the method shouldn't matter that much.

The simplest method would be to just copy all of your music into your VM from your host computer. This might work well if you have a small collection, have a lot of disk space to dedicate to your VM or don't mind maintaining your music collection in two places. I'm trying to keep the maintenance of my collection as simple as possible so this isn't appealing to me. If this works for you though, you should be able to copy the music in through several methods. If it's already stored on a USB device, you could mount that from the VM much like mounting your iPhone and copy it to a local directory. You could also download an SCP client like WinSCP inside your VM and scp the files in from your host Gentoo machine, as long as you have ssh running on the host.

Another method would be to enable CIFS in your kernel and install Samba. This would allow you to create Windows shares on your Gentoo host machine and make them available to your VM. Then you would just have to mount that share in your VM and point iTunes to the directory. This slightly more appealing to me, even though it means running CIFS.

Yet another method would be to enable NFS in your kernel and install nfs-utils. This would allow you to create NFS shares on your Gentoo host machine and make them available to your Windows machine. You would then mount the share in Windows and add the directory to iTunes as in the Samba method above. This has an advantage to me since I alreay have NFS running on my home network to share some directories out. It may be less desirable for you if you don't already have NFS running as the client setup process can be a bit tedious in Windows. If you wan't to try this, you can get the Windows SFU (Services for Unix), install it and give it a shot.

There is also a VirtualBox specific method that would probably be preferable if you go that route. In VirtualBox there's an option called "Shared Folders". Click on the Devices Menu and go to Shared Folders. From there you can highlight the Machine Folders and click the plus sign on the right of the dialog to add a new folder. Next, select the path of the directory you want to share and the name you want to give the share. Once that's done, go into the VM. On the command line, type:

You can change the drive letter to any unused letter you like. You cannot change the shared volume name "vboxsvr" as that is predefined. The  is whatever name you gave the share in the VirtualBox dialog box. This will now mount a Windows network drive under My Computer. You can then point iTunes to the drive and import your music. This will only work if you have the VirtualBox Guest Additions installed.

Upgrading iOS
I have worked on a way to upgrade iOS on my iPhone for over a year and I cannot get it to work with VMware Server or Workstation. The problem seems to be that when the phone goes into recovery mode, VMware is too aggressive in trying to reclaim the USB interface and UDEV/HAL get confused. I finally went back to trying VirtualBox and it worked perfectly. I still don't use VirtualBox for daily syncing though.

Here's what to do. If you don't have one, install a fresh copy of XP in a VirtualBox VM and install iTunes. Then if you're using VMware normally, let it start the process of updating iOS. It will get stuck in recovery mode before iTunes can finish the upgrade. Next, copy the iPhone firmware file out of

and scp it onto your Gentoo host. Then move the file into the same location on the VirtualBox Guest. Once that's done, plug in your iPhone, connect it to the guest OS and wait for to iTunes see it. Restore the phone, and it will upgrade to the newest firmware image you have in that directory. Once the phone reboots, reconnect it to iTunes and make sure it shows up.

Once that's done, you can disconnect the phone from iTunes and power down VirtualBox. Then you will need to power up your VMware guest, connect to iTunes, restore your settings from a backup and sync your content again.