Sunday 30 March 2008

GNU/Linux SYSTEM DIRECTORIES and their FILESYSTEMS

Executing $ ls / lists the top level of the root filesystem tree. There appear directories like:
/dev, /etc, /proc, /sys, /var, /tmp and others.

In this article we are going to deal with directories that have special filesystems associated with them.


For a general view of the filesystem hierarchy we can visit the Filesystem Hierarchy Standard page.

FHS standard is a set of guidelines about where to place files and directories within UNIX based operating systems.


Check which filesystems are mounted


Two commands allow us to examine which filesystems and where are currently mounted in our system.

$ mount
or
$ cat /etc/mtab


Their response will be something like that:
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
/sys on /sys type sysfs (rw,noexec,nosuid,nodev)
varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
udev on /dev type tmpfs (rw,mode=0755)
devshm on /dev/shm type tmpfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
lrm on /lib/modules/2.6.22-14-generic/volatile type tmpfs (rw)
securityfs on /sys/kernel/security type securityfs (rw)


We are specially interested in /proc, /sys, /var/run, /var/lock, /dev, /dev/shm, /dev/pts, /sys/kernel/security




/proc directory


procfs is a virtual file system, dynamically created in memory. It is usually mounted in /proc.

The proc file system provides an interface to kernel internal data structures. It allow us obtain information about the system and change certain kernel parameters at runtime (sysctl)


It can be accessed in a text mode way:

e.g: $ cat /proc/meminfo #shows how memory is distributed.



/proc/sys directory allows us change kernel parameters.

e.g $ sudo echo "fooname" > /proc/sys/kernel/hostname #changes your box hostname.


Every process has a subdirectory at proc filesystem named by its PID.


MOUNTING proc FILESYSTEM

$ mount -t proc proc /mnt/proc # This command mounts a proc filesystem at /mnt/proc




/sys directory


sysfs is a RAM-based filesystem initially based on ramfs. It provides a way to export kernel data structures, their attributes, and the linkages between them to userspace.


MOUNTING sys FILESYSTEM

$ sudo mount -t sysfs sysfs /sys # The sys filesystem is always compiled and with this command you can access it.




tmpfs filesystem


Basically, tmpfs is a type of filesystem which uses RAM (and swap) as its device, but it is dynamically allocated, unlike a ramdisk. Therefore, it only takes as much RAM as it needs.

tmpfs is a filesystem, not a block device; you just mount it, and it's there. You don't need to use mkfs command.

This filesystem is used in several files and devices like /tmp, /dev/shm, etc.


MOUNTING A tmpfs FILESYSTEM

$ sudo mount -t tmpfs tmpfs /mnt/tmpfs

/mnt/tmpfs will initially have a very small capacity, but as files are copied and created, the tmpfs filesystem driver will allocate more VM and will dynamically increase the filesystem capacity as needed. And, as files are removed from /mnt/tmpfs, the tmpfs filesystem driver will dynamically shrink the size of the filesystem and free VM resources, and by doing so return VM into circulation so that it can be used by other parts of the system as needed

Speed is the great advantage of tmpfs. As it usually resides in RAM, accesses are almost instantaneous. Swap also provides good performance.

tmpfs is not preserved between reboots, because RAM memory is volatile.


In order to avoid virtual memory exhaustion we can pass tmpfs size as an option:

$ sudo mount tmpfs /tmp -t tmpfs -o size=64m # Limits its size to 64 megabytes.


More info at:

http://www.ibm.com/developerworks/library/l-fs3.html#2




/dev directory


In Unix philosophy every device is attached to a file.
/dev directory is the place where device files are located.

http://en.wikipedia.org/wiki/Udev

udev is the device manager for the Linux 2.6 kernel series. Its primary function is managing device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles the /dev directory and all user space actions when adding/removing devices, including firmware load.

$ sudo mount -t tmpfs udev /dev




/dev/pts directory


The devpts file system provides an interface to pseudo terminal (pty) devices. It is typically mounted at /dev/pts. A new pty device file is dynamically created when the /dev/ptmx pty master multiplex device is opened.

$ sudo mount -t devpts devpts /dev/pts




/dev/shm directory


This device implements POSIX shared memory.

E.g: Shared memory can be used as a quick way to share data between programs.


$ sudo mount -t tmpfs shm /dev/shm




/tmp directory


This place is where programs store their temporary files. It is not guaranteed that data will be preserved between program invocations.

/tmp should be cleared by the system every boot.

$ sudo mount -t tmpfs tmpfs /tmp




securityfs filesystem


This is a filesystem used by linux kernel security modules.
It is mounted in /sys/kernel/security

$ sudo mount -t securityfs securityfs /sys/kernel/security




/var/run directory


Contains system information data and it is cleared every system boot.

$ sudo mount -t tmpfs varrun /var/run




/var/lock directory


Lock files should be placed in this directory.

$ sudo mount -t tmpfs varlock /var/lock




REFERENCE


There is some good info in the kernel documentation about filesystems.

To read it, run these commands:

$ sudo aptitude install linux-doc-2.6.22
$ zless /usr/share/doc/linux-doc-2.6.22/Documentation/filesystems/proc.txt.gz
$ zless /usr/share/doc/linux-doc-2.6.22/Documentation/filesystems/sysfs.txt.gz
$ zless /usr/share/doc/linux-doc-2.6.22/Documentation/filesystems/tmpfs.txt.gz

NOTE: Substitute 2.6.22 by your linux version.

0 comentarios: