6
  1. I am mounting an image, and just realized that I have to create the directory for mounting point first. The command mount won't mount to a non-existing directory.

    But I seem to remember in some cases, when I insert some external device, such as external HDD and CD, I don't have to create mounting point myself. So I wonder when mounting points are required to be created by myself and when not?

  2. Is it correct that mounting point directories can be created anywhere?

    Then what is the purposes of the directories /mnt and /media? Aren't they for hosting mounting points of devices?

PS: I am under Ubuntu 12.04.

Thanks!

daisy
  • 54,555
Tim
  • 101,790

4 Answers4

10

You can indeed create a mount point directory anywhere. /mnt and /media are conventions, not technical requirements, just like e.g., /home.

/media is used in particular for auto-mounting. Some part of your desktop environment (possibly udisks) auto-mounts removable media when its connected. It does so by creating a directory under /media, and running mount (just like you would by hand). An image isn't "connected" (unlike a USB thumb drive, for example), so the desktop environment isn't noticing it and auto-mounting it.

/mnt is often used for remote file servers. Unlike /media, your desktop environment won't automatically mount stuff there. If you're not using /mnt for remote file servers, its a perfectly good place for your temporary mount.

You can see descriptions of the conventions for these directories in the Filesystem Hierarchy Standard.

derobert
  • 109,670
  • What's a good mount point for a persistent mount? The FHS doesn't specify where additional disks (as seen on cloud platforms) should be mounted? – eternaltyro May 29 '19 at 03:04
  • 1
    @eternaltyro The Unix tradition would be to mount it wherever it belongs in the filesystem tree (e.g., if you want to put /srv on it, you'd mount it at /srv). You can mount it wherever you want, really, if nothing else makes sense you can always make yourself a new root-level directory (e.g., /disks) and mount them all there. Or you could use /mnt or /media, since your cloud server probably doesn't have e.g., udisks and a desktop environment fighting for those spots. – derobert May 29 '19 at 18:08
2

You always need to create the mount point if you're going to be the one running mount; it won't do it for you. In the case of external devices, some distros will detect when they're plugged in, automatically make the mount point, and then mount the device there (udev is common for this), but they're still making the directory first if it doesn't exist

As for where the mount point should be, /mnt and /media are just conventions; they can appear anywhere. I commonly mount things at /tmp/whatever if I'm only going to need them for a minute

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
2

Mount points can be created anywhere. In fact, inspect your system with mount (with no arguments) or cat /proc/mounts. On a typical Linux system, you will see many “technical” filesystems mounted at various locations: /dev, /dev/shm, /lib/init/rw, /proc, /run, /sys, etc. You might also find GVFS (the Gnome virtual filesystem wrapper) mounted at ~/.gvfs (with Samba, SSHfs and other mounts below it), AVFS at ~/.avfs and so on.

The Linux convention defined in the filesystem hierarchy standard is to have two system directories: /mnt left to the discretion of the system administrator, and /media containing one subdirectory per mounted or potentially-mountable filesystem. /media was conceived because there were two opposing traditions for /mnt: either as a temporary mount point, or as a directory containing mount points.

On some systems, you'll see directories such as /home that are mount points (if home directories are on a central server). You might see a mount point at /boot on systems where the bootloader can't access the OS root directory. It's rare nowadays, but a separate, read-only /usr used to be more common.

The mount command requires an existing directory for a mount point. The directory should be empty; this is not required, but mounting hides any data that was in that directory.

Other commands and system components (pmount, udev, …) create the requisite directory automatically and typically remove it when done. Since they do the mounting, they might as well create the directory while they're at it.

0

To automatically mount everything in your /etc/fstab, but try to make the mount points first, you can use a one-liner like:

$ grep -E '^\s*/dev' /etc/fstab | awk '{ print $2 }' | xargs mkdir -p
$ mount -a

Broken down:

Search for all fstab entries which mounting physical devices (not e.g. the procfs or tempfs):

grep -E '^\s*/dev' /etc/fstab

Select the second column (separated by either spaces or tabs) which is the mount point:

awk '{ print $2 }'

And make each of these directories, including any predecessors

xargs mkdir -p
Cuadue
  • 151