企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 练习 19:文件系统:挂载,`mount`,`/etc/fstab` > 原文:[Exercise 19. Filesystems: mounting, mount, /etc/fstab](https://archive.fo/9OnRm) > 译者:[飞龙](https://github.com/wizardforcel) > 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) > 自豪地采用[谷歌翻译](https://translate.google.cn/) 我希望你熟悉分区的概念。如果没有,我会简要介绍一下。首先引用自维基百科: > 磁盘分区是一种行为,将硬盘驱动器分为多个逻辑存储单元,它们被称为分区,来将一个物理磁盘驱动器视为多个磁盘。 看一看: ``` user1@vm1:~$ sudo parted /dev/vda GNU Parted 2.3 Using /dev/vda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) unit GB (parted) p Model: Virtio Block Device (virtblk) Disk /dev/vda: 17.2GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 0.00GB 13.3GB 13.3GB extended 5 0.00GB 1.02GB 1.02GB logical ext3 boot 6 1.03GB 2.05GB 1.02GB logical linux-swap(v1) 7 2.05GB 3.07GB 1.02GB logical ext3 8 3.07GB 5.12GB 2.05GB logical ext3 9 5.12GB 9.22GB 4.09GB logical ext3 10 9.22GB 13.3GB 4.09GB logical ext3 (parted) ``` 这是一个物理硬盘,分为 7 个不同的分区。这样做的原因很多,但最好被理解为“分治”原则的应用。以这种方式分割时,流氓程序不能通过占用所有磁盘空间,使整个服务器崩溃,该程序将限制在其分区中。我不会再谈论磁盘分区,但是我会继续关注文件系统,再次引用[维基百科](http://en.wikipedia.org/wiki/File_system): > 文件系统是一种组织数据的手段。通过提供存储,检索和更新数据的过程,以及管理包含它的设备上的可用空间,数据预期在程序终止后保留。文件系统以有效的方式组织数据,并根据设备的特定特性进行调整。在操作系统和文件系统之间,通常存在紧耦合。一些文件系统提供了机制来控制数据和元数据的访问。确保可靠性是文件系统的主要职责。一些文件系统允许多个程序几乎同时更新同一个文件。 > 类 Unix 操作系统创建一个虚拟文件系统,这使得所有设备上的所有文件似乎都存在于单个层次结构中。这意味着,在这些系统中,有一个根目录,系统上存在的每个文件位于它下方的某个地方。类 Unix 系统可以使用 RAM 磁盘或网络共享资源作为其根目录。 这意味着,所有文件系统都集成在一个大树中。对于熟悉 Microsoft Windows 的人来说,这意味着比起`C:\`和`D:\`等盘符,这种命名方案有一个单独的根,`/`,所有其他分区都连接到它上面。将文件系统连接到现有目录的过程称为挂载。连接文件系统的目录称为挂载点。同样,看一看: ``` user1@vm1:~$ mount /dev/vda5 on / type ext3 (rw,errors=remount-ro) tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/vda10 on /home type ext3 (rw) /dev/vda7 on /tmp type ext3 (rw) /dev/vda9 on /usr type ext3 (rw) /dev/vda8 on /var type ext3 (rw) ``` 这是我之前展示给你的相同分区,你可以在这个列表中看到挂载点。不以`/dev/vda`开头的是虚拟文件系统,它允许访问不同的系统设施,但它们和此练习无关。现在我们来看看`/etc/fstab`文件: ``` user1@vm1:~$ cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc defaults 0 0 # / was on /dev/vda5 during installation UUID=128559db-a2e0-4983-91ad-d4f43f27da49 / ext3 errors=remount-ro 0 1 # /home was on /dev/vda10 during installation UUID=32852d29-ddee-4a8d-9b1e-f46569a6b897 /home ext3 defaults 0 2 # /tmp was on /dev/vda7 during installation UUID=869db6b4-aea0-4a25-8bd2-f0b53dd7a88e /tmp ext3 defaults 0 2 # /usr was on /dev/vda9 during installation UUID=0221be16-496b-4277-b131-2371ce097b44 /usr ext3 defaults 0 2 # /var was on /dev/vda8 during installation UUID=2db00f94-3605-4229-8813-0ee23ad8634e /var ext3 defaults 0 2 # swap was on /dev/vda6 during installation UUID=3a936af2-2c04-466d-b98d-09eacc5d104c none swap sw 0 0 /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto 0 0 ``` 看起来很恐怖,但让我们选取一行: ``` # <file system> <mount point> <type> <options> <dump> <pass> UUID=128559db-a2e0-4983-91ad-d4f43f27da49 / ext3 errors=remount-ro 0 1 ``` 按照字段将其拆开。 ``` UUID=128559db-a2e0-4983-91ad-d4f43f27da49 # Filesystem to mount. This UUID is synonim for /dev/vda5 / # This is root filesystem, mount it to / ext3 # This is ext3 filesystem. There are many different filesystems out there errors=remount-ro # If any errors encountered during mounting filesystem should be remounted read-only 0 # This filesystem should not be backed up by dump utility 1 # This filesystem should be checked first by fsck utility ``` 和之前一样,这些信息可以通过`man fstab`提供给你。现在我将向你展示使用现有文件系统的几个命令: + `mount` - 打印出所有已挂载的文件系统。 + `mount -a` - 挂载`/etc/fstab`中描述的所有文件系统。 + `mount /dev/sda<N> /<mount point>` - 挂载分区。 + `umount /dev/sda<N> /<mount point>` - 解除挂载分区。 + `mount -h` - 打印出使用`mount`的简短帮助。 + `fsck` - 检查分区是否有错误。 + `blkid` - 打印出唯一的分区标识符。 现在,你将学习如何列出已安装的分区,挂载和解除挂载它们。 ## 这样做 ``` 1: cat /etc/fstab 2: mount 3: sudo blkid 4: sudo umount /tmp 5: mount 6: sudo fsck /tmp 7: sudo mount -a 8: mount ``` ## 你会看到什么 ``` user1@vm1:~$ cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc defaults 0 0 # / was on /dev/sda1 during installation UUID=05d469bb-dbfe-4d5a-9bb2-9c0fe9fa8577 / ext3 errors=remount-ro 0 1 # /home was on /dev/sda9 during installation UUID=a1b936a0-df38-4bf5-b095-6220ffdfc63c /home ext3 defaults 0 2 # /tmp was on /dev/sda8 during installation UUID=d0a86453-0dbb-4f33-a023-6c09fe9fa202 /tmp ext3 defaults 0 2 # /usr was on /dev/sda5 during installation UUID=b9544cbb-cdb6-4f3b-89e7-a339f52bfac7 /usr ext3 defaults 0 2 # /var was on /dev/sda6 during installation UUID=e15e713b-5850-4bc3-b99e-ab6f1d037caa /var ext3 defaults 0 2 # swap was on /dev/sda7 during installation UUID=4d516f09-80ff-4956-8a75-e9757697f6b1 none swap sw 0 0 /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto 0 0 user1@vm1:~$ mount /dev/sda1 on / type ext3 (rw,errors=remount-ro) tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/sda9 on /home type ext3 (rw) /dev/sda5 on /usr type ext3 (rw) /dev/sda6 on /var type ext3 (rw) /dev/sda8 on /tmp type ext3 (rw) /dev/sda8 on /tmp type ext3 (rw) user1@vm1:~$ sudo blkid /dev/sda1: UUID="05d469bb-dbfe-4d5a-9bb2-9c0fe9fa8577" TYPE="ext3" /dev/sda5: UUID="b9544cbb-cdb6-4f3b-89e7-a339f52bfac7" TYPE="ext3" /dev/sda6: UUID="e15e713b-5850-4bc3-b99e-ab6f1d037caa" TYPE="ext3" /dev/sda7: UUID="4d516f09-80ff-4956-8a75-e9757697f6b1" TYPE="swap" /dev/sda8: UUID="d0a86453-0dbb-4f33-a023-6c09fe9fa202" TYPE="ext3" /dev/sda9: UUID="a1b936a0-df38-4bf5-b095-6220ffdfc63c" TYPE="ext3" user1@vm1:~$ sudo umount /tmp user1@vm1:~$ mount /dev/sda1 on / type ext3 (rw,errors=remount-ro) tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/sda9 on /home type ext3 (rw) /dev/sda5 on /usr type ext3 (rw) /dev/sda6 on /var type ext3 (rw) user1@vm1:~$ sudo fsck /tmp fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) /dev/sda8: clean, 11/61752 files, 13973/246784 blocks user1@vm1:~$ sudo mount -a user1@vm1:~$ mount /dev/sda1 on / type ext3 (rw,errors=remount-ro) tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/sda9 on /home type ext3 (rw) /dev/sda5 on /usr type ext3 (rw) /dev/sda6 on /var type ext3 (rw) /dev/sda8 on /tmp type ext3 (rw) user1@vm1:~$ ``` ## 解释 1. 打印你的`/etc/fstab`文件的内容,它包含分区信息以及挂载位置。 1. 打印当前已挂载的分区。 1. 打印系统中所有分区的 UUID。 1. 解除挂载`/tmp`分区,以便你可以检查它。 1. 再次打印出当前已挂载的分区。`/tmp`现在不存在于此列表中。 1. 检查`/tmp`分区是否有错误。`fsck`通过读取相应的`/etc/fstab`条目知道要检查哪个分区。 1. 挂载`/etc/fstab`中描述的所有分区。 1. 再次打印当前已挂载的分区。`/tmp`已经返回了此列表。 ## 附加题 + 阅读`man fstab`, `man mount`。 + 阅读 <http://tldp.org/LDP/sag/html/filesystems.html>。