Website Magazine


 

Linux Virtual File System
Roan Sez:

If you're familiar with Linux you might find this post a bit simple, but perhaps there will be one or two new bits of information.

The Linux virtual filesystem is one of the most important concepts to understand in order to set up and maintain a Linux system. Ordinary users will also benefit from understanding the basics.

The Linux virtual filesystem makes all the folders and files on all the disks and partitions appear to be one large filesystem. Not only that, but programs and the user don't have to know or be concerned with exactly which disks or partitions store the file. Folders on disks can be placed anywhere within the virtual filesystem with a few restrictions for booting.

The virtual filesystem looks like a tree (or an upside down tree if you like). The root directory is the base (top) of the tree. The Linux root directory is designated by a single slash "/".

Other directories (folders) and files appear below the root. There are some standard folders such as "/bin" and "/etc" to contain files for the Linux operating system.

Linux has to read some directories and files in order to boot. For that reason, the location of the "root" directory is a boot parameter. The location of "root" is given as a device (partition) name and a directory in that filesystem. Usually that's actually the top-level directory of the partition containing most of the Linux files.

When Linux boots, it only knows about the "root" filesystem. Other filesystems (partitions) are added to the virtual filesystem using a command called "mount". The "mount" command has two important parameteres. The first is a name identifying a device, the second is the name of an empty directory (folder) where the mounted filesystem should appear. If that directory happens to have anything in it, the contents will be hidden and inaccessible while a filesystem is mounted there. Here is an example of a mount command.

mount -t ext3 /dev/hda3 /usr/local

The above command will make the contents of the third partition on the disk drive "hda" appear under the directory name "/usr/local".

Why is that so useful? Suppose you start out with your music collection on your Linux root partition in a folder called "/musiclib". Under that folder you might have separate folders for different kinds of music. For example "/musiclib/jazz", "/musiclib/pop", "/musiclib/rap". If your disk partition gets filled up because you have a lot of "pop" music, then you might want to relocate the files to a different hard disk. So you move all the "pop" music files to another disk partition and do something like this.

mount -t ext3 /dev/hdb1 /musiclib/pop

Now how does your music player find the files? Exactly the same way that it did when they were located in the root filesystem partition, using "/musiclib/pop".

Because there are often things that should remain mounted all the time, Linux has a file called "/etc/fstab" that lists filesystems to be mounted shortly after booting.

The mount command isn't limited to mounting just hard disk partitions. Any directory (folder) on any disk can be mounted somewhere else.

mount --bind /musiclib/jazz /home/joe/jazzmusic

The above command makes the contents of "/musiclib/jazz" also appear in the virtual filesystem as "/home/joe/jazzmusic". Why do that? Usually the reason is to give "joe" diffrent file permissions than other users. For example, Joe may be able to create, modify and delete files and ordinary users may only be able to read files.

There is another way to do something similar. You can create a "link" that refers to some other file or folder. Programs accessing the link are actually accessing the real folder or file, as if the real folder or file name had been given. There are two kinds of links, soft links and hard links. For now I'm going to talk about soft links.

ln -s /musiclib/jazz /home/joe/jazzmusic

The above command creates a soft link (really a special file) called "/home/joe/jazzmusic" that refers to "/musiclib/jazz". It does basically the same thing as the "mount --bind" except that it only has to be done once (not every time the system boots) and it uses the same exact file permissions to access the folder. Joe can only write into the "/home/joe/jazzmusic" folder if he can write into the "/musiclib/jazz" folder.

Soft links remain even if the "target" they refer to is deleted. Even though the links remain, they will act like the file doesn't exist, since they don't link to anything.

So what are hard links. A hard link creates another name for a file. Both names point to the file, and neither one is a more or less direct way to access the file. The file remains on the disk until both names are deleted. You can only create hard links to files within the same filesystem, so they are less versatile than soft links. All the hard links that refer to a file share the same permissions, so you can't create hard links to a file with different permissions. Here's an example of a hard link.

ln /musiclib/jazz/countbasie.mp3 /home/joe/basie.mp3

Even if someone deletes the "countbasie.mp3" file, the "basie.mp3" name for it will still exist, and so will the actual file. A soft link would have left a useless file called "basie.mp3" that linked to nothing.

There are "special files" that can be created in the virtual filesystem. They are used to access character and block devices. There's nothing magic about the "/dev" directory. What's magic is the files in the "/dev" directory. For example "hda1". Files that represent a device are created (anywhere) using the "mknod" command.

mknod hda1 b 3 1

The above command creates a file to access a device. The name of the file is "hda1". The device is actually identified internally to Linux as major ID 3, minor ID 1. Major device numbers are defined by the Linux operating system. Minor device numbers depend on what disks or sub-devices the driver can access. The point is that you can give a device any name you like, and as long as the major and minor ID are correct you can access the device. Older versions of Linux have a static set of device names under "/dev". Newer versions of Linux create the device names under "/dev" based on the devices detected after booting.

If you want an alias for a device name it's better to create a soft link to the standard device name. That way if the device ID changes in a new version of Linux, your alias will still work correctly. In fact that's what Linux does when it creates aliases for device names automatically.

The last thing I'm going to mention about the virtual filesystem is that it can mount filesystems that aren't actually associated with hard disk partitions. For example, you can have a ram disk as the Linux root directory. Boot CDs often do that. There are also "pseudo" filesystems that really aren't used to store normal files and folders. A pseduo filesystem called "proc" will let you read information about your Linux system. You use the normal "ls" and "cat" commands to list information and you can access the "proc" filesystem within scripts just like normal folders and files. The "proc" filesystem is normally mounted as "/proc".

mount -t proc proc /proc

The filesystem type is "proc" and the device name "proc" is really not important, since it is ignored.

Well, that pretty much covers the major points. Look at the information for the various commands if you want to know more.
man mount
man ln
man mknod
man fs
man -S 5 proc