如何使用具壓縮功能之 linux 檔案系統 (ZFS)

1. 初始化 ZFS 檔案系統 (若已備妥 ZFS 檔案系統則可略過此段落)

  1. 首先需要 fdisk 確認當前磁碟的分割方式,如下為 GPT

    fdisk -l
    
    Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: BFCF6B5D-2B26-45A9-9A0D-4793FB0D30B5
    
    Device       Start      End  Sectors  Size Type
    /dev/vda1     2048     4095     2048    1M BIOS boot
    /dev/vda2     4096   413695   409600  200M EFI System
    /dev/vda3   413696  2510847  2097152    1G Linux filesystem
    /dev/vda4  2510848 41943006 39432159 18.8G Linux filesystem
    
    
    Disk /dev/vdb: 1.82 TiB, 1999542157312 bytes, 3905355776 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
  1. 建立 LVM 磁區 (以便後續擴容,非必要)

    yum install -y lvm2
    
    pvcreate /dev/vdb # 因已藉由 `fdisk -l` 確認到 /dev/vda 已使用
    
    vgcreate lvm_data /dev/vdb # 在 PV /dev/vdb 上再新增 VG lvm_data
    
    lvcreate -l 100%FREE -n home_data lvm_data # 分配全部 VG lvm_data 至 LV home_data 內
    
    fdisk -l # 確認分配結果
    
    Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: BFCF6B5D-2B26-45A9-9A0D-4793FB0D30B5
    
    Device       Start      End  Sectors  Size Type
    /dev/vda1     2048     4095     2048    1M BIOS boot
    /dev/vda2     4096   413695   409600  200M EFI System
    /dev/vda3   413696  2510847  2097152    1G Linux filesystem
    /dev/vda4  2510848 41943006 39432159 18.8G Linux filesystem
    
    
    Disk /dev/vdb: 1.82 TiB, 1999542157312 bytes, 3905355776 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/mapper/lvm_data-home_data: 1.81 TiB, 1988800544768 bytes, 3884376064 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
  2. 安裝 ZFS

    dnf install https://zfsonlinux.org/epel/zfs-release-2-3$(rpm --eval "%{dist}").noarch.rpm
    yum install -y zfs
    
    /sbin/modprobe zfs # load ZFS module
    zpool --help
    
    usage: zpool command args ...
    where 'command' is one of the following:
    
    				version
    
    				create [-fnd] [-o property=value] ... 
    						[-O file-system-property=value] ... 
    						[-m mountpoint] [-R root] <pool> <vdev> ...
    				destroy [-f] <pool>
    ....
    
  3. 掛載 ZFS 檔案系統

    zpool create zfs_pool /dev/mapper/lvm_data-home_data # 建立名為 zfs_pool (可自定義) 的 ZFS pool
    zpool status
    
    	pool: zfs_pool
     state: ONLINE
    config:
    
    				NAME                  STATE     READ WRITE CKSUM
    				zfs_pool              ONLINE       0     0     0
    					lvm_data-home_data  ONLINE       0     0     0
    
    errors: No known data errors
    
    df -Th
    
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs  4.0M     0  4.0M   0% /dev
    tmpfs          tmpfs     385M  168K  384M   1% /dev/shm
    tmpfs          tmpfs     154M  3.4M  151M   3% /run
    /dev/vda4      xfs        19G   15G  3.9G  80% /
    /dev/vda3      xfs       960M  218M  743M  23% /boot
    /dev/vda2      vfat      200M  7.1M  193M   4% /boot/efi
    tmpfs          tmpfs      77M  8.0K   77M   1% /run/user/0
    tmpfs          tmpfs      77M   92K   77M   1% /run/user/1001
    shm            tmpfs      63M     0   63M   0% /var/lib/containers/storage/overlay-containers/5f7fe414bb14742cb988b902ceab032b9a06e79336a179c80b1c6eb1a154d4a1/userdata/shm
    overlay        overlay    19G   15G  3.9G  80% /var/lib/containers/storage/overlay/5f202781fe4309300f7dd22360e437aa5b21f62be1beaf8223d39c749bcc20c6/merged
    zfs_pool       zfs       1.8T  128K  1.8T   1% /zfs_pool
    
    zfs set mountpoint=/temp zfs_pool # 掛載 zfs_pool 至指定路徑 /temp
    df -hT
    
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs  4.0M     0  4.0M   0% /dev
    tmpfs          tmpfs     385M  168K  384M   1% /dev/shm
    tmpfs          tmpfs     154M  3.4M  151M   3% /run
    /dev/vda4      xfs        19G   15G  3.9G  80% /
    /dev/vda3      xfs       960M  218M  743M  23% /boot
    /dev/vda2      vfat      200M  7.1M  193M   4% /boot/efi
    tmpfs          tmpfs      77M  8.0K   77M   1% /run/user/0
    tmpfs          tmpfs      77M   92K   77M   1% /run/user/1001
    shm            tmpfs      63M     0   63M   0% /var/lib/containers/storage/overlay-containers/5f7fe414bb14742cb988b902ceab032b9a06e79336a179c80b1c6eb1a154d4a1/userdata/shm
    overlay        overlay    19G   15G  3.9G  80% /var/lib/containers/storage/overlay/5f202781fe4309300f7dd22360e437aa5b21f62be1beaf8223d39c749bcc20c6/merged
    zfs_pool       zfs       1.8T  128K  1.8T   1% /zfs_pool
    

2. 設置 ZFS 壓縮格式

zfs set compress=lz4 zfs_pool # 將 zfs_pool 內的寫入壓縮方式更換為 lz4
zfs get compression zfs_pool
NAME             PROPERTY       VALUE  SOURCE
zfs_pool	 compressratio  1.03x  -

若要查看 zfs_pool 內檔案壓縮後的大小,則需要使用 du -sh 方可獲取,若用 ls 則會顯示壓縮前的檔案大小

參考資料:

https://help.aliyun.com/zh/ecs/user-guide/extend-the-partitions-and-file-systems-of-disks-on-a-linux-instance#da4ea1a6ad59y https://sc8log.blogspot.com/2017/03/linux-lvm-lvm.html

Was this article helpful? Votes: 0
Article details:
Published date: 04/03/2025 11:29AM
Last updated: 05/03/2025 6:56AM (Keeper - keeper.kao@nuwainfo.com)
Share article: 
Author: Keeper (keeper.kao@nuwainfo.com)
ZFS | 檔案系統壓縮 |