歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux btrfs文件系統及管理

linux btrfs文件系統及管理

日期:2017/3/1 17:29:38   编辑:Linux技術

什麼是btrfs?

Btrfs(B-tree文件系統,通常念成Butter FS,Better FS或B-tree FS),linux文件系統,具有寫時復制COW(copy-on-write),改善ext3文件系統單文件大小限制,並加入其他特性,如可寫快照,快照的快照,內建RAID,子卷(subvloume),專注於容錯,修復和易於管理。單文件可達16EB,最大文件數量2^64,最大卷容量16EB,等。

btrfs功能特性

1,COW:寫時復制,每次寫入數據時,先將數據寫入到新的block,寫入成功後,更改舊數據塊指針到新數據塊,而非更改本身。
2,多物理卷支持,btrfs內建raid,可在線增刪磁盤設備,可在線擴展和縮減磁盤空間。
3,數據和元數據校驗碼,checksum
4,子卷,可單獨掛載子卷
5,可寫快照,快照的快照,單個文件快照。
6,透明壓縮
7,ext3/4和btrfs無痛互轉

btrfs 基本用法:

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# btrfs --help #查看幫助可以看到btrfs 有很多子命令,用法也很多,這裡只舉例常用選項。
  2. usage: btrfs [--help] [--version] <group> [<group>...] <command> [<args>]
  3. btrfs subvolume create [-i <qgroupid>] [<dest>/]<name> #創建子卷
  4. Create a subvolume
  5. btrfs subvolume delete [options] <subvolume> [<subvolume>...] #刪除子卷
  6. Delete subvolume(s)
  7. btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] [--sort=gen,ogen,rootid,path] <path> #顯示子卷列表
  8. List subvolumes (and snapshots)
  9. btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name> #創建子卷快照
  10. Create a snapshot of the subvolume
  11. btrfs subvolume get-default <path> #獲取子卷默認的文件系統
  12. Get the default subvolume of a filesystem
  13. btrfs subvolume set-default <subvolid> <path> #設置默認系統給子卷
  14. Set the default subvolume of a filesystem
  15. btrfs subvolume find-new <path> <lastgen> #列出btrfs文件系統中最近修改的文件,結合find命令
  16. List the recently modified files in a filesystem
  17. btrfs subvolume show <subvol-path> #顯示更多的子卷信息
  18. Show more information of the subvolume
  19. btrfs subvolume sync <path> [<subvol-id>...] #子卷同步,類似mount同步模式,內存數據同步到磁盤,有待查證。
  20. Wait until given subvolume(s) are completely removed from the filesystem.
  21. btrfs filesystem df [options] <path> #顯示掛載的文件系統詳細信息。
  22. Show space usage information for a mount point
  23. btrfs filesystem show [options] [<path>|<uuid>|<device>|label] #顯示創建文件系統的磁盤信息。
  24. Show the structure of a filesystem
  25. btrfs filesystem sync <path> #強制文件系統同步,
  26. Force a sync on a filesystem
  27. btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...] #碎片整理
  28. Defragment a file or a directory
  29. btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path> #btrfs文件系統在線擴展和縮減空間
  30. Resize a filesystem
  31. btrfs filesystem label [<device>|<mount_point>] [<newlabel>] #改變btrfs文件系統卷標
  32. Get or change the label of a filesystem
  33. btrfs filesystem usage [options] <path> [<path>..] #顯示文件系統當前的使用信息。
  34. Show detailed information about internal filesystem usage .
  35. btrfs balance start [options] <path> #改變磁盤chunk,在線改 數據和元數據 存儲方式,單盤改raid,前提滿足raid要求。
  36. Balance chunks across the devices
  37. btrfs balance pause <path> #暫停chunk更改,數據量較大,轉換時間較長時,先暫停。
  38. Pause running balance
  39. btrfs balance cancel <path> #取消chunk更改,如上
  40. Cancel running or paused balance
  41. btrfs balance resume <path> #中斷balance的操作,如上
  42. Resume interrupted balance
  43. btrfs balance status [-v] <path> #顯示balance操作狀態 如上
  44. Show status of running or paused balance
  45. btrfs device add [options] <device> [<device>...] <path> #文件系統增加磁盤
  46. Add a device to a filesystem
  47. btrfs device delete <device> [<device>...] <path> #文件系統刪除磁盤
  48. Remove a device from a filesystem
  49. btrfs device scan [(-d|--all-devices)|<device> [<device>...]] #文件系統磁盤掃描
  50. Scan devices for a btrfs filesystem
  51. btrfs device ready <device> #猜測是檢測加入的設備有沒有被掛載
  52. Check device to see if it has all of its devices in cache for mounting
  53. btrfs device stats [-z] <path>|<device> #顯示文件系統的設備狀態
  54. Show current device IO stats. -z to reset stats afterwards.
  55. btrfs device usage [options] <path> [<path>..] #顯示文件系統內部設備詳細使用信息
  56. Show detailed information about internal allocations in devices.

btrfs文件系統管理

為分區創建btrfs文件系統

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# fdisk -l | grep "^Disk /dev/sd[a-z]" #准備sd{b,c,d,e}4塊20G磁盤,未做任何分區。
  2. Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
  3. Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
  4. Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
  5. Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors
  6. Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors

創建單分區btrfs並查看

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# mkfs.btrfs -L 'btrfs' /dev/sdb
  2. btrfs-progs v3.19.1
  3. See http://btrfs.wiki.kernel.org for more information.
  4. Turning ON incompat feature 'extref': increased hardlink limit per file to 65536
  5. Turning ON incompat feature 'skinny-metadata': reduced-size metadata extent refs
  6. fs created label btrfs on /dev/sdb
  7. nodesize 16384 leafsize 16384 sectorsize 4096 size 20.00GiB
  8. [root@localhost ~]# btrfs filesystem show
  9. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  10. Total devices 1 FS bytes used 112.00KiB
  11. devid 1 size 20.00GiB used 2.04GiB path /dev/sdb
  12. btrfs-progs v3.19.1
  13. [root@localhost ~]# mount -o compress=lzo -L btrfs /btrfs/ 掛載時可以設定透明壓縮機制。
  14. [root@localhost ~]# btrfs filesystem df /btrfs
  15. Data, single: total=8.00MiB, used=256.00KiB
  16. System, DUP: total=8.00MiB, used=16.00KiB
  17. System, single: total=4.00MiB, used=0.00B
  18. Metadata, DUP: total=1.00GiB, used=112.00KiB
  19. Metadata, single: total=8.00MiB, used=0.00B
  20. GlobalReserve, single: total=16.00MiB, used=0.00B

再添加一塊磁盤

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# btrfs device add /dev/sdc /btrfs #添加磁盤,刪除用btrfs device delete /dev/sdc /btrfs
  2. [root@localhost ~]# btrfs fi sh
  3. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  4. Total devices 2 FS bytes used 384.00KiB
  5. devid 1 size 20.00GiB used 2.04GiB path /dev/sdb
  6. devid 2 size 20.00GiB used 0.00B path /dev/sdc
  7. btrfs-progs v3.19.1
  8. [root@localhost ~]#

在線增加或縮減空間,在線改變空間,可以讓lvm坐冷板凳了。

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# btrfs fi resize -10G /btrfs/ #在線縮減空間
  2. Resize '/btrfs/' of '-10G'
  3. [root@localhost ~]# btrfs fi sh
  4. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  5. Total devices 2 FS bytes used 384.00KiB
  6. devid 1 size 10.00GiB used 2.04GiB path /dev/sdb
  7. devid 2 size 20.00GiB used 0.00B path /dev/sdc
  8. btrfs-progs v3.19.1
  9. [root@localhost ~]# btrfs fi resize +7G /btrfs/ #在線增加空間
  10. Resize '/btrfs/' of '+7G'
  11. [root@localhost ~]# btrfs fi sh
  12. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  13. Total devices 2 FS bytes used 384.00KiB
  14. devid 1 size 17.00GiB used 2.04GiB path /dev/sdb
  15. devid 2 size 20.00GiB used 0.00B path /dev/sdc
  16. btrfs-progs v3.19.1
  17. [root@localhost ~]#

在線更改數據和元數據的結構

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# btrfs fi df /btrfs/
  2. Data, single: total=8.00MiB, used=256.00KiB
  3. System, DUP: total=8.00MiB, used=16.00KiB
  4. System, single: total=4.00MiB, used=0.00B
  5. Metadata, DUP: total=1.00GiB, used=112.00KiB
  6. Metadata, single: total=8.00MiB, used=0.00B
  7. GlobalReserve, single: total=16.00MiB, used=0.00B
  8. [root@localhost ~]# btrfs balance start -mconvert=raid1 /btrfs/ #-mconvert 為改變metadata元數據區的存儲結構
  9. Done, had to relocate 4 out of 5 chunks
  10. [root@localhost ~]# btrfs fi sh
  11. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  12. Total devices 2 FS bytes used 192.00KiB
  13. devid 1 size 17.00GiB used 296.00MiB path /dev/sdb
  14. devid 2 size 20.00GiB used 288.00MiB path /dev/sdc
  15. btrfs-progs v3.19.1
  16. [root@localhost ~]# btrfs fi df /btrfs/
  17. Data, single: total=8.00MiB, used=64.00KiB
  18. System, RAID1: total=32.00MiB, used=16.00KiB
  19. Metadata, RAID1: total=256.00MiB, used=112.00KiB #對比上改為了raid1
  20. GlobalReserve, single: total=16.00MiB, used=0.00B
  21. [root@localhost ~]# btrfs balance start -dconvert=raid1 /btrfs/ #-dconvert 為改變data數據區的存儲結構
  22. Done, had to relocate 1 out of 3 chunks
  23. [root@localhost ~]# btrfs fi df /btrfs/
  24. Data, RAID1: total=1.00GiB, used=320.00KiB #對比上面改為了raid1
  25. System, RAID1: total=32.00MiB, used=16.00KiB
  26. Metadata, RAID1: total=256.00MiB, used=112.00KiB
  27. GlobalReserve, single: total=16.00MiB, used=0.00B
  28. [root@localhost ~]#

顯示當前文件系統使用信息

bash/shell Code復制內容到剪貼板
  1. [root@localhost btrfs]# btrfs fi usage /btrfs/
  2. Overall:
  3. Device size: 37.00GiB #總的空間
  4. Device allocated: 2.56GiB #分給元數據管理區的空間
  5. Device unallocated: 34.44GiB #未分出去的
  6. Device missing: 0.00B
  7. Used: 896.00KiB #使用了的空間
  8. Free (estimated): 18.22GiB(min: 18.22GiB) #可用空間,這裡因是raid1 鏡像卷
  9. Data ratio: 2.00
  10. Metadata ratio: 2.00
  11. Global reserve: 16.00MiB(used: 0.00B)
  12. Data,RAID1: Size:1.00GiB, Used:320.00KiB
  13. /dev/sdb 1.00GiB
  14. /dev/sdc 1.00GiB
  15. Metadata,RAID1: Size:256.00MiB, Used:112.00KiB
  16. /dev/sdb 256.00MiB
  17. /dev/sdc 256.00MiB
  18. System,RAID1: Size:32.00MiB, Used:16.00KiB
  19. /dev/sdb 32.00MiB
  20. /dev/sdc 32.00MiB
  21. Unallocated:
  22. /dev/sdb 15.72GiB #這裡2各磁盤 分配的空間不同,可以說明btrfs的內建raid1不是按物理盤劃分的,而是按chunk,
  23. /dev/sdc 18.72GiB #可以看到具體某塊磁盤的使用狀況。
  24. [root@localhost btrfs]# btrfs device stats /btrfs/ #查看磁盤錯誤信息
  25. [/dev/sdb].write_io_errs 0
  26. [/dev/sdb].read_io_errs 0
  27. [/dev/sdb].flush_io_errs 0
  28. [/dev/sdb].corruption_errs 0
  29. [/dev/sdb].generation_errs 0
  30. [/dev/sdc].write_io_errs 0
  31. [/dev/sdc].read_io_errs 0
  32. [/dev/sdc].flush_io_errs 0
  33. [/dev/sdc].corruption_errs 0
  34. [/dev/sdc].generation_errs 0
  35. [root@localhost btrfs]# btrfs device usage /btrfs/ #磁盤的空間使用狀態
  36. /dev/sdb, ID: 1
  37. Device size: 20.00GiB
  38. Data,RAID1: 1.00GiB
  39. Metadata,RAID1: 256.00MiB
  40. System,RAID1: 32.00MiB
  41. Unallocated: 15.72GiB
  42. /dev/sdc, ID: 2
  43. Device size: 20.00GiB
  44. Data,RAID1: 1.00GiB
  45. Metadata,RAID1: 256.00MiB
  46. System,RAID1: 32.00MiB
  47. Unallocated: 18.72GiB
  48. 壓縮測試
  49. [root@localhost btrfs22]# du -sh /btrfs /btrfs22
  50. 5.1G/btrfs
  51. 5.1G/btrfs22
  52. [root@localhost btrfs22]# btrfs fi show
  53. Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
  54. Total devices 2 FS bytes used 178.63MiB #btrfs掛載采用lzo壓縮,空間被壓縮,因是文本文檔壓縮效果很明顯。
  55. devid 1 size 17.00GiB used 1.28GiB path /dev/sdb
  56. devid 2 size 20.00GiB used 1.28GiB path /dev/sdc
  57. Label: 'btrfs22' uuid: d4da4497-b82e-4846-b51c-cd7127ff5c74
  58. Total devices 2 FS bytes used 5.01GiB #btrfs22掛載沒有啟用壓縮功能。
  59. devid 1 size 20.00GiB used 4.04GiB path /dev/sdd
  60. devid 2 size 20.00GiB used 4.00GiB path /dev/sde
  61. btrfs-progs v3.19.1

子卷和快照的管理

bash/shell Code復制內容到剪貼板
  1. btrfs subvolume
  2. [root@localhost btrfs22]# btrfs subvolume create /btrfs/btrfs.sub #創建子卷
  3. Create subvolume '/btrfs/btrfs.sub'
  4. [root@localhost btrfs22]# btrfs subvolume list /btrfs #顯示子卷
  5. ID 259 gen 43 top level 5 path btrfs.sub
  6. [root@localhost btrfs22]# umount /btrfs
  7. [root@localhost ~]# mount -o subvol=btrfs.sub /dev/sdc /btrfs.sub/ #單獨掛載子卷
  8. [root@localhost ~]# btrfs subvolume show /btrfs.sub/
  9. /btrfs.sub
  10. Name: btrfs.sub
  11. uuid: 811deb2f-764f-6440-899c-cddb2b9867e2
  12. Parent uuid: -
  13. Creation time: 2016-05-11 08:43:51
  14. Object ID: 259
  15. Generation (Gen): 43
  16. Gen at creation: 43
  17. Parent: 5
  18. Top Level: 5
  19. Flags: -
  20. Snapshot(s):

子卷刪除

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# umount /btrfs.sub/ #卸卸載子卷
  2. [root@localhost ~]# btrfs subvolume delete /btrfs/btrfs.sub
  3. #執行刪除命令
  4. [root@localhost ~]# btrfs subvolume list /btrfs
  5. #查看 已經被刪除
  6. ID 260 gen 45 top level 5 path btrfs.sub22

子卷快照,類似創建子卷

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# btrfs subvolume list /btrfs
  2. ID 260 gen 45 top level 5 path btrfs.sub22
  3. [root@localhost ~]# btrfs subvolume snapshot /btrfs/btrfs.sub22/ /btrfs/btrfs.sub22_snapshot
  4. Create a snapshot of '/btrfs/btrfs.sub22/' in '/btrfs/btrfs.sub22_snapshot'
  5. [root@localhost ~]# btrfs subvolume list /btrfs
  6. ID 260 gen 50 top level 5 path btrfs.sub22
  7. ID 262 gen 50 top level 5 path btrfs.sub22_snapshot

單個文件快照

bash/shell Code復制內容到剪貼板
  1. [root@localhost btrfs]# cp --relink hostname hostname_snap
  2. cp: unrecognized option '--relink'
  3. Try 'cp --help' for more information.
  4. [root@localhost btrfs]# cp --reflink hostname hostname_snap
  5. [root@localhost btrfs]# cp --reflink hostname hostname_snap^C
  6. [root@localhost btrfs]# echo "magedu.com" >> hostname
  7. [root@localhost btrfs]# cat hostname
  8. localhost.localdomain
  9. magedu.com
  10. [root@localhost btrfs]# cat hostname_snap
  11. localhost.localdomain
  12. [root@localhost btrfs]#

ext文件系統轉換為btrfs

bash/shell Code復制內容到剪貼板
  1. [root@localhost ~]# cp -r /tmp/* /sdf/
  2. [root@localhost ~]# cd /sdf/
  3. [root@localhost sdf]# ls
  4. ks-script-OqV4Gb lost+found yum.log
  5. [root@localhost ~]# umount /sdf/ #第一步,先卸載需要轉換的ext分區
  6. [root@localhost ~]# fsck -f /dev/sdf #第二步,強制檢測磁盤
  7. fsck from util-linux 2.23.2
  8. e2fsck 1.42.9 (28-Dec-2013)
  9. Pass 1: Checking inodes, blocks, and sizes
  10. Pass 2: Checking directory structure
  11. Pass 3: Checking directory connectivity
  12. Pass 4: Checking reference counts
  13. Pass 5: Checking group summary information
  14. /dev/sdf: 13/1310720 files (0.0% non-contiguous), 126323/5242880 blocks
  15. [root@localhost ~]# btrfs-convert /dev/sdf #第三步,執行轉換命令
  16. creating btrfs metadata.
  17. copy inodes [o] [ 0/ 13]
  18. creating ext2fs image file.
  19. cleaning up system chunk.
  20. conversion complete.
  21. [root@localhost ~]# mount /dev/sdf /sdf/
  22. [root@localhost ~]# cd /sdf/
  23. [root@localhost sdf]# ls
  24. ext2_saved ks-script-OqV4Gb lost+found yum.log #轉換產生的ext2_saved,不可刪除,刪除就無法轉回ext文件系統
  25. [root@localhost sdf]# cat ks-script-OqV4Gb #轉換後查看文件正常。
  26. restorecon -ir /etc/sysconfig/network-scripts /var/lib /etc/lvm \
  27. /dev /etc/iscsi /var/lib/iscsi /root /var/lock /var/log \
  28. /etc/modprobe.d /etc/sysconfig /var/cache/yum
  29. # Also relabel the OSTree variants of the normal mounts (if they exist)
  30. restorecon -ir /var/roothome /var/home /var/opt /var/srv /var/media /var/mnt
  31. restorecon -i /etc/rpm/macros /etc/dasd.conf /etc/zfcp.conf /lib64 /usr/lib64 \
  32. /etc/blkid.tab* /etc/mtab /etc/fstab /etc/resolv.conf \
  33. /etc/modprobe.conf* /var/log/*tmp /etc/crypttab \
  34. /etc/mdadm.conf /etc/sysconfig/network /root/install.log* \
  35. /etc/*shadow* /etc/dhcp/dhclient-*.conf /etc/localtime \
  36. /etc/hostname /root/install.log*
  37. if [ -e /etc/zipl.conf ]; then
  38. restorecon -i /etc/zipl.conf
  39. fi
  40. [root@localhost sdf]# blkid /dev/sdf
  41. /dev/sdf: UUID="20bd2176-28d9-49fb-88e9-b746f5ea2736" UUID_SUB="9076e700-22a2-420e-81df-feb79449ab7c" TYPE="btrfs" #查看文件系統,已經變為btrfs
  42. btrfs轉回ext3
  43. [root@localhost ~]# umount /sdf/ #第一步,卸載
  44. [root@localhost ~]# btrfs-convert -r /dev/sdf #第二步,執行轉換命令
  45. rollback complete.
  46. [root@localhost ~]# blkid /dev/sdf
  47. /dev/sdf: UUID="8d6c4b81-9602-428a-a340-f01b176fa82b" SEC_TYPE="ext2" TYPE="ext3" #查看轉換結果,已經變為etx3格式。
  48. [root@localhost ~]#


總結:初步了解btrfs,首先,擴展性基於B-tree架構,單個文件,磁盤,文件個數,大大超過現在對擴展性的要求,其次,數據一致性,基於COW和checksum確保數據正常(btrfs的數據檢測,修復,恢復相關的命令操作後面補充),再次,設備管理功能強大,內建raid,聯機raid調整,聯機空間擴展和縮減,聯機物理卷增刪,快照,子卷,可脫離raid和lvm.

以上個人總結,不足之處歡迎指正,謝謝。

Copyright © Linux教程網 All Rights Reserved