USB Mass Storage Devices (Compact Flash Card Readers) in Linux

Presented to Ottawa Canada Linux Users Group (OCLUG), January 7, 2003.

Last updated: 2003/05/22 18:26:14. Please note that this document is several years old now. With most modern Linux distributions, your USB Mass Storage Devices should probably work automatically. Check your distribution's documentation.


This talk is based on my own experiences using the following hardware andsoftware:

  • SanDisk ImageMate SDDR-75 CF/SM Card Reader
  • My parents' PC with Redhat 7.2
  • Home PC with Debian Unstable (my own kernel 2.4.18)
  • Work PC with Redhat 8.0
  • ....with a slant towards using it for reading digital pictures

What is a USB Mass Storage Device?

  • "the driver is really an interface between the USB stack and the
    SCSI layer"

    • When you plug it in, a USB Mass Storage Device appears to be
      just another SCSI disk.

  • Examples:

    • Compact Flash card readers
    • Certain digital cameras
    • "Keychain" hard drives
    • USB CD-ROMs/floppies, etc...etc..

Why would you want to use the USB Mass Storage Driver?

  • To access USB mass Storage devices (of course)
  • Alternative way to access digital pictures (instead of gphoto2)
  • For digital cameras not supported in Linux (or not well supported)

    • E.g. I can now upload files to my CF cards, which I cannot do via the Canon Powershot S40 driver in gphoto2.
    • Use this for sending firmware upgrades to my camera

  • To conserve batteries in your digital camera
  • For using CF or SmartMedia cards as portable storage: Smaller and more convenient (if readers are available) than CDRW or Zip Disk
  • They generally work very well in Linux!


The Easy Way!

If you have a relatively modern Linux distribution that is pre-configured to work with USB Mass Storage Devices (I've tested with Redhat 7.2 and 8.0), there's nothing you need to set up!. It should be truly "plug and play":

  • Plug your device in to the USB slot
  • It will be automatically detected by the kernel, and an appropriate entry in /etc/fstab will be added automatically.
  • The device will be mounted under /mnt/flash
  • Use it, just like any other storage device

Manual Setup

If you're running a Linux distribution that doesn't set up USB Mass Storage devices automatically, here are the steps you need to follow:

  • Configure the Linux Kernel:

    • Add SCSI Support

      • SCSI Support (CONFIG_SCSI)
      • SCSI disk support (CONFIG_BLK_DEV_SD)
      • SCSI generic support (CONFIG_CHR_DEV_SG)

    • Add USB Support

      • Support for USB (CONFIG_USB)
      • Preliminary USB device file system (CONFIG_USB_DEVICEFS)
      • USB Mass Storage support (CONFIG_USB_STORAGE)

  • Plug in your USB device. You should see your device listed in /proc/bus/usb/devices. If not, you need to fix the kernel or check your cabling.
  • Use the sg3-utils package to determine the device name of your USB Mass Storage Device:

    • Install the sg3-utils package
      (e.g. apt-get install sg3-utils)
    • List your raw SCSI devices by running sg_scan -i. The output will look something like this:

/dev/sg0: scsi0 channel=0 id=0 lun=0 [em]  type=5
MATSHITA  CD-RW  CW-7585    1.01 [wide=0 sync=1 cmdq=0 sftre=0 pq=0x0A]
/dev/sg1: scsi1 channel=0 id=0 lun=0 [em]  type=0
SanDisk   ImageMate CF-SM   0100 [wide=0 sync=0 cmdq=0 sftre=0 pq=0x0]
/dev/sg2: scsi1 channel=0 id=0 lun=1 [em]  type=0
SanDisk   ImageMate CF-SM   0100 [wide=0 sync=0 cmdq=0 sftre=0 pq=0x0]

  • This tells me that I have 3 SCSI devices on my system: My CD-RW drive, and two Sandisk ImageMate devices (it's a single hardware device, but the first slot is the CF card reader, and the second is the SM card reader)
  • Determine the real SCSI device associated with your USB Mass Storage Device with sg_map. The output looks like:
  • /dev/sg0  /dev/scd0
    /dev/sg1  /dev/sda
    /dev/sg2  /dev/sdb

  • Now we can match the results of these last two steps. For example my Compact Flash reader is on the /dev/sda device.
  • In most cases, USB Mass Storage Devices will only have one partition on them, so we can safely assume that the final, mountable device is /dev/sda1 (the first partition on /dev/sda).

  • Create a mount point for your device: mkdir /mnt/flash
  • Add an entry to /etc/fstab for this mount point:
    /dev/sda1   /mnt/flash   auto   defaults,user,noauto   0 0
  • Now it's ready to go. Any user can mount the device:
    mount /mnt/flash
  • Make sure you unmount it before taking the card out or unplugging it:
    umount /mnt/flash
  • Other Stuff

    • Like any other "hard drive" device, you can run fdisk on it and play with the partitions:
      fdisk /dev/sda
    • Format the partition(s) on the device, usually with a DOS/FAT filesystem, e.g.:
      mkfs -t vfat /dev/sda1
      ...or with a journalled Linux ext3 filesystem (probably not useful when used with cameras, etc!):
      mkfs -t ext3 /dev/sda1