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

參見[編輯 | 編輯原始碼]