Manual creation of device nodes under / dev / in linux

Keywords: Linux

Manual creation of device nodes under / dev / in linux

/ There are many device node files in dev / directory, such as file / dev/sda of u disk, file / dev/mmcblk0 of mmc card. These files are usually created automatically by udev or mdev program after uevent event is detected. We can also create it manually with the mknod command.


The following mmc card as an example, describes how to create its device node file.

1. mknod command

The format of the command is:
mknod device name device type (character: c, block: b) master device number and slave device number

Among them, the main device number is used to distinguish different types of equipment, while the secondary device number is used to distinguish multiple devices of the same type.

Therefore, to create a device node, you need to know the device type and its master-slave device number.

2. Getting device type

When the device is inserted, we can see the type of device in / proc/devices, as shown below. Character devices and block devices are displayed separately, with the main device number on the left and the device name on the right. Therefore, the MMC card belongs to the Block device, the main device number is 179, the device name is mmc.
/ # cat /proc/devices 
Character devices:
  1 mem
  2 pty
  3 ttyp
  4 /dev/vc/0
...
254 ttySDIO

Block devices:
  1 ramdisk
259 blkext
  7 loop
179 mmc
/ # 

3. Obtain the master-slave device number of the device

/ The main device number is only shown in proc/devices, but not from the device number. We can get it from the sys system, as shown below.
(1) / sys/block / you can see that the corresponding directory of mmc card devices is mmcblk0.
(2) In the / sys/block/mmcblk0/uevent node, we can see that the main device (MAJOR) of the mmc card is 179, from the device number (MINOR) is 0, and the device type (DEVTYPE) is disk.
/ The sys/block/mmcblk0/mmcblk0p1 directory indicates that the disk has one partition
(3) In the / sys/block/mmcblk0/mmcblk0p1/uevent node, we can see that the main device (MAJOR) of the mmc card is 179, the MINOR is 1, and the device type (DEVTYPE) is partition.
/ # ls /sys/block/
loop0    loop4    mmcblk0  ram11    ram15    ram5     ram9
loop1    loop5    ram0     ram12    ram2     ram6
loop2    loop6    ram1     ram13    ram3     ram7
loop3    loop7    ram10    ram14    ram4     ram8
/ # ls /sys/block/mmcblk0/
bdi         device      mmcblk0p1   removable   slaves      uevent
capability  ext_range   queue       ro          stat
dev         holders     range       size        subsystem
/ # cat /sys/block/mmcblk0/uevent 
MAJOR=179
MINOR=0
DEVTYPE=disk
PHYSDEVPATH=/class/mmc_host/mmc0/mmc0:0002
PHYSDEVBUS=mmc
PHYSDEVDRIVER=mmcblk
/ # 
/ # cat /sys/block/mmcblk0/mmcblk0p1/uevent 
MAJOR=179
MINOR=1
DEVTYPE=partition
PHYSDEVPATH=/class/mmc_host/mmc0/mmc0:0002
PHYSDEVBUS=mmc
PHYSDEVDRIVER=mmcblk
/ # 

4. Creating Nodes

Use the mknod command to create disk and partitioned nodes, respectively.
mknod /dev/mmcblk0 b 179 0
mknod /dev/mmcblk0p1 b 179 1
Once created, you can see the following newly created nodes in the / dev / directory.
/ # ls /dev/mmcblk0* -l
brw-r--r--    1 root     root     179,   0 Jan  1 00:34 /dev/mmcblk0
brw-r--r--    1 root     root     179,   1 Jan  1 00:36 /dev/mmcblk0p1

5. Mount partition

After mounting the partition, we can access the files in the mmc card.
/ # mount /dev/mmcblk0p1 /mnt/
/ # ls /mnt/
a                dd               x.docx
/ # 

6. Delete Nodes

Deleting nodes is like deleting ordinary files, as shown below.
/ # rm /dev/mmcblk0p1 -f
/ # 
/ # ls /dev/mmcblk0* -l
brw-r--r--    1 root     root     179,   0 Jan  1 00:34 /dev/mmcblk0
/ # 

Okay, so far, the method of manually creating nodes has been introduced, ^-^.

Posted by The.Pr0fess0r on Mon, 15 Jul 2019 17:28:25 -0700