Thursday, November 27, 2008

Configuring Filesystem Mounts with /etc/fstab

Problem

Users need to make their own backups on CDs, and use USB memory sticks and other types of removable media. You don't want to give them root privileges just so they can mount these devices. But only root can use the mount command we discussed in Recipe 9.9. And you also want to control which file systems are automatically mounted at boot.

Solution

Add entries to /etc/fstab, defining mount points and access permissions.
This example shows a Linux partition, two Windows partitions, and removable media:

#
/dev/hda6 /rh-data reiserfs defaults,user,noauto 0 1
/dev/hda1 /win2k ntfs defaults,user,ro,gid=win2k 0 0
/dev/hda2 /win98 vfat defaults,user,gid=win98 0 0
/dev/hdc0 /cdrom auto defaults,user,noauto,ro 0 0
/dev/fd0 /floppy auto defaults,user,noauto 0 0
/dev/sda1 /memstick auto defaults,user,noauto 0 0

Once a device has an entry in /etc/fstab, it can be mounted by using the mountpoint:
$ mount /cdrom
$ mount /memstick

And unmounted the same way: $ umount /cdrom


Discussion

These are the six fields that make up /etc/fstab:

device The device name assigned by the kernel.

mountpoint The directory to which the filesystem is attached, which is user-defined.


type The filesystem type. It's okay to use "auto" in this field for most filesystems. See mount(8) for the supported filesystem types.


options Command options in a comma-delimited list. See mount(8) for a complete list

dump
If you're using the dump command for backups, this tells dump the backup interval, in days. 1 means every day, 2 means every other day, and so on.


pass This tells fsck which filesystem to check first at boot up, if it ever needs to. Make your root filesystem 1, any other Linux filesystems 2, and non-Linux filesystems 0.

Let's take a closer look at what goes in the options field. All these values belong to the defaults option:

rw Read/write.
suid Allow setuid and setgid bits to operate.
dev Interpret block and character devices.
exec Allow execution of binaries.

auto
This is used in boot scripts (Debian uses /etc/init.d/mountall.sh; Red Hat uses /etc/rc.d/rc.sysinit), indicating which filesystems are to be started at boot up.

nouser Non-root users cannot mount or unmount the filesystem.

async Asynchronous I/O, which is standard for Linux.


The defaults values are overridden by appending additional options, as on the win2k line in the /etc/fstab example above (defaults,user,ro,gid=win2k). The options are:

user
Non-root users can mount and unmount the device, provided they were the ones who mounted it

users
Any user can unmount the device.

noauto
The device must be explicitly mounted by a user and does not automatically mount at boot.

ro
The device is read-only. Be sure to use this for NTFS volumes.

noatime
Do not update the "time accessed" file attribute. This speeds up filesystem performance. It also extends the life of CD-RWs and DVD-RWs that use packet-writing, and of USB storage media, by reducing the number of writes.

gid=win2k
Assign group membership, if you are controlling access with groups.

Mounting filesystems, not devices

We tend to think of mounting devices and partitions, but strictly speaking, only filesystems are mounted. "Mount" and "attach" mean the same thing, if you want a different word to use. Some Linux distributions, such as Red Hat, use the /mnt directory. Debian uses top-level directories, such as /floppy or /cdrom. There's no hard-and-fast rule; put them wherever it suits you. Just be careful not to mount two filesystems in the same directory. If you do, the existing files will disappear until the intruder filesystem is unmounted.

Usually it's not necessary to specify the filesystem type, because mount will figure it out. First, it will probe the superblock. Currently adfs, bfs, cramfs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, ntfs, qnx4, reiserfs, romfs, udf, ufs, vxfs, xfs, and xiafs are supported. If that fails, it will try each filesystem listed in /proc/filesystems, which shows all the filesystems supported by your kernel.

source Carla Schroder

No comments:

Post a Comment