linux btrfs文件系統(tǒng)及管理
什么是btrfs?
Btrfs(B-tree文件系統(tǒng),通常念成Butter FS,Better FS或B-tree FS),linux文件系統(tǒng),具有寫時復(fù)制COW(copy-on-write),改善ext3文件系統(tǒng)單文件大小限制,并加入其他特性,如可寫快照,快照的快照,內(nèi)建RAID,子卷(subvloume),專注于容錯,修復(fù)和易于管理。單文件可達(dá)16EB,最大文件數(shù)量2^64,最大卷容量16EB,等。
btrfs功能特性
1,COW:寫時復(fù)制,每次寫入數(shù)據(jù)時,先將數(shù)據(jù)寫入到新的block,寫入成功后,更改舊數(shù)據(jù)塊指針到新數(shù)據(jù)塊,而非更改本身。
2,多物理卷支持,btrfs內(nèi)建raid,可在線增刪磁盤設(shè)備,可在線擴展和縮減磁盤空間。
3,數(shù)據(jù)和元數(shù)據(jù)校驗碼,checksum
4,子卷,可單獨掛載子卷
5,可寫快照,快照的快照,單個文件快照。
6,透明壓縮
7,ext3/4和btrfs無痛互轉(zhuǎn)
btrfs 基本用法:
- [root@localhost ~]# btrfs --help #查看幫助可以看到btrfs 有很多子命令,用法也很多,這里只舉例常用選項。
- usage: btrfs [--help] [--version] <group> [<group>...] <command> [<args>]
- btrfs subvolume create [-i <qgroupid>] [<dest>/]<name> #創(chuàng)建子卷
- 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> #創(chuàng)建子卷快照
- Create a snapshot of the subvolume
- btrfs subvolume get-default <path> #獲取子卷默認(rèn)的文件系統(tǒng)
- Get the default subvolume of a filesystem
- btrfs subvolume set-default <subvolid> <path> #設(shè)置默認(rèn)系統(tǒng)給子卷
- Set the default subvolume of a filesystem
- btrfs subvolume find-new <path> <lastgen> #列出btrfs文件系統(tǒng)中最近修改的文件,結(jié)合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>...] #子卷同步,類似mount同步模式,內(nèi)存數(shù)據(jù)同步到磁盤,有待查證。
- Wait until given subvolume(s) are completely removed from the filesystem.
- btrfs filesystem df [options] <path> #顯示掛載的文件系統(tǒng)詳細(xì)信息。
- Show space usage information for a mount point
- btrfs filesystem show [options] [<path>|<uuid>|<device>|label] #顯示創(chuàng)建文件系統(tǒng)的磁盤信息。
- Show the structure of a filesystem
- btrfs filesystem sync <path> #強制文件系統(tǒng)同步,
- 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> #btrfs文件系統(tǒng)在線擴展和縮減空間
- Resize a filesystem
- btrfs filesystem label [<device>|<mount_point>] [<newlabel>] #改變btrfs文件系統(tǒng)卷標(biāo)
- Get or change the label of a filesystem
- btrfs filesystem usage [options] <path> [<path>..] #顯示文件系統(tǒng)當(dāng)前的使用信息。
- Show detailed information about internal filesystem usage .
- btrfs balance start [options] <path> #改變磁盤chunk,在線改 數(shù)據(jù)和元數(shù)據(jù) 存儲方式,單盤改raid,前提滿足raid要求。
- Balance chunks across the devices
- btrfs balance pause <path> #暫停chunk更改,數(shù)據(jù)量較大,轉(zhuǎn)換時間較長時,先暫停。
- Pause running balance
- btrfs balance cancel <path> #取消chunk更改,如上
- Cancel running or paused balance
- btrfs balance resume <path> #中斷balance的操作,如上
- Resume interrupted balance
- btrfs balance status [-v] <path> #顯示balance操作狀態(tài) 如上
- Show status of running or paused balance
- btrfs device add [options] <device> [<device>...] <path> #文件系統(tǒng)增加磁盤
- Add a device to a filesystem
- btrfs device delete <device> [<device>...] <path> #文件系統(tǒng)刪除磁盤
- Remove a device from a filesystem
- btrfs device scan [(-d|--all-devices)|<device> [<device>...]] #文件系統(tǒng)磁盤掃描
- Scan devices for a btrfs filesystem
- btrfs device ready <device> #猜測是檢測加入的設(shè)備有沒有被掛載
- Check device to see if it has all of its devices in cache for mounting
- btrfs device stats [-z] <path>|<device> #顯示文件系統(tǒng)的設(shè)備狀態(tài)
- Show current device IO stats. -z to reset stats afterwards.
- btrfs device usage [options] <path> [<path>..] #顯示文件系統(tǒng)內(nèi)部設(shè)備詳細(xì)使用信息
- Show detailed information about internal allocations in devices.
btrfs文件系統(tǒng)管理
為分區(qū)創(chuàng)建btrfs文件系統(tǒng)
- [root@localhost ~]# fdisk -l | grep "^Disk /dev/sd[a-z]" #準(zhǔn)備sd{b,c,d,e}4塊20G磁盤,未做任何分區(qū)。
- Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
- Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
- Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
- Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors
- Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors
創(chuàng)建單分區(qū)btrfs并查看
- [root@localhost ~]# mkfs.btrfs -L 'btrfs' /dev/sdb
- 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
- fs created label btrfs on /dev/sdb
- nodesize 16384 leafsize 16384 sectorsize 4096 size 20.00GiB
- [root@localhost ~]# btrfs filesystem show
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 1 FS bytes used 112.00KiB
- devid 1 size 20.00GiB used 2.04GiB path /dev/sdb
- btrfs-progs v3.19.1
- [root@localhost ~]# mount -o compress=lzo -L btrfs /btrfs/ 掛載時可以設(shè)定透明壓縮機制。
- [root@localhost ~]# btrfs filesystem df /btrfs
- Data, single: total=8.00MiB, used=256.00KiB
- System, DUP: total=8.00MiB, used=16.00KiB
- System, single: total=4.00MiB, used=0.00B
- Metadata, DUP: total=1.00GiB, used=112.00KiB
- Metadata, single: total=8.00MiB, used=0.00B
- GlobalReserve, single: total=16.00MiB, used=0.00B
再添加一塊磁盤
- [root@localhost ~]# btrfs device add /dev/sdc /btrfs #添加磁盤,刪除用btrfs device delete /dev/sdc /btrfs
- [root@localhost ~]# btrfs fi sh
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 2 FS bytes used 384.00KiB
- devid 1 size 20.00GiB used 2.04GiB path /dev/sdb
- devid 2 size 20.00GiB used 0.00B path /dev/sdc
- btrfs-progs v3.19.1
- [root@localhost ~]#
在線增加或縮減空間,在線改變空間,可以讓lvm坐冷板凳了。
- [root@localhost ~]# btrfs fi resize -10G /btrfs/ #在線縮減空間
- Resize '/btrfs/' of '-10G'
- [root@localhost ~]# btrfs fi sh
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 2 FS bytes used 384.00KiB
- devid 1 size 10.00GiB used 2.04GiB path /dev/sdb
- devid 2 size 20.00GiB used 0.00B path /dev/sdc
- btrfs-progs v3.19.1
- [root@localhost ~]# btrfs fi resize +7G /btrfs/ #在線增加空間
- Resize '/btrfs/' of '+7G'
- [root@localhost ~]# btrfs fi sh
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 2 FS bytes used 384.00KiB
- devid 1 size 17.00GiB used 2.04GiB path /dev/sdb
- devid 2 size 20.00GiB used 0.00B path /dev/sdc
- btrfs-progs v3.19.1
- [root@localhost ~]#
在線更改數(shù)據(jù)和元數(shù)據(jù)的結(jié)構(gòu)
- [root@localhost ~]# btrfs fi df /btrfs/
- Data, single: total=8.00MiB, used=256.00KiB
- System, DUP: total=8.00MiB, used=16.00KiB
- System, single: total=4.00MiB, used=0.00B
- Metadata, DUP: total=1.00GiB, used=112.00KiB
- Metadata, single: total=8.00MiB, used=0.00B
- GlobalReserve, single: total=16.00MiB, used=0.00B
- [root@localhost ~]# btrfs balance start -mconvert=raid1 /btrfs/ #-mconvert 為改變metadata元數(shù)據(jù)區(qū)的存儲結(jié)構(gòu)
- Done, had to relocate 4 out of 5 chunks
- [root@localhost ~]# btrfs fi sh
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 2 FS bytes used 192.00KiB
- devid 1 size 17.00GiB used 296.00MiB path /dev/sdb
- devid 2 size 20.00GiB used 288.00MiB path /dev/sdc
- btrfs-progs v3.19.1
- [root@localhost ~]# btrfs fi df /btrfs/
- Data, single: total=8.00MiB, used=64.00KiB
- System, RAID1: total=32.00MiB, used=16.00KiB
- Metadata, RAID1: total=256.00MiB, used=112.00KiB #對比上改為了raid1
- GlobalReserve, single: total=16.00MiB, used=0.00B
- [root@localhost ~]# btrfs balance start -dconvert=raid1 /btrfs/ #-dconvert 為改變data數(shù)據(jù)區(qū)的存儲結(jié)構(gòu)
- Done, had to relocate 1 out of 3 chunks
- [root@localhost ~]# btrfs fi df /btrfs/
- Data, RAID1: total=1.00GiB, used=320.00KiB #對比上面改為了raid1
- System, RAID1: total=32.00MiB, used=16.00KiB
- Metadata, RAID1: total=256.00MiB, used=112.00KiB
- GlobalReserve, single: total=16.00MiB, used=0.00B
- [root@localhost ~]#
顯示當(dāng)前文件系統(tǒng)使用信息
- [root@localhost btrfs]# btrfs fi usage /btrfs/
- Overall:
- Device size: 37.00GiB #總的空間
- Device allocated: 2.56GiB #分給元數(shù)據(jù)管理區(qū)的空間
- Device unallocated: 34.44GiB #未分出去的
- Device missing: 0.00B
- Used: 896.00KiB #使用了的空間
- Free (estimated): 18.22GiB(min: 18.22GiB) #可用空間,這里因是raid1 鏡像卷
- Data ratio: 2.00
- Metadata ratio: 2.00
- Global reserve: 16.00MiB(used: 0.00B)
- Data,RAID1: Size:1.00GiB, Used:320.00KiB
- /dev/sdb 1.00GiB
- /dev/sdc 1.00GiB
- Metadata,RAID1: Size:256.00MiB, Used:112.00KiB
- /dev/sdb 256.00MiB
- /dev/sdc 256.00MiB
- System,RAID1: Size:32.00MiB, Used:16.00KiB
- /dev/sdb 32.00MiB
- /dev/sdc 32.00MiB
- Unallocated:
- /dev/sdb 15.72GiB #這里2各磁盤 分配的空間不同,可以說明btrfs的內(nèi)建raid1不是按物理盤劃分的,而是按chunk,
- /dev/sdc 18.72GiB #可以看到具體某塊磁盤的使用狀況。
- [root@localhost btrfs]# btrfs device stats /btrfs/ #查看磁盤錯誤信息
- [/dev/sdb].write_io_errs 0
- [/dev/sdb].read_io_errs 0
- [/dev/sdb].flush_io_errs 0
- [/dev/sdb].corruption_errs 0
- [/dev/sdb].generation_errs 0
- [/dev/sdc].write_io_errs 0
- [/dev/sdc].read_io_errs 0
- [/dev/sdc].flush_io_errs 0
- [/dev/sdc].corruption_errs 0
- [/dev/sdc].generation_errs 0
- [root@localhost btrfs]# btrfs device usage /btrfs/ #磁盤的空間使用狀態(tài)
- /dev/sdb, ID: 1
- Device size: 20.00GiB
- Data,RAID1: 1.00GiB
- Metadata,RAID1: 256.00MiB
- System,RAID1: 32.00MiB
- Unallocated: 15.72GiB
- /dev/sdc, ID: 2
- Device size: 20.00GiB
- Data,RAID1: 1.00GiB
- Metadata,RAID1: 256.00MiB
- System,RAID1: 32.00MiB
- Unallocated: 18.72GiB
- 壓縮測試
- [root@localhost btrfs22]# du -sh /btrfs /btrfs22
- 5.1G/btrfs
- 5.1G/btrfs22
- [root@localhost btrfs22]# btrfs fi show
- Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a
- Total devices 2 FS bytes used 178.63MiB #btrfs掛載采用lzo壓縮,空間被壓縮,因是文本文檔壓縮效果很明顯。
- devid 1 size 17.00GiB used 1.28GiB path /dev/sdb
- devid 2 size 20.00GiB used 1.28GiB path /dev/sdc
- Label: 'btrfs22' uuid: d4da4497-b82e-4846-b51c-cd7127ff5c74
- Total devices 2 FS bytes used 5.01GiB #btrfs22掛載沒有啟用壓縮功能。
- devid 1 size 20.00GiB used 4.04GiB path /dev/sdd
- devid 2 size 20.00GiB used 4.00GiB path /dev/sde
- btrfs-progs v3.19.1
子卷和快照的管理
- btrfs subvolume
- [root@localhost btrfs22]# btrfs subvolume create /btrfs/btrfs.sub #創(chuàng)建子卷
- Create subvolume '/btrfs/btrfs.sub'
- [root@localhost btrfs22]# btrfs subvolume list /btrfs #顯示子卷
- ID 259 gen 43 top level 5 path btrfs.sub
- [root@localhost btrfs22]# umount /btrfs
- [root@localhost ~]# mount -o subvol=btrfs.sub /dev/sdc /btrfs.sub/ #單獨掛載子卷
- [root@localhost ~]# btrfs subvolume show /btrfs.sub/
- /btrfs.sub
- Name: btrfs.sub
- uuid: 811deb2f-764f-6440-899c-cddb2b9867e2
- Parent uuid: -
- Creation time: 2016-05-11 08:43:51
- Object ID: 259
- Generation (Gen): 43
- Gen at creation: 43
- Parent: 5
- Top Level: 5
- Flags: -
- Snapshot(s):
子卷刪除
- [root@localhost ~]# umount /btrfs.sub/ #卸卸載子卷
- [root@localhost ~]# btrfs subvolume delete /btrfs/btrfs.sub
- #執(zhí)行刪除命令
- [root@localhost ~]# btrfs subvolume list /btrfs
- #查看 已經(jīng)被刪除
- ID 260 gen 45 top level 5 path btrfs.sub22
子卷快照,類似創(chuàng)建子卷
- [root@localhost ~]# btrfs subvolume list /btrfs
- ID 260 gen 45 top level 5 path btrfs.sub22
- [root@localhost ~]# btrfs subvolume snapshot /btrfs/btrfs.sub22/ /btrfs/btrfs.sub22_snapshot
- Create a snapshot of '/btrfs/btrfs.sub22/' in '/btrfs/btrfs.sub22_snapshot'
- [root@localhost ~]# btrfs subvolume list /btrfs
- ID 260 gen 50 top level 5 path btrfs.sub22
- ID 262 gen 50 top level 5 path btrfs.sub22_snapshot
單個文件快照
- [root@localhost btrfs]# cp --relink hostname hostname_snap
- cp: unrecognized option '--relink'
- Try 'cp --help' for more information.
- [root@localhost btrfs]# cp --reflink hostname hostname_snap
- [root@localhost btrfs]# cp --reflink hostname hostname_snap^C
- [root@localhost btrfs]# echo "magedu.com" >> hostname
- [root@localhost btrfs]# cat hostname
- localhost.localdomain
- magedu.com
- [root@localhost btrfs]# cat hostname_snap
- localhost.localdomain
- [root@localhost btrfs]#
ext文件系統(tǒng)轉(zhuǎn)換為btrfs
- [root@localhost ~]# cp -r /tmp/* /sdf/
- [root@localhost ~]# cd /sdf/
- [root@localhost sdf]# ls
- ks--OqV4Gb lost+found yum.log
- [root@localhost ~]# umount /sdf/ #第一步,先卸載需要轉(zhuǎn)換的ext分區(qū)
- [root@localhost ~]# fsck -f /dev/sdf #第二步,強制檢測磁盤
- 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/sdf: 13/1310720 files (0.0% non-contiguous), 126323/5242880 blocks
- [root@localhost ~]# btrfs-convert /dev/sdf #第三步,執(zhí)行轉(zhuǎn)換命令
- creating btrfs metadata.
- copy inodes [o] [ 0/ 13]
- creating ext2fs image file.
- cleaning up system chunk.
- conversion complete.
- [root@localhost ~]# mount /dev/sdf /sdf/
- [root@localhost ~]# cd /sdf/
- [root@localhost sdf]# ls
- ext2_saved ks--OqV4Gb lost+found yum.log #轉(zhuǎn)換產(chǎn)生的ext2_saved,不可刪除,刪除就無法轉(zhuǎn)回ext文件系統(tǒng)
- [root@localhost sdf]# cat ks--OqV4Gb #轉(zhuǎn)換后查看文件正常。
- restorecon -ir /etc/sysconfig/network-s /var/lib /etc/lvm \
- /dev /etc/iscsi /var/lib/iscsi /root /var/lock /var/log \
- /etc/modprobe.d /etc/sysconfig /var/cache/yum
- # Also relabel the OSTree variants of the normal mounts (if they exist)
- restorecon -ir /var/roothome /var/home /var/opt /var/srv /var/media /var/mnt
- restorecon -i /etc/rpm/macros /etc/dasd.conf /etc/zfcp.conf /lib64 /usr/lib64 \
- /etc/blkid.tab* /etc/mtab /etc/fstab /etc/resolv.conf \
- /etc/modprobe.conf* /var/log/*tmp /etc/crypttab \
- /etc/mdadm.conf /etc/sysconfig/network /root/install.log* \
- /etc/*shadow* /etc/dhcp/dhclient-*.conf /etc/localtime \
- /etc/hostname /root/install.log*
- if [ -e /etc/zipl.conf ]; then
- restorecon -i /etc/zipl.conf
- fi
- [root@localhost sdf]# blkid /dev/sdf
- /dev/sdf: UUID="20bd2176-28d9-49fb-88e9-b746f5ea2736" UUID_SUB="9076e700-22a2-420e-81df-feb79449ab7c" TYPE="btrfs" #查看文件系統(tǒng),已經(jīng)變?yōu)閎trfs
- btrfs轉(zhuǎn)回ext3
- [root@localhost ~]# umount /sdf/ #第一步,卸載
- [root@localhost ~]# btrfs-convert -r /dev/sdf #第二步,執(zhí)行轉(zhuǎn)換命令
- rollback complete.
- [root@localhost ~]# blkid /dev/sdf
- /dev/sdf: UUID="8d6c4b81-9602-428a-a340-f01b176fa82b" SEC_TYPE="ext2" TYPE="ext3" #查看轉(zhuǎn)換結(jié)果,已經(jīng)變?yōu)閑tx3格式。
- [root@localhost ~]#
總結(jié):初步了解btrfs,首先,擴展性基于B-tree架構(gòu),單個文件,磁盤,文件個數(shù),大大超過現(xiàn)在對擴展性的要求,其次,數(shù)據(jù)一致性,基于COW和checksum確保數(shù)據(jù)正常(btrfs的數(shù)據(jù)檢測,修復(fù),恢復(fù)相關(guān)的命令操作后面補充),再次,設(shè)備管理功能強大,內(nèi)建raid,聯(lián)機raid調(diào)整,聯(lián)機空間擴展和縮減,聯(lián)機物理卷增刪,快照,子卷,可脫離raid和lvm.
關(guān)鍵字:linux、btrfs、文件系統(tǒng)、管理
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解