Mount

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-21

What is mounting?

In UNIX, all files in the system are organized into a single directory tree structure with root / (as opposed to Windows, where you have a separate directory tree for each drive - e.g. "C:", "E:)

The root / corresponds to the top directory on the main drive/partition. Any other directory and file in the system can be reached from the root, by walking down sub-directories.

How can you make other drives/partitions visible on unix? You mount them: mounting a drive/partition on a directory (e.g., /media/usb) means that the top directory on that drive/partition becomes visible as the directory being mounted. So, if I insert a USB stick in Windows I get a new drive, e.g., F:; if in Linux I mount it on directory /media/usb, then the top directory on the USB stick (what I would see by opening the F: drive in Windows) will be visible in Linux as directory /media/usb. In this case, the /media/usb directory is called a "mount point".

What can be mounted?

In general you can mount not only actual storage devices but also representations of anything that can be accessed as files.

A list of examples:

How to mount and unmount something

Let's say it's 1999 and you want to access files on a CD-ROM. You must mount the CD-ROM on a location in the directory tree (this may be done automatically when you insert the CD). Let's say the CD-ROM device is /dev/cdrom and the chosen mount point is /media/cdrom. The corresponding command is

$ mount /dev/cdrom /media/cdrom
# i.e. mount source_file_or_device folder_where_accessible

When you are done, you can unmount with the following

$ umount /dev/cdrom
# or
$ umount /media/cdrom
# i.e. both the device name and the mount point are valid arguments

How to see all current mounts

# macos
$ mount

# linux
$ mount -l

I get the following output on my mac:

# macOS Catalina mounts the read-only system volume at /.
/dev/disk1s5 on / (apfs, local, read-only, journaled)

devfs on /dev (devfs, local, nobrowse)

# The read-write data volume is here. This seems to correspond to where I, in user-space, usually work (but I'm not sure)
/dev/disk1s1 on /System/Volumes/Data (apfs, local, journaled, nobrowse)
/dev/disk1s4 on /private/var/vm (apfs, local, journaled, nobrowse)
map auto_home on /System/Volumes/Data/home (autofs, automounted, nobrowse)
/Users/jack/Library/Application Support/Google/Android File Transfer/Android File Transfer Agent.app on /private/var/folders/25/f__zt0z94vl3t812lw8zxdhw0000gn/T/AppTranslocation/EFFC1396-76BD-4D96-9575-0AD82D6DC1DA (nullfs, local, nodev, nosuid, read-only, nobrowse, mounted by jack)

How to get size of each mount (often useful for figuring out what each mount correspond to)

# on macos
# I have a 500G drive FYI
$ df -h

Filesystem                                                                                             Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1s5                                                                                          466Gi   10Gi  179Gi     6%  484398 4881968482    0%   /
devfs                                                                                                 340Ki  340Ki    0Bi   100%    1176          0  100%   /dev
/dev/disk1s1                                                                                          466Gi  273Gi  179Gi    61% 3700657 4878752223    0%   /System/Volumes/Data
/dev/disk1s4                                                                                          466Gi  3.0Gi  179Gi     2%       4 4882452876    0%   /private/var/vm
map auto_home                                                                                           0Bi    0Bi    0Bi   100%       0          0  100%   /System/Volumes/Data/home
/Users/jack/Library/Application Support/Google/Android File Transfer/Android File Transfer Agent.app  466Gi  263Gi  191Gi    58% 3625407 4878827473    0%   /private/var/folders/25/f__zt0z94vl3t812lw8zxdhw0000gn/T/AppTranslocation/EFFC1396-76BD-4D96-9575-0AD82D6DC1DA

Resources