Friday, May 25, 2018

Understanding Linux : /dev

/dev

dev is the location of special or device files. It is a very interesting directory that highlights one important aspect of the Linux filesystem - everything is a file or a directory. Look through this directory and you should hopefully see hda1, hda2 etc.... which represent the various partitions on the first master drive of the system. /dev/cdrom and /dev/fd0 represent your CD-ROM drive and your floppy drive. This may seem strange but it will make sense if you compare the characteristics of files to that of your hardware. Both can be read from and written to. Take /dev/dsp, for instance. This file represents your speaker device. Any data written to this file will be re-directed to your speaker. If you try 'cat /boot/vmlinuz > /dev/dsp' (on a properly configured system) you should hear some sound on the speaker. That's the sound of your kernel! 

A file sent to /dev/lp0 gets printed. Sending data to and reading from /dev/ttyS0 will allow you to communicate with a device attached there - for instance, your modem.

The majority of devices are either block or character devices; however other types of devices exist and can be created. In general, 'block devices' are devices that store or hold data, 'character devices' can be thought of as devices that transmit or transfer data. For example, diskette drives, hard drives and CD-ROM drives are all block devices while serial ports, mice and parallel printer ports are all character devices. There is a naming scheme of sorts but in the vast majority of cases these are completely illogical.

List Block Devices in the system:
arun@Arun:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda         8:0    0 465.8G  0 disk   
├─sda1   8:1    0   350M  0 part
├─sda2   8:2    0 119.7G  0 part
├─sda3   8:3    0     1K  0 part
├─sda4   8:4    0   290G  0 part
├─sda5   8:5    0   8.6G  0 part [SWAP]
├─sda6   8:6    0  28.6G  0 part /
└─sda7   8:7    0  15.3G  0 part /home
sr0     11:0    1  1024M  0 rom 







To summarize then, the best way to list anything out in Linux is to remember the following ls commands:
  • ls - list files in the file system.
  • lsblk - list the block devices (i.e. drives)
  • lspci - list the pci devices.
  • lsusb - list the USB devices.
  • lsdev - list all the devices.

 Devices are defined by type, such as 'block' or 'character', and 'major' and 'minor' number. The major number is used to categorize a device and the minor number is used to identify a specific device type. For example, all IDE device connected to the primary controller have a major number of 3. Master and slave devices, as well as individual partitions are further defined by the use of minor numbers. These are the two numbers precede the date in the following display:

No comments:

Post a Comment