Open Source

Mounting external media under Linux

Mounting removable media under Linux is not that difficult once you know what you're doing. In this Daily Drill Down, Vincent Danen explains how to mount removable media, and he introduces a few simple tools for managing your system.

In this Daily Drill Down, I’ll show you how to mount removable media under Linux and how to organize them to make it easy to find everything on your Linux system. I’ll also introduce you to a few simple tools to manage and keep an eye on your system.

Plug-n-Play mounting?
How Linux treats any kind of drive is fundamentally different than how a Windows-like operating system does. In Windows, each hard drive or partition is assigned a drive letter starting with the letter C. If you have three partitions on a hard drive, you have three drive letters that you use to access them. In Linux, there is no such thing as a drive letter. Each drive or partition is assigned a mount point or directory. Because Linux uses a directory structure for the entire system, you may find one partition being mounted as /boot, another as / (or the root partition), and perhaps yet another as /home. The drives are transparent to the operating system in that you can change directories (much as you would under Windows) and be navigating multiple hard drives or partitions without even being aware that you are doing so.

Using this directory structure makes it a little challenging when you are looking at removable media. Whereas in Windows your A drive is your first floppy drive, in Linux it might be /mnt/floppy or any other directory you specify as the mount point. Basically, you need to rethink your approach to hard drives, partitions, and external media because of this major difference between the two types of operating systems. But what it all boils down to is this: It may be easier to navigate a single directory tree spanning multiple disks than using multiple disks to span multiple directory trees.

The df utility: Your first step
The first tool you should become familiar with is the df utility. This utility lets you view how much disk space you have left on any given device. In its simplest form, df will report the disk space usage on every mounted file system currently active on your system. It will show you the mount point, the available space, and the amount of space being taken up by files on the device. The output that df provides may look something like this:
FilesystemSizeUsedAvailUse%Mounted on
/dev/hdc52.9G1.8G 949M66%/
/dev/hda11002M 615M387M61%/mnt/win98
/dev/hda47.0G578M 6.1G9%/mp3

This output shows us a number of things:
  • The filesystem, or the actual hardware device
  • The total size of the device
  • The amount of space that has been used
  • The amount of space available
  • A percentage of the used space compared to the total filesystem size
  • The mount point of the filesystem

Well, that's all fine and dandy, but all of those filesystems are hard drives, not removable drives. How do you mount a floppy disk to see what's on it?

The mounting
Say you've placed the floppy disk into the drive and now you want to know how to read and write to the floppy itself. First, you need to make a mount point for the floppy. Traditionally—and this depends on the distribution you are using—you will by default have a /floppy or /mnt/floppy directory. SuSE uses /floppy by default, whereas Red Hat uses /mnt/floppy. You must ensure that these directories are empty. The rules of mounting any sort of media on a Linux system are quite simple. The directory you are using for the mount point must exist and it must be empty. If you have written files to that directory, you will not be able to mount anything to that directory.

To mount your floppy disk, issue the following command:
mount -t msdos /dev/fd0 /mnt/floppy

This command assumes that your floppy disk is MS-DOS (or FAT16) formatted. It also assumes that you are using the first floppy disk on the chain (drive A in DOS-speak) and that your mount point is /mnt/floppy. The basic syntax for mount is:
mount -t [filesystem] [device] [mount point]

The same command can be used for anything. If your floppy is ext2 (the standard Linux filesystem) formatted, you would replace the msdos filesystem type with an ext2 filesystem type.

The mount command works for removable and fixed media. For example, if you dual-boot with OS/2 and want to mount your OS/2 drive (which is HPFS formatted) to the /mnt/os2 mount point, you would use:
mount -t hpfs /dev/hda5 /mnt/os2

Of course, this command assumes that /dev/hda5 (or the first extended partition of the primary IDE drive) is your OS/2 drive.

Now you want to mount your CD-ROM to access the files stored on a new CD you just bought. Insert the CD into your CD-ROM (for this example, let's assume the CD-ROM is the slave device on the primary IDE channel). Now issue the following command:
mount -t iso9660 /dev/hdb /mnt/cdrom

Since ISO9660 is the standard filesystem for all CD-ROM media, we tell the command mount to mount the iso9660 filesystem type, /dev/hdb (the second device on the primary IDE channel), to the /mnt/cdrom mount point. If you changed to the /mnt/cdrom directory and issued an ls command, the contents of the CD-ROM would be displayed to you. If you used df at this point, you would see /dev/hdb listed in the output as a mounted device.

Now that you know how to mount a removable disk or CD, you should know how to dismount, or umount, the media as well. Simply issue:
umount /mnt/cdrom

to dismount the device mounted at the mount point/mnt/cdrom (in this case, our CD-ROM).

Make it easy with fstab
Right now you're probably thinking about how much of a pain this mounting and dismounting thing is. And you're right. It is a bit of a pain, but there are easier ways to manage your mount points and external devices. What you have seen is one way to do it. Let me introduce you to the /etc/fstab file.

This particular file controls how filesystems are mounted on your Linux system. In it, you will see entries for various partitions with mount points and options. An example /etc/fstab file might look like:
/dev/hda1/mnt/win98vfatuser,exec,conv=auto,umask=00 0
/dev/hda3/mnt/suseext2defaults1 2
/dev/hda4/mp3ext2defaults1 2
/dev/hdc1/bootext2defaults1 2
/dev/hdc2swapswapdefaults0 0
/dev/hdc5/ext2defaults1 1
/dev/hdc6/homeext2defaults1 2
none/procprocdefaults0 0
none/dev/ptsdevptsmode=06200 0
/dev/sda4/mnt/zipvfatdefaults0 0
/dev/hdb/mnt/cdromiso9660defaults,noauto0 0

Your /etc/fstab file should look relatively similar. You’ll see the following elements:
  • The actual filesystem device
  • The mount point
  • The filesystem type
  • The options for the filesystem
  • A number that indicates whether the filesystem needs to be dumped (if you want to know more about filesystem dumping, use man dump to read the manpage)
  • The order in which fsck will check the drives (a value of 0 means not to check the drive)

As you can see, the format of this file is pretty straightforward. Linux uses this file to determine which devices need to be mounted (and where) during boot time.

The mount command also uses this file. Previously you saw how to use mount to mount a removable filesystem by telling it the device, mount point, and filesystem type. There’s an easier way to do the same thing if your /etc/fstab file is set up correctly. Instead of using:
mount -t iso9660 /dev/hdb /mnt/cdrom

to mount your CD-ROM, you can use the following:
mount /mnt/cdrom

When you give mount a command like this, it will look in the /etc/fstab file to see what device belongs to the /mnt/cdrom mount point and what filesystem to use. It will then mount the device.

Fstab drawbacks
The only drawback to this method involves the use of Zip disks or LS-120 disks in cases where some are FAT16-formatted and others are ext2-formatted. You have to decide which filesystem is used the most and make that the filesystem type entry in /etc/fstab. If you want to mount a different type of filesystem (for example, your default is ext2 but you want to mount a FAT16-formatted Zip disk), you will need to explicitly specify the filesystem with the mount command.

There is yet another way to mount external media, one that is available only in recent kernels. Linux Mandrake 7.0 comes with a feature called supermount, and we can expect other distributions to make use of it in the near future (I'm betting that Red Hat 6.2 will utilize it). The supermount feature is an automatic way of mounting external media without having to explicitly mount and umount your floppies and CD-ROMs. Basically, supermount mounts external media when required but does not lock it in so that you don’t have to manually dismount it when finished.

For example, suppose you place a CD in the CD-ROM drive. You have the CD-ROM defined in /etc/fstab as /mnt/cdrom. Simply change to the /mnt/cdrom directory and issue the ls command (or any other command you like that will read the contents of the directory or act on a file on the CD), and supermount will quickly and easily mount the CD, making it available for you. As soon as you leave the directory, the CD-ROM device is unlocked by supermount and you are able to eject the CD, swap it with another, and so on. This makes using removable media as easy under Linux as it is under any other operating system. For newcomers to Linux, this will make things much easier for them simply because it will remind them of the operating system they came from (Windows, DOS, or OS/2).

An example /etc/fstab that uses supermount may include something like this:
/mnt/floppy/mnt/floppysupermountfs=ext2,dev=/dev/fd00 0
/mnt/cdrom/mnt/cdromsupermountfs=iso9660,dev=/dev/hdb0 0
/mnt/cdrw/mnt/cdrwsupermountfs=iso9660,dev=/dev/hdd0 0
/mnt/zip/mnt/zipsupermountfs=vfat,dev=/dev/sda40 0

As you can see, the syntax is a little different when using supermount as opposed to the regular mount and umount commands. The first two elements of the command indicate the mount point, the third indicates that this is a filesystem managed by supermount, and the last element contains the options for this device, which basically indicate the default filesystem and the hardware device.

Now that you know a few ways to use removable media under Linux, let’s examine a few important considerations you need to be aware of. Not all removable media under Linux acts the same. I’ll take three popular removable media and examine each a little closer.

If you were lucky, when you installed your Linux system, the install program found all of your removable devices and made corresponding entries to your /etc/fstab file. However, if this did not happen, you will need to know a few things about partitions and devices in order to make use of your removable media.

CD-ROM devices are pretty straightforward. To understand what device your CD-ROM is called, you need to understand the /dev directory tree and its purpose. The entries in the /dev directory are your hardware devices. In this directory are links to every hardware component on your system, from serial ports to scanners. If you look in the directory, you’ll find many files—perhaps far too many to really understand what devices belong with which file.

If your CD-ROM is an IDE device, it's pretty simple to figure out what device it is. Since you can have only four IDE devices on your system, Linux has a very easy way to distinguish IDE devices. The /dev/hda device is the first IDE device on the primary channel, while /dev/hdb is the second device. The first IDE device on the secondary channel is /dev/hdc while /dev/hdd is the second device (or the fourth overall IDE device). This never changes regardless of whether the device is a hard drive, CD-ROM, or LS-120 drive.

The other thing you will need to keep in mind is the partition information on Zip and LS-120 disks. By default, Zip drives use the fourth partition (/dev/hdb4) for the main partition while LS-120 drives use the first partition (/dev/hdb1). You will need to know this information when you attempt to mount these types of removable disks. CD-ROMs, however, do not use a partition, so you’d simply use /dev/hdb without a numeric partition identifier.

Mounting removable media under Linux is not very difficult once you know what you are doing. It's the getting there that's more difficult than actually making use of it. In this Daily Drill Down, I tried to lift the fog on the "art" of dealing with removable media.

Vincent Danen, a native Canadian in Edmonton, Alberta, has been computing since the age of 10, and he’s been using Linux for nearly two years. Prior to that, he used OS/2 exclusively for approximately four years. Vincent is a firm believer in the philosophy behind the Linux "revolution,” and heattempts to contribute to the Linux causein as many ways as possible—from his FreezerBurn Web site to building and submitting custom RPMs for the Linux Mandrake project. Vincent also has obtained his Linux Administrator certification from Brainbench .He hopes to tackle the RHCE once it can be taken in Canada.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.


Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks

Free Newsletters, In your Inbox