练习 19:文件系统:挂载,mount/etc/fstab

原文:Exercise 19. Filesystems: mounting, mount, /etc/fstab

译者:飞龙

协议:CC BY-NC-SA 4.0

自豪地采用谷歌翻译

我希望你熟悉分区的概念。如果没有,我会简要介绍一下。首先引用自维基百科:

磁盘分区是一种行为,将硬盘驱动器分为多个逻辑存储单元,它们被称为分区,来将一个物理磁盘驱动器视为多个磁盘。

看一看:

  1. user1@vm1:~$ sudo parted /dev/vda
  2. GNU Parted 2.3
  3. Using /dev/vda
  4. Welcome to GNU Parted! Type 'help' to view a list of commands.
  5. (parted) unit GB
  6. (parted) p
  7. Model: Virtio Block Device (virtblk)
  8. Disk /dev/vda: 17.2GB
  9. Sector size (logical/physical): 512B/512B
  10. Partition Table: msdos
  11. Number Start End Size Type File system Flags
  12. 1 0.00GB 13.3GB 13.3GB extended
  13. 5 0.00GB 1.02GB 1.02GB logical ext3 boot
  14. 6 1.03GB 2.05GB 1.02GB logical linux-swap(v1)
  15. 7 2.05GB 3.07GB 1.02GB logical ext3
  16. 8 3.07GB 5.12GB 2.05GB logical ext3
  17. 9 5.12GB 9.22GB 4.09GB logical ext3
  18. 10 9.22GB 13.3GB 4.09GB logical ext3
  19. (parted)

这是一个物理硬盘,分为 7 个不同的分区。这样做的原因很多,但最好被理解为“分治”原则的应用。以这种方式分割时,流氓程序不能通过占用所有磁盘空间,使整个服务器崩溃,该程序将限制在其分区中。我不会再谈论磁盘分区,但是我会继续关注文件系统,再次引用维基百科

文件系统是一种组织数据的手段。通过提供存储,检索和更新数据的过程,以及管理包含它的设备上的可用空间,数据预期在程序终止后保留。文件系统以有效的方式组织数据,并根据设备的特定特性进行调整。在操作系统和文件系统之间,通常存在紧耦合。一些文件系统提供了机制来控制数据和元数据的访问。确保可靠性是文件系统的主要职责。一些文件系统允许多个程序几乎同时更新同一个文件。

类 Unix 操作系统创建一个虚拟文件系统,这使得所有设备上的所有文件似乎都存在于单个层次结构中。这意味着,在这些系统中,有一个根目录,系统上存在的每个文件位于它下方的某个地方。类 Unix 系统可以使用 RAM 磁盘或网络共享资源作为其根目录。

这意味着,所有文件系统都集成在一个大树中。对于熟悉 Microsoft Windows 的人来说,这意味着比起C:\D:\等盘符,这种命名方案有一个单独的根,/,所有其他分区都连接到它上面。将文件系统连接到现有目录的过程称为挂载。连接文件系统的目录称为挂载点。同样,看一看:

  1. user1@vm1:~$ mount
  2. /dev/vda5 on / type ext3 (rw,errors=remount-ro)
  3. tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
  4. proc on /proc type proc (rw,noexec,nosuid,nodev)
  5. sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
  6. udev on /dev type tmpfs (rw,mode=0755)
  7. tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
  8. devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
  9. /dev/vda10 on /home type ext3 (rw)
  10. /dev/vda7 on /tmp type ext3 (rw)
  11. /dev/vda9 on /usr type ext3 (rw)
  12. /dev/vda8 on /var type ext3 (rw)

这是我之前展示给你的相同分区,你可以在这个列表中看到挂载点。不以/dev/vda开头的是虚拟文件系统,它允许访问不同的系统设施,但它们和此练习无关。现在我们来看看/etc/fstab文件:

  1. user1@vm1:~$ cat /etc/fstab
  2. # /etc/fstab: static file system information.
  3. #
  4. # Use 'blkid' to print the universally unique identifier for a
  5. # device; this may be used with UUID= as a more robust way to name devices
  6. # that works even if disks are added and removed. See fstab(5).
  7. #
  8. # <file system> <mount point> <type> <options> <dump> <pass>
  9. proc /proc proc defaults 0 0
  10. # / was on /dev/vda5 during installation
  11. UUID=128559db-a2e0-4983-91ad-d4f43f27da49 / ext3 errors=remount-ro 0 1
  12. # /home was on /dev/vda10 during installation
  13. UUID=32852d29-ddee-4a8d-9b1e-f46569a6b897 /home ext3 defaults 0 2
  14. # /tmp was on /dev/vda7 during installation
  15. UUID=869db6b4-aea0-4a25-8bd2-f0b53dd7a88e /tmp ext3 defaults 0 2
  16. # /usr was on /dev/vda9 during installation
  17. UUID=0221be16-496b-4277-b131-2371ce097b44 /usr ext3 defaults 0 2
  18. # /var was on /dev/vda8 during installation
  19. UUID=2db00f94-3605-4229-8813-0ee23ad8634e /var ext3 defaults 0 2
  20. # swap was on /dev/vda6 during installation
  21. UUID=3a936af2-2c04-466d-b98d-09eacc5d104c none swap sw 0 0
  22. /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto 0 0

看起来很恐怖,但让我们选取一行:

  1. # <file system> <mount point> <type> <options> <dump> <pass>
  2. UUID=128559db-a2e0-4983-91ad-d4f43f27da49 / ext3 errors=remount-ro 0 1

按照字段将其拆开。

  1. UUID=128559db-a2e0-4983-91ad-d4f43f27da49 # Filesystem to mount. This UUID is synonim for /dev/vda5
  2. / # This is root filesystem, mount it to /
  3. ext3 # This is ext3 filesystem. There are many different filesystems out there
  4. errors=remount-ro # If any errors encountered during mounting filesystem should be remounted read-only
  5. 0 # This filesystem should not be backed up by dump utility
  6. 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. 1: cat /etc/fstab
  2. 2: mount
  3. 3: sudo blkid
  4. 4: sudo umount /tmp
  5. 5: mount
  6. 6: sudo fsck /tmp
  7. 7: sudo mount -a
  8. 8: mount

你会看到什么

  1. user1@vm1:~$ cat /etc/fstab
  2. # /etc/fstab: static file system information.
  3. #
  4. # Use 'blkid' to print the universally unique identifier for a
  5. # device; this may be used with UUID= as a more robust way to name devices
  6. # that works even if disks are added and removed. See fstab(5).
  7. #
  8. # <file system> <mount point> <type> <options> <dump> <pass>
  9. proc /proc proc defaults 0 0
  10. # / was on /dev/sda1 during installation
  11. UUID=05d469bb-dbfe-4d5a-9bb2-9c0fe9fa8577 / ext3 errors=remount-ro 0 1
  12. # /home was on /dev/sda9 during installation
  13. UUID=a1b936a0-df38-4bf5-b095-6220ffdfc63c /home ext3 defaults 0 2
  14. # /tmp was on /dev/sda8 during installation
  15. UUID=d0a86453-0dbb-4f33-a023-6c09fe9fa202 /tmp ext3 defaults 0 2
  16. # /usr was on /dev/sda5 during installation
  17. UUID=b9544cbb-cdb6-4f3b-89e7-a339f52bfac7 /usr ext3 defaults 0 2
  18. # /var was on /dev/sda6 during installation
  19. UUID=e15e713b-5850-4bc3-b99e-ab6f1d037caa /var ext3 defaults 0 2
  20. # swap was on /dev/sda7 during installation
  21. UUID=4d516f09-80ff-4956-8a75-e9757697f6b1 none swap sw 0 0
  22. /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto 0 0
  23. user1@vm1:~$ mount
  24. /dev/sda1 on / type ext3 (rw,errors=remount-ro)
  25. tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
  26. proc on /proc type proc (rw,noexec,nosuid,nodev)
  27. sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
  28. udev on /dev type tmpfs (rw,mode=0755)
  29. tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
  30. devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
  31. /dev/sda9 on /home type ext3 (rw)
  32. /dev/sda5 on /usr type ext3 (rw)
  33. /dev/sda6 on /var type ext3 (rw)
  34. /dev/sda8 on /tmp type ext3 (rw)
  35. /dev/sda8 on /tmp type ext3 (rw)
  36. user1@vm1:~$ sudo blkid
  37. /dev/sda1: UUID="05d469bb-dbfe-4d5a-9bb2-9c0fe9fa8577" TYPE="ext3"
  38. /dev/sda5: UUID="b9544cbb-cdb6-4f3b-89e7-a339f52bfac7" TYPE="ext3"
  39. /dev/sda6: UUID="e15e713b-5850-4bc3-b99e-ab6f1d037caa" TYPE="ext3"
  40. /dev/sda7: UUID="4d516f09-80ff-4956-8a75-e9757697f6b1" TYPE="swap"
  41. /dev/sda8: UUID="d0a86453-0dbb-4f33-a023-6c09fe9fa202" TYPE="ext3"
  42. /dev/sda9: UUID="a1b936a0-df38-4bf5-b095-6220ffdfc63c" TYPE="ext3"
  43. user1@vm1:~$ sudo umount /tmp
  44. user1@vm1:~$ mount
  45. /dev/sda1 on / type ext3 (rw,errors=remount-ro)
  46. tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
  47. proc on /proc type proc (rw,noexec,nosuid,nodev)
  48. sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
  49. udev on /dev type tmpfs (rw,mode=0755)
  50. tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
  51. devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
  52. /dev/sda9 on /home type ext3 (rw)
  53. /dev/sda5 on /usr type ext3 (rw)
  54. /dev/sda6 on /var type ext3 (rw)
  55. user1@vm1:~$ sudo fsck /tmp
  56. fsck from util-linux-ng 2.17.2
  57. e2fsck 1.41.12 (17-May-2010)
  58. /dev/sda8: clean, 11/61752 files, 13973/246784 blocks
  59. user1@vm1:~$ sudo mount -a
  60. user1@vm1:~$ mount
  61. /dev/sda1 on / type ext3 (rw,errors=remount-ro)
  62. tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
  63. proc on /proc type proc (rw,noexec,nosuid,nodev)
  64. sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
  65. udev on /dev type tmpfs (rw,mode=0755)
  66. tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
  67. devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
  68. /dev/sda9 on /home type ext3 (rw)
  69. /dev/sda5 on /usr type ext3 (rw)
  70. /dev/sda6 on /var type ext3 (rw)
  71. /dev/sda8 on /tmp type ext3 (rw)
  72. user1@vm1:~$

解释

  1. 打印你的/etc/fstab文件的内容,它包含分区信息以及挂载位置。
  2. 打印当前已挂载的分区。
  3. 打印系统中所有分区的 UUID。
  4. 解除挂载/tmp分区,以便你可以检查它。
  5. 再次打印出当前已挂载的分区。/tmp现在不存在于此列表中。
  6. 检查/tmp分区是否有错误。fsck通过读取相应的/etc/fstab条目知道要检查哪个分区。
  7. 挂载/etc/fstab中描述的所有分区。
  8. 再次打印当前已挂载的分区。/tmp已经返回了此列表。

附加题