Mounting a device — role of /dev, /media and /mnt, and the mount command

50,811

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
50,811

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim over 1 year

    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
    Tim almost 13 years
    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?
  • tcoolspy
    tcoolspy almost 13 years
    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.
  • Hugh
    Hugh almost 13 years
    wow. cat /dev/sr0 > mycd.iso - Never thought about this.
  • boehj
    boehj almost 13 years
    Thanks for drawing my attention to cat and dd re: device nodes. I completely overlooked these things.
  • Tim
    Tim almost 13 years
    Thanks! I still wonder what differences are between "a mount point for removable devices" and "a temporary mount point"?
  • tcoolspy
    tcoolspy almost 13 years
    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.