当前位置: 移动技术网 > 科技>操作系统>Linux > linux btrfs文件系统及管理

linux btrfs文件系统及管理

2018年04月18日  | 移动技术网科技  | 我要评论

什么是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.        
  5. [root@localhost ~]# btrfs subvolume list /btrfs   
  6.       #查看 已经被删除    
  7. 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.

以上个人总结,不足之处欢迎指正,谢谢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网