【Linux】3、文件系统、Rootfs、overlayfs、squashfs

文章目录

  • 一、文件系统
    • 1.1 mount
    • 1.2 umount
  • 二、Rootfs
    • 2.1 各目录约定
      • 2.1.1 /bin
      • 4.2 /boot
      • 4.3 /dev
      • 4.4 /etc
      • 4.5 /home
      • 4.6 /lib
      • 4.7 /media
      • 4.8 /mnt
      • 4.9 /opt
      • 4.10 /root
      • 4.11 /run
      • 4.12 /sbin
      • 4.13 /srv
      • 4.14 /tmp
      • 4.15 /usr
      • 4.16 /var
  • 三、overlayfs
    • 3.1 概念
    • 3.2 使用
  • 四、squashfs

【Linux】3、文件系统、Rootfs、overlayfs、squashfs_第1张图片

一、文件系统

Linux 的文件系统遵循 HFS 设计,有如下实现:

Ext (Extended Filesystem), Ext2, Ext3, Ext4
JFS (Journaling Filesystem)
ReiserFS
XFS (Extent Filesystem)
Btrfs (B Tree Filesystem)
Swap

1.1 mount

unix 有树状的文件系统,根节点是 /,每个节点可能来自不同的 devices。mount 将 device 上的某文件系统放在树的某节点上。

  • 标准格式是 mount -t type device dir,会把类型为type的device挂载到dir目录上,之前dir目录的内容还在而只是被隐藏了。
  • 如果 mount /dir 则会在 /etc/fstab 中找挂载点
  • 也可以 mount --target /mountpoint
  • mount [-l] [-t type] 会列出已挂载的设备

mount–bind

通过更改 /etc/fstab 文件可以挂载,内容如下:

  • Device: usually the given name or UUID of the mounted device (sda1/sda2/etc).
  • Mount Point: designates the directory where the device is/will be mounted.
  • File System Type: nothing trick here, shows the type of filesystem in use.
  • Options: lists any active mount options. If using multiple options they must be separated by commas.
  • Backup Operation: (the first digit) this is a binary system where 1 = dump utility backup of a partition. 0 = no backup. This is an outdated backup method and should NOT be used.
  • File System Check Order: (second digit) Here we can see three possible outcomes. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. The root filesystem should be set to 1 and other partitions set to 2.
/dev/mapper/rhel-root   					/     xfs     defaults        0 0
UUID=64351209-b3d4-421d-8900-7d940ca56fea 	/boot xfs     defaults        0 0
/dev/mapper/rhel-swap   					swap  swap    defaults        0 0

1.2 umount

先停服务,再 umount,再 mkfs.ext4,再 mount

二、Rootfs

rootfs 是什么

Rootfs 位于分层文件树的顶部(也称为‘/’)。Linux内核通过配置参数‘ROOT=’直接挂载 Rootfs。

根文件系统还具有 mount point(挂载点),我们也可在挂载点上挂载其他文件系统,以便将它们连接到此文件系统层次结构。它有许多目录,其中包含对引导系统和系统操作至关重要的文件。

2.1 各目录约定

2.1.1 /bin

The /bin directory contains binaries of essential user commands that can be used by all users of the system. Here we can find ready-to-execute Linux commands like ls, cp, and echo, and shells like bash and csh. Moreover, /bin also contains the ready-to-run executables that may be required in order to boot or repair a system.

4.2 /boot

In the /boot directory we can find files used for booting an operating system, usually kernel images, bootloader (for example LILO, GRUB) related files, configuration files, and map installer. Generally, we can find vmlinuz, System.map, initrd, and config files here. Among them, vmlinuz is our actual kernel. The bootloader loads this kernel and executes it. The System.map file has a list of kernel symbols and respective addresses. The initial ramdisk (initrd) file is useful in loading drivers that are necessary for the initial booting process. The config file contains a list of kernel options, a list of devices, and device options.

4.3 /dev

The /dev directory is where all device files are stored. The device files residing in this directory represent devices attached to our system. Applications can interact with them using system calls to perform I/O operations. The majority of the device drivers that correspond to the hardware devices are one of two types: block device drivers or character device drivers.

4.4 /etc

Configuration files specific to the local machine are contained in the /etc directory. This directory holds most global config files. However, larger software packages can have their own subdirectories under /etc. Some examples are /etc/X11, /etc/sgml, and /etc/xml.

4.5 /home

The /home directory is the default location to create home directories for different users. For example, let’s say there’s a user with the username “Sam“. Then, the default home directory of Sam will be /home/Sam. In other words, directories under /home provide a personal workspace to regular users who don’t have root privileges.

4.6 /lib

The /lib directory contains essential shared libraries for system boot. Device drivers necessary for system boot are placed under subdirectory /lib/modules/’kernel-version’. It also contains the libraries needed by binaries from /bin and /sbin to run the commands in the root filesystem.

4.7 /media

The /media directory contains subdirectories that are utilized as mount points when we connect any removable media devices to the system. We can find subdirectories or symbolic links to directories representing different removable media devices like CD-ROMs and USB sticks. For example, on inserting a CD into our Linux system, a directory will automatically be created inside the /media directory. We can use this to access the contents of the CD inside this directory.

4.8 /mnt

The /mnt directory is a mount point where we can mount a temporary filesystem that resides on a storage device like a hard-disk drive, USB stick, or CD-ROM. Unlike /media, where the system mounts removable media automatically, under /mnt we need to mount manually. This directory can be empty or may have subdirectories to mount individual devices.

4.9 /opt

The /opt directory contains optional software packages. In this context, optional software packages are those that are not part of the system — for instance, third-party software that we install as add-ons.

4.10 /root

The /root directory is the home directory of the root user of the system.

4.11 /run

The /run directory stores the system information data describing the system since its booting. Applications store their transient files like process IDs, socket descriptors, and more in this directory.

4.12 /sbin

Similar to the /bin directory, the /sbin (system binaries) directory contains ready-to-run executables needed to boot our Linux system. However, unlike /bin binaries, /sbin contents are intended to be executed by the root user.

4.13 /srv

The /srv directory has service data. In other words, site-specific data served by our system is likely to be stored here. For instance, if we’re using an HTTP server to serve a website, then we may store files related to our website inside this directory.

4.14 /tmp

The /tmp directory contains files that are temporary. Many of these files are created by currently running processes on our system that store their temporary data under this directory. Therefore, a clearing out of this directory may happen at booting or at system shutdown.

4.15 /usr

The /usr directory contains executables and read-only data. In contrast with files that are used by the system, files under this directory are used by users. So, /usr/sbin and /usr/bin directories contain non-essential /bin and /sbin binaries, respectively. The default installation location for locally compiled applications is under /usr/local.

4.16 /var

The /var (variable) directory contains transient files and temporary files whose size may change. We can find a number of spools and log files here.

三、overlayfs

参考

3.1 概念

OverlayFS伪文件系统最初包含在Linux内核3.18版本中:它允许我们以一种对用户完全透明的方式组合两个目录树或文件系统(“上层”和“下层”),它能够访问“合并”层上的文件和目录,就像在标准文件系统上所做的那样。

我们都应该熟悉Linux内核在挂载文件系统时采用的标准行为:作为挂载点的目录中存在的文件和目录被屏蔽,并且对用户不可用,而挂载的文件系统上存在的文件和目录被显示。只有在卸载文件系统后,才能再次访问原始文件。当我们在同一目录上挂载多个文件系统时,也会发生这种情况。相反,当使用OverlayFS伪文件系统时,不同层上存在的文件被「组合」在一起,生成的文件系统可以自己挂载。

OverlayFS通常用于运行在嵌入式设备(如OpenWRT)上的系统,在这种情况下,保留基本配置集并同时允许用户执行修改非常有用。OverlayFS也是“overlay”和“overlay2”Docker存储驱动程序的基础。让我们来看看背后的主要逻辑是什么。

OverlayFS的工作涉及两层:lower 和 upper。lower 通常以 readonly 模式 mount。使用此设置时,由于不可能对其上托管的文件和目录进行直接更改,因此可以将其用作安全的备用设置。相反,upper 可以以 read/write 模式安装。存在于这两个层上的文件被 combine 在一起,并且可以在我们所称的 merged 层中进行访问,该层本身可以作为标准文件系统来挂载。

正如我们在下图中所观察到的,存在于 lower 中的文件被存在于 upper 中的同名文件“obscured 遮挡” 或 “masked 屏蔽”。当用户修改属于前者的文件时,在后者的 writable 层中创建该文件的副本(该策略被称为:“copy-up 复制”):该文件在对用户透明的过程中屏蔽原始文件。类似的事情也发生在被合并的目录上:

【Linux】3、文件系统、Rootfs、overlayfs、squashfs_第2张图片

What happens when delete a file or a directory? If the file we delete belongs to the upper layer, it is deleted in place; if it belongs to the lower layer, instead, the deletion is simulated by using a whiteout file (or an opaque directory – a directory with the trusted.overlay.opaque extended attribute), which is created in the writable layer and obscures the original element.

The OverlayFS is the base of the Docker overlay and overlay2 drivers. On such implementation the lower, read-only layer is represented by images; the writable, upper layer, instead, is represented by the containers based on them. Images are immutable: all changes happen inside containers and are lost when containers are deleted (that is why volumes are used for persistence):

【Linux】3、文件系统、Rootfs、overlayfs、squashfs_第3张图片

3.2 使用

Let’s see how to use the OverlayFS. For the sake of this example I will assume we want to combine two filesystems: the lower one existing on the /dev/sda1 partition, and the one to be used in read-write mode to be on the /dev/sda2 partition. The first thing we want to do, is to create the directories which we will use as mount points:

四、squashfs

Squashfs is a highly compressed read-only filesystem for Linux. It uses either gzip/xz/lzo/lz4/zstd compression to compress both files, inodes and directories. Inodes in the system are very small and all blocks are packed to minimise data overhead. Block sizes greater than 4K are supported up to a maximum of 1Mbytes (default block size 128K).

Squashfs is intended for general read-only filesystem use, for archival use (i.e. in cases where a .tar.gz file may be used), and in constrained block device/memory systems (e.g. embedded systems) where low overhead is needed.

squashfs-tools 内含四个命令:

  • mksquashfs
  • sqfscat
  • sqfstar
  • unsquashfs

你可能感兴趣的:(Linux,实战,linux,运维,服务器)