Ext4

来自 Arch Linux 中文维基

本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队

附注: 请完成翻译。(在 Talk:Ext4# 中讨论)

来自 Ext4 - Linux Kernel Newbies

Ext4 是最常用的 Linux 文件系统 Ext3 的进化。在许多方面,Ext4对于 Ext3 有着比 Ext3 对于 Ext2 更多更深的改变。Ext3 主要是向 Ext2 添加了日志系统,而 Ext4 修改了重要的文件系统的数据结构,比如用来存储文件数据的那部分。当然结果就是文件系统有更好的设计,更好的性能,稳定性还有更多的功能。

创建 ext4 文件系统[编辑 | 编辑源代码]

安装 e2fsprogs

要格式化分区,使用:

# mkfs.ext4 /dev/分区
提示:
  • 更多选项参见 mke2fs(8);编辑 /etc/mke2fs.conf 可以查看/修改默认配置。
  • 如果支持,可以启用元数据校验和

Bytes-per-inode 比例[编辑 | 编辑源代码]

来自 mke2fs(8):

mke2fs 在硬盘上为每个bytes-per-inode大小的空间创建一个 inode. Bytes-per-inode 的比例越大, 创建的 inode 就越少.

创建新文件、目录、符号链接等都需要至少一个空闲的 inode。如果 inode 数过低,即使文件系统中仍有空间也无法创建文件。

由于无法在创建文件系统后修改 bytes-per-inode 比例以及 inode 数量, mkfs.ext4 默认使用更低的 inode 比例,即每 16384 bytes (16 KiB) 一个 inode 以避免这个问题.

然而,对于一个具有对于大小为数百或数千GB、平均文件大小为兆字节的分区,这通常会导致 inode 过大,因为创建的文件数永远不会达到 inode 数。

这样的结果是浪费硬盘空间, 因为这些未使用的 inode 每个都会占用文件系统 256 bytes 的空间 (这是在 /etc/mke2fs.conf 中设置的,但是不应该改变). 256 * 数百万 = 许多GB在未使用的节点中被浪费了.

这种情况可以通过比较以下几点进行评价, Use% 以及 IUse%dfdf -i 提供:

$ df -h /home
Filesystem              Size    Used   Avail  Use%   Mounted on
/dev/mapper/lvm-home    115G    56G    59G    49%    /home
$ df -hi /home
Filesystem              Inodes  IUsed  IFree  IUse%  Mounted on
/dev/mapper/lvm-home    1.8M    1.1K   1.8M   1%     /home

要指定一个不同的 Bytes-per-inode 比率,你可以使用 -T usage-type 选项,将会使用/etc/mke2fs.conf中定义的类型,表示文件系统预期的用法。 在这些类型中较大的 largefilelargefile4 将提供更相关联的比例,即每 1 MiB 或 4 MiB 一个 inode。

# mkfs.ext4 -T largefile /dev/device

Bytes-per-inode 比例可以直接通过 -i 选项设定: e.g. 使用 -i 2097152 对应 2 MiB 的比例以及 -i 6291456 对应 6 MiB 的比例.

提示:相反地,如果你需要建立一个分区用于存放数百万计的小文件(例如邮件或新闻组项目)你可以使用更小的 usage-type 值,例如 news (每 4096 bytes 一个 节点) 或者 small (相同但更小的 inode 和 block 大小).
警告: 如果你大量使用符号链接,请确保保持足够高的 inode 数和较低的 bytes-per-inode,因为符号链接虽然不会占用更多的空间,但每一个新的符号链接都会消耗一个新的 inode,因此文件系统中的 inode 可能会被很快用完。

保留块[编辑 | 编辑源代码]

默认情况下,5% 的文件系统块会被预留给超级用户,以避免碎片化并“允许由 root 拥有的守护程序在非特权进程被阻止向文件系统写入后继续正常运行”(来自 mke2fs(8))。

对于现在的大容量磁盘,如果分区被用于长期存储或对系统运作并非至关重要(例如 /home),这将比必要的大小更大。See this email for the opinion of ext4 developer Ted Ts'o on reserved blocks and this superuser answer for general background on this topic.

如果分区满足以下条件之一,则通常可以放心缩小保留块的比例:

  • 非常大(例如 > 50G)
  • 用于长期存储,即文件不会被非常频繁地创建和删除

在 ext4 相关的实用程序中可以使用 -m 选项指定保留块的比例。

要在创建文件系统时不创建保留块,使用:

# mkfs.ext4 -m 0 /dev/设备

要在之后将比例改为 1%,使用:

# tune2fs -m 1 /dev/设备

要将保留块空间设置为以千兆字节为单位的绝对大小,使用 -r:

# tune2fs -r $((ngigs * 1024**3 / blocksize)) /dev/设备

blocksize is the block size of the filesystem in bytes. This is almost always 4096, but you can check to be sure:

# tune2fs -l /dev/设备 | grep 'Block size:'
Block size:               4096

The $(()) syntax is for math expansion. This syntax works in bash and zsh, but it will not work in fish. For fish, this is the syntax:

# tune2fs -r (math 'ngigs * 1024^3 / blocksize') /dev/设备

这些命令可以应用于已挂载的文件系统,改变将立即生效。可以使用 findmnt(8) 查找设备名:

# tune2fs -m 1 "$(findmnt -no SOURCE 挂载点路径)"

To query the current number of reserved blocks:

# tune2fs -l /dev/mapper/proxima-root | grep 'Reserved block count:'
Reserved block count:     2975334

这是块的数量,所以需要乘上文件系统的块大小才能得到字节数:2975334 * 4096 / 1024**3 = 11.34 GiB

从 ext2/ext3 迁移到 ext4[编辑 | 编辑源代码]

不转换直接把 ext2/ext3 分区挂载成 ext4 分区格式[编辑 | 编辑源代码]

基本原理[编辑 | 编辑源代码]

转换到ext4和继续使用 ext2/ext3 格式的折中的办法就是把分区当作 ext4 分区来挂载。

优点:

  • 兼容性(分区的文件系统可以继续挂载为 ext3) – 这允许用户继续在不支持 ext4 文件格式的操作系统中读取分区。(例如:带 ext2/ext3 驱动的 Windows 系统)
  • 提高性能(不过没有完全转换成 ext4 分区后好)。[1] [2]

缺点:

  • 仅能使用少部分 ext4 特性。(只有那些不改变分区格式的功能能被使用,例如 multiblock allocation 和 delayed allocation)
注意: 除了由 ext4 格式带来的相对新的不一样的特性(可以看作一种潜在风险)之外,这种技术没有大的缺点

步骤[编辑 | 编辑源代码]

  1. 修改 /etc/fstab,把你想要挂载成 ext4 的分区的“type”栏的内容从 ext2/ext3 改为 ext4。
  2. 重新挂载分区。

将 ext2/ext3 分区转换为 ext4 格式[编辑 | 编辑源代码]

基本原理[编辑 | 编辑源代码]

为了能够使用 ext4 的全部特性,必须完成一个不可逆的转换过程。

优点:

  • 更好的性能以及新功能。[3] [4]

缺点:

  • Partitions that contain mostly static files, such as a /boot partition, may not benefit from the new features. Also, adding a journal (which is implied by moving a ext2 partition to ext3/4) always incurs performance overhead.
  • 不可逆(ext4 分区不能“降级”到 ext2/ext3。It is, however, backwards compatible until extent and other unique options are enabled)

步骤[编辑 | 编辑源代码]

这些指令改编自内核文档BBS thread

警告:
  • If you convert the system's root filesystem, ensure that the 'fallback' initramfs is available at reboot. Alternatively, add ext4 according to Mkinitcpio#MODULES and regenerate the initramfs before starting.
  • If you decide to convert a separate /boot partition, ensure the bootloader supports booting from ext4.

在下面的步骤中,/dev/sdxX 代表要转换的分区的路径,例如 /dev/sda1

  1. Back up all data on any ext3 partitions that are to be converted to ext4. A useful package, especially for root partitions, is clonezilla.
  2. Edit /etc/fstab and change the 'type' from ext3 to ext4 for any partitions that are to be converted to ext4.
  3. Boot the live medium (if necessary). The conversion process with e2fsprogs must be done when the drive is not mounted. If converting a root partition, the simplest way to achieve this is to boot from some other live medium.
  4. 确保分区没有被挂载
  5. If you want to convert a ext2 partition, the first conversion step is to add a journal by running tune2fs -j /dev/sdxX as root; making it a ext3 partition.
  6. Run tune2fs -O extent,uninit_bg,dir_index /dev/sdxX as root. This command converts the ext3 filesystem to ext4 (irreversibly).
  7. 以 root 身份运行 fsck -f /dev/sdxX
    • This step is necessary, otherwise the filesystem will be unreadable. This fsck run is needed to return the filesystem to a consistent state. It will find checksum errors in the group descriptors - this is expected. The -f option asks fsck to force checking even if the file system seems clean. The -p option may be used on top to "automatically repair" (otherwise, the user will be asked for input for each error).
  8. Recommended: mount the partition and run e4defrag -c -v /dev/sdxX as root.
    • Even though the filesystem is now converted to ext4, all files that have been written before the conversion do not yet take advantage of the extent option of ext4, which will improve large file performance and reduce fragmentation and filesystem check time. In order to fully take advantage of ext4, all files would have to be rewritten on disk. Use e4defrag to take care of this problem.
  9. 重新启动

提升性能[编辑 | 编辑源代码]

E4rat[编辑 | 编辑源代码]

E4rat 是为 ext4 文件系统设计的预加载应用程序。它监视启动时打开的文件,并通过优化它们在分区上所处的位置来提升访问效率,并在启动过程之初就预加载它们。于机械硬盘不同的是 E4rat 不会提升固态硬盘的性能,因为后者的访问时间与前者相比可以忽略不计。

禁用访问时间更新[编辑 | 编辑源代码]

ext4 文件系统会记录于文件上次被访问的时间相关的信息,而记录这些信息会导致开销。使用 noatime 选项可防止更新访问时间戳。

/etc/fstab
/dev/sda5    /    ext4    defaults,noatime    0    1

Doing so breaks applications that rely on access time, see fstab#atime options for possible solutions.

Increasing commit interval[编辑 | 编辑源代码]

The sync interval for data and metadata can be increased by providing a higher time delay to the commit option.

The default 5 sec means that if the power is lost, one will lose as much as the latest 5 seconds of work. It forces a full sync of all data/journal to physical media every 5 seconds. The filesystem will not be damaged though, thanks to the journaling. The following fstab illustrates the use of commit:

/etc/fstab
/dev/sda5    /    ext4    defaults,noatime,commit=60    0    1

关闭屏障[编辑 | 编辑源代码]

警告: 如果磁盘无法保证在电源掉电时缓存正确写入,禁用屏障可能会导致严重的文件系统损坏和数据丢失。

Ext4 默认启用写屏障。它确保文件系统元数据磁盘上被正确地写入和排序,即使在写缓存掉电时也是如此。这会带来性能成本,特别是对于大量使用 fsync 或创建和删除许多小文件的应用程序。对于写缓存由电池供电的磁盘,禁用障碍可以会安全地提高性能。

要关闭屏障,将 barrier=0 选项添加到文件系统中。例如:

/etc/fstab
/dev/sda5    /    ext4    noatime,barrier=0    0    1

禁用日志[编辑 | 编辑源代码]

警告: 使用没有日志的文件系统,在突发卸载的情况下,例如断电或者内核锁定,将导致数据丢失。

ext4 中禁用日志,可以对已卸载的硬盘使用下列指令完成操作:

# tune2fs -O "^has_journal" /dev/sdXN

Tips and tricks[编辑 | 编辑源代码]

Using file-based encryption[编辑 | 编辑源代码]

Since Linux 4.1, ext4 natively supports file encryption, see the fscrypt article. Encryption is applied at the directory level, and different directories can use different encryption keys. This is different from both dm-crypt, which is block-device level encryption, and from eCryptfs, which is a stacked cryptographic filesystem.

在现有的文件系统中启用元数据校验和[编辑 | 编辑源代码]

When a filesystem has been created with e2fsprogs 1.43 (2016) or later, metadata checksums are enabled by default. 可以转换现有文件系统以启用元数据校验和支持。

If the CPU supports SSE 4.2, make sure the crc32c_intel kernel module is loaded in order to enable the hardware accelerated CRC32C algorithm [5]. If not, load the crc32c_generic module instead.

要了解有关元数据校验和的更多信息,请参阅 ext4 wiki

提示:Use dumpe2fs to check the features that are enabled on the filesystem:
# dumpe2fs -h /dev/path/to/disk
注意: 必须卸载文件系统。

First the partition needs to be checked and optimized using e2fsck:

# e2fsck -Df /dev/path/to/disk  

将文件系统转换为 64 位:

# resize2fs -b /dev/path/to/disk 

最后启用校验和支持:

# tune2fs -O metadata_csum /dev/path/to/disk

验证:

# dumpe2fs -h /dev/path/to/disk | grep features:
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum

Enabling fast_commit in existing filesystems[编辑 | 编辑源代码]

Starting from the 5.10 kernel, ext4 may have a performance boost by enabling the fast_commit option:

# tune2fs -O fast_commit /dev/drivepartition

To clarify the current configuration or changes:

# tune2fs -l /dev/drivepartition | grep features

参见[编辑 | 编辑源代码]