Wednesday, September 7, 2011

Understanding Linux File Types.


There are seven basic types of file types in Linux.
Regular Files
Directories
Character  Device Files
Block Device Files
Local Domain Sockets
Named Pipes
Symbolic Links

You can use the “ls -l” command to see the various types of files.  In the following example, the first character in the output is a “-”, which indicates that it’s a regular file.

ls -la /var/log/messages
-rw——- 1 root root 204909 Jun  5 10:50 /var/log/messages

The next example shows that it is a directory as it starts with a “d”.

ls -ld /etc
drwxr-xr-x 105 root root 12288 Jun  5 08:36 .

File Type Encoding When Using ls
Regular file        -
Directory        d
Character Device     c
Block Device        b
Local Domain Socket    s
Named Pipe        p
Symbolic Link        l

Device files facilitate the communication between hardware and software.  The kernel manages modules that know how to communicate with system devices.  These device drivers create a standard method of communication with the hardware.  They look like regular files.  When the kernel receives a request for a character or block device it contacts the right device driver to take care of the communication.

Device files are assigned both a “major” and a “minor” number.  (An “ls -l” command will show you these instead of the file size that you’d see for regular files.)  The major number refers to the device driver, and the minor number tells you which physical device goes with that file.  For example, the device files “/dev/lp0″  and “/dev/lp1″ would both have a major number of “6″, indicating that they both represent parallel ports.  Their minor numbers of “0″ and “1″, respectively, refer to two different “lp” devices on the same system.

Major and minor numbers are very important to understand when you are scripting for software RAID devices for example.  RAID devices are indicated with md0, etc.   The major device number is 9 and then minor device number starts with 0 and will have to be incremented as you will need to create new RAID devices in order to add more than one RAID device on a server.

Local Domain Sockets, often called “UNIX Domain Sockets”, allow local processes to communicate with each other.  This is similar to how network sockets allow global communications with other hosts.  You can use the netstat command to view domain sockets.

netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                    1547   @/org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    9009   @/org/freedesktop/hal/udev_event
unix  28     [ ]         DGRAM                    7384   /dev/log
unix  3      [ ]         STREAM     CONNECTED     23307  /tmp/orbit-mike/linc-151b-0-472aed0fe5012
unix  3      [ ]         STREAM     CONNECTED     23306
unix  3      [ ]         STREAM     CONNECTED     23285
unix  3      [ ]         STREAM     CONNECTED     23274  /tmp/orbit-mike/linc-151b-0-472aed0fe5012

Named pipes, also known as “FIFO” files, are another type of inter-process communications device.

Symbolic links are special files that point to either another file or to a directory.

Example:
ln -s /var/log/dmesg /home/mike/dmesg

Here you can see the link that was created.

lrwxrwxrwx  1 mike mike   14 Jun  5 14:13 dmesg -> /var/log/dmesg

Device nodes will allow users access to device files.  These can be listed with:

ls -l /dev

ls -l /dev
total 0
crw——-  1 root root    36,   8 May  9 02:10 arpd
lrwxrwxrwx  1 root root          3 May  9 02:10 cdrom -> hdc
lrwxrwxrwx  1 root root          3 May  9 02:10 cdwriter -> hdc
crw——-  1 mike root     5,   1 May  9 08:10 console
lrwxrwxrwx  1 root root         11 May  9 02:10 core -> /proc/kcore
crw——-  1 root root    36,  14 May  9 02:10 dnrtmsg
lrwxrwxrwx  1 root root          3 May  9 02:10 dvd -> hdc
lrwxrwxrwx  1 root root          3 May  9 02:10 dvdwriter -> hdc
crw——-  1 root root    13,  64 May  9 02:10 event0
lrwxrwxrwx  1 root root         13 May  9 02:10 fd -> /proc/self/fd
brw-rw—-  1 mike floppy   2,   0 May  9 08:10 fd0
—cut—
lrwxrwxrwx  1 root root          3 May  9 08:10 floppy -> fd0
crw-rw-rw-  1 root root     1,   7 May  9 02:10 full
crw——-  1 root root    36,   3 May  9 02:10 fwmonitor
srwx——  1 mike root          0 May  9 08:13 gpmctl
brw-rw—-  1 root disk     3,   0 May  9 02:10 hda
brw-rw—-  1 root disk     3,   1 May  9 02:10 hda1
brw-rw—-  1 root disk     3,   2 May  9 02:10 hda2
brw-rw—-  1 root disk     3,   3 May  9 02:10 hda3
brw-rw—-  1 root disk     3,  64 May  9 02:10 hdb
brw-rw—-  1 root disk     3,  65 May  9 02:10 hdb1
brw——-  1 mike disk    22,   0 May  9 02:10 hdc
brw-rw—-  1 root disk    22,  64 May  9 02:10 hdd
brw-rw—-  1 root disk    22,  65 May  9 02:10 hdd1

The device nodes consist of two types; character (stream-orientated) and block (random access).  In the list you can see the “c” for character and the “b” for block at the start of each line.  Again, note that file ownership and permissions are a part of the device nodes.  In addition, each node has a major and minor number.  The major number represents a specific device driver that is in the kernel while the minor number points to the device it indexes.

Device nodes can be created with the /bin/mknod command:

mknod device type major minor

mknod /dev/md1 b 9 1

This command would indicate that the device md1is a block device with a major number of 9, meaning it is a software RAID device, and a minor number of 1.

The kernel source contains a document called devices.txt which lists all of the major and minor numbers.

No comments:

Post a Comment