2

When using command mount for a device, I was wondering about the following questions:

  1. Since device file is a parameter to mount, how does one know what the device file for a device is in general?
  2. Do I have to create in advance the directory to which the device is mounted to, if it does not exist yet? I saw this was said to be required, but my CD with name "mycd" is automatically mounted to /media/mycd, which doesn't exist beforehand.
  3. can a device be mounted to several places, without unmounting?

Thanks and regards!

Tim
  • 101,790

2 Answers2

6

(You didn't specify your operating system. I'm assuming it's some variant of GNU/Linux, the general concept applies to other UNIXes as well; details may not.)

1. How does one know what the device file for a device is in general?

Basically, you have to know which device file name corresponds to which device.

Sources of this information are the Linux kernel documentation, the udev configuration files (look into /etc/udev) and the MAKEDEV script.

The correct explanation is quite longer here: the Linux kernel identifies devices by a pair of numbers, called the "major" and the "minor" device numbers. Any device file having the major and minor number of your CD-ROM device will be treated by the kernel as that CD-ROM device; so you could create (see the mknod command) a CD-ROM device /my/cdrom and use that; likewise, you could use any naming convention you like for any device. However, so much system software depends on finding a device by name that it's too much work to change device names from the "standard".

The actual device names used on the system are partly the result of history (e.g., the /dev/sdX and /dev/hdX names for disk drives - somebody started using those in the beginning of times and the name stuck), part the result of an agreement between the people developing some low-level parts of the system (mainly, the kernel, libC and udev).

2. Do I have to create in advance the directory to which the device is mounted to?

Yes, mount will not create that directory for you.

The reason you see the mount points for CDs, USB sticks and other devices automagically appearing into /media is that some daemon process has created that for you. (On GNU/Linux running the GNOME desktop it goes roughly as follows: you insert the CD, the mount directory is created, the CD is mounted and -possibly- a file manager window is opened. Almost everything can change, depending on the exact Linux version and distribution.)

But on the command-line, you're on your own and have to create the mount point yourself.

3. Can a device be mounted to several places, without unmounting?

If you mean "how to make the contents of the CD appear in various places of the filesystem", then yes, you can do that using a feature called "bind mount".

Bind mount can be "replicate" any directory on the filesystem appear in another, disjoint, part of the filesystem. For instance, you could give the command:

mount --bind /var/tmp /mnt

and this will make replicate the contents of /var/tmp into the directory /mnt: if you create a file /var/tmp/foo, you will see the same file appearing as /mnt/foo.

Further reading

You can find more information on mount and its operation at:

  • Thanks! (1) My OS is Ubuntu, a kind of Linux. Is it GNU/Linux? Is GNU/Linux a special kind of Linux? (2) I would like to know for other OSes as well, i.e. just want to know the general idea. – Tim May 30 '11 at 21:01
  • It's GNU/Linux. 2) This covers other unices as well.
  • – boehj May 30 '11 at 21:09
  • @Tim: (1) About the reason why some people (including myself) call the operating system GNU/Linux, you can read this page: http://www.gnu.org/gnu/why-gnu-linux.html and yes, Ubuntu is a kind of GNU/Linux; (2) the "general idea" of mounting devices is the same on all UNIX variants, but detailing all the differences will probably take a book chapter :-) – Riccardo Murri May 30 '11 at 21:09
  • +1 - Nice answer. I'd add that you don't necessarily need to use mount --bind dir1 dir2 in order to have multiple mounts of the same filesystem. You could also achieve this by doing, say, $ mount foo.iso -o loop /mnt/dir1 && mount foo.iso -o loop /mnt/dir2. – boehj May 30 '11 at 21:18
  • For those reading my comment above, note that that should be # mount .... – boehj May 30 '11 at 21:32