CLI Magic: Taming a wild mount

31

Author: Joe Barr

Cowboy up, pilgrims. Today we’re going to learn how to (sur)mount the twin beasts of fear and uncertainty. At least that’s how many Windows users view the specter of being asked to mount a CD-ROM or camera or keydrive. The mount command itself is confusing enough, but what in the heck is it doing? And what are these mysterious things called filesystems, anyway? If that’s your thinking, get down out of the plush, gas-gobbling GUI you’ve been riding around in, and walk this way to learn a little more CLI Magic.Mount what?

If you don’t understand what filesystems are, learning the mount command is not going to be of much use. It would be like learning how to adjust the tilt of the steering wheel when you don’t know how to drive a car. So let’s look at that.

If you’re new to Linux from some other operating system, like, oh, say, Windows, you’re probably already familiar with a filesystem, but you may not realize it. Have you ever seen the C: prompt, or looked at a directory listing? If so, you’ve seen a filesystem trail sign. A filesystem is nothing more than a method of organizing and keeping track of files.

Even Windows has a couple of options for filesystems. You can use FAT, or FAT-32, or NTFS. There are some mythical Windows filesystems — like OFS or WinFS — you can’t use, at least not just yet. Still, most Windows users never change filesystems since the choice is made for them at the factory where Windows is installed; and Joe Sixpack is not likely to be installing NFS, so it’s all a mystery to them.

So why bother?

Unless you have a pressing need, you don’t really have to bother researching filesystems. Many modern Linux distributions run autofs, which automatically mounts a new filesystem when one is detected. But some distro designers prefer — for security reasons — not to have filesystems automatically mounted.

Linux supports a wide range of filesystems: adfs, affs, coherent, devpts, ext2, ext3, fat, hpfs, iso9660, minix, msdos, ncpfs, nfs, ntfs, ramfs, reiserfs, romfs, smbfs, tmpfs, udf, ufs, umsdos, vfat, xenix, and xfs to name most of them.

Why does Linux support so many while Windows supports so few? Good question, grasshopper. The answer is that Linux doesn’t have a monopoly to protect, so it doesn’t have to discourage easy interoperability with other platforms.

When it’s not done for you automagically, mount is the command you use to tell Linux that there’s a new filesystem in town, and that you want to use it. Probably the most often seen need for mounting a filesystem is to read or write a CD, so we’ll use that scenario for our examples.

The basic format of the mount command is:


mount [-t type] [device] dir

The format allows you to tell Linux to mount a filesystem of the type specified in -t type, on the device named device, at the directory given as dir. So, in our example, we want to read a data CD we’ve just placed in the drive. We can mount it by typing:


mount /mnt/cdrom

But how did you know?

Why didn’t I have to enter the type and device? Because there is an entry in my /etc/fstab file which describes /mnt/cdrom. If it weren’t there, we could specify the device by entering:


mount /dev/hdb /mnt/cdrom

How did we know the device? BIOS told us at boot time, and we can query dmesg to learn it when we need it. Like this, for example:


dmesg | grep CD

And the system will reply with something like this:


hdb: Hewlett-Packard CD-Writer Plus 9100b, ATAPI CD/DVD-ROM drive
hdb: ATAPI 32X CD-ROM CD-R/RW drive, 2048kB Cache
Uniform CD-ROM driver Revision: 3.20

Note that the directory given in the mount command — which the command will associate with the appropriate device and filesystem type — must exist. Also note that if the directory given is not used in an entry in the /etc/fstab, we will need to use the full form of the command, and include the filesystem type and the device in the mount command, like this:


mount -t iso9660 /dev/hdb /mnt/cdrom

Unmounting the beast

What goes up, must come down. If you are going to mount filesystems manually, you should also learn to unmount them. The command begs you to make a typo, and I often do. It’s not unmount, it’s:


umount /mnt/cdrom

How about music CDs?

Unlike data discs, music CDs are a piece of cake. Music CDs are not really filesystems, so you don’t have to mount them. Just put the CD in the drive, find a player, and have at it.

And that ends our introduction to mysteries of mount. As usual, we’ve just scratched the surface (don’t try that with your CDs). To learn a whole lot more, find a couple of spare hours and peruse the man pages for the mount command.