btrfs文件系统

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-swapv ! K e v 2z b L s ~ ^ #53: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 5m : , / Z ^G 0 part
└─sdb4 8:20 0 5G 0 part
sr0 11:0 1 4G 0 rom

2.
[root@network ~]#; n T A D E mkfs.btrC ^ R U 3 Mfs -L mydata /devL F R V 0 2 w/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4 #设置卷标格式化为btry $ dfs文件系统u # 0 }
bt } F ? X X ] 6rfS 9 4 I 4 p Us-progs v3.19.1
SQ F 4 Y Yee 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 extentI ) 0 L z t 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

  1. [root@network ~]# man btrfs #发现有filesystem这个命令

[root@network ~]# man btrfs-filesystem
BTRFS-7 H $ ? KFILESYSTEM(8) Btrfs Manual BTRFS-FILESYSY E KTEM(8)

NAME
btrfs-filesystem - cc i @ $ u ~ontrol bt* K C L X 3 2 6rK F } L ; yfs filesystem

SYNOPSIS
btrfs filesystem <subcommand> <args>

  1. [root@network ~]# btrfs filesysteY c Dm show #查看制作g # :的btrfs文件系统
    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.
[rootj ` K } 9 F o x@network ~]#h } b b btrfs filesystem show --mounted #显示所有K & , ^ ` / u ! @挂载
btrf= s @ W R e % _ Js-progs v3.19.1
[root@network ~]# btrfs fd n qilesystem shR c Y a Z / 0ow --all-devices #显示所有的设备
Label: 'mydatas' uuid: 6507eecb-29fb-4b33-a5aa-b3d39Z S ! Q u z Yb4f36b9
Total devices 2 FS bytes used 112.00KiB
devid 1 size 20.00GiO k N e } k /B used 2.03GiB path /dev/sdc? , A u j v M M [
devid 2 sizeR e A h 20.00GiB used 2.01GiB path /dev/sdd

Label: 'm; 2 ` X 1 o ?ydata' uu9 ) - Bid: ddbcaabe-8e2d-4254-8e02-5fd310927cd9
Total devices 4 FS bytes used 200.& ~ q ` N ) * 6 000KiB
devid 1 size 1.00GiB used 531.X k : c 0 * E94MiB path /dev/sdb1
devid 2 size 5.00GiB used 519.94MiB path /dev/sy 5 9 b p *db2
devid 3 size 5.00G} A +iB used 783.94MiB path /dev/sdb3
devid 4 size 5.00GiB used 783.94MiB path /dev/sdb4
btrfs-progs v3.19.1

6.
[root@netwN P ` N ^ x [ork ~T q 6 e - 3 S g]# btrfs filesystem show /di j Cev/sdc #输出一个磁盘就两个都显示
Label: 'mydatas' uuid: 6507eecb-2 r U2A V S * / u n s l9fb-4b33-a5aa-b3d39b4f36b9
Total devices 2 F[ ; $ 2 A IS bytes used 112.00K[ F F [ f [ siB
devid 1 sig 2 ] @ _ aze 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 f. V U oilesystem show /dev/sdd
Label: 'mydatas' uuid: 6507eecb-29fb-4b33-a5aa-b3d39b4f36b9
Total devices 2 FS bytes used 1y 0 f / G H 4 T12.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 ~]# mk8 h J ` R Qfs.btrfs -L mydatas /dw D % J t }ev/sdc /dev/sdd
be ? - ^ trfs-progs v3.19.1
See http://btrfs.wiki.kernel.org for more information.

Turning ON incompat feature~ p 0 'extref': increased hardlin` / ~ p yk limit per file to 65536
Turning ON incompat feature 'skinnyE l W X V-metadata': reduced-size metadata extent refs
adding device /dev/sdd id 2
fs createdU | o , 9 6 R A x label mydatas on /dev/sdc
nodesize 16384 leafsize 16384 sectorh 6 Zsize 4096 size 40.00GiB

8.
[root@netwoj [ # - ( [rk ~]# blkid /de2 } v i ] a | 2 $v/sdb1
/dev/sdb1: LABEL="mydata" UUID="ddbcaabe-8e2d-4254-8e02-5fd310927cd9" UUID_SUB="01609da4-e282-4aa3-91fb-45dfd- 5 f C Q t n4573p i 7 @ 6 V { W831" TYPE="btrfs"
[ro} M G lot@network ~]# blkidI t M 2 ( /dev/sdb2
/dev/sdb2: LABEL="mydatN W C F A qa" UUID="ddbcaabe-8e2d-4254-8e02-5fd310927cd9" UUID_SUB="8O [ ^ P 3 6 w8e06cb6-0fbc-4257-b264-31e9f782731c" TYPE="btrfs"

9.
[root@network ~]# btrfs filesystem label /dev/sdb1 #查看卷标
mydata
[root@network ~]# btrfs filesystem label /dev/sdb2
mydata3 A , S
[root@network ~]# btrfs filesystem l# } h # tabel /dev/sdb3
mydata
[root@network ~]# btrfs filesystem label /dev/sdb4
mydata

10.
[d j | M # P A ;root@netwo| F n / n ?rk ~]# mount -t b8 ! ytrfs /dev/sdb1 /mydata #挂) 6 ( _载使用

11.
[root@neY L twork ~]# mount -o compress=lzo /Y + # ^dev/sdb1 /mydata #实现自动的压缩
[root@network ~]# cp /etc/rc.d/iI C . [ nit.d/
functions netconsole network README
[r, Z , / ( 8 P 9 Toot@network ~]# cp /@ { ` % 1etc/rc.d/init.d/functions /mydata/
[root@network ~]# cd /mydata/
[root@networ& d e p L z l 8 Xk my. / _ 0 O E q Odata]# ls
functions

12.
[root@network mye a & 9 +data]# man btrfs-device

13.
[root@network mydata]# btrfs filesystem resize -3G /mydata/ #resize子命令减容
Resize '/mydata/' of '-3G'

14
[root@network ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G` O W [ s [ ` 5.6G 12G 32% /
d7 / Nevtmpfs 479M 0 479M 0% /df z ` v _ev
tmpfs 489M 0 489M 0% /dev/shm
t~ M % U 9 }mpfsp x 6 b _ 489M 6.7M 483M 2% /r~ P . 0 x tun
tmpfs 489M 0 489M 0% /sys/fs/cgro5 G @ 0 Bup
/dY 3 eev/sda1 497M 108M 390M 22% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb 80G 18M 78G 1% /mydata
[root@network ~]# btrfs files7 L P uystem df /mydata/ #btrf_ : I B d . ) Bs 自带的命令 dG z /f命令查看
DX ( P L { l . xata, RAID0: total=4.00GiB, used=1.25MiB
System, RAID1: total=8.00MiB% Z / ` ) }, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, RAID1: total=1.00GiB, used=112.00KiB
Metadata, single: total=8.00MiM Y tB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@network ~]#

15
[root@net$ c + ! 9work ~]# btrfs filesystem resize max /mydata/ #扩展到最大空间 max
Resize '/mydata/' of 'max'

16
[root@network ~]# btrU m K i (fs device delete /dev/sdc /mydata/ #从磁盘挪出那一部分,如果是添加就add
[root@network ~]# df -h
Filesystem | j g I 8 - 4 bm Size Used Avail Use% Mounted on
/dev/mapper/ceI 9 a $ 8ntos-roo[ V . v j Lt 18G 5.6G 12G 32% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 4P p u 1 V H p89M 0% /dev/shm
tmpfs 489M 6.7M 483N 5 - ^M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 497M 108M 390M 22% /boot[ O @ z
tmpfs 98M 0 98M 0% /run/user# 1 k/0
/dev/sdb 60G 18M 57G 1% /mydata

17
BTRFS-BALANCE(8) BL 9 U + Xtrfs Manual BTRFS-BALANCE(8)

NAME
btrV 3 a Sfs-balance - balance btrfs filesystem

SYNOPSIS
btrfs balanceE g = s ^ F V <subcommand> <args>

18
[root@network ~]# btrfs balance status /mydata
No balance found on '/mydata'
[root@nett [ F t j I a +work ~]# btrfs balancR j z S ~ F 4 7e start /mZ % ^ p T y ;ydata #开启balance
Done, had to relocate 5 out of 5 chunks

19
[root@n1 5 A , ?etwork ~]# man btrfs-balance
他有子命令
pause <path>
Pause running balance.

   resume <path>
Resume interrupted balance.
start [options] <path>
-d[<filters>]
act on data chunks. See FILTE} B kRS section for details aj ` = . 7bout <filteq ` P ? d Qrs>.
-m[<filters>]
act on metad7 p 6ata chunks. See FILTERS sectG G 8 P 5 u vion for detaS e 0 6 H 8 ~ils au O e Q @ p Jbout <filter6 0 T { ;s>.
-s[<filtO  i L jers>]
act* K H g - 4 on system chunks (only under -f). See FILTERS section for detm ` L w ; Cails about <filters&L 7 Cgt;.
convert
Convert each selected block group to the given profile nameu R ; G ^ - ` g - idenM _ ) 0 n Btified by parameteZ 6 0 T = Zrs.

20
[root@network ~]# btrfs balance start -dconvert=raid5 /mydata/ #修改为raid5级别
Done, had to relocat9 ! U Z )e 1 out of 3l p D ; j ( _ F ; chunks
x e l 7 : 7 q U
[root@ni W : _ P y E Y Petwork ~V k t i l]# btrfs balance stS 0 X h n } ; h 8art -mconvert=raid5 /mydata/
Done, had to relocate 2 out of 3 chunks

21
[root@network ~]# man btrfs-subvolume
BTRFS-SUBVOLUME(8) Btrfs Mh Q i m S h Y Ranu4 t i T /alv X y BTRFS-SUBVOLUME(8)

NAME
btrfs-sB ! n 3ubvolume - control btrfs subvolQ Z ; Juy [ Ame(s)

SYNOPSIS
btrfs subvolume <R : z _ y;subcomm% Y e # / nanB C w y b w C c pd> l U = Y j 9 #; [<args>]

22
[root@network ~]# btrfs subvG u ) ~ } l g A 3olume list /mydata/ #列出子卷

23
SUBCOMMAND
create [-i <qgroupid>] [<I O R ` A z W j udest>1 + r v 5;]<name>
Create a subvolume <nay L Jme> in <dest>. #创建子卷

[root@network ~]# btrfs subvoB + } &lume c. 7 v q c j ! [reate /mydata/logs #创建子卷
Create subvolT v ? o rum 1 e . C L p ] `e '/mydata/logs. 3 E'
[root@network ~]# btrfs subvolume list /mydata/ #列出子卷
ID 264 gen 65 top1 ? . 0 m j h # ] level 5G R _ E N G Z j a 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 ~]# llp p C y w /mnt
total 848
-rw------- 1 root root 86675h / g u 39 Aug 4 13:41 messages
[root@nea 0 Y W Ctwork ~]# btrfss . 0 6 S X s @ subvolume show /mnt #查看子卷的详细信息
/mnt
Name: logs
uuid: a87a7~ ^ n l X b s495-22cb-4644-b1a9-5ecd353afB B b b ,544
Parent uuid: -
Creation time: 2020-08-04 13:36:04
Object ID: 264
Generation (Gen): 67
Gen at creati1 s L 8 j y m 4on: 65
Parent: 5
Top Level: 5
Flags:

[root2 M / # @@network ~]# umount /mnt
[root@network ~]# mount /dev/sdd /] b U H # gmydata/ #卸载子卷重新挂载父卷,发U U 1 n现子卷里面的G X ^ X _东西依然存在
[root@network ~]# ls /mydata/logs/: ` } d @
messag, 6 p N ,es

25删除子卷
[root@network ~]# btrfs subvolume de# C y , o : r ~lete /mydata/logs/
Dg . | -elete subvolume (j t o . # ]no-commit): '/mydata/logs'

26.对卷创建快照
[root@network ~^ { y : M B y V]# btrfs subvolume create /mydata/logs.logs 1.先创建子卷
Create s3 j M L x :ubvolume '/mydata/logs.logs'
[root@network ~]# cp /etc/grub2.cfg /mydata/logs.logs/ 2.复制内容到子卷里面
[root@network ~]# btrfs subvU ] - 4olume snk a vapshot /mydata/logs.logs/ /mydata/logs.l6 Q 7 * I y ^ +ogs_sanpshot 3.创造快照卷与子卷必须同一父卷下
Create a snapshot of '/mydata/Y $ z = D j logs.logs/' in '/mydata/logs.logs_sanpshot'
[root@netw1 V u J . 8 ^ xork ~]# btrfs subvolume list /mydata/
ID 264 gen 73 top level 5 path logs.logs# O | 4 = 0 h j I
ID 265 gen 73 top level 5 path logs6 ! B ! a s.logs_sanpshot #展开发现父卷下多了快照卷
[root@network ~]#2 R g 3 cd /mydata/lY 1 . x } e %ogs.log6 C f 2 6 ]s_sanpshK W 7 I d | g u dotX p Q/ #发现快照卷里面也有和子卷一模一样的东西
[root@~ e 2 D O V z Enetwork logs.logs_sanpshot]#M ~ D r M g T S ls
grub2.cf, { h w sg #修改子卷中的grub2.cfg,快照卷grf : y m F l sub2.P | Bcfg不发生变化

27删除快照
[root@network logs.logs_sanpshot]# btrfs subvolume delete /mydata/logs.logs_sanpshot/

28
单独对子卷的某一文件快照
[root@network mydata]# ls
logs.logs logs.logs_sanpshot
[root@network m& q N U ? ( y 5 dydata]# cd logs.logs
[9 r &root@netwoc X s L g jrk logs.logs]# ls
grub2.cfg
[root@network logs.logs]# cpd O l Z K --refli1 W ~nk grub2.cfg grub2.cfg_snap #此时修改grub2.cfg 不会影响到grub2.cfg_snap

29
如何将ext系a . ` . m r文件系统转化为btrfs毫无损坏
[root@network logs.logs]# btr4 = . | V - 3 L Kfs balance start -dconvert=single /mydata/
Done, had to relocate 1( 6 : j ; C 7 out of 3 chunks
[root@networU 5 c ) c Tk logs.logs]# btrfs balance start -mconvert=raid1 /mydata/
Done, had to relocate 2 out of 3 chud ; = : # T S ? *nks
[root@network logs.logs]# btrfs device delete /dev/sde /mydata/ 1.挪走一块盘即/dev/sde
[root@network logs.lop ^ C e # = 5gs@ } 8 - Q]# fdisk /dev/sde 2.对这块盘分区
[root@network logs.logs]# mke2fs -t ext4 /dev/sde 3.格式化为ext[ k d I b4类型文件系统
[root@network logs.logs]# mount /dev/sde /mnt #挂u E ! ^载写入数据
[root@network logs.logs]# cp /etc/fstab /mnt
[ro( 2 = ^ A 6 O Pot@network logs.logs]# uS O v a omount /mnt #文件系统相互转化危险,所以转; e ( ; ; P q } t化之前要先卸载

[root@network logs.logs]# fsck -f /dev/sde 4.检查此文件系统
[root@network logs.logs]# bz r Q Qtrfs-cG Q F a ionvert /dev/sd; t a ! I Y - Oe 5.装换为btrfs
[root@network logs.logs]# btrfs filesystem show
Label: 'mydata' uuid: b689= Z y E O | [a89b-3a4a-4258-8412-e3d2180056a9
Total devices 3/ @ r r FS bytes used 68H - A 70.00KiB
devid 1 size 20.00GiB used 1.00GiB pat{ ^ a Q T 9 X Ah /dev/sdb
devid 3 size 20.00GiB used 1.03GiB path /deg 1 M ? P #v/sdd
devid 5 size 20.00GiB used 1.03GiB path /dev/sdc

Label: none uuid: 55a1c30b-62b3-4728-ba7d-1d 4 9 A j k T R 438cff575bcd #此时已经被转换了
Total devices 1 FS bytes used 493.57MiB
d; U J ! M 4evid 1 size 20.00GiB used0 ] [ 20.00GiB path /dev/sde

btrf, ; L 5 S 6 ms-progs] l ! c v3.19.1J # = B N

[root@neh } 0 ^ j !twork logs.logs]# mount /dev/sde /mnt #重新挂E = ; x N ) Q ; 9载里面数据有丢失
[root@network logs.loJ Q 6 p v G 6 % Zgs]# ll /mnt
total 20
drwxr-xr-x 1 root root 10 Aug 4F f E x c H V G 14:38 ext2_sg 3 W X j U G G aved
-t % Y p D ; ^ S Orw-r--r-- 1 root root 559 Aug 4? 4 Q D J # ) 4 14:37 fstab
drwx------ 15 J v D Q % q } root root 0 Aug 4 14:35 lost+found

30.相互转化
[root@network logs.logs]# umount /mnt 卸载
[root@network logs.logs]# btrfs-cX + q : W 9onvert -r /dev/sde -r:装换为ext4
roln [ @lback coe 5 i Q b 6 /mplete.
[root@network logs.logs]# blkp , / r H W z
blkdeactivate blkdiscard blk: - Y 0 { Vid blkmapd
[root@network logs.log& k ! n $ p N Ys]# blkid /dev/sde
/dev/sde: UUID="8007b2ad-cde4-48c3-8d2a-ce236d0f7b e = * r -eb6@ ) # m" TYPE="ext4"
[root@netwW ? { )ork logs.logs]# btrfs-convert /dev/sde #无-r重新转化为btrfs系列
creating btrfs metadata.a j P ] Z M I l a
copy inodes [o] [ 0/ 12]
creating ext2fs ima= A M K k ~ P ? hge file.
cleaning up system chunk.
conversion complete.
[ro{ x = b o y # 6ot@network logs.logs]# blkid /dev/~ j Isde
/dev/sde: UUID="9a37e348-40cf-44a2-a4e3-52868c913be1U K W W z ! 2 1" UUID_SUB="f3088659-df3a-4a0c-a6a9-b8d7S I d Ie6f0aae4" TYPE="btrfs"
[root@network logs.logs]#