推荐-btrfs文件系统介绍及操作

btrfs文件系统支持写时复制功能,对备份很有帮助。在单个硬盘时,btrfs是没有意义的。

    核心特性:

        多物理卷支持:btrfs可由多个底层物理卷组成;支持RAID,以联机“添加”、“移除”,“修改”;

        写时复制更新机制(CoW):复制、更新及替换指针,而非“就地”更新;在文件进行修改的时候,首先将文件复制一份出来,在复制出来的文件上进行修改,修改完成之后,将指向原有文件的指针修改指向到修改完成的文件上,若修改完成的文件出现了错误,则我们可以通过原文件进行修复

        数据及元数据校验码;checksum ,当存储某个文件时,checksum会将数据的源数据和数据的校验码,分别通过文件的属性扩展进行保存,当我们再次读取数据时可以方便的检测数据是否受损,如果文件受损系统可以完成自动修复;

        卷:sub_volume,在一个卷上创建多个子卷,在每一个子卷上创建文件系统,并挂载使用;

        快照,以及支持快照的快照,即增量快照备份,也可对单个文件进行快照,

        内置RAID,支持RAID1\0\10\5\6\

        透明压缩 :如果我们在存储文件时,进行压缩存储,那么在文件发往btrfs时,会自动的占用时钟周期,完成数据的压缩存放,而用户并不知道,在用户读取文件时,会自动的进行文件的解压缩,可以实现节约磁盘空间。但是压缩和解压缩会占用时钟周期;

mkfs-btrfs常用选项介绍

mkfs.btrfs [options] btrfsfile_name /dev/sd[a-z] 
   -L " LABEL"
   -d 'raid_type' 指定数据存储时的类型 : raid 0 , raid 1 ,raid 5 ,raid 6, raid 10 ,single,默认是single(单份)
   -m 'raid_profile': 指定元数据(属性数据)存储时的类型 raid 0 ,raid 1 ,raid 5 ,raid 6 ,raid 10 ,single,dup(冗余)
   -f 强制在分区上创建btrfs
   -O <featuer>(以下是子命令,要全敲)
        -O list-all 查看所支持的所有featuer(特性)
        -U 指定UUID

btrfs常用子命令介绍 

[root@localhost ~]# btrfs --help
usage: btrfs [--help] [--version] <group> [<group>...] <command> [<args>]
 
btrfs subvolume create [-i <qgroupid>] [<dest>/]<name> #创建子卷的命令
 Create a subvolume
btrfs subvolume delete [options] <subvolume> [<subvolume>...] #删除子卷
 Delete subvolume(s)
btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] [--sort=gen,ogen,rootid,path] <path> #查看子卷信息
 List subvolumes (and snapshots)
btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name> #创建子卷快照。子卷的快照必须与子卷同处一个目录下
 Create a snapshot of the subvolume
btrfs subvolume get-default <path>  #获取子卷默认的文件系统
 Get the default subvolume of a filesystem
btrfs subvolume set-default <subvolid> <path> #设定子卷的默认文件系统
 Set the default subvolume of a filesystem
btrfs subvolume find-new <path> <lastgen> #查找子卷目录下最近更新文件,结合find命令使用
 List the recently modified files in a filesystem
btrfs subvolume show <subvol-path>#查看子卷
 Show more information of the subvolume
btrfs subvolume sync <path> [<subvol-id>...] 
 Wait until given subvolume(s) are completely removed from the filesystem.
 
btrfs filesystem df [options] <path> #查看使用空间
 Show space usage information for a mount point
btrfs filesystem show [options] [<path>|<uuid>|<device>|label]
 Show the structure of a filesystem #查看btrfs下硬盘的空间及使用空间、卷的UUID等信息
btrfs filesystem sync <path> 
 Force a sync on a filesystem
btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...] #碎片整理
 Defragment a file or a directory
btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path> #调整卷的空间
 Resize a filesystem
btrfs filesystem label [<device>|<mount_point>] [<newlabel>] #查看或设定卷的label
 Get or change the label of a filesystem
btrfs filesystem usage [options] <path> [<path>..] #查看使用情况
 Show detailed information about internal filesystem usage .
 
btrfs balance start [options] <path> #设置文件平衡存储到每个分区中
 Balance chunks across the devices
btrfs balance pause <path> #暂停
 Pause running balance
btrfs balance cancel <path>
 Cancel running or paused balance
btrfs balance resume <path> #恢复 
 Resume interrupted balance
btrfs balance status [-v] <path> #查看状态
 Show status of running or paused balance
 
btrfs device add [options] <device> [<device>...] <path> #添加硬盘
 Add a device to a filesystem
btrfs device delete <device> [<device>...] <path> #删除硬盘
 Remove a device from a filesystem
btrfs device scan [(-d|--all-devices)|<device> [<device>...]] #设备扫描
 Scan devices for a btrfs filesystem
btrfs device ready <device>
 Check device to see if it has all of its devices in cache for mounting
btrfs device stats [-z] <path>|<device>
 Show current device IO stats. -z to reset stats afterwards.
btrfs device usage [options] <path> [<path>..]
 Show detailed information about internal allocations in devices.
btrfs help [--full]
 Display help information
btrfs version
 Display btrfs-progs version
 
Use --help as an argument for information on a specific group or command.
[root@localhost ~]#


练习操作

1、在/dev/sdd /dev/sde 上创建卷标是test_btrfs的btrfs文件系统

[root@localhost ~]# mkfs.btrfs -f -L test_btrfs /dev/sdd /dev/sde
btrfs-progs v3.19.1
See http://btrfs.wiki.kernel.org for more information.

Turning ON incompat feature 'extref': increased hardlink limit per file to 65536
Turning ON incompat feature 'skinny-metadata': reduced-size metadata extent refs
adding device /dev/sde id 2
fs created label test_btrfs on /dev/sdd
nodesize 16384 leafsize 16384 sectorsize 4096 size 7.00GiB
[root@localhost ~]# man mkfs.btrfs
[root@localhost ~]#
[root@localhost ~]# btrfs filesystem show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 2 FS bytes used 112.00KiB
devid    1 size 5.00GiB used 744.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde

2、查看test_btrfs的各种信息

[root@localhost ~]# mkdir /test_btrfs  #创建挂载目录
[root@localhost ~]# mount /dev/sde /test_btrfs/  #挂载。挂载可以使用test_btrfs两个分区名称中的一个来代替
[root@localhost ~]# btrfs filesyste df  -h /test_btrfs/ #查看使用空间。注意,挂载后,用挂载目录的名称来代替btrfs
Data, RAID0: total=716.75MiB, used=192.00KiB
Data, single: total=8.00MiB, used=0.00B
System, RAID1: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, RAID1: total=358.38MiB, used=112.00KiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@localhost ~]#
[root@localhost ~]# btrfs filesyste  label  /test_btrfs/  #查看卷标 
test_btrfs

[root@localhost ~]# btrfs fi usage /test_btrfs/  #查看空间使用率
Overall:
   Device size:   7.00GiB
   Device allocated:   1.43GiB
   Device unallocated:   5.57GiB
   Device missing:     0.00B
   Used: 448.00KiB
   Free (estimated):   6.27GiB (min: 3.49GiB)
   Data ratio:      1.00
   Metadata ratio:      1.98
   Global reserve:  16.00MiB (used: 0.00B)

Data,RAID0: Size:716.75MiB, Used:192.00KiB
  /dev/sdd 358.38MiB
  /dev/sde 358.38MiB

Metadata,single: Size:8.00MiB, Used:0.00B
  /dev/sdd   8.00MiB

Metadata,RAID1: Size:358.38MiB, Used:112.00KiB
  /dev/sdd 358.38MiB
  /dev/sde 358.38MiB

System,single: Size:4.00MiB, Used:0.00B
  /dev/sdd   4.00MiB

System,RAID1: Size:8.00MiB, Used:16.00KiB
  /dev/sdd   8.00MiB
  /dev/sde   8.00MiB

Unallocated:
  /dev/sdd   4.28GiB
  /dev/sde   1.29GiB

3、添加/dev/sdb和/dev/sdc到test_btrfs后,删除/dev/sdd

[root@localhost ~]# btrfs device add  -f /dev/sdb /test_btrfs #添加
[root@localhost ~]# btrfs device add  -f /dev/sdc /test_btrfs
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 4 FS bytes used 320.00KiB
devid    1 size 5.00GiB used 736.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde
devid    3 size 5.00GiB used 0.00B path /dev/sdb
devid    4 size 5.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1
[root@localhost ~]#
[root@localhost ~]# btrfs device de /dev/sdb /test_btrfs #删除
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 3 FS bytes used 320.00KiB
devid    1 size 5.00GiB used 736.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde
devid    4 size 5.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1
[root@localhost ~]#

4、先缩小test_btrfs空间1GB,然后再增加1GB

[root@localhost ~]# btrfs fi resize -1g /test_btrfs/ #减小1G
Resize '/test_btrfs/' of '-1g'
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 3 FS bytes used 320.00KiB
devid    1 size 4.00GiB used 736.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde
devid    4 size 5.00GiB used 0.00B path /dev/sdc
[root@localhost ~]# btrfs fi resize +1g /test_btrfs/   #再添加1G
Resize '/test_btrfs/' of '+1g'
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 3 FS bytes used 320.00KiB
devid    1 size 5.00GiB used 736.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde
devid    4 size 5.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1
[root@localhost ~]# btrfs fi resize  max /test_btrfs/ #也可以使用max选项,把/test_btrfs的空间扩展到/dev/sd{d,e,c}三个分区的最大空间
Resize '/test_btrfs/' of 'max'
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 3 FS bytes used 320.00KiB
devid    1 size 5.00GiB used 736.75MiB path /dev/sdd
devid    2 size 2.00GiB used 724.75MiB path /dev/sde
devid    4 size 5.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1
[root@localhost ~]#

5、注意到上面btrfs filesystem show其中的used的信息了吗?/dec/sdc的使用空间为零。/dev/sdc要是后来手动加入到test_btrfs,是吧。所以默认情况需要前两个分区的空间用完后,才会使用/dev/sdc的空间。那么现在把/dev/sdc的空间也使用起来吧!

[root@localhost ~]# btrfs balance  start  /test_btrfs/ #把数据平衡使用到所有分区
Done, had to relocate 5 out of 5 chunks
[root@localhost ~]# btrfs fi show
Label: 'test_btrfs'  uuid: 2827ef62-3b45-464b-b6cd-8799f4d143cf
Total devices 3 FS bytes used 448.00KiB
devid    1 size 5.00GiB used 704.00MiB path /dev/sdd
devid    2 size 2.00GiB used 416.00MiB path /dev/sde
devid    4 size 5.00GiB used 704.00MiB path /dev/sdc

btrfs-progs v3.19.1
[root@localhost ~]#

6、创建子卷test_btrfs1和test_btrfs2

[root@localhost ~]# cd /test_btrfs/ 
[root@localhost test_btrfs]# ls
[root@localhost test_btrfs]# btrfs subvolume  create /test_btrfs/test_btrfs1 #创建test_btrfs1
Create subvolume '/test_btrfs/test_btrfs1'
[root@localhost test_btrfs]# btrfs subvolume  create /test_btrfs/test_btrfs2 #创建test_btrfs2
Create subvolume '/test_btrfs/test_btrfs2'
[root@localhost test_btrfs]# ls
test_btrfs1  test_btrfs2
[root@localhost test_btrfs]# btrfs subvolume  list -a /test_btrfs/ #查看
ID 260 gen 39 top level 5 path test_btrfs1
ID 261 gen 40 top level 5 path test_btrfs2

7、创建test_btrfs1的快照分区test_btrfs_sanpshot,并验证文件testsnapshot.txt的单个文件快照功能

[root@localhost test_btrfs]# cat test_btrfs1/test.txt 
test
123456
[root@localhost test_btrfs1]# btrfs sub snapshot  /test_btrfs/test_btrfs1/ /test_btrfs/test_btrfs_snapshot #分区快照 ,快照分区必须与源分区同属一个父目录
Create a snapshot of '/test_btrfs/test_btrfs1/' in '/test_btrfs/test_btrfs_snapshot'
[root@localhost test_btrfs1]# btrfs sub show  /test_btrfs/test_btrfs1 | grep "shot" Snapshot(s): test_btrfs_snapshot
[root@localhost test_btrfs]# cat test_btrfs_snapshot/test.txt
test
123456
[root@localhost test_btrfs1]# cp --reflink test.txt testsnapshot.txt #建立文件的快照,cp --relink是建立是福本的意思
[root@localhost test_btrfs1]# ls
testsnapshot.txt  test.txt
[root@localhost test_btrfs1]# cat testsnapshot.txt
test
123456
[root@localhost test_btrfs1]# cat test.txt
test
123456
123aabcd
[root@localhost test_btrfs1]# cat testsnapshot.txt
test
123456
[root@localhost test_btrfs1]#

8、手动挂载子卷,然后父卷直接路径访问

[root@localhost /]# ls /test_btrfs/test_btrfs1  
testsnapshot.txt  test.txt
[root@localhost /]# mount -o subvol=test_btrfs1 /dev/sdd /mnt  #把子卷test_btrfs1挂载到/mnt
[root@localhost /]# btrfs subv show /mnt  #通过挂载目录来查看子卷的信息,说明已挂载成功
/mnt
Name: test_btrfs1
uuid: 747a2849-7c62-1647-a05d-e1fb10bf1907
Parent uuid: -
Creation time: 2016-05-24 22:50:58
Object ID: 258
Generation (Gen): 26
Gen at creation: 6
Parent: 5
Top Level: 5
Flags: -
Snapshot(s):
test_btrfs_snapshot
[root@localhost /]# touch /mnt/{1..10} #在/mnt目录下创建1-10的文件
[root@localhost /]# ls /mnt/ #挂载的目录除了创建的1-10文件之外,还会自动同步/test_btrfs/test_btrfs1下的文件
1  10  2  3  4  5  6  7  8  9  testsnapshot.txt  test.txt
[root@localhost /]# ls /test_btrfs/test_btrfs1 #在父卷的子卷名称目录下,就会自动同步显示/mnt的文件
1  10  2  3  4  5  6  7  8  9  testsnapshot.txt  test.txt
说明:在挂载子卷的目录时,如果父卷是处于挂载的状态,那么就会自动在父卷目录下挂载子卷的挂载目录;如果父卷是处于没有挂载的状态,那么只要父卷一完成再挂载,也会自动挂载子卷挂载的目录

9、把ext系列的分区/dev/sdb转换成btrfs文件系统

使用到btrfs-conver 转换命令,其中-r 选项是从btrfs 回退到ext

[root@localhost /]# mkfs.ext3 /dev/sdb #把/dev/sdb格式为ext3
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736
 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 
 
[root@localhost /]# mount /dev/sdb /root/test #挂载
[root@localhost /]# ls /root/test
lost+found
[root@localhost /]# touch /root/test/{1..10} #创建1-10文件
[root@localhost /]# ls /root/test
1  10  2  3  4  5  6  7  8  9  lost+found
[root@localhost /]# umount /root/test #卸载
[root@localhost /]# fsck -f /dev/sdb #强制检查/dev/sdb
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb: 21/327680 files (0.0% non-contiguous), 55935/1310720 blocks
[root@localhost /]# btrfs-convert /dev/sdb  #转换成btrfs
creating btrfs metadata.
copy inodes [o] [         0/        21]
creating ext2fs image file.
cleaning up system chunk.
conversion complete.
[root@localhost /]# mount /dev/sdb /root/test/ 
[root@localhost /]# ls root/test #再挂载后,文件依然存在
1  10  2  3  4  5  6  7  8  9  ext2_saved  lost+found
[root@localhost /]# blkid | grep "sdb"
/dev/sdb: UUID="06ab4751-a5ca-467e-92f5-c7b6ed2efd1c" UUID_SUB="1fba9c79-dd9a-425e-9d29-e72149661bee" TYPE="btrfs"  #已经是btrfs
[root@localhost /]# umount /root/test
[root@localhost /]# btrfs fi show
Label: 'test_btrfs'  uuid: 8b7db284-7c8b-496c-8cca-1a1278d13ffc
	Total devices 3 FS bytes used 816.00KiB
	devid    1 size 5.00GiB used 1.51GiB path /dev/sdd
	devid    2 size 5.00GiB used 520.00MiB path /dev/sdc
	devid    3 size 5.00GiB used 1.51GiB path /dev/sde
 
Label: none  uuid: 06ab4751-a5ca-467e-92f5-c7b6ed2efd1c
	Total devices 1 FS bytes used 219.39MiB
	devid    1 size 5.00GiB used 5.00GiB path /dev/sdb
 
btrfs-progs v3.19.1
[root@localhost /]# btrfs-convert -r /dev/sdb #回退到ext3
rollback complete.
[root@localhost /]# mount /dev/sdb /root/test
[root@localhost /]# blkid | grep "sdb"
/dev/sdb: UUID="f83914d8-aade-4adb-b8d2-a2e47b665bfc" TYPE="ext3"  #已回退成功
[root@localhost /]# ls /root/test/ #回退后文件依然在
1  10  2  3  4  5  6  7  8  9  lost+found
[root@localhost /]#

10、把test_btrfs的元数据、数据存储方式从默认转换到raid5

[root@localhost /]# btrfs fi df -h /test_btrfs/ 
Data, RAID0: total=1.50GiB, used=640.00KiB #数据默认的是raid0
System, RAID1: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, RAID1: total=1.00GiB, used=160.00KiB  #元数据默认的是raid1
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@localhost /]# btrfs balance start -mconvert=raid5 /test_btrfs/ #把元数据存储转换成raid5
Done, had to relocate 4 out of 5 chunks
[root@localhost /]# btrfs fi df -h /test_btrfs/
Data, RAID0: total=1.50GiB, used=448.00KiB
System, RAID5: total=64.00MiB, used=16.00KiB #已转换成功
Metadata, RAID5: total=256.00MiB, used=160.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@localhost /]# btrfs balance start -dconvert=raid5 /test_btrfs/  #把数据存储转换成raid5
Done, had to relocate 1 out of 3 chunks
[root@localhost /]# btrfs fi df -h /test_btrfs/
Data, RAID5: total=1.50GiB, used=832.00KiB #已转换成功
System, RAID5: total=64.00MiB, used=16.00KiB
Metadata, RAID5: total=256.00MiB, used=160.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B

原创文章,作者:Net20-deamon,如若转载,请注明出处:http://www.178linux.com/17150

(0)
Net20-deamonNet20-deamon
上一篇 2016-06-01
下一篇 2016-06-01

相关推荐

  • sed工具 介绍

    sed工具 地址定界 不给地址:对全文进行处理 单地址: #:指定行 /pattern/:被此处模式所能够匹配到的每一行 地址范围: #,# #,+# /pat1/,/pat2/ #,/pat1/ ~:步进 1~2 奇树行 2~2 偶数行 cat -n passwd >passwd2 新建文件 sep -n ‘/^h//^s/’…

    Linux干货 2017-05-31
  • 设计模式(三)建造者模式Builder(创建型)

    1. 概述        在软件开发的过程中,当遇到一个“复杂的对象”的创建工作,该对象由一定各个部分的子对象用一定的算法构成,由于需求的变化,复杂对象的各个部分经常面临剧烈的变化,但将它们组合在一起的算法相对稳定。        例子1:买肯德基     &…

    Linux干货 2015-06-25
  • 网络配置——CentOS7

    CentOS 7 网络属性配置 使用传统命名方式: (1) 编辑/etc/default/grub 配置文件 GRUB_CMDLINE_LINUX ="rhgb quiet net.ifnames=0" 或:修改/boot/grub2/grub.cfg (2) 为grub2 生成其配置文件 grub2-mkconfig -o /etc/g…

    Linux干货 2016-09-11
  • GPG——另一种加密信息的方式

    GPG ·使用gpg实现对称加密 ·对称加密file文件          gpg -c file          ls file.gpg ·在另一台主机上解密file   &n…

    Linux干货 2016-09-22
  • 关于Linux文件系统的的简单理解和认识-20160730

    关于Linux文件系统的的简单理解和认识 关于文件系统的运作,这与操作系统带的档案数据有关。例如Linux操作系统的档案权限(rwx)与文件属性(拥有者,群组,时间参数等)。文件系统通常会将这两部分的数据分别存放在不同的区块,权限与属性放置到inode中,至于实际数据则放置到date block区块中,另外,还有一个超级块区(super block)会记录整…

    Linux干货 2016-08-04
  • 文件编辑之神器Sed

    pattern space   //  文本中每行内容都会进入到pattern space中, 如果匹配到了,就会进入到右分支, 如果没有匹配到,则进入左分支。 hold space  //  就是已经被模式匹配到, 并且编辑后保存的内容就是hold space 中. 默认情况下,当没有被匹配之后,没有匹配到…

    Linux干货 2016-08-15

评论列表(1条)

  • stanley
    stanley 2016-06-01 15:30

    已置顶,非常不错。欢迎加入免检家族