btrfs文件系统简介及CentOS7下实例
由Oracle于2007年宣布并进行中的COW(copy-on-write式)文件系统。目标是取代Linux目前的ext3文件系统,改善ext3的限制,特别是单一文件大小的限制,总文件系统大小限制以及加入文件校验和特性。加入目前ext3/4未支持的一些功能,例如可写的磁盘快照(snapshots),以及支持递归的快照(snapshots of snapshots),内建磁盘阵列(RAID)支持,支持子卷(Subvolumes)的概念,允许在线调整文件系统大小。(自百度百科)
以下自马哥课程笔记和CentOS7帮助文档:
核心特性:
多物理卷支持:btrfs可由多个底层物理卷组成;支持RAID,以联机“添加”、“移除”,“修改”;
写时复制更新机制(CoW):复制、更新及替换指针(),而非“就地”更新;
数据及元数据校验码(checksum): 能够将文件和源数据和数据的校验吗保存下来,用于检查文件是否损坏,如果损坏还能尝试自动进行修复。保证了数据的可靠性;
子卷:sub_volume,能够在一个卷上创建子卷,每个卷都可以单独进行挂载和使用,子卷和父卷也可以同时挂载;
快照:快照是子卷的一个非完全副本,支持快照的快照,即增量快照机制;
透明压缩:用户无感知压缩,数据在存储和读取时自动压缩和解压缩,以节约存储空间。缺点:启用透明压缩后会增加CPU负载。
命令介绍:
文件系统创建:
mkfs.btrfs
-L 'LABEL'
-d
-m
-O
-O list-all: 列出支持的所有feature;
属性查看:
btrfs filesystem show [--mounted|--all-devices|
or
blkid [DEVICE]
管理命令介绍:
btrfs文件系统管理命令使用子命令的方式进行管理,具体了命令的功能可通过man SUBCOMMEND来查看。
btrfs命令的常用子命令:filesystem, device, balance, subvolume
btrfs:
NAME
btrfs - control a btrfs filesystem
SYNOPSIS
btrfs
COMMANDS
balance
Balance btrfs filesystem chunks across single or several devices.
See btrfs-balance(8) for details.
check
Do off-line check on a btrfs filesystem.
See btrfs-check(8) for details.
device
Manage devices managed by btrfs, including add/delete/scan and so on.
See btrfs-device(8) for details.
filesystem
Manage a btrfs filesystem, including label setting/sync and so on.
See btrfs-filesystem(8) for details.
receive
Receive subvolume data from stdin/file for restore and etc.
See btrfs-receive(8) for details.
replace
Replace btrfs devices.
See btrfs-replace(8) for details.
subvolume
Create/delete/list/manage btrfs subvolume.
See btrfs-subvolume(8) for details.
btrfs filesystem:
NAME
btrfs-filesystem - control btrfs filesystem
SYNOPSIS
btrfs filesystem
SUBCOMMAND
df [options]
Show space usage information for a mount point.
show [--mounted|--all-devices|
Show the btrfs filesystem with some additional info.
resize [
Resize a filesystem identified by
不常用,一般调整子卷。
label [
Show or update the label of a filesystem.
btrfs device:
NAME
btrfs-device - control btrfs devices
SYNOPSIS
btrfs device
DEVICE MANAGEMENT
RAID level
Btrfs filesystem supports most of the standard RAID level: 0/1/5/6/10.
Balance
btrfs-balance(8) subcommand can be used to balance or rebuild chunks to the desired profile.
Device add/remove/replace
Device can be added/removed using btrfs-replace(8) subcommand and replaced using btrfs-replace(8).
When device is removed or replaced, btrfs will do the chunk rebuild if needed.
SUBCOMMAND
add [-Kf]
Add device(s) to the filesystem identified by
delete
Remove device(s) from a filesystem identified by
拆除物理卷时会自动迁移要移除的物理卷中的数据。
ready
Check device to see if it has all of it’s devices in cache for mounting.
scan [(--all-devices|-d)|
Scan devices for a btrfs filesystem.
stats [-z]
Read and print the device IO stats for all devices of the filesystem identified by
btrfs balance:
NAME
btrfs-balance - balance btrfs filesystem
SYNOPSIS
btrfs balance
SUBCOMMAND
cancel
Cancel running or paused balance.
pause
Pause running balance.
resume
Resume interrupted balance.
start [options]
Balance chunks across the devices online.
Options:
-d[
act on data chunks. See FILTERS section for details about
修改数据的组织机制。
-m[
act on metadata chunks. See FILTERS section for details about
修改元数据的组织机制。
-s[
act on system chunks (only under -f). See FILTERS section for details about
-v
be verbose
-f
force reducing of metadata integrity
status [-v]
Show status of running or paused balance.
If -v option is given, output will be verbose.
FILTERS
convert
Convert each selected block group to the given profile name identified by parameters.
btrfs subvolume:
NAME
btrfs-subvolume - control btrfs subvolume(s)
SYNOPSIS
btrfs subvolume
DESCRIPTION
btrfs subvolume is used to control the filesystem to create/delete/list/show subvolumes and snapshots.
SUBCOMMAND
create [-i
Create a subvolume
If
Options
-i
Add the newly created subvolume to a qgroup. This option can be given multiple times.
delete [options]
-c|--commit-after
wait for transaction commit at the end of the operation
-C|--commit-each
wait for transaction commit after delet each subvolume
list
List the subvolumes present in the filesystem
snapshot [-r]
挂载文件系统:
mount -t btrfs /dev/sdb MOUNT_POINT
启用透明压缩机制:
压缩和解压缩自动透明运行,启用后会增加CPU负载
mount -o compress={lzo|zlib} DEVICE MOUNT_POINT
btrfs文件系统管理实例:
(1)、CentOS7中加载磁盘设备(多块);
如/dev/sd{b,c,d};
(2)、将硬盘格式化为btrfs文件系统;
命令:# mkfs.btrfs -L MYDATA /dev/sd{b,c}
查看:# btrfs filesystem show
(3)、挂载btrfs文件系统;
命令:# mount -t btrfs /dev/sdc /mydata/
设备可为任意一个btrfs设备名,如/dev/sdb或/dev/sdc
启用透明压缩机制(可选):# mount -t btrfs -o compress=lzo /dev/sdc /mydata/
(4)、在线调整btrfs文件系统空间大小;
命令:# btrfs filesystem resize -20G /mydata/
(5)、btrfs文件系统中添加/删除磁盘设备;
添加设备命令:# btrfs device add /dev/sdb /mydata/
删除设备命令:# btrfs device delete /dev/sdd /mydata/
(6)、调整数据分部,使数据均衡的分部到各磁盘设备;
启动balance:# btrfs balance start /mydata/
查看balance运行时的状态:# btrfs balance status /mydata/
(7)、动态修改磁盘设备卷的RAID级别;
修改数据的RIAD级别为RAID5:# btrfs balance start -dconvert=raid5 /mydata/
修改元数据的RIAD级别为RAID5:# btrfs balance start -mconvert=raid5 /mydata/
检查:# btrfs filesystem df /mydata/
(8)、btrfs文件系统上创建和管理子卷;
查看子卷信息:# btrfs subvolume list /mydata/
创建子卷,结果类似于在父卷下创建子目录:
# btrfs subvolume create /mydata/bin ID 269 gen 133 top level 5 path bin//ID 269为该子卷的ID号
单独挂载子卷,如果挂载父卷,则父卷下的所有子卷都会自动挂载:
1) # umount /mydata///首先卸载父卷
2) # mount -o subvol=logs /dev/sdb /mnt///使用子卷名挂载
or
# mount -o subvolid=269 /dev/sdb /mnt///使用子卷ID挂载
查看子卷的详细信息:
# btrfs subvolume show /mnt
删除子卷:
# btrfs subvolume delete /mydata/bin/
(9)、btrfs文件系统中子卷和文件快照的管理和使用;
创建子卷快照:# btrfs subvolume snapshot /mydata/logs/ /mydata/logs_snapshot //子卷的快照必须和该子卷在同一父卷下。
查看子卷是否生成:# btrfs subvolume list /mydata/
挂载/删除btrfs文件系统子卷快照的方法和挂载/删除子卷一致:
挂载子卷:
# mount -o subvolid=269 /dev/sdb /mnt/
删除子卷快照:
# btrfs subvolume delete /mydata/logs_snapshot/
创建文件快照:
# cp --reflink /mydata/fstab /mydata/fstab_snap
(10)、ext4和btrfs文件系统转换;
ext4 to btrfs:
1) 首先卸载原ext4文件系统的磁盘,并强制检测:
# umount /mnt/; fsck -f /dev/sdd1
2) 使用btrfs-convert转换文件系统:
# btrfs-convert /dev/sdd1 creating btrfs metadata. copy inodes [o] [ 0/ 12] creating ext2fs image file. cleaning up system chunk. conversion complete.
3) 检查btrfs文件系统是否完成转换:
# btrfs filesystem show
4) 挂载使用:
# mount /dev/sdd1 /mnt/
btrfs to ext4:
1) 卸载文件系统:# umount /mnt/
2)btrfs降级为ext4:
# btrfs-convert -r /dev/sdd1 rollback complete.
3) 检查:
# blkid /dev/sdd1 /dev/sdd1: UUID="af904001-4dac-4b10-87b5-8ff9c06f9e36" TYPE="ext4"
(11)、拆除btrfs文件系统;
1) 拆除前首先需要确认当前数据和无数据的RAID是否满足RAID的最少设备限制,若不满足修改之。若全部拆除可修改为single。
查看当前设备卷的RAID状态:
# btrfs filesystem df /mydata/ Data, RAID5: total=2.00GiB, used=788.00KiB System, RAID5: total=64.00MiB, used=16.00KiB Metadata, RAID5: total=1.00GiB, used=144.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B
修改数据和元数据的RAID级别:
# btrfs balance start -mconvert=single /mydata/ # btrfs balance start -f -mconvert=single /mydata/
检查:
# btrfs filesystem df /mydata/ Data, single: total=1.00GiB, used=532.00KiB System, single: total=32.00MiB, used=16.00KiB Metadata, single: total=1.00GiB, used=144.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B
2) 删除btrfs卷中的磁盘卷:
# btrfs device delete /dev/sdd /mydata/
(12)、查看当前系统下挂载的所有btrfs文件系统及挂载情况:
$ df -Th | grep -i btrfs /dev/sdb3 btrfs 231G 79G 151G 35% / /dev/sdb3 btrfs 231G 79G 151G 35% /home $ sudo btrfs subvolume list / ID 257 gen 52532 top level 5 path @ ID 258 gen 52534 top level 5 path @home $ sudo btrfs subvolume show / / Name: @ UUID: 2fa0d52a-e539-d84d-a51b-07accbc97d00 Parent UUID: - Received UUID: - Creation time: 2018-01-07 02:32:04 +0800 Subvolume ID: 257 Generation: 52532 Gen at creation: 8 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s): $ sudo btrfs subvolume show /home/ /home Name: @home UUID: abdf02ed-2dbc-2346-8569-9ee9437fb467 Parent UUID: - Received UUID: - Creation time: 2018-01-07 02:32:04 +0800 Subvolume ID: 258 Generation: 52534 Gen at creation: 11 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s): $ cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # ## / was on /dev/sdb3 during installation UUID=dd132b10-bcd5-48a8-af21-792b7c1ba8dd / btrfs defaults,subvol=@ 0 1 # /boot was on /dev/sdb1 during installation UUID=645c2b8e-f17d-42a7-9629-882b6e28c10e /boot ext4 defaults 0 2 # /home was on /dev/sdb3 during installation UUID=dd132b10-bcd5-48a8-af21-792b7c1ba8dd /home btrfs defaults,subvol=@home 0 2 # swap was on /dev/sdb2 during installation UUID=d91e030b-2e6d-4670-8682-5bd82d83c2f0 none swap sw 0 0