Ext4

来自 Arch Linux 中文维基

Tango-preferences-desktop-locale.png本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队Tango-preferences-desktop-locale.png

附注: 请完成翻译。(在 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 ratio[编辑 | 编辑源代码]

From mke2fs(8):

mke2fs creates an inode for every bytes-per-inode bytes of space on the disk. The larger the bytes-per-inode ratio, the fewer inodes will be created.

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

Because it is not possible to change either the bytes-per-inode ratio or the inode count after the filesystem is created, mkfs.ext4 uses by default a rather low ratio of one inode every 16384 bytes (16 KiB) to avoid this situation.

However, for partitions with size in the hundreds or thousands of GB and average file size in the megabyte range, this usually results in a much too large inode number because the number of files created never reaches the number of inodes.

This results in a waste of disk space, because all those unused inodes each take up 256 bytes on the filesystem (this is also set in /etc/mke2fs.conf but should not be changed). 256 * several millions = quite a few gigabytes wasted in unused inodes.

This situation can be evaluated by comparing the Use% and IUse% figures provided by df and df -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

To specify a different bytes-per-inode ratio, you can use the -T usage-type option which hints at the expected usage of the filesystem using types defined in /etc/mke2fs.conf. Among those types are the bigger largefile and largefile4 which offer more relevant ratios of one inode every 1 MiB and 4 MiB respectively. It can be used as such:

# mkfs.ext4 -T largefile /dev/device

The bytes-per-inode ratio can also be set directly via the -i option: e.g. use -i 2097152 for a 2 MiB ratio and -i 6291456 for a 6 MiB ratio.

提示:Conversely, if you are setting up a partition dedicated to host millions of small files like emails or newsgroup items, you can use smaller usage-type values such as news (one inode for every 4096 bytes) or small (same plus smaller inode and block sizes).
警告: If you make a heavy use of symbolic links, make sure to keep the inode count high enough with a low bytes-per-inode ratio, because while not taking more space every new symbolic link consumes one new inode and therefore the filesystem may run out of them quickly.

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

默认情况下,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/设备

To set the number of reserved block space to an absolute size in gigabytes, use -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

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

警告: Using a filesystem without journaling can result in data loss in case of sudden dismount like power failure or kernel lockup.

Disabling the journal with ext4 can be done with the following command on an unmounted disk:

# 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

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