What happens if you delete a device file?

5,523

Solution 1

Those are simply (special) files. They only serve as "pointers" to the actual device. (i.e. the driver module inside the kernel.)

If some command/service already opened that file, it already has a handle to the device and will continue working.

If some command/service tries to open a new connection, it will try to access that file and fail because of "file not found".

Usually those files are populated by udev, which automatically creates them at system startup and on special events like plugging in a USB device, but you could also manually create those using mknod.

Solution 2

Device files are actually a filesystem alias for an entry in the kernel's device table. If you look at the /dev files with "ls -l" you'll see they have a major device number and a minor device number. If you delete the files from the filesystem, you can always recreate them using the appropriate tools to relink the special file to the entry in the kernel device table -- see mknod(1).

Solution 3

From that moment on, they can only be accessed by programs that had those devices already open. So, it's no way to unmount filesystems. And with udev, a reboot might restore those devices. A strange way to learn unix.

Share:
5,523

Related videos on Youtube

alpha
Author by

alpha

Star wars. Linux. Star Wars

Updated on September 18, 2022

Comments

  • alpha
    alpha over 1 year

    I roughly know about the files located under /dev.

    I know there are two types (character/block), accessing these files communicates with a driver in the kernel.

    I want to know what happens if I delete one -- specifically for both types of file. If I delete a block device file, say /dev/sda, what effect -- if any -- does this have? Have I just unmounted the disk?

    Similarly, what if I delete /dev/mouse/mouse0 -- what happens? Does the mouse stop working? Does it automatically replace itself?

    Can I even delete these files? If I had a VM set up, I'd try it.

  • Gogeta70
    Gogeta70 over 6 years
    This doesn't really answer the question...
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 6 years
    "Usually those files are populated by udev" Usually, but on Linux. There's makedev command which usually does the job, and unlike udev it seems to be present among most unix-like os.
  • Ruslan
    Ruslan over 6 years
    @Gogeta70 which part of the question does this post leave unanswered?
  • Michael Mrozek
    Michael Mrozek over 6 years
    @RonJohn The question is tagged linux and udev
  • RonJohn
    RonJohn over 6 years
    @MichaelMrozek forgive me if it came across as accusatory, but I'm truly curious.
  • RonJohn
    RonJohn over 6 years
    @SergiyKolodyazhnyy does something in the boot script scan all detected devices (and partitions) and makedev each one of them?
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 6 years
    @RonJohn Yes,but with a "but". The Ubuntu Hacks book which is from like '06 shows there is /etc/init.d/makedev. The freebsd manual mentions MAKEDEV appeared in 4.2BSD (from 1983). I think in 1983 it would be just as logical to make MAKEDEV automated via init as in 06
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 6 years
    @RonJohn The "but" part of my comment is that when I mentioned '06 Ubuntu, I can't tell you how it does things now, because I've sort of switched away from it now.