Team LiB
Previous Section Next Section

Hack 97. Use an iPod with Linux

You don't need a Windows PC or Mac to use an iPod.

This hack shows how to use an iPod with Linux. It's aimed at Linux puriststhat is, people who don't want to have to use a Mac or Windows-based PC, nor Wine nor Windows softwareto get going. (I fall into this category, not because of any religious convictions, but merely because Linux is all I have. For updates, visit http://pag.csail.mit.edu/~adonovan/hacks/ipod.html)

This hack assumes that you have reasonable level of Linux competence. You should be comfortable with downloading, compiling, and installing software, as well as general system administration tasks.


Here's what you'll need if you want to use an iPod with your Linux box:


A Mac or Windows iPod (obviously)

The iPod is basically just a FireWire hard disk, with its own operating software stored in one partition. The two variants of the iPod are formatted with different filesystems: HFS+ in the case of the Mac, and FAT32 in the case of Windows. Indeed, this is the only difference.

Ideally, you want to start with a Windows iPod. Linux has extremely limited support for the Apple HFS+ filesystem, and thus it is necessary to convert HFS+ iPods to FAT32, erasing the disk in the process. The iPod firmware is identical, though, so you must save this before you begin.

To do the conversion, don't mess around with Wine, or with WinniePod Updater, the Apple-sanctioned tool for HFS-to-FAT32 conversion. The GNU instructions for how to convert are sufficient and require only fdisk, dd, and mkfs.vfat, which are standard Unix tools.

The latest breed of iPod appears to come in a single flavor called "for Windows and Mac." They are actually HFS-formatted but come with software for Windows that invisibly does the conversion the first time they are used. So these are really just Mac iPods. If you have access to a PC with MS Windows, you can use that to do the conversion to FAT32. (Thanks to Zach Hobbs for this information.)



A Linux system with a recent, FireWire-capable kernel (2.4.12 or laternow might be a good time to upgrade to RedHat 9.0 or similar)

Note that the version of RedHat Package Manager (RPM) that comes with RedHat 9.0 (Shrike) has an annoying bug: sometimes it will crash, and on subsequent executions, it will hang, waiting for a mutex (in the futex syscall, as can be observed using strace). If this happens, simply remove the /var/lib/rpm/_ _dbxxx temporary files from the RPM database and try again.


A working FireWire interface

I use an Orange Micro PCMCIA card (http://www.orangemicro.com/firewire.html; $59.00) for a laptop. It still seems that the kernel support for FireWire is a little flaky, so try to avoid issuing and/or interrupting commands unnecessarily, or removing the interface while the drivers are doing something.


The GtkPod package

GtkPod (http://gtkpod.sourceforge.net; free) is a graphical tool for transferring files to and from the iPod. It is the Linux equivalent of the iTunes software used for this purpose on the Mac.

I used the gtkpod 0.50 RPM, available free from http://www.rpmfind.net. This package requires the id3lib package.

You must use a tool such as GtkPod; you cannot simply copy files onto the iPod's hard disk, because the iPod's database must be updated for it to see the new tracks. Furthermore, the first time you use GtkPod, you must select FileCreate Directories to set up the database on the iPod.


The grip package

Grip is a free, graphical tool for ripping CDs and encoding them as MP3s.

Note that when ripping CDs to files, the actual filenames are not important to the iPod. However, because its music database is populated from the ID3 tags embedded within the MP3 files, it is important that these are accurate.

This means that you should encode MP3 files from an album all together, or else you will lose the album track-numbering information. It also means that you can use convenient filenames (such as track07.mp3) instead of naming the files with the actual track names (e.g., 07. Voodoo Chile [Slight Return].mp3); the shell metacharacters present in the latter make them a pain to work with.

11.7.1. Setting Up Your Linux Desktop

Assuming you're using a PCMCIA FireWire card, once the card is inserted, the cardmgr daemon should take care of loading the ieee1394 and ohci1394 modules. If you have a PCI card, these should be loaded by system startup (/etc/rc.local).

When you attach the iPod to the FireWire interface, the sbp2 module is loaded automatically. (If it's not, load it with modprobe.) You should see messages appear in dmesg indicating that the device is recognized. Additionally, /proc/bus/ieee1394/devices contains information on each device, including the string [Apple Computer, Inc.] for the iPod:

ieee1394: Host added: Node[00:1023]  GUID[00d0f5cd4008049d]  [Linux OHCI-1394]
ieee1394: Device added: Node[00:1023]  GUID[000a2700020e545e]  [Apple Computer, Inc.]
ieee1394: Node 00:1023 changed to 01:1023
SCSI subsystem driver Revision: 1.00
ieee1394: sbp2: Logged into SBP-2 device
ieee1394: sbp2: Node[00:1023]: Max speed [S400] - Max payload [2048]
scsi0 : IEEE-1394 SBP-2 protocol driver (host: ohci1394)
$Rev: 707 $ James Goodwin SBP-2 module load options:
- Max speed supported: S400
- Max sectors per I/O supported: 255
- Max outstanding commands supported: 64
- Max outstanding commands per lun supported: 1
- Serialized I/O (debug): no
- Exclusive login: yes
  Vendor: Apple     Model: iPod              Rev: 1.40
  Type:   Direct-Access                      ANSI SCSI revision: 02
Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0
SCSI device sda: 58595040 512-byte hdwr sectors (30001 MB)
sda: test WP failed, assume Write Enabled
 sda: sda1 sda2

The iPod appears as a fake SCSI device (typically /dev/sda if you have no other SCSI devices) and can be accessed using the regular Unix tools for block devices. However, if you are using a Mac iPod, fdisk will not recognize the partition map, and you will get a message resembling "Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel." In this case, it is time to follow the GNU instructions (for conversion).

At this point, the Linux IEEE1394 drivers (ieee1394, ohci1394) should have recognized the hardware:

% cat /proc/bus/ieee1394/devices
Node[00:1023]  GUID[001106000000649a]:
  Vendor ID: `Linux OHCI-1394' [0x004063]
  Capabilities: 0x0083c0
  Bus Options:
    IRMC(1) CMC(1) ISC(1) BMC(0) PMC(0) GEN(0)
    LSPD(2) MAX_REC(2048) CYC_CLK_ACC(0)
  Host Node Status:
    Host Driver     : ohci1394
    Nodes connected : 2
    Nodes active    : 2
    SelfIDs received: 2
    Irm ID          : [00:1023]
    BusMgr ID       : [00:1023]
    In Bus Reset    : no
    Root            : no
    Cycle Master    : no
    IRM             : yes
    Bus Manager     : yes
Node[01:1023]  GUID[000a2700020ec65a]:
  Vendor ID: `Apple Computer, Inc.' [0x000a27]
  Capabilities: 0x0083c0
  Bus Options:
    IRMC(0) CMC(0) ISC(0) BMC(0) PMC(0) GEN(0)
    LSPD(2) MAX_REC(2048) CYC_CLK_ACC(255)
  Unit Directory 0:
    Vendor/Model ID: Apple Computer, Inc. [000a27] / iPod [000000]
    Software Specifier ID: 00609e
    Software Version: 010483
    Driver: SBP2 Driver
    Length (in quads): 8

% cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: Apple    Model: iPod             Rev: 1.40
  Type:   Direct-Access                    ANSI SCSI revision: 02

Performing the HFS-to-FAT32 conversion involves the following steps:

  1. Save the first 32 MB of the second partition, which contains the iPod firmware image. Keep this file safe somewhere on your PC:

    % dd if=/dev/sda2 of=backup_firmware

  2. Splat zeros all over the partition map so that all disk data is effectively erased. Unload and reload the sbp2 driver to update its world-view:

    % dd if=/dev/zero of=/dev/sda bs=1M count=10
    % rmmod sbp2 && insmod sbp2

  3. Create two partitions. The first should be large enough to hold the 32-MB file you saved earlier; the second will hold the remaining 30 GB of the disk. Tag the two partitions as Empty and FAT32, respectively:

    % fdisk /dev/sda
    n   [make new partition]
    p   [primary]
    1   [first partition]
        [just press enter -- default first sector is 1]
    5S  [5 sectors -- big enough to hold 32MB]
    
    n   [make new partition]
    p   [primary]
    2   [second partition]
        [just press enter -- default first sector is 6]
        [just press enter -- default size uses all remaining space]
    
    t   [modify type]
    1   [first partition]
    0   [first partition has no filesystem; ignore warning]
    
    t   [modify type]
    2   [second partition]
    b   [second partition is FAT32]
    
    p   [show partition map]
    
       Device Boot    Start       End    Blocks   Id  System
    /dev/sda1             1         5     40131    0  Empty
    /dev/sda2             6      3647  29254365    b  Win95 FAT32
    
    w   [commit changes to disk]

  4. Copy the firmware back to the first (small) partition:

    dd if=backup_firmware of=/dev/sda1

  5. Make a FAT32 filesystem on the second (large) partition:

    mkfs.vfat -F 32 -n "My iPod" /dev/sda2

    If all goes well, resetting the iPod (by holding down the Menu and Play buttons for 10 seconds) will cause it to reboot to the familiar menus. If not, go through the instructions again. Remember, the iPod is just a hard disk, so as long as you have the original firmware backed up correctly and safely on your PC, you can reformat it as many times as you like. (It worked for me the first time.) Be wary about installing different firmware from the one it came with, however.

    At this point, you should be able to mount the disk in the usual way. Once this works, setup is complete and you are through to the normal usage instructions described in the following section.

11.7.2. Normal Usage

The Linux drivers for the iPod are still a little flakey; sometimes, the sbp2 driver gets stuck indefinitely in its initializing state and cannot be removed, and at other times the machine hangs.

To minimize the risk of such errors, I strongly advise you to follow a disciplined procedure for docking and undocking the iPod. Here's the order of events I usually employ:

  1. Insert the IEEE1394 PCMCIA card into my laptop. Check that this succeeded by running lsmod and looking for ieee1394 and ohci1394.

  2. Attach the iPod. This time the sbp2 driver should appear. If it does not, try detaching and reattaching it.

  3. Mount the iPod as a disk, copy files across, and then unmount it again.

  4. rmmod the sbp2 driver.

  5. Detach the iPod.

  6. Remove the IEEE1394 card.

Note that these steps are perfectly symmetrical. This seems to achieve greater reliability than performing them in an arbitrary order.

I use two scripts, dock-ipod and undock-ipod, whenever I attach or detach the iPod to or from the interface card. Here's dock-ipod:

#!/bin/sh

modprobe sbp2
mount /dev/sda2 /mnt/ipod/

And undock-ipod:

#!/bin/sh

umount /mnt/ipod
rmmod sbp2

They must both be run as root:

% su - root -c ./dock-ipod

or:

% sudo ./dock-ipod (if the user is a sudoer)

or:

% su - root
Password:
root$ ./dock-ipod

11.7.3. Downloaded MP3 Files and ID3 Tags

The iPod does not care about the filenames of MP3 files; all its database information is supplied by ID3 tags within the MP3 files. Therefore, these must be present for transferred files even to appear on the iPod.

You might want to add MP3 files that did not come from a CD (e.g., those downloaded from Napster, Kazaa, etc.) to your iPod. The ID3 tags in such files are often inappropriate; for example, because they feature the original artist/album name from the CD they came from, instead of the logical group to which they will belong on your iPod (e.g., Misc/80s Synth Pop). If you do nothing about this, you will find each song appearing in its own artist/album category, with no useful grouping. You'll also need to tag manually when CDDB lookup fails (e.g., for non-industry CDs) or for MP3 files that were hand-encoded from WAV.

To change the tags, you'll need a tool such as ID3ed (http://www.dakotacom.net/~donut/programs/id3ed.html; free). This tool is pretty straightforward, and it comes with a helpful man page. The synopsis says:

id3ed [-s songname] [-n artist] [-a album] [-y year] 
[-c comment] [-k tracknum] [-g genre] [-q] [-SNAYCKG] 
[-l] [-L] [-r] [-i] [-v] files...

Obviously, you don't need to include all of those options. Here's an example:

% id3ed -s "Red House" -n "Jimi Hendrix" \
-a "Are You Experienced?" -k 3 redhouse.mp3

Alternatively, you can use a graphical tool such as xid3 (www.nebel.gmxhome.de/xid3/; free), which has a Tcl/Tk-based front end for ID3-tag editing that makes it a lot easier to use for this information. The most important ID3 tags for the iPod are Artist, Album, Title, and Track Index (and Genre, if you actually bother to use that).

With minimal effort, your iPod will play nicely with Linux. No, you won't be able to buy songs from the iTunes Music Store, but you'll still have most of the functionality Macintosh and Windows users have.

Alan Donovan

    Team LiB
    Previous Section Next Section