1.查看磁盘情况:

[root@network ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
├─sdb1            8:17   0    5G  0 part 
├─sdb2            8:18   0    5G  0 part 
├─sdb3            8:19   0    5G  0 part 
└─sdb4            8:20   0    5G  0 part 
sr0              11:0    1    4G  0 rom  

2.设置卷标格式化为btrfs文件系统

[root@network ~]# mkfs.btrfs -L mydata /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4 
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/sdb2 id 2
adding device /dev/sdb3 id 3
adding device /dev/sdb4 id 4
fs created label mydata on /dev/sdb1
    nodesize 16384 leafsize 16384 sectorsize 4096 size 20.00GiB

3. 发现有filesystem这个命令

[root@network ~]# man btrfs  
[root@network ~]# man btrfs-filesystem 
BTRFS-FILESYSTEM(8)                                  Btrfs Manual                                 BTRFS-FILESYSTEM(8)

NAME
       btrfs-filesystem - control btrfs filesystem

SYNOPSIS
       btrfs filesystem <subcommand> <args>

4. 查看制作的btrfs文件系统

[root@network ~]# btrfs filesystem show    
Label: 'mydata'  uuid: ddbcaabe-8e2d-4254-8e02-5fd310927cd9
    Total devices 4 FS bytes used 112.00KiB
    devid    1 size 5.00GiB used 1.52GiB path /dev/sdb1
    devid    2 size 5.00GiB used 1.50GiB path /dev/sdb2
    devid    3 size 5.00GiB used 519.94MiB path /dev/sdb3
    devid    4 size 5.00GiB used 519.94MiB path /dev/sdb4

btrfs-progs v3.19.1

5. #显示所有挂载

[root@network ~]# btrfs filesystem show --mounted
btrfs-progs v3.19.1
[root@network ~]# btrfs filesystem show --all-devices  #显示所有的设备
Label: 'mydatas'  uuid: 6507eecb-29fb-4b33-a5aa-b3d39b4f36b9
    Total devices 2 FS bytes used 112.00KiB
    devid    1 size 20.00GiB used 2.03GiB path /dev/sdc
    devid    2 size 20.00GiB used 2.01GiB path /dev/sdd

Label: 'mydata'  uuid: ddbcaabe-8e2d-4254-8e02-5fd310927cd9
    Total devices 4 FS bytes used 200.00KiB
    devid    1 size 1.00GiB used 531.94MiB path /dev/sdb1
    devid    2 size 5.00GiB used 519.94MiB path /dev/sdb2
    devid    3 size 5.00GiB used 783.94MiB path /dev/sdb3
    devid    4 size 5.00GiB used 783.94MiB path /dev/sdb4
btrfs-progs v3.19.1

6. 输出一个磁盘就两个都显示

[root@network ~]# btrfs filesystem show /dev/sdc 
Label: 'mydatas'  uuid: 6507eecb-29fb-4b33-a5aa-b3d39b4f36b9
    Total devices 2 FS bytes used 112.00KiB
    devid    1 size 20.00GiB used 2.03GiB path /dev/sdc
    devid    2 size 20.00GiB used 2.01GiB path /dev/sdd

btrfs-progs v3.19.1
[root@network ~]# btrfs filesystem show /dev/sdd
Label: 'mydatas'  uuid: 6507eecb-29fb-4b33-a5aa-b3d39b4f36b9
    Total devices 2 FS bytes used 112.00KiB
    devid    1 size 20.00GiB used 2.03GiB path /dev/sdc
    devid    2 size 20.00GiB used 2.01GiB path /dev/sdd

btrfs-progs v3.19.1

7.或者最直接对磁盘:

[root@network ~]# mkfs.btrfs  -L mydatas  /dev/sdc /dev/sdd 
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/sdd id 2
fs created label mydatas on /dev/sdc
    nodesize 16384 leafsize 16384 sectorsize 4096 size 40.00GiB
[root@network ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="mydata" UUID="ddbcaabe-8e2d-4254-8e02-5fd310927cd9" UUID_SUB="01609da4-e282-4aa3-91fb-45dfd4573831" TYPE="btrfs" 
[root@network ~]# blkid /dev/sdb2
/dev/sdb2: LABEL="mydata" UUID="ddbcaabe-8e2d-4254-8e02-5fd310927cd9" UUID_SUB="88e06cb6-0fbc-4257-b264-31e9f782731c" TYPE="btrfs" 

9. 查看卷标

[root@network ~]# btrfs filesystem label  /dev/sdb1 
mydata
[root@network ~]# btrfs filesystem label  /dev/sdb2
mydata
[root@network ~]# btrfs filesystem label  /dev/sdb3
mydata
[root@network ~]# btrfs filesystem label  /dev/sdb4
mydata

10. 挂载使用

[root@network ~]# mount -t  btrfs  /dev/sdb1  /mydata 

11.实现自动的压缩

[root@network ~]# mount -o compress=lzo /dev/sdb1  /mydata  
[root@network ~]# cp /etc/rc.d/init.d/
functions   netconsole  network     README      
[root@network ~]# cp /etc/rc.d/init.d/functions  /mydata/
[root@network ~]# cd /mydata/
[root@network mydata]# ls
functions

[root@network mydata]# man btrfs-device

13.resize子命令减容

Resize '/mydata/' of '-3G'
[root@network mydata]# btrfs filesystem resize -3G  /mydata/   
[root@network ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  5.6G   12G  32% /
devtmpfs                 479M     0  479M   0% /dev
tmpfs                    489M     0  489M   0% /dev/shm
tmpfs                    489M  6.7M  483M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                497M  108M  390M  22% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/sdb                  80G   18M   78G   1% /mydata
[root@network ~]# btrfs filesystem  df   /mydata/     #btrfs 自带的命令 df命令查看
Data, RAID0: total=4.00GiB, used=1.25MiB
System, RAID1: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, RAID1: total=1.00GiB, used=112.00KiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@network ~]# 

15 扩展到最大空间 max

[root@network ~]# btrfs filesystem resize max /mydata/  
Resize '/mydata/' of 'max'

16 从磁盘挪出那一部分,如果是添加就add

[root@network ~]# btrfs device delete /dev/sdc /mydata/
[root@network ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  5.6G   12G  32% /
devtmpfs                 479M     0  479M   0% /dev
tmpfs                    489M     0  489M   0% /dev/shm
tmpfs                    489M  6.7M  483M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                497M  108M  390M  22% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/sdb                  60G   18M   57G   1% /mydata
17
BTRFS-BALANCE(8)                                     Btrfs Manual                                    BTRFS-BALANCE(8)

NAME
       btrfs-balance - balance btrfs filesystem

SYNOPSIS
       btrfs balance <subcommand> <args>

18
[root@network ~]# btrfs balance status /mydata
No balance found on '/mydata'
[root@network ~]# btrfs balance start /mydata  #开启balance
Done, had to relocate 5 out of 5 chunks
19
[root@network ~]# man btrfs-balance
他有子命令
pause <path>
           Pause running balance.

       resume <path>
           Resume interrupted balance.

       start [options] <path>
           -d[<filters>]
               act on data chunks. See FILTERS section for details about <filters>.

           -m[<filters>]
               act on metadata chunks. See FILTERS section for details about <filters>.

           -s[<filters>]
               act on system chunks (only under -f). See FILTERS section for details about <filters>.
            convert
           Convert each selected block group to the given profile name identified by parameters.

20 修改为raid5级别

Done, had to relocate 1 out of 3 chunks
[root@network ~]# btrfs balance start  -dconvert=raid5 /mydata/  
或者
[root@network ~]# btrfs balance start  -mconvert=raid5 /mydata/ 
Done, had to relocate 2 out of 3 chunks
21
[root@network ~]# man btrfs-subvolume 
BTRFS-SUBVOLUME(8)                                   Btrfs Manual                                  BTRFS-SUBVOLUME(8)

NAME
       btrfs-subvolume - control btrfs subvolume(s)

SYNOPSIS
       btrfs subvolume <subcommand> [<args>]

22 列出子卷
[root@network ~]# btrfs subvolume list /mydata/

23
SUBCOMMAND
       create [-i <qgroupid>] [<dest>]<name>
           Create a subvolume <name> in <dest>.   #创建子卷

[root@network ~]# btrfs subvolume create  /mydata/logs  #创建子卷
Create subvolume '/mydata/logs'
[root@network ~]# btrfs subvolume list  /mydata/  #列出子卷
ID 264 gen 65 top level 5 path logs

24 挂载子卷,挂载父卷以后子卷就自动挂载,现在单独挂载子卷

[root@network ~]# umount /mydata   #先卸载父卷
[root@network ~]# mount -o subvol=logs /dev/sde  /mnt  #挂载子卷,也可以不用logs用卷ID来挂载
[root@network ~]# cp /var/log/messages  /mnt
[root@network ~]# ll /mnt
total 848
-rw------- 1 root root 866759 Aug  4 13:41 messages
[root@network ~]# btrfs subvolume  show /mnt  #查看子卷的详细信息
/mnt
    Name:           logs
    uuid:           a87a7495-22cb-4644-b1a9-5ecd353af544
    Parent uuid:        -
    Creation time:      2020-08-04 13:36:04
    Object ID:      264
    Generation (Gen):   67
    Gen at creation:    65
    Parent:         5
    Top Level:      5
    Flags: 
-
[root@network ~]# umount /mnt
[root@network ~]# mount /dev/sdd /mydata/  #卸载子卷重新挂载父卷,发现子卷里面的东西依然存在
[root@network ~]# ls /mydata/logs/
messages  

25删除子卷

 [root@network ~]# btrfs subvolume delete /mydata/logs/
 Delete subvolume (no-commit): '/mydata/logs'

26.对卷创建快照

 [root@network ~]# btrfs subvolume create  /mydata/logs.logs   1.先创建子卷
reate subvolume '/mydata/logs.logs'   
[root@network ~]# cp /etc/grub2.cfg  /mydata/logs.logs/   2.复制内容到子卷里面

创造快照卷与子卷必须同一父卷下

[root@network ~]# btrfs subvolume snapshot /mydata/logs.logs/  /mydata/logs.logs_sanpshot 3.
 Create a snapshot of '/mydata/logs.logs/' in '/mydata/logs.logs_sanpshot'
 [root@network ~]# btrfs subvolume list /mydata/ ID 264 gen 73 top level 5 path logs.logs
 ID 265 gen 73 top level 5 path logs.logs_sanpshot  #展开发现父卷下多了快照卷
 [root@network ~]# cd /mydata/logs.logs_sanpshot/   #发现快照卷里面也有和子卷一模一样的东西
 [root@network logs.logs_sanpshot]# ls

grub2.cfg #修改子卷中的grub2.cfg,快照卷grub2.cfg不发生变化

27删除快照

[root@network logs.logs_sanpshot]# btrfs subvolume delete /mydata/logs.logs_sanpshot/

单独对子卷的某一文件快照

[root@network mydata]# ls
logs.logs  logs.logs_sanpshot
[root@network mydata]# cd logs.logs
[root@network logs.logs]# ls
grub2.cfg
[root@network logs.logs]# cp --reflink grub2.cfg  grub2.cfg_snap  #此时修改grub2.cfg 不会影响到grub2.cfg_snap

如何将ext系列文件系统转化为btrfs毫无损坏

[```
root@network logs.logs]# btrfs balance start -dconvert=single /mydata/
Done, had to relocate 1 out of 3 chunks
[root@network logs.logs]# btrfs balance start -mconvert=raid1 /mydata/
Done, had to relocate 2 out of 3 chunks
[root@network logs.logs]# btrfs device delete /dev/sde /mydata/ 1.挪走一块盘即/dev/sde
[root@network logs.logs]# fdisk /dev/sde 2.对这块盘分区
[root@network logs.logs]# mke2fs -t ext4 /dev/sde 3.格式化为ext4类型文件系统
[root@network logs.logs]# mount /dev/sde /mnt #挂载写入数据
[root@network logs.logs]# cp /etc/fstab /mnt
[root@network logs.logs]# umount /mnt #文件系统相互转化危险,所以转化之前要先卸载



[root@network logs.logs]# fsck -f /dev/sde           4.检查此文件系统
[root@network logs.logs]# btrfs-convert /dev/sde     5.装换为btrfs
[root@network logs.logs]# btrfs filesystem show
Label: 'mydata'  uuid: b689a89b-3a4a-4258-8412-e3d2180056a9
    Total devices 3 FS bytes used 680.00KiB
    devid    1 size 20.00GiB used 1.00GiB path /dev/sdb
    devid    3 size 20.00GiB used 1.03GiB path /dev/sdd
    devid    5 size 20.00GiB used 1.03GiB path /dev/sdc

Label: none  uuid: 55a1c30b-62b3-4728-ba7d-138cff575bcd  #此时已经被转换了
    Total devices 1 FS bytes used 493.57MiB
    devid    1 size 20.00GiB used 20.00GiB path /dev/sde

btrfs-progs v3.19.1

[root@network logs.logs]# mount /dev/sde /mnt  #重新挂载里面数据有丢失
[root@network logs.logs]# ll /mnt
total 20
drwxr-xr-x 1 root root  10 Aug  4 14:38 ext2_saved
-rw-r--r-- 1 root root 559 Aug  4 14:37 fstab
drwx------ 1 root root   0 Aug  4 14:35 lost+found

30.相互转化
[root@network logs.logs]# umount /mnt  卸载
[root@network logs.logs]# btrfs-convert -r /dev/sde       -r:装换为ext4
rollback complete.
[root@network logs.logs]# blk
blkdeactivate  blkdiscard     blkid          blkmapd        
[root@network logs.logs]# blkid /dev/sde
/dev/sde: UUID="8007b2ad-cde4-48c3-8d2a-ce236d0f7eb6" TYPE="ext4" 
[root@network logs.logs]# btrfs-convert  /dev/sde   #无-r重新转化为btrfs系列
creating btrfs metadata.
copy inodes [o] [         0/        12]
creating ext2fs image file.
cleaning up system chunk.
conversion complete.
[root@network logs.logs]# blkid /dev/sde
/dev/sde: UUID="9a37e348-40cf-44a2-a4e3-52868c913be1" UUID_SUB="f3088659-df3a-4a0c-a6a9-b8d7e6f0aae4" TYPE="btrfs" 
[root@network logs.logs]#