30

I have several closely related questions about what happens when I insert a CD. The files on the CD /media/Ubuntu\ 11.04\ i386/, but from what I've seen /dev/cdrom is also involved.

  1. What is the difference between /dev, /media and /mnt? Following is what I have found from internet but I still have little idea:

    • /dev — this folder contains device files
    • /media — this is a mount point for removable devices
    • /mnt — this is a temporary mount point
  2. What is the purpose of mount? In other words, if a device has been represented by the OS as a device file under /dev, why can it not be accessed via the device file directly without mounting?

    Is mount only used for storage device, not for non-storage device, such as graphical card, network card, camera, ...?

  3. Where is a device file under /dev mounted to, under /media or under /mnt? I remember I have seen both, but am curious when to mount to which?

  4. I found my CD was automatically mounted to /media/Ubuntu 11.04 i386. I guess the device file of the CD is /dev/cdrom, but I cannot confirm it by looking into /dev/cdrom and /media/Ubuntu 11.04 i386:

    $ ls -l /media/Ubuntu\ 11.04\ i386/
    total 3522
    -r--r--r-- 1 Tim Tim     143 2011-04-27 13:04 autorun.inf
    ...
    $ ls -l /dev/cdrw
    lrwxrwxrwx 1 root root 3 2011-05-28 15:12 /dev/cdrw -> sr0
    $ ls -l /dev/cdrom
    lrwxrwxrwx 1 root root 3 2011-05-28 15:12 /dev/cdrom -> sr0
    

    How can I find out which device file is for my CD?

Tim
  • 101,790

3 Answers3

19

There are a lot of questions here and I'll do my best to answer them. I'm certain that those more knowledgeable than I will be able to help you further. (I'd appreciate if those people could help me out too.)

In *nix, everything is a file. For example, your CD-ROM is a file.

  • /dev - Here you'll find physical devices as well as things you wouldn't normally think of as devices such as /dev/null.
  • /media & /mnt are directories where you may mount a physical device such as a CD-ROM, HDD partition, USB stick, etc.

The purpose of mount (and the opposite umount) is to allow dynamic mounting of devices. What I mean here is that perhaps you may want to only mount a device under certain circumstances, and at other times have it not readily accessible. You may wish to mount an entire file system at /mnt when repairing a system. You may wish to mount a disc image (e.g. foo.iso) from time to time. Etc.

You may choose to mount a device in /dev at either /media or /mnt. There are more or less correct ways of doing this. For example, from your question you say:

/media this is a mount point for removable devices

/mnt this is a temporary mount point

That's pretty much correct. Read here for how /media and /mnt should be used according to the Filesystem Hierarchy Standard. I do this pretty incorrectly, opting to use /media when in fact I should be using /mnt, most of the time. It's also worth noting that an internal HDD with associated partitions may be referred to, somewhat confusingly, removeable media.

I'm on OS X here so I can't check right now (BSD does things slightly differently regarding optical drives) but /dev/cdrom is a device file for your CD-ROM. As is /dev/cdrw. See the '->' in the ls -l output in your question? That's indicating that both /dev/cdrom and /dev/cdrw are symbolically linked to /dev/sr0. 'sr' is the device driver name; 'sr0' is the device file name.

/media/Ubuntu 11.04 i386 is simply an .iso image that has been auto-mounted at /media.

I hope that helps a bit.

GAD3R
  • 66,769
boehj
  • 2,630
  • 1
    Thanks! I still wonder what differences are between "a mount point for removable devices" and "a temporary mount point"? – Tim May 30 '11 at 05:47
  • 3
    In function, they are the same. The theoretically difference is that the same removable devices might show up repeatedly and if possible it's nice to have them mount to the same directory. Any time you insert a CD it's nice to have that CD at a unique path like "/media/My_CD_Title". Your camera memory card might be "/media/SD_Card". In contrast a temporary mount point is likely to have the same path but you mount different drives to it based on the need of the moment, and only your knowledge of what you mounted there identifies the drive because it's in the same place the LAST temporary drive. – Caleb May 30 '11 at 11:26
  • 2
    Whereas /mnt tends to be used my systems administrators - say, when we want to restore a backup, or migrate a partition to a new disk, we create (for instance) /mnt/homebackup and mount the backup disk image to that mount point, copy the lost files back to /home and then release the mount point. – Shadur-don't-feed-the-AI Mar 16 '14 at 10:13
17

The answer from boehj explains the basics pieces in play here. The one thing I would add is about the difference between a device and a mounted file system. The fact of the matter is that you can access a device node directly. For example you could use dd if=/dev/sda of=/dev/sdb to make your second ATA device an exact copy of the first one, or you can cat /dev/sr0 > mycd.iso to rip a CD and make an iso image of it.

The difference is that when you mount a device to a location, you create a path in your directory structure that accesses the device using a file system driver. The file system driver handles things all the special things that need to happen like caching, indexing, seeking, etc in order for your raw drive device to appear to you with all the conveniences of a file system.

Caleb
  • 70,105
  • Thanks! Are partitions and filesystems on a storage device also regarded as devices? Do they have drivers themselves, besides that the storage device has one? – Tim May 29 '11 at 06:17
  • Yes partitions are addressable as devices, as are all storage devices (whatever you mean by that). The whole device will run under one hardware driver, but of course each partition can have it's own file system so the mount command might use different kernel drivers to handle the different file systems. – Caleb May 29 '11 at 07:21
  • 1
    wow. cat /dev/sr0 > mycd.iso - Never thought about this. – asgs May 29 '11 at 09:45
  • 1
    Thanks for drawing my attention to cat and dd re: device nodes. I completely overlooked these things. – boehj May 29 '11 at 22:34
5

Building on boehj's answer, mount is used behind the scenes at boot time to check in /etc/fstab to see where each existing partition that it's supposed to know about should be mounted into the actual filesystem.

Unlike with - for instance - Windows, where you don't get much of a choice beyond what drive letter a partition gets, this allows any device or partition to be mounted anywhere in the filesystem tree if you so wish -- for example, university network computers would typically only have /bin/ and /lib and a few temporary partitions mounted locally, while /usr/ (containing almost all of the software that isn't required during the boot phase) and /home/ (containing all users' home directories) would be mounted from a centrally accessible NFS server.

It's also responsible for quietly mounting various temporary and virtual filesystems such as /dev/shm/, /sys/, /dev/pts/, and on more modern systems /run/. Chances are you'll rarely if ever do anything directly with these, but a lot of software relies on these to exist behind the scenes. Take a look at the output of the bare mount command, or in /etc/fstab -- you might learn something interesting.