Sunday, February 25, 2007

Mounting Devices

/etc/fstab: Automating the mount process

The file /etc/fstab (it stands for "file system table") contains descriptions of filesystems that you mount often. These filesystems can then be mounted with a shorter command, such as mount /cdrom. You can also configure filesystems to mount automatically when the system boots. You'll probably want to mount all of your hard disk filesystems when you boot.

Look at this file now, by typing more /etc/fstab. It will have two or more entries that were configured automatically when you installed the system. It probably looks something like this:

     # /etc/fstab: static file system information.
/dev/hda1 / ext2 defaults 0 1
/dev/hda3 none swap sw 0 0
proc /proc proc defaults 0 0

/dev/hda5 /tmp ext2 defaults 0 2
/dev/hda6 /home ext2 defaults 0 2
/dev/hda7 /usr ext2 defaults 0 2

/dev/hdc /cdrom iso9660 ro 0 0
/dev/fd0 /floppy auto noauto,sync 0 0

The first column lists the device the filesystem resides on. The second lists the mount point, the third the filesystem type. The line beginning proc is a special filesystem explained in The proc filesystem, Section 4.8.3. Notice that the swap partition (/dev/hda3 in the example) has no mount point, so the mount point column contains none.

The last three columns may require some explanation.

The fifth column is used by the dump utility to decide when to back up the filesystem. FIXME: cross ref to dump

The sixth column is used by fsck to decide in what order to check filesystems when you boot the system. The root filesystem should have a 1 in this field, filesystems which don't need to be checked (such as the swap partition) should have a 0, and all other filesystems should have a 2. FIXME: cross ref to fsck, also, is the swap partition really a filesystem?

Column four contains one or more options to use when mounting the filesystem. Here's a brief summary (some of these probably won't make much sense yet - they're here for future reference):

async and sync
Do I/O synchronously or asynchronously. Synchronous I/O writes changes to files immediately, while asynchronous I/O may keep data in buffers and write it later, for efficiency reasons. FIXME: cross ref to section on sync for full explanation. Also, should recommend when to choose one or the other.
ro and rw
Mount the filesystem read-only or read-write. If you don't need to make any changes to the filesystem, it's a good idea to mount it read-only so you don't accidentally mess something up. Also, read-only devices (such as CD-ROM drives and floppy disks with write protection tabs) should be mounted read-only.
auto and noauto
When the system boots, or whenever you type mount -a, mount tries to mount all the filesystems listed in /etc/fstab. If you don't want it to automatically mount a filesystem, you should use the noauto option. It's probably a good idea to use noauto with removable media such as floppy disks, because there may or may not be a disk in the drive. You'll want to mount these filesystems manually after you put in a disk.
dev and nodev
Use or ignore device files on this filesystem. You might use nodev if you mount the root directory of another system on your system - you don't want your system to try to use the devices on the other system.
user and nouser
Permit or forbid ordinary users to mount the filesystem. nouser means that only root can mount the filesystem. This is the normal arrangement. You might use the user option to access the floppy drive without having to be root.
exec and noexec
Allow or do not allow the execution of files on this filesystem. Probably you won't need these options.
suid and nosuid
Allow or do not allow the suid bit to take effect. Probably you won't need these options. See Making files suid/sgid, Section
Equivalent to: rw, dev, suid, exec, auto, nouser, async. You can specify defaults followed by other options to override specific aspects of defaults.
fstab Syntax
[Device] [Mount Point] [File_system] [Options] [dump] [fsck order]
Device = Physical location.
/dev/hdxy or /dev/sdxy.
x will be a letter starting with a, then b,c,....
y will be a number starting with 1, then 2,3,....
Thus hda1 = First partition on the master HD.
See Basic partitioning for more information
Note: zip discs are always numbered "4".
Example: USB Zip = /dev/sda4.

Note: You can also identify a device by udev, volume label (AKA LABEL), or uuid.

These fstab techniques are helpful for removable media because the device (/dev/sdxy) may change. For example, sometimes the USB device will be assigned /dev/sda1, other times /dev/sdb1. This depends on what order you connect USB devices, and where (which USB slot) you use to connect. This can be a major aggravation as you must identify the device before you can mount it. fstab does not work well if the device name keeps changing.

To list your devices, first put connect your USB device (it does not need to be mounted).
By volume label:
ls /dev/disk/by-label -lah
By id:
ls /dev/disk/by-id -lah
By uuid:
ls /dev/disk/by-uuid -lah
IMO, LABEL is easiest to use as you can set a label and it is human readable.

The format to use instead of the device name in the fstab file is:
  1. /mnt Typically used for fixed hard drives HD/SCSI.
  2. /media Typically used for removable media (CD/DVD/USB/Zip).
  1. /mnt/windows
  2. /mnt/data
  3. /media/usb
To make a mount point:
sudo mkdir /media/usb
File types:
Linux file systems: ext2, ext3, jfs, reiserfs, reiser4, xfs, swap.

vfat = FAT 32, FAT 16
ntfs= NTFS

Note: For NTFS rw ntfs-3g

CD/DVD/iso: iso9660
To mount an iso image (*.iso NOT CD/DVD device):
sudo mount -t iso9660 -o ro,loop=/dev/loop0
Network file systems:
nfs Example:
server:/shared_directory /mnt/nfs nfs 0 0

Make a directory for each device to mount it

makedir /floppy
makedir /cdrom
makedir /usb

Mounting a filesystem

Before mounting a filesystem, or to actually create a filesystem on a disk that doesn't have one yet, it's necessary to refer to the devices themselves. All devices have names, and these are located in the /dev directory. If you type ls /dev now, you'll see a pretty lengthy list of every possible device you could have on your Debian system.

Possible devices include:

  • /dev/hda is IDE drive A, usually called C:\ on a DOS or Windows system. In general, this will be a hard drive. IDE refers to the type of drive - if you don't know what it means, you probably have this kind of drive, because it's the most common.
  • /dev/hdb is IDE drive B, as you might guess. This could be a second hard drive, or perhaps a CD-ROM drive. Drives A and B are the first and second (master and slave) drives on the primary IDE controller. Drives C and D are the first and second drives on the secondary controller.
  • /dev/hda1 is the first partition of IDE drive A. Notice that different drives are lettered, while specific partitions of those drives are numbered as well.
  • /dev/sda is SCSI disk A. SCSI is like IDE, only if you don't know what it is you probably don't have one. They're not very common in home Intel PC's, though they're often used in servers and Macintoshes often have SCSI disks.
  • /dev/fd0 is the first floppy drive, generally A:\ under DOS. Since floppy disks don't have partitions, they only have numbers, rather than the letter-number scheme used for hard drives. However, for floppy drives the numbers refer to the drive, and for hard drives the numbers refer to the partitions.
  • /dev/ttyS0 is one of your serial ports. /dev contains the names of many devices, not just disk drives.

To mount a filesystem, tell Linux to associate whatever filesystem it finds on a particular device with a particular mount point.

  1. su

    If you haven't already, you need to either log in as root or gain root privileges with the su (super user) command. If you use su, enter the root password when prompted.

  1. ls /cdrom

    See what's in the /cdrom directory before you start. If you don't have a /cdrom directory, you may have to make one using mkdir /cdrom.

  1. mount

    Typing simply mount with no arguments lists the currently mounted filesystems.

  1. mount -t iso9660 CD device /cdrom

    For this command, you should substitute the name of your CD-ROM device for CD device in the above command line. If you aren't sure, /dev/hdc is a good guess. If that fails, try the different IDE devices: /dev/hda, etc. You should see a message like:

         mount: block device /dev/hdc is write-protected, mounting read-only

    The -t option specifies the type of the filesystem, in this case iso9660. Most CDs are iso9660. The next argument is the name of the device to mount, and the final argument is the mount point. There are many other arguments to mount; see the man page for details. (For example, you could avoid the above message by specifying read-only on the command line.)

    Once a CD is mounted, you may find that your drive tray will not open. You must unmount the CD before removing it.

  1. ls /cdrom

    Confirm that /cdrom now contains whatever is on the CD in your drive.

  1. mount

    Look at the list of filesystems again, noticing that your CD drive is now mounted.

  1. umount /cdrom

    This unmounts the CD. It's now safe to remove the CD from the drive. Notice that the command is umount with no "n", even though it's used to unmount the filesystem.


Junglee said...

Thanks chris, your explanation was really helpful; especially regarding mounting usb devices by UUID.

My external USB hard-drive's dev id would change every now and then, causing fstab to fail. Finding the UUID of the usb drive and using it in fstab fixed it.

Thanks heaps.

ikko karima said...

Thank you!