8

How to create the character device files?

I have tried cat > xxx^c

But I can't create a character device file

  • somehow related (but not dupplicate) to http://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files – Archemar Sep 18 '15 at 13:23
  • 1
    see man mknod however, I am not sure you speaks about "character device file", you usually never create it manually. – Archemar Sep 18 '15 at 13:37

5 Answers5

6

You can create device file using of mknod command provided by linux.

mknod <name> <type of device c:- character b:- block device etc> <major> <minor>

example:

mknod test_device c 89 1

for you case you have to provide c , as you are creating character device file. After creating device file you also have to change permissions of file if you want to manipulate file in future.

Archemar
  • 31,554
2

With mknod:

mknod <name> c <major> <minor>

Read 'linux device drivers' (free book) to understand all details about what is character device.

gena2x
  • 2,397
1

Find the character device number in /proc/devices

For character devices that dynamically allocate device numbers, which is the norm to avoid conflicts, find the number with cat /proc/devices, which contains lines like:

195 nvidia-frontend

Automation:

insmod /device_name.ko
dev="device_name"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0

Here's an example that also contains a module for you to try out: Understanding character device (or character special) files

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102
0

It may be worth looking at the lofi command to see if it fits your purpose

from the description in the man page

"The lofi file driver exports a file as a block device"

Example I just tried

mkfile -n 512k test_file

sudo lofiadm -a ~a6098/test_file /dev/lofi/1

ls -l /dev/lofi/1
lrwxrwxrwx   1 root     root          29 Sep 18 14:33 /dev/lofi/1 -> ../../devices/pseudo/lofi@0:1

ls -l /devices/pseudo/lofi@0:1  
brw-------   1 root     sys      147,  1 Sep 18 14:33 /devices/pseudo/lofi@0:1
0

When you have a cpio file with "character special file specified within", then you will need to use root to unpack (sudo cpio -idmv) otherwise the character special file will not be created.

So yes, another way to create character special file (by copying from existing character special file):

a. Create the file listing the files to be copied:

enter image description here

b. create the cpio file itself (must be running as root):

enter image description here

(note that "cpio -it" is just to list and verify)

c. from the cpio file, recreate the character file in another directory:

enter image description here

(here running as root is necessary, and we can see that "cpio" itself is using mknod command to create the special files).