Skip to content

文件系统

2023-03-29 · 2443字 · 9分钟

一切皆文件

之前一直听过这句话,今天终于明白了,原来对于 linux 系统而言,连硬件都是文件。(无端联想 lisp 一切皆链表)

在终端输入命令

df -h

输出

文件系统        容量  已用  可用 已用% 挂载点
udev            3.7G     0  3.7G    0% /dev
tmpfs           767M  3.3M  764M    1% /run
/dev/nvme0n1p5   16G  8.8G  6.1G   60% /
tmpfs           3.8G  170M  3.6G    5% /dev/shm
tmpfs           5.0M  4.0K  5.0M    1% /run/lock
tmpfs           4.0M     0  4.0M    0% /sys/fs/cgroup
/dev/nvme0n1p6   76G  2.2G   70G    3% /data
/dev/nvme0n1p2  1.5G  121M  1.3G    9% /boot
/dev/nvme0n1p4   16G  7.2G  7.7G   49% /recovery
/dev/sda1       916G  2.8G  867G    1% /home
tmpfs           767M   92K  767M    1% /run/user/1000

可以看见文件系统一列就是一些硬件的名称:其中出现最多的前缀/dev/表示 device 即设备。往后最主要出现的是 nvme0n1p[1-6] 其中 nvme 是一种固态硬盘技术,0 指该硬盘的主控芯片,n1 是主存储器,p[1-6] 是它的六个分区。 之后四列意思明显。而挂载点就是文件系统对应的入口,即此入口下所有文件都存放在挂载点对应的文件系统中。譬如根目录/下所有文件都存放在/dev/nvme0n1p5。这一点可以类比某 win 的盘符 CDEF。

常见挂载点

搞清楚常见挂载点是为了合理安排系统分区。所谓分区,就是将系统的不同功能对应到硬件的不同物理位置。物理上不同分区互相隔离,确保系统的部分功能失效时,不至于影响其他功能。譬如将系统的启动目录/boot与根目录/隔离开,即使根目录出问题,系统也能正常启动。

有人可能疑惑,/boot不应该是/的子目录吗?为什么能够和根目录隔离开呢?这就是 linux 挂载的自由性了。挂载点是可以精确到一个具体目录的,尽管逻辑上二者是上下级关系,但物理上是互不包含的

/ (根)

我们知道/是 linux 目录的分隔符,如果只有一根/单独存在,它指的就是(根)目录。逻辑上它是所有目录的上级,根目录没有上级。(指令cd ..的收敛点与不动点)

根目录是安装 linux 系统唯一必须挂载的目录。其余目录为了系统的稳定、管理、维护方便,建议单独安排分区挂载。

注意不要将根目录/与根用户目录/root搞混

/boot

linux 内核启动所需文件目录,大小约为 100M。建议单独挂载。

/home

用于存储用户自己创建文件的目录。/home是所有用户目录的总和,譬如在我的系统中ls /home返回

alephpi  lost+found  timeshift

可以看到这个目录中只有一个用户目录/alephpi。特别的,对于当前用户而言,他的用户目录在命令行的别名(alias)是~。譬如对于我(alephpi)而言,~就等于/home/alephpi

swap

交换分区,或者说虚拟内存。一般设置为物理内存大小,物理内存过小可以多分配,若足够大可以不分配。

efi

efi 分区,用于以 UEFI 方式启动系统。一般 300M。

以下为常见目录,通常不必单独挂载,如有自定义需求,可以参考 此文章 的挂载方式

/usr

应用程序所在目录

/var

指 variable,用于存放经常变动的文件,譬如日志、缓存。

/etc

存放各种配置文件的目录。

/tmp

用于存放临时文件,因 linux 临时垃圾很少,可不单独挂载。

/opt

指 optional, 某些应用程序会安装至此。

Filesystem Hierarchy Standard

以上是磁盘分区时常见的文件目录。趁此机会我们不妨全面了解一下 linux 下的文件系统层级标准。下表摘取于英文 wiki 并作了删减。

DirectoryDescription
/Primary hierarchy root and root directory of the entire file system hierarchy.
/binEssential command binaries that need to be available in single-user mode, including to bring up the system or repair it,[3] for all users (e.g., cat, ls, cp).
/bootBoot loader files (e.g., kernels, initrd).
/devDevice files (e.g., /dev/null, /dev/disk0, /dev/sda1, /dev/tty, /dev/random).
/etcHost-specific system-wide configuration files.There has been controversy over the meaning of the name itself. In early versions of the UNIX Implementation Document from Bell labs, /etc is referred to as the etcetera directory,[4] as this directory historically held everything that did not belong elsewhere (however, the FHS restricts /etc to static configuration files and may not contain binaries).[5] Since the publication of early documentation, the directory name has been re-explained in various ways. Recent interpretations include backronyms such as "Editable Text Configuration" or "Extended Tool Chest".[6]
/etc/optConfiguration files for add-on packages stored in /opt.
/etc/sgmlConfiguration files, such as catalogs, for software that processes SGML.
/etc/X11Configuration files for the X Window System, version 11.
/etc/xmlConfiguration files, such as catalogs, for software that processes XML.
/homeUsers' home directories, containing saved files, personal settings, etc.
/libLibraries essential for the binaries in /bin and /sbin.
/lib<qual>Alternate format essential libraries. These are typically used on systems that support more than one executable code format, such as systems supporting 32-bit and 64-bit versions of an instruction set. Such directories are optional, but if they exist, they have some requirements.
/mediaMount points for removable media such as CD-ROMs (appeared in FHS-2.3 in 2004).
/mntTemporarily mounted filesystems.
/optAdd-on application software packages.[7]
/procVirtual filesystem providing process and kernel information as files. In Linux, corresponds to a procfs mount. Generally, automatically generated and populated by the system, on the fly.
/rootHome directory for the root user.
/runRun-time variable data: Information about the running system since last boot, e.g., currently logged-in users and running daemons. Files under this directory must be either removed or truncated at the beginning of the boot process, but this is not necessary on systems that provide this directory as a temporary filesystem (tmpfs).
/sbinEssential system binaries (e.g., fsck, init, route).
/srvSite-specific data served by this system, such as data and scripts for web servers, data offered by FTP servers, and repositories for version control systems (appeared in FHS-2.3 in 2004).
/sysContains information about devices, drivers, and some kernel features.[8]
/tmpDirectory for temporary files (see also /var/tmp). Often not preserved between system reboots and may be severely size-restricted.
/usrSecondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications. Should be shareable and read-only.[9][10]
/usr/binNon-essential command binaries (not needed in single-user mode); for all users.
/usr/includeStandard include files.
/usr/libLibraries for the binaries in /usr/bin and /usr/sbin.
/usr/libexecBinaries run by other programs that are not intended to be executed directly by users or shell scripts (optional).
/usr/lib<qual>Alternative-format libraries (e.g., /usr/lib32 for 32-bit libraries on a 64-bit machine (optional)).
/usr/localTertiary hierarchy for local data, specific to this host. Typically has further subdirectories (e.g., bin, lib, share).[NB 1]
/usr/sbinNon-essential system binaries (e.g., daemons for various network services).
/usr/shareArchitecture-independent (shared) data.
/usr/srcSource code (e.g., the kernel source code with its header files).
/usr/X11R6X Window System, Version 11, Release 6 (up to FHS-2.3, optional).
/varVariable files: files whose content is expected to continually change during normal operation of the system, such as logs, spool files, and temporary e-mail files.
/var/cacheApplication cache data. Such data are locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. The cached files can be deleted without loss of data.
/var/libState information. Persistent data modified by programs as they run (e.g., databases, packaging system metadata, etc.).
/var/lockLock files. Files keeping track of resources currently in use.
/var/logLog files. Various logs.
/var/mailMailbox files. In some distributions, these files may be located in the deprecated /var/spool/mail.
/var/optVariable data from add-on packages that are stored in /opt.
/var/runRun-time variable data. This directory contains system information data describing the system since it was booted.[11] In FHS 3.0, /var/run is replaced by /run; a system should either continue to provide a /var/run directory or provide a symbolic link from /var/run to /run for backwards compatibility.[12]
/var/spoolSpool for tasks waiting to be processed (e.g., print queues and outgoing mail queue).
/var/spool/mailDeprecated location for users' mailboxes.[13]
/var/tmpTemporary files to be preserved between reboots.
返回

人同此心,心同此理;如风沐面,若水润心