推薦-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.www58058.com/17150

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

相關推薦

  • vsftpd虛擬用戶搭建

    首選的FTP服務器搭建方式 安裝vsftpd      yum install -y vsftpd      systemctl enable vsftpd     #設置開機啟動      systemctl is-enabled vsf…

    Linux干貨 2017-04-27
  • 計算機和操作系統的一些概念

    一、計算機組成     (一) 硬件         CPU:運算器、控制器、寄存器、緩存器         存儲器:主內存,RAM(Random Access…

    Linux干貨 2016-08-15
  • http狀態碼大全

    狀態值:100 客戶端應當繼續發送請求。這個臨時響應是用來通知客戶端它的部分請求已經被服務器接收,且仍未被拒絕。客戶端應當繼續發送請求的剩余部分,或者如果請求已經完成,忽略這個響應。服務器必須在請求完成后向客戶端發送一個最終響應。 狀態值:101 服務器已經理解了客戶端的請求,并將通過Upgrade 消息頭通知客戶端采用不同的協議來完成這個請求。在發送完這個…

    Linux干貨 2016-11-01
  • CentOS 6.5 LVM磁盤管理學習筆記(二)

    ACTIVE            '/dev/vg51cto/LogVol04' [22.80 GiB]inherit  ACTIVE       …

    Linux干貨 2016-08-29
  • scp命令介紹

    scp是secure copy的簡寫,用于在Linux下進行遠程拷貝文件的命令,和它類似的命令有cp,不過cp只是在本機進行拷貝不能跨服務器,而且scp傳輸是加密的??赡軙晕⒂绊懸幌滤俣?。當你服務器硬盤變為只讀 read only system時,用scp可以幫你把文件移出來。另外,scp還非常不占資源,不會提高多少…

    Linux干貨 2017-07-24
  • 分布式文件系統之fastDFS部署

    fastDFS下載地址:https://github.com/happyfish100/fastdfs部署參考文檔:http://joelhy.github.io/2015/01/27/FastDFS-v5-06-deploy/ 我們接下來來部署fastDFS 部署結構: 192.168.42.150 node1 [Tracker]192.168.42.15…

    Linux干貨 2017-06-16

評論列表(1條)

  • stanley
    stanley 2016-06-01 15:30

    已置頂,非常不錯。歡迎加入免檢家族

欧美性久久久久