Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Windows-to-Linux roadmap: Part 6 Working With Partitions
By Chris Walden - 2004-07-08 Page:  1 2 3 4

Mount Up

The term for adding a device to the file system is mounting. Linux will automatically mount a / (root) file system. There may also be a separate /boot file system, containing the core kernel boot files. Linux will also mount some special file systems. The swap space is not shown as a part of the file system, but is handled by the kernel. However, other special file systems such as proc, are seen as a normal part of the file system, and its contents can be handled just like normal files.

What is /proc?
The /proc file system is an excellent example of the difference between thinking in Windows and thinking in Linux. /proc contains a virtual representation of various aspects of the running system. There is information about IRQ settings, memory usage, loaded device drivers, network status, and much, much more. There is even a file called /proc/kcore, which is a virtual representation of all of the used system memory. Each of these files can be parsed just like a normal text or binary file. Some files can be written to change the behavior of the running kernel, without rebooting. For example, to turn on IP-forwarding for the first active ethernet device on the system, you can use a file command:

echo 1 > /proc/sys/net/ipv4/conf/eth0/forwarding

The main benefit of such a system is that you can use simple scripting techniques to do very deep and powerful things to your running system.

Other file systems, such as removable media or remote file systems, will need to be manually mounted. When mounting a file system, you will need to know the correct way to reference it from Linux, and have an empty directory to use as a mount point. For removable media, Linux will probably create mount points for you during installation. In Red Hat Linux, the cdrom device is set up to mount to the directory /mnt/cdrom. That means that when you put a CD into the CDROM device, you enter the command:

mount /mnt/cdrom

The CD is added to the file system and the CDROM device is locked so that it cannot be accidentally ejected. To access the contents of the CD, simply use the directory /mnt/cdrom. When you are finished using the CD, you can remove it from the file system with the command:

umount /mnt/cdrom

The /mnt/cdrom directory will empty and the CDROM device will be unlocked. You can now safely eject the CD. The same behavior would be used with other removable media, such as a floppy drive (/mnt/floppy).

Running mount with no arguments will show you the currently mounted file systems.

Why all of this locking behavior?
Remember that Linux is not only multi-user, but also multi-session. That means that several users may be logged into the system, running processes and using resources all at the same time. This is not the same as logging in to use a file share in Windows. Each user can use the system just as though they were sitting at the console. Linux maintains stability by not arbitrarily releasing file systems that are currently in use, and by locking CDs so they cannot be ejected until the file system has been unmounted and no one is using it.

The /etc/fstab file

The association between a device and its mount point is configured in the /etc/fstab file. It can be human-edited, or maintained with an administrative tool. Here is a sample of /etc/fstab/:

Understanding /etc/fstab

/dev/hda5 ext3defaults1 1
/dev/hda2/bootext3exec,dev,duid,rw1 2
/dev/hda6swapswapdefaults0 0
/dev/scd0/mnt/cdromautoro,noauto,exec0 0
none/dev/ptsdevptsid=5,mode=6200 0
none/procprocdefaults0 0
none/dev/shmtmpfsdefaults0 0

Each line represents a file system to be mounted. The first column identifies the device to be mounted. The second column contains the mount point, the location for that device in the file system. The third column identifies the file system type. The fourth column has options for how that file system should be handled. The last column contains flags about that file system. The first number is either 1 or 0 and indicates whether the system should be copied with a dump (an option for system backups). The second number is 0, 1, or 2 and indicates the order in which the file system should be checked upon boot. 0 is not checked at all. 1 is checked first and should be used for the root (/) file system. Other file systems should be 2.

In the fstab file listed above, the root file system is on the first IDE hard drive in the fifth partition, the first logical drive in an extended partition. The /boot file system, where the kernel startup files are located, is on the first IDE hard drive in the second primary partition. Swap space is located in the first IDE hard drive in the sixth partition, the second logical drive in an extended partition. Other file systems listed show their device as "none." We'll cover these shortly. For now let's concentrate on physical disks.

Everything's a file
In Linux, file systems are represented by a file-like name. All of the files in the /dev directory are special files called nodes that link to physical devices through the device driver. This allows you to do some interesting things. For example, to make an ISO image of a CD, you can use the cp (copy) command:

cp /dev/cdrecorder MyCD.iso

Rather than copying the file structure of the CD, a binary image is copied.

This file-centric approach also allows you to alias device names to something more meaningful. For example, there is usually an alias called /dev/cdrom that points to the physical CDROM device, often /dev/hdc. Once that alias is created, you can always refer to the device as /dev/cdrom, which is much easier to remember. This aliasing technique also allows you to standardize scripts across systems that might have different physical configurations.

The options in the fourth column will vary depending on the file system type. In the example above, / and /boot are mounted with the "default" options. That, is they are mounted automatically as read-write with asynchronous I/O. Only root can mount or unmount the device but users can execute binaries and use the "sticky bit" (covered later). The file system will be handled as a block character device. For the /mnt/cdrom, however, the options are different. It is not automatically mounted and will mount as a read-only file system. Users will be able to execute scripts and programs in that file system.

Adding file systems

You can add file systems into /etc/fstab by adding new lines to the file. As a practical example, I have a RAID device that contains file resources for use by the department. This device will only contain data files and will be kept separate from the operating system, so it will be easy to move the device to another system in case of a hardware failure. The RAID has already been configured, and is recognized by Linux as /dev/sdc, the third SCSI device. A journaled ext3 file system has been created on the first partition, so we can access it as /dev/sdc1. I want to have this RAID automatically mount into the file system when the computer boots.

I add the following line to the /etc/fstab:

/dev/sdc1 /data ext3 defaults 0 0

This will cause the RAID to be mounted at boot just like the / and /boot systems. Now I simply create the directory I specified as my mount point:

mkdir /data

Once this empty directory is created, we can mount the file system into it:

mount /data

The RAID is now associated to /data. If the system ever reboots, then /data will automatically be mounted.

View Windows-to-Linux roadmap: Part 6 Working With Partitions Discussion

Page:  1 2 3 4 Next Page: Partitions

First published by IBM developerWorks

Copyright 2004-2017 All rights reserved.
Article copyright and all rights retained by the author.