connglli / blog-notes

My personal notes ✍️
MIT License
32 stars 2 forks source link

Unix Mount #108

Open connglli opened 4 years ago

connglli commented 4 years ago

Overview

All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several (physical-, virtual-, or pseudo-) devices. The mount command serves to attach the file system found on some device to the big file tree. Conversely, the umount command will detach it again.

The standard form of the mound command is

# mount -t <fstype> <device> <dir>

This tells the kernel to attach the file system on with type at directory

  • \<fstype>: this can be any file system name that supported by one's system, e.g., ext4, fat, but one can use auto to tell mount detect the file system automatically
  • \<device>: considering everything is a file on unix-like system, this argument is the file path of one's device, and it is often prefixed with /dev/ cause most devices created/plugged are under /dev/ directory
  • \<dir>: path of the directory to be mounted; this is often called a mount point

Device Mount: mount physical/virtual devcies

A common usage of mount for common users is to access their usb/disk drivers. For example, if one's plugged usb driver is /dev/sdc1, one can mount it to /media/usb-driver by

# mkdir -p /media/usb-driver
# mount -t auto /dev/sdc1 /media/usb-driver

Except the physical devices, developers can also create their virtual devices (block devices by linux kernel module), and mount them in the same way.

Loop Mount: mount image files

Like tar/zip/..., an image file (suffixed with .img or .iso) is also a file archieve. However, different from other archive files, image file holds no aditional data beyond the disk contents, and files in the archieve can only be automatically handled by programs that can detect their file systems (not a disk file system like EXT4, but special designed file systems like unionfs, squashfs). For example, the android.img in Android contains the rootfs of Andorid, and system.img contains the system partition.

Image files are often used like what android.img is used, i.e., mount to the system as a disk device. To mount the image file, a pseudo device called loop device is required (because image file is not a device). The loop devcie itself, when attached an image file, behaves as a block device (e.g., a usb-driver), and thus can be mounted.

The following command mounts mysystem.img to /mysystem,

# mkdir /mysystem
# mount -t squashfs -o loop=/dev/loop3 /path/to/mysystem.img /mysystem

The command works by firstly create a loop pseudo device under /dev/loop3, attch mysystem.img to it, and then mount it to /mysystem. Then one can access files in mysystem.img by accessing /mysystem directory. The access process works like following

/mysystem -> VFS -> /dev/loop3 -> VFS -> mysystem.img

Follow this to try creating an image file, formatting it as a specific file system, mounting it, and unmounting it.

References

Bind Mount: mount existing directories

Also, one can remount part of the file hierarchy somewhere else.

# mount --bind <olddir> <newdir>

the file system mount options will remain the same as those on the original mount point.

:thinking: TO DO: What happened to the old mount?

References