DæmonNews: News and views for the BSD community

Daemon News Ezine BSD News BSD Mall BSD Support Forum BSD Advocacy BSD Updates

Mounting Compact Flash under FreeBSD

Dan Pelleg <daniel+cfread@pelleg.org>

Many consumer electronic devices now support removable memory cards. They provide a fast and easy way to transfer information to and from a PC. In this article we will explore the ways to use Compact Flash (CF) cards under FreeBSD. Our goal is to minimize the administrative overhead for the user - eliminating the need for root access or manual mounts. For simple tasks such as uploading files from the CF card we can even come up with a completely hands-off operation - the user doesn't have to do anything but insert the card in, wait, and then pull it back out.

1. Introduction

Compact Flash cards typically interface to a PC in one of two ways: an external USB card-reader, or through the PC-CARD (also known as "PCMCIA") slot. For the PC-CARD case, a cheap adapter allows insertion of CF media to a standard PC-CARD slot. Some laptops have a special CF slot, eliminating the need for an adapter. The way FreeBSD accesses your device will vary, depending on the reader type. PC-CARD readers will show up as ATA disks, while USB readers will show up as SCSI disks.

Once the "disks" show up, we can mount the filesystem on them, and then manipulate it in the standard way - copying and deleting files. We explore two approaches to doing this. The first involves a script that handles the specific task of uploading all of the new files from the CF card to the PC, requiring no user intervention. The second is more general, and uses the automounter to mount the filesystem as soon as it is accessed, letting the user directly manipulate it.

To make things easy to follow, we first detail the steps for USB readers, and then explain how the same procedures work for PC-CARD devices.

2. USB-Based Compact-Flash Readers

Most of the discussion here also applies, with slight modifications, to other types of 'flash' media (SmartMedia, MMC, MemoryStick and all their variations), as well as cameras, MP3 players, and other devices supported by the umass(4) device driver.

2.1 Requirements

Support for USB readers requires the umass(4) driver. As the manpage tells you, you will need the following options in your kernel config:

	device usb
	device ohci (OR device uhci)
	device umass
	device scbus
	device da
	device pass

Note that the GENERIC kernel that comes with 4.8 already includes them all. After recompiling your kernel (or not, if you're using GENERIC), you should be able to plug the reader into the USB slot and have it show up. Usually, the media has to be already in the reader before you plug in the USB connector. So, if things don't work for you, unplug the reader from the USB slot, stick the memory card in, and plug it into USB again.

The message log will tell you the device name for the CF card. umass-attached devices will show up like this:

	da0 at umass-sim0 bus 0 target 0 lun 0

indicating that the device name is da0. Note that if you already have SCSI devices in the system, this might interfere with whatever you have in fstab(5). To avoid nasty surprises, you should wire down all of your other SCSI devices so their numbering doesn't change.

If the reader is supported, at this point you should be able to mount the device. If you are using a card from a standard camera or music player, the following command, typed as root, will mount it under /mnt:

	mount -t msdos /dev/da0s1 /mnt

If this fails, it's possible that your reader is not supported under FreeBSD yet. It is possible that adding a "quirk" for your reader will help FreeBSD detect it. This involves some kernel hacking, which not everyone feels comfortable with. If you are brave enough to try, check the quirk reporting page at http://www.root.org/~nate/freebsd/quirks.html. My very limited experience indicates the first quirk to try is DA_Q_NO_6_BYTE.

In theory, at this point you can become root whenever you want and mount the device. However the goal here is to try and avoid this step. The following section and section 4 describe two different strategies that accomplish this.

2.2 Using an upload script

Most often, the computer is used as a backing store for the small-capacity CF card. You take a few photos on your digital camera, and want to make sure you have copied them to a "real" disk so you can later process them. We will use a script that uploads all the new photos from a CF card into the PC's disk-drive. Once installed, it will go into action as soon as the CF media is attached. It will mount the filesystem, copy the new files over, and then unmount it. The only thing the user has to do is insert the media and later pull it out.

You can download a copy of the script. We assume you have it installed at /usr/local/sbin/copy-flash and marked is as executable.

Next, you will need the USB daemon, usbd(8). Add this line to /etc/rc.conf, unless it's already there:

	usbd_enable="YES"

To make the script run when a USB reader is attached, add an entry to /etc/usbd.conf. It will look like this:

	device "CF card"
		devname "umass[0-9]+"
		attach "/usr/local/sbin/copy-flash da0 /tmp/cf CFOWNER"

where "da0" should be substituted by whatever device you see in the message log. Before you use this hook, replace the string CFOWNER. It should be a user name, and the copied files will be owned by that user. This is normally you or the primary user of the machine. Also, create the target directory into which the files will be copied. In this example it is /tmp/cf. Make sure you choose a partition that is big enough to contain all the files you plan on using. Finally, don't forget to kill and restart usbd.

This script will also sound a short melody once the copying is done. This lets you know it is safe to pull out the CF card. If you don't hear it, add the spkr(4) psuedo-device to your kernel.

3. PC-CARD readers

This section repeats the previous one, this time explaining how to make a PC-CARD slot (or a dedicated CF slot) work.

3.1 Requirements

Compared to USB readers, PC-CARD readers are simpler. They only require the pccardd(8) daemon to run. In all likelihood, you already have it. If not, add the following line to /etc/rc.conf:

	pccard_enable="YES"

Again, the message log will tell us the name of the device. For example, my pccardd-attached device shows up as:

	ad8: 124MB <SAMSUNG CF/ATA> [496/16/32] at ata4-master BIOSPIO

indicating that the device is ad8.

3.2 Using an upload script

The same script as above can be used for PC-CARD readers. The only difference is what runs it - in this case, it is pccardd instead of usbd. Add this entry to /etc/pccardd.conf (create the file if it doesn't exist on your system):

	# GENERIC Flash ATA / ATA HDD
	generic fixed_disk
		config  0x1 "ata" ?
		insert /usr/local/sbin/copy-flash $device /tmp/cf CFOWNER
		logstr  "GENERIC Flash ATA / ATA HDD"

Again, change CFOWNER to the name of the user you want to have access to the files.

4. Using the automounter

Much of the information in this section is from a Dæmon News article by Renaud Waldura. The automounter can mount filesystems automatically whenever they are accessed. That is, you simply "ls" or "cd" to a directory that represents a filesystem, and the automounter intercepts the operation, and makes sure the underlying filesystem is mounted. It is typically used for managing NFS mounts, but here we use it for local mounts.

Add these lines to /etc/amd.map:

	localhost		type:=auto;fs:=${map};pref:=${key}/

	localhost/cf            type:=program;fs:=/mnt/cf;\
				mount:="/sbin/mount mount /mnt/cf";\
				unmount:="/sbin/umount umount /mnt/cf"

Next, add a line for the filesystem in /etc/fstab. It will look like this, assuming that in your system the reader appears as the device /dev/ad8:

	/dev/ad8s1              /mnt/cf         msdos   rw,noauto       0       0

Note that we specify the first slice on the disk, and the msdos filesystem. Many digital cameras expect their CF cards to be configured in this way. We also need to create the mount-point:

	mkdir -p /mnt/cf

To enable the automounter, add this line to /etc/rc.conf:

	portmap_enable=YES
	amd_enable="YES"
	amd_flags="-a /.amd_mnt -l syslog /host /etc/amd.map"

Once you start amd (or reboot), any user should be able to access the CF card, simply by doing a "ls" or "cd" on the directory /host/localhost/cf

Remember that amd only unmounts the filesystem after it has been idle for some time (usually 5 minutes). This means that before you take the CF card out of the slot, you need to stop using it (remembering to "cd" out of any directories on it), and wait. Pulling out a mounted device can cause all kinds of trouble, including data loss and system crashes. So you want to make sure you unmount first. If you're in a hurry and can't wait for amd to time out, you can ask it to unmount the filesystem immediately by doing:

	amq -u /host/localhost/cf

This, again, is not a privileged operation and does not require root access.

Summary

We eliminated the need for root access to read and write files stored on CF media. We can let the automounter do the mounting for us, or have a script that does all the copying as well. In fact, both of these solutions can co-exist: whenever media is inserted, new files are immediately copied over. If the user later wants to access the card and, say, delete some files, they can do that and amd will take care of mounting.

Once you start using the file-copy script, you will probably discover that managing multiple copies of the same data is hard. The original photo is kept on the CF card, the script makes another one on your local disk, and you will also probably make a final copy in your electronic album. To make room and reduce clutter, you will want to delete redundant copies. It is often easiest to do this on the PC where you can easily view the photos on a big screen. The problem is, the files will re-appear the next time you pop the CF card in. One solution is to wipe the CF card clean every time you insert it. But it's wiser to give the good photos another chance, just in case they get lost. A trick I find useful is to create a text file in the directory that stores the files from the CF card, and in that file I record the name of each file that I delete from the PC copy. This way, I can also quickly delete the same files from the CF card whenever it fills up.

Acknowledgments

Thanks to Joshua Schachter and Nadav Eiron. Scott Mitchell provided greatly valuable help for this article.

Google
Web daemonnews.org

More Articles
  • Interview with Jan Schaumann
  • Interview with Theo de Raadt
  • Book Review: Virtualization with VMware ESX Server
  • Editorial: Not Quite Dead Yet
  • The Design of OpenBGPd
  • Interview with der Mouse
  • Letter to Steve Jobs
  • Interview with Manuel Bouyer on Xen
  • Apple and Open Source
  • BSDCan 2006
  • BSD Certification Survey Results
  • Lab in a Box
  • Ike Notes on BSDCan 2005
  • BSDCan 2005 Photos
  • FreeBSD Developer Summit Pictures

  • Advertisements




    Author maintains all copyrights on this article.
    Images and layout Copyright © 1998-2006 Dæmon News. All Rights Reserved.