Why I can't create a hard link from device file in other than /dev directory?

72

Solution 1

But I could only create a hard link in the /dev directory and it was not possible in other directories.

As shown by the error message, it is not possible to create a hard link across different filesystems; you can create only soft (symbolic) links.

For instance, if your /home is in a different partition than your root partition, you won't be able to hard link /tmp/foo to /home/user/.

Now, as @RichardNeumann pointed out, /dev is usually mounted as a devtmpfs filesystem. See this example:

[dr01@centos7 ~]$ df
Filesystem                      1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos_centos7-root  46110724 3792836  42317888   9% /
devtmpfs                          4063180       0   4063180   0% /dev
tmpfs                             4078924       0   4078924   0% /dev/shm
tmpfs                             4078924    9148   4069776   1% /run
tmpfs                             4078924       0   4078924   0% /sys/fs/cgroup
/dev/sda1                         1038336  202684    835652  20% /boot
tmpfs                              815788      28    815760   1% /run/user/1000

Therefore you can only create hard links to files in /dev within /dev.

Solution 2

A hard link cannot be used to achieve what you want, because hard links do not work between file systems.

However, you can achieve what you want with the mknod command.

  1. Run ls -l /dev/devicefile. You should see an output like this:

    crw-rw-rw- 1 root root 1, 9 Mar 29 15:46 /dev/urandom
    
  2. Take note of the number in the size column (1, 9).
  3. Run the command mknod /path/to/destination c 1 9 (substituting the values you want).

Why does this work?

Device files are effectively hard links to an abstract file implemented by the kernel or kernel drivers. Whilst you can't create hard links to file system objects from another file system, these aren't file system objects and so, by knowing their major and minor reference numbers, you can create a reference to them from any file system.

Solution 3

Hard links just create another entry in a directory, pointing to a file (where file is a file like thing, such as a directory). Therefore hard-links can not reference a file in another file-system.

Soft-links are you friend in this case. Use ln -s. Softlinks can go across filesystems, and can even point to something that is not there.

Share:
72

Related videos on Youtube

the_darkside
Author by

the_darkside

Updated on September 18, 2022

Comments

  • the_darkside
    the_darkside almost 2 years

    I have a dataframe testData which is made up of many unique ids. My objective is to identify whether or not the ids contain all of the numbers in the range of month, yday, and week

    In other words, if id has all possible values in the range in month, then it should receive a t. If id has all possible values in the range in yday, it should receive a t, and if id has all possible values in the range in week, it should receive a t. Otherwise, it should receive an f

    A sample of the data looks like this:

    > testData
       id month yday week
    1   1     1    1    1
    2   3     1    2    1
    3   4     1    3    1
    4   2     1    4    1
    5   3     3    5    1
    6   4     1    6    1
    7   2     1    7    1
    8   3     1    8    2
    9   1     1    9    2
    10  5     1   10    2
    11  3     2   11    1
    12  4     1   12    1
    13  5     1   13    1
    14  1     1   14    1
    

    The output should look something like this:

    > output
      id month yday week
    1  1     f    f    t
    2  2     f    f    f
    3  3     t    f    t
    4  4     f    f    f
    5  5     f    f    t
    

    I know that one can check if a numbers are within a certain range with findInterval(), but could someone suggest a method to check if numbers in a vector contain all integers within a range?

    > dput(testData)
    structure(list(id = c(1L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 1L, 5L, 
    3L, 4L, 5L, 1L), month = c(1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 
    1L, 2L, 1L, 1L, 1L), yday = 1:14, week = c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L)), .Names = c("id", "month", 
    "yday", "week"), class = "data.frame", row.names = c(NA, -14L
    )) 
    
  • nicola
    nicola about 7 years
    Didn't see your answer when posting mine. Guess mine is more general and can adapt to a bigger number of columns. Feel free to edit to include my Map way of dealing with all the columns and I will delete my answer.
  • Richard Neumann
    Richard Neumann about 6 years
    The partition is not the issue. It is, as you stated the cross-filesystem limitation. /dev is usually mounted as a devtmpfs filesystem. Thus you can only create hardlinks to files in /dev within /dev. You e.g. cannot even create hardlinks to /dev/null within /dev/pts, because it is mounted with devpts as a different file system.
  • dr_
    dr_ about 6 years
    @RichardNeumann Good point. I corrected my answer which was only partly correct.