Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
2  
In the future, please ask separate questions separately — otherwise you're making things difficult if people only want to answer one of your questions. –  Gilles May 30 '11 at 20:34
add comment

2 Answers

up vote 6 down vote accepted

(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:

share|improve this answer
    
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
    
1) 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: 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
add comment

The device file is how you tell mount what device or other location you want to access. So there's no way it can guess.¹

There are several different kinds of filesystems you can mount. For disk-backed filesystems (the first kind that generally come to mind), the device is a block device. Its name is generally /dev/something, where the something part encodes the disk driver to use, which particular disk to use if that driver manages more than one, and what part of the disk to access if relevant. Different unix variants have different ways of designating devices. The official reference is your unix variant's device driver manual pages, typically section 4: AIX, FreeBSD, Linux, Mac OS X (section 4), MINIX, NetBSD, OpenBSD, Solaris 11, Unix 6th ed., Unix 8th ed., …

On Linux, here are a few other ways to find out about device names and alternate ways to designate devices. Some of these have analogs under other unix variants.

  • In /proc/partitions, there's a list of disks, disk partitions, logical volumes and other block devices. Not all of these have a filesystem on them.
  • All logical volumes have an entry in /dev/mapper.
  • The mount command accepts filesystem labels and UUIDs in lieu of a device name, specified as LABEL=foo or UUID=1234… instead of /dev/something.
  • When a device is plugged into the system, udev assigns it a device name.
  • Most Linux distributions arrange (through udev) to have various alternate names for disk-like devices, under /dev/disk. These are alternate names for the same devices. For example on the machine I'm writing this on, the device that hosts my home filesystem (which is an LVM logical volume) is variously accessible as /dev/dm-7, /dev/disk/by-id/dm-name-VolumeGroupName-LogicalVolumeName, /dev/disk/by-id/dm-uuid-LVM-LongStringOfDigitsAndLetters, /dev/disk/by-label/\x2fhome and /dev/disk/by-uuid/SomeUUID.

There are also filesystems that don't come from a disk file. For example, for remote filesystems such as NFS or Samba, the device name designates a machine name and a path or share name on that machine (e.g. machinename:/shared/directory).

Many desktop systems arrange to have notifications when a new device is plugged in, and to automatically mount recognisable filesystems.

¹ However, you can tell mount once and for all that a particular mount point corresponds to a particular device under a particular filesystem type with particular options, by recording a line in /etc/fstab.


The mount command requires that the directory (the mount point) exists. Automatic mounting systems typically create directories as needed, before doing the actual mounting (and remove the directory after unmounting).


Not all unices can mount the same filesystem in several places natively. Under Linux, this is possible through a bind mount. On most unices, you can use the bindfs FUSE filesystem for that.

share|improve this answer
    
Thanks! (1) There are many files that fit /dev/something. The various names for "something" are not familiar to me, and is there something I can read in order to learn which device a file is for based on the file name? (2) Even If I can guess what kind of device a device file is for, there may well be number in its name. How shall I know which particular device the file is for? –  Tim May 30 '11 at 20:54
    
@Tim: (1) the corresponding section 4 man pages, or for Linux the kernel documentation; but in practice you can just find a device that looks right in /proc/partitions, or explore /dev/disk/by-*. (2) In /dev/sdz42, the third letter in sda, sdb, etc, depend on the order in which the disks were detected. The number designates the partition. Again, in practice, you'd find the right disk through /proc/partition or /dev/disk/*, or by looking at the kernel logs after plugging in a device. –  Gilles May 30 '11 at 21:03
    
+1 - Another great answer Gilles. You almost write a full wikipedia page for every answer you give! I, for one, am very thankful. –  boehj May 30 '11 at 21:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.