Identify the type of device (disk) on Linux

Source: Internet
Author: User
Tags sprintf

1. Devices on Linux

In the Linux operating system, various device drivers (devices driver) manage various devices (device) through device controllers, as shown in the following relationship:

Among these devices,

    • Devices managed by the same device driver have the same major number, which can be regarded as the category numbers of the device and used by the kernel to identify a class of devices
    • Each device in the same type of device managed by the same device driver has a different minor number, which can be seen as a device number that is driven by the device to identify each device

There are three major types of device drivers:

    • Packet-oriented network device driver (package oriented-driver)
    • Block-oriented storage device driver (block oriented storage device driver) provides buffered (buffered) access to devices.
    • Byte-oriented character device drivers (byte oriented char driver), sometimes also known as bare devices (raw devices), provide non-buffered direct device access (unbuffered directly access), such as serial devices, Cameras, sound devices and more. In fact, devices other than network devices and storage devices are some kind of character device.

In addition, there is a class of devices called pseudo-devices (pseudo device), which are software devices. The device on Linux does not have to have hardware devices, such as/dev/null,/dev/zero, etc.

More about character devices and block devices:

    • Block devices can only accept input and return output in blocks, while character devices are in bytes. Most devices are character devices because they do not need to be buffered and do not operate at a fixed block size.
    • Block devices have buffers for I/O requests, so they can choose in what order to respond, and character devices need not be buffered and read and written directly. The order of read and write is significant for storage devices because the sequential sector is faster to read and write than the detached sector.
    • Character devices can only be read and written sequentially, while block devices are randomly accessible. Although block devices can be accessed randomly, the sequential organization of block device access can improve performance for mechanical devices such as disks.

The various applications of user space are operated by device driver:

This is the case in more detail:

(Photo source)

As you can see from this diagram:

    • Network device driver, there are Packet Scheduler (Packet Scheduler), network protocol layer (networks protocols), NetFilter (firewall) and Scoket layer, wherein the network device drives the socket as the application layer interface
    • Block device drivers, respectively, there are I/O Scheduler, the general block layer (generic block layers) and the file system, wherein the block device driver to the device file as the application layer of the access port
    • Character device drivers, with line discipline and terminals respectively, where terminals as the access interface for and application

"All Files" in the Linux system. Each device has a device file in the/dev directory, such as/DEV/SDA for the first scsi/ide disk, and/dev/vda for the first virtio. Applications access these devices by accessing these device files as if they were operations files, and the interfaces that can be used include:

    • int open (const char *path, int oflag, ...)
    • int close (int fd);
    • ssize_t Write (int fd, const void *buf, size_t nbyte)
    • ssize_t Read (int fd, void *buf, size_t nbyte)
    • int ioctl (int d, int request, ...)

On Linux systems, device drivers can be dynamically loaded and deleted

    • Lsmod-Lists the modules that are currently loaded
    • Insmod <module_file>-Insert/load The specified module file
    • Modprobe <module>-Insert/load The specified module, and all dependent
    • Rmmod <module>-Remove/unload The specified module
2. Major and minor number2.1 for Linux devices are obtained with LS

Major and number are mentioned above. Simply, the two numbers of the device can be seen from the output of the LS command:

[Email protected]:/home/sammy# CD/Dev[email protected]:/dev# LS-Ltotal0 cRW-------1Root rootTen, 175Jul -  the: -AGPGARTCRW-------1Root rootTen,235Jul -  the: -autofsbrw-RW----1Root disk7,5Jul -  the: -LOOP5BRW-RW----1Root disk7,6Jul -  the: -LOOP6BRW-RW----1Root disk8,0Jul -  the: -SDABRW-RW----1Root disk8,1Jul -  the: -SDA1BRW-RW----1Root disk8,2Jul -  the: -SDA2BRW-RW----1Root disk8,5Jul -  the: -SDA5CRW--W----1Root TTY4,TenJul -  the: -TTY10CRW--W----1Root TTY4, OneJul -  the: -Tty11
    • A line beginning with ' C ' means that the device is a character device, and a row beginning with ' B ' indicates that this is a block device.
    • 10,175 of these two digits, the preceding 10 represents major number, and the subsequent 175 represents minor.
2.2 Settings for major and minor values

Historically, the major number of the device was registered, and the device manufacturers registered their devices in http://www.lanana.org/with major number. The static major numbers assigned in the Linux 2.6 kernel can also be seen from the http://www.lanana.org/docs/device-list/devices-2.6+.txt.

However, now, the registration site has no one to maintain, replaced by a dynamic distribution system. The issuer is the Udev module of the Linux kernel. It will ensure that the combination of <major Number>:<minor number> is unique in this system, and beyond this range, it will not guarantee its uniqueness. Once assigned, you can read out the assigned major numbers from the/proc/device file, such as:

2Pty3Ttyp4TTYs6LP7VCsTenMisc -input - Sound +SG theUsbblock Devices:2FD8SD OneSR $SD theSd
3. Identify the disk Type 3.1 identification process according to major and Minior numbers

Follow these steps to identify the disk type:

(1) Use the Stat command to get the major and minor numbers of the device files. Note that the result is 16 binary.

[Email protected]:/dev# stat-c%t/dev/#minornumber0[email protected]:/dev# stat-c%t/dev/ VDB Ten [email protected]:/dev# stat-c%t/dev/SDA0[email protected]:/dev# stat-c%t/dev/  #majornumber Fd[email protected]:/dev# stat-c%t/dev/vdbfd[email protected]:/dev# Stat-c%t/dev/SDA8

(2) Convert 16 binary digits to 10, and stitch the string/sys/dev/block/$decmajor: $minor/device/driver

/sys/dev/block/253:0/device/driver/sys/dev/block/253:/device/ driver/sys/dev/block/8:0/device/driver

(3) Call the READLINK-F command to get device driver

[Email protected]:/dev# readlink-f/sys/dev/block/253:0/device/driver/sys/bus/virtio /drivers/virtio_blk

[Email protected]:/dev# readlink-f/sys/dev/block/253:16/device/driver
/sys/bus/virtio/drivers/virtio_blk
[email protected]:/dev# readlink-f/sys/dev/block/8:0/device/driver/sys/bus/ Scsi/drivers/sd

As you can see from the output,/dev/vda and/dev/vdb are all virtio-block types of devices, and/DEV/SDA are SD-or SCSI-type devices.

Common naming:

    • FD: Floppy Drive
    • Hd:ide disk
    • SD:SCSI disk
    • Tty:terminals
    • Vd:virtio disk
Implementation of 3.2 Virtio block driver

The implementation code for the VIRTIO-BLK driver is https://github.com/spotify/linux/blob/master/drivers/block/virtio_blk.c. You can see how major and minor number are assigned, and how the device is named.

(1) Device naming method

if(Index < -) {sprintf (vblk->disk->disk_name,"vd%c",'a'+ Index% -); } Else if(Index < ( -+1) * -) {sprintf (vblk->disk->disk_name,"vd%c%c",            'a'+ Index/ --1,'a'+ Index% -); } Else {        ConstUnsignedintM1 = (Index/ --1) / --1; ConstUnsignedintM2 = (Index/ --1) % -; ConstUnsignedintM3 = index% -; sprintf (vblk->disk->disk_name,"vd%c%c%c",            'a'+ M1,'a'+ m2,'a'+m3); }

Visible:

    • The name of the VIRTIO-BLK device begins with ' VD '. Increase from ' VDA ' to 26 or less, to ' vdz '; if more than 26, it will grow from ' vdaa ' to ' vdzz '; The maximum can be increased to ' vdzzz '.
    • The name is determined after the device is loaded, regenerated after a reload or a system restart, so its name may change for the same device. My other article understands QEMU/KVM and Ceph (3): Storage volume hooking and device name talk about the problem caused by this change.

(2) Major number is obtained by the kernel registration

Static int __init init (void) {    register_blkdev(0"virtblk ");     if 0 )        return  major;     return register_virtio_driver (&virtio_blk);}

Register_blkdev This method is a kernel system call that registers a block device and needs to specify the main device number. If the specified device number is 0, one is automatically assigned by the system. After the method is called, you can see the block device and its major number in the/proc/devices file.

(3) Minor number is converted from the index of the device

Vblk->disk->first_minor = Index_to_minor (index);

Reference Links:

    • Https://en.wikipedia.org/wiki/Device_file

Identify the type of device (disk) on Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.