Linux Btrfs file system and management


What is Btrfs?

Btrfs (B-tree file system, usually read as butter FS, better FS or B-tree FS), Linux file system, with copy on write (copy on write), improve the single file size limit of ext3 file system, and add other features, such as writable snapshot, snapshot snapshot, built-in raid, subvolume, focusing on fault tolerance, repair and easy management. Single file can reach 16eb, the maximum number of files is 2 ^ 64, the maximum volume capacity is 16eb, etc.

Functional characteristics of Btrfs

1. Cow: copy when writing. When writing data, write the data to the new block first. After writing successfully, change the old data block pointer to the new data block instead of changing itself.
2. Multi physical volume support, Btrfs built-in raid, can add and delete disk devices online, and can expand and reduce disk space online.
3. Data and metadata check code
4. Subvolume, which can be mounted separately
5. Writable snapshot, snapshot of snapshot, single file snapshot.
6. Transparent compression
7, ext3 / 4 and Btrfs are painless

Basic usage of Btrfs:

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]#Btrfs — help ා view help. You can see that Btrfs has many subcommands and uses a lot. Here are just some common options.  
  2. usage: btrfs [–help] [–version] <group> [<group>…] <command> [<args>]   
  3.     btrfs subvolume create [-i <qgroupid>] [<dest>/]<name> #Create subvolume  
  4.         Create a subvolume                     
  5.     btrfs subvolume delete [options] <subvolume> [<subvolume>…] #Delete subvolume  
  6.         Delete subvolume(s)   
  7.     btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] [–sort=gen,ogen,rootid,path] <path> #Show list of subvolumes  
  8.         List subvolumes (and snapshots)   
  9.     btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name> #Create a subvolume snapshot  
  10.         Create a snapshot of the subvolume   
  11.     btrfs subvolume get-default <path>  #Get subvolume default file system  
  12.         Get the default subvolume of a filesystem   
  13.     btrfs subvolume set-default <subvolid> <path> #Set default system to subvolume  
  14.         Set the default subvolume of a filesystem   
  15.     btrfs subvolume find-new <path> <lastgen>  #List the most recently modified files in the Btrfs file system, combined with the find command  
  16.         List the recently modified files in a filesystem   
  17.     btrfs subvolume show <subvol-path>  #Show more subvolume information  
  18.         Show more information of the subvolume   
  19.     btrfs subvolume sync <path> [<subvol-id>…]  #Subvolume synchronization, similar to mount synchronization mode, synchronizes memory data to disk, which needs to be verified.  
  20.         Wait until given subvolume(s) are completely removed from the filesystem.   
  21.     btrfs filesystem df [options] <path>  #Displays the mounted file system details.  
  22.         Show space usage information for a mount point   
  23.     btrfs filesystem show [options] [<path>|<uuid>|<device>|label]  #Displays the disk information for creating the file system.  
  24.         Show the structure of a filesystem   
  25.     btrfs filesystem sync <path>  #Force file system synchronization,  
  26.         Force a sync on a filesystem   
  27.     btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>…]  #Defragmentation  
  28.         Defragment a file or a directory   
  29.     btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>  #Btrfs file system online expansion and reduction of space  
  30.         Resize a filesystem   
  31.     btrfs filesystem label [<device>|<mount_point>] [<newlabel>]  #Change Btrfs file system volume label  
  32.         Get or change the label of a filesystem   
  33.     btrfs filesystem usage [options] <path> [<path>..]  #Displays the current usage information of the file system.  
  34.         Show detailed information about internal filesystem usage .   
  35.     btrfs balance start [options] <path>  #Change the disk chunk, change the data and metadata storage mode online, and change the raid for a single disk, provided that the raid requirements are met.  
  36.         Balance chunks across the devices   
  37.     btrfs balance pause <path>                                                                       #Pause chunk changes. When the data volume is large and the conversion time is long, pause first.  
  38.         Pause running balance   
  39.     btrfs balance cancel <path>                                                                     #Cancel chunk changes as above  
  40.         Cancel running or paused balance   
  41.     btrfs balance resume <path>                                                                   #Interrupt balance operation, as above  
  42.         Resume interrupted balance   
  43.     btrfs balance status [-v] <path>                                                                  #Show balance operation status as above  
  44.         Show status of running or paused balance   
  45.     btrfs device add [options] <device> [<device>…] <path>  #File system add disk  
  46.         Add a device to a filesystem   
  47.     btrfs device delete <device> [<device>…] <path>   #File system delete disk  
  48.         Remove a device from a filesystem   
  49.     btrfs device scan [(-d|–all-devices)|<device> [<device>…]]  #File system ScanDisk  
  50.         Scan devices for a btrfs filesystem   
  51.     btrfs device ready <device>                                                                          #Guess is to check whether the added device is mounted  
  52.         Check device to see if it has all of its devices in cache for mounting       
  53.     btrfs device stats [-z] <path>|<device>   #Displays the device status of the file system  
  54.         Show current device IO stats. -z to reset stats afterwards.   
  55.     btrfs device usage [options] <path> [<path>..]    #Display file system internal device details  
  56.         Show detailed information about internal allocations in devices.  

Btrfs file system management

Create Btrfs file system for partition

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]#Fdisk – L | grep “^ disk / dev / SD [A-Z]” ා prepare four 20g disks of SD {B, C, D, e}, without any partition.  
  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  

Create single zone Btrfs and view

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]# mkfs.btrfs -L ‘btrfs’ /dev/sdb   
  2. btrfs-progs v3.19.1   
  3. See 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. [[email protected] ~]# 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. [[email protected] ~]#Mount – O compress = LZO – L Btrfs / Btrfs / transparent compression mechanism can be set when mounting.  
  14. [[email protected] ~]# 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  

Add another disk

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]#Btrfs device add / dev / SDC / Btrfs ා add disk, delete with Btrfs device delete / dev / SDC / Btrfs  
  2. [[email protected] ~]# 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. [[email protected] ~]#  

Online increase or decrease space, online change space, can let LVM sit on the bench.

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]#Btrfs fi resize – 10G / Btrfs / ා online space reduction  
  2. Resize ‘/btrfs/’ of ‘-10G’  
  3. [[email protected] ~]# 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. [[email protected] ~]#Btrfs fi resize + 7g / Btrfs / ා increase space online  
  10. Resize ‘/btrfs/’ of ‘+7G’  
  11. [[email protected] ~]# 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. [[email protected] ~]#  

Change the structure of data and metadata Online

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]# 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. [[email protected] ~]#Btrfs balance start – mconvert = RAID1 / Btrfs / ා – mconvert to change the storage structure of metadata area  
  9. Done, had to relocate 4 out of 5 chunks   
  10. [[email protected] ~]# 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. [[email protected] ~]# 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    #Change to RAID1 by comparison  
  20. GlobalReserve, single: total=16.00MiB, used=0.00B   
  21. [[email protected] ~]#Btrfs balance start – dconvert = RAID1 / Btrfs / ා – dconvert to change the storage structure of data area  
  22. Done, had to relocate 1 out of 3 chunks   
  23. [[email protected] ~]# btrfs fi df /btrfs/   
  24. Data, RAID1: total=1.00GiB, used=320.00KiB    #Compared with the above, change to 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. [[email protected] ~]#  

Display current file system usage information

bash/shell CodeCopy content to clipboard
  1. [[email protected] btrfs]# btrfs fi usage /btrfs/   
  2. Overall:   
  3.     Device size:  37.00GiB   #Total space  
  4.     Device allocated:   2.56GiB   #Space allocated to metadata management area  
  5.     Device unallocated:  34.44GiB  #Undivided  
  6.     Device missing:     0.00B   
  7.     Used: 896.00KiB    #Used space  
  8.     Free (estimated):  18.22GiB(min: 18.22GiB)  #Free space, because it is a RAID1 mirrored volume  
  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    #Here 2, the space allocated by each disk is different, which shows that the built-in RAID1 of Btrfs is not divided by physical disk, but by chunk,  
  23.    /dev/sdc  18.72GiB     #You can see the usage of a specific disk.  
  24. [[email protected] btrfs]#Btrfs device stats / Btrfs / ා view disk error information  
  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. [[email protected] btrfs]#Btrfs device usage / Btrfs / ා space usage status of disk  
  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. Compression test
  49. [[email protected] btrfs22]# du -sh /btrfs /btrfs22   
  50. 5.1G/btrfs   
  51. 5.1G/btrfs22   
  52. [[email protected] btrfs22]# btrfs fi show   
  53. Label: ‘btrfs’  uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a   
  54.  Total devices 2 FS bytes used 178.63MiB        #Btrfs mount uses LZO compression, and the space is compressed, because the compression effect of text document is very obvious.  
  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    #The btrfs22 mount does not have compression enabled.  
  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  

Management of subvolumes and snapshots

bash/shell CodeCopy content to clipboard
  1. btrfs subvolume    
  2. [[email protected] btrfs22]#Btrfs subvolume create / Btrfs / btrfs.sub  
  3. Create subvolume ‘/btrfs/btrfs.sub’  
  4. [[email protected] btrfs22]#Btrfs subvolume list /btrfs display subvolumes  
  5. ID 259 gen 43 top level 5 path btrfs.sub   
  6. [[email protected] btrfs22]# umount /btrfs   
  7. [[email protected] ~]#Mount – O subvol = Btrfs. Sub / dev / SDC / Btrfs. Sub / ා mount subvolumes separately  
  8. [[email protected] ~]# 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):  

Subvolume deletion

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]#Umount / btrfs.sub/? Unmount subvolumes  
  2. [[email protected] ~]# btrfs subvolume delete /btrfs/btrfs.sub   
  3.     #Execute delete command  
  5. [[email protected] ~]# btrfs subvolume list /btrfs   
  6.       #View has been deleted  
  7. ID 260 gen 45 top level 5 path btrfs.sub22  

Subvolume snapshot, similar to creating a subvolume

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]# btrfs subvolume list /btrfs   
  2. ID 260 gen 45 top level 5 path btrfs.sub22   
  3. [[email protected] ~]# btrfs subvolume snapshot /btrfs/btrfs.sub22/ /btrfs/btrfs.sub22_snapshot   
  4. Create a snapshot of ‘/btrfs/btrfs.sub22/’ in ‘/btrfs/btrfs.sub22_snapshot’  
  5. [[email protected] ~]# 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  

Single file snapshot

bash/shell CodeCopy content to clipboard
  1. [[email protected] btrfs]# cp –relink hostname hostname_snap   
  2. cp: unrecognized option ‘–relink’  
  3. Try ‘cp –help’ for more information.   
  4. [[email protected] btrfs]# cp –reflink hostname hostname_snap   
  5. [[email protected] btrfs]# cp –reflink hostname hostname_snap^C   
  6. [[email protected] btrfs]# echo “” >> hostname   
  7. [[email protected] btrfs]# cat hostname   
  8. localhost.localdomain   
  10. [[email protected] btrfs]# cat hostname_snap   
  11. localhost.localdomain   
  12. [[email protected] btrfs]#  

Ext file system to Btrfs

bash/shell CodeCopy content to clipboard
  1. [[email protected] ~]# cp -r /tmp/* /sdf/   
  2. [[email protected] ~]# cd /sdf/   
  3. [[email protected] sdf]# ls   
  4. ks-script-OqV4Gb  lost+found  yum.log   
  5. [[email protected] ~]#Umount / SDF / ා the first step is to uninstall the EXT partition to be converted  
  6. [[email protected] ~]#Fsck – F / dev / SDF step 2, force disk detection  
  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. [[email protected] ~]#Btrfs convert / dev / SDF step 3, execute the conversion command  
  16. creating btrfs metadata.   
  17. copy inodes [o] [         0/        13]   
  18. creating ext2fs image file.   
  19. cleaning up system chunk.   
  20. conversion complete.   
  21. [[email protected] ~]# mount /dev/sdf /sdf/   
  22. [[email protected] ~]# cd /sdf/   
  23. [[email protected] sdf]# ls   
  24. ext2_saved  ks-script-OqV4Gb  lost+found  yum.log    #The ext2 [saved] generated by the conversion cannot be deleted. If it is deleted, it cannot be transferred back to the EXT file system  
  25. [[email protected] sdf]#After cat KS script oqv4gb conversion, it is normal to view the file.  
  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/* /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. [[email protected] sdf]# blkid /dev/sdf   
  41. /dev/sdf: UUID=“20bd2176-28d9-49fb-88e9-b746f5ea2736” UUID_SUB=“9076e700-22a2-420e-81df-feb79449ab7c” TYPE=“btrfs”  #Viewing the file system has changed to Btrfs  
  42. Btrfs back to ext3
  43. [[email protected] ~]#Umount / SDF / ා first step, uninstall  
  44. [[email protected] ~]#Btrfs convert – R / dev / SDF step 2, execute the conversion command  
  45. rollback complete.   
  46. [[email protected] ~]# blkid /dev/sdf   
  47. /dev/sdf: UUID=“8d6c4b81-9602-428a-a340-f01b176fa82b” SEC_TYPE=“ext2” TYPE=“ext3”  #View the conversion result. It has been changed to etx3 format.  
  48. [[email protected] ~]#  

Summary: we have a preliminary understanding of Btrfs. First, the scalability is based on B-tree architecture, and the number of single file, disk, and file greatly exceeds the current requirements for scalability. Second, data consistency is based on cow and check sum to ensure the normal data (Btrfs data detection, repair, recovery of related command operations are supplemented later). Third, the device management function is powerful, built-in raid, online RA ID adjustment, online space expansion and reduction, online physical volume addition and deletion, snapshot, subvolume, detachable from raid and LVM

The above personal summary, shortcomings welcome to correct, thank you.