USB Mass Storage Devices (Compact Flash) for Linux

By Van Emery



Test Environment:

Red Hat 7.3, 8.0, and 9 on a Pentium III 667 MHz
Multiple IDE drives, no SCSI card or devices
PQI USB Compact Flash reader with 3 different vendors' flash cards in 8MB, 64MB, and 128 MB
The reader was also tested successfully on a Dell Optiplex GX-1 Pentium II running Red Hat 7.3

Introduction:

I wanted to see if I could use the new generation of removable flash drives (USB) with Linux.  This would make extracting digital camera images easy, as well as sharing large files between Linux machines and Windows 2000/XP machines.  As it turns out, this was much easier than I thought it would be!

The cost of Compact Flash cards (which can be used in PCMCIA slots on laptops using a special adapter), is minimal.  You can easily find a large selection of compact flash cards with sizes ranging from 8MB to 3 GB.  You can expect to pay under 41 cents per MB.


CF Image

I foresee these devices completely replacing Zip disks, Jaz disks, and floppy disks in the roles of walking files from one person's machine to another's, and moving personal data between home and workplace computers.  This is because they are smaller and lighter, cheaper and faster than Zip disks, have no moving parts, and are rapidly portable between platforms. A no-name Compact Flash USB reader can be had for less than $10.


CF Reader Image

The Johnny Sixpack experience:

I thought it would be valuable to see what the common, computer-illiterate user would experience when using a Compact Flash USB reader in a Windows XP environment.  Using a Dell laptop loaded with XP Professional, I plugged the reader into the USB port.  A notification immediately popped up informing me that a new device had been detected, of the removable drive variety.  I was immediately able to view the contents of the new "E:" drive and copy new files into it.  I then yanked it out with no complaints from the OS, although there is an easy method for stopping the device before removing it.  It worked just as easily with Windows 2000 server.

The verdict:  it passes the Johnny Sixpack test.  These USB Compact Flash devices should become popular with the Windows-using masses.

The Linux experience:

I initially read Dave Kelly's USB Digital Camera HOWTO after finding it on the gPhoto documentation page.  This got me started in the right direction.  Basically, your platform will need USB ports and kernel support for USB.  On my system, this was verified by using the dmesg command and the lsmod command (make sure that you are root).

Dmesg will let you know if Linux detected USB devices during boot.  Look through the output of the command to see what USB devices were found and what modules were loaded.  Using lsmod, you will be able to see what modules are loaded.  On my system, the following modules were loaded:

Before proceeding, go ahead and plug your flash card and flash card reader into one of your USB ports. This should automatically add an automount entry for the flash card in the /etc/fstab file.  You do not need this to use the flash, but it makes the whole process a little bit easier. 

My fstab entry looks like this:

/dev/sda1	/mnt/flash	auto	noauto,owner,kudzu   0 0

Of course, you would want to make sure that /mnt/flash exists.  It should be created automatically by the OS whenever the flash reader is plugged in.  I also created a directory /mnt/cf for testing non-automatic mounting of the USB mass storage device:

# mkdir /mnt/cf

By default, the Compact Flash is formatted as a VFAT drive.  You will also note that the fstab entry specifies /dev/sda1.  This is the first available SCSI disk device name on my system.  The USB mass storage device uses SCSI emulation.

Hot-plugging:  my Linux system allowed the USB flash reader to be plugged into a USB port, and later removed.  You can watch the kernel recognize the insertion and extraction of the device by opening a window and issuing the following command:

# tail -f /var/log/messages

Now, remove the USB Compact Flash reader, wait a few moments, then reinsert it.  You should see all of the USB activity in the log.  Now, make sure the CF flash is installed, and let's manually mount the flash: 

# mount -t vfat /dev/sda1 /mnt/cf

You should now be able to access the flash via command line or your favorite graphical file manager.  You can manually view the status of the mounting operation with the mount command.

Using the following commands will give you information on the space used/available on the flash card:

du -h /mnt/cf
             
df -h

You should now be able to move, copy, view, or delete files to your heart's content.  The Compact Flash cards seem reasonably fast to me.  I was able to copy about 60MB worth of files to the flash in less than 62 seconds.  The reader and the motherboard support USB 1.1, which allows about 8.5 Mbps of data transfer speed (max).  Different manufacturers' flash cards have varying read/write speeds.

I was also able to pull JPEG images off of my Kodak digital camera using standard shell commands instead of using gPhoto.  In fact, it was easier to use gqview to preview the images and open the appropriate editors than it was to use gPhoto.

To unmount the Compact Flash, you can simply type:

# umount /mnt/cf

Note:  Make sure that the device is not in use by any users or programs before you unmount it. This includes having your shell in the /mnt/cf directory.

Now remove the reader.  You can now transport your files to any other GNU/Linux host or even your neighbor's Windows 2000/XP machine.

Auto-mounting the Flash:

To use the automount entry, you can simply insert the device, and type:

$ mount /mnt/flash

or right click on the desktop and select Disks => Flash.  You will then see the removable drive icon appear.  To unmount, right click on the icon and select unmount, or simply type:

$ umount /mnt/flash 

Note that you can use the automount entry as a regular (non-root) user, whereas the previous method of manually mounting the flash disk to /mnt/cf requires that you be root.

Formatting the Compact Flash:

Although my digital camera has a method for doing this, I would like to be able to do this from Linux.  The easy way to format the flash, and erase everything on it, is:

# mkfs -t vfat -v /dev/sda1
mkfs.vfat 2.8 (28 Feb 2001)
/dev/sda1 has 8 heads and 32 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 250592 sectors;
file system has 2 16-bit FATs and 4 sectors per cluster.
FAT size is 245 sectors, and provides 62517 clusters.
Root directory contains 512 slots.
Volume ID is 3fbed858, no volume label.

Note:  This assumes that the device is not mounted. You cannot format a mounted device!

Now, the problem with the VFAT file system is that it really doesn't understand the concept of security.  Basic UNIX file permissions and ownership don't work.  You can format the flash card with the ext2 file system, which will preserve the permissions and ownership attributes.  If you are only going the be using the Compact Flash with your Linux systems, this may be the way to go.  An ext2 file system can be created in the following manner:

# mkfs -t ext2 -v /dev/sda1
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
31360 inodes, 125296 blocks
6264 blocks (5.00%) reserved for the super user
First data block=1
16 block groups
8192 blocks per group, 8192 fragments per group
1960 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729
 
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

This worked fine on my 64 MB and 128 MB flash cards, but the 8 MB flash card did not work as well.  The format operation worked with 8 MB, and files could be read and written, but the capacity of the flash was misinterpreted to be 64 MB.  I do not know what the lower limit to this problem is, but the ext2 file system definitely works on 64 MB and larger flash cards.

There was no problem re-formatting the flash as a VFAT file system.


Additional Notes:

I subsequently learned that some Compact Flash readers are not properly recognized by the Linux kernel, even in Red Hat 9.  These will not work correctly.  You will definitely want to make sure that the flash reader that you purchase is Linux compatible.


Conclusion:

Using USB-attached Compact Flash with Linux is easy and useful!




Updated November, 2003



Back to Linux Gouge...

Valid HTML 4.01!