如何在Linux中管理BTRFS文件系统

时间:2020-03-05 15:26:02  来源:igfitidea点击:

BTRFS,也发音为"Butter FS"或者"B树FS",是一个用于Linux的写字编写(COW)文件系统。
它是GPL许可的,专注于提供具有可扩展性,数据完整性,碎片整理和容错等功能的功能。
在本文中,让我们了解如何安装和使用BTRFS的一些重要功能。

安装和创建

首先,如果尚未安装,请安装BTRF软件包。
基于Redhat的系统,使用:

yum install btrfs-progs

在基于Debian的系统上,使用:

apt-get install btrfs-tools

使用以下方式启用BTRFS内核模块:

modprobe btrfs

现在让我们在磁盘上进行分区并在其上创建BTRFS文件系统。
在下面的示例中,我们将使用虚拟磁盘/dev/xvdc,该虚拟磁盘/dev/xvdc尚未对此进行任何分区。

使用"fdisk",在设备上创建新分区。
出现提示时,为新分区选择"n",然后为分区编号为主和"1"。
接下来,通过选择"Enter"键选择默认值。
然后选择"W"以保存更改并退出。

[root@localhost ~]# fdisk /dev/xvdc
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4ea901ae.
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance Jan be impacted.
Command (m for help): p
Disk /dev/xvdc: 2097 MB, 2097152000 bytes, 4096000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/4096 bytes
I/O size (minimum/optimal): 4096 bytes/4096 bytes
Disk label type: dos
Disk identifier: 0x4ea901ae
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4095999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4095999, default 4095999): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/xvdc: 2097 MB, 2097152000 bytes, 4096000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/4096 bytes
I/O size (minimum/optimal): 4096 bytes/4096 bytes
Disk label type: dos
Disk identifier: 0x4ea901ae
Device Boot Start End Blocks Id System
/dev/xvdc1 2048 2099199 1048576 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

我们现在将在分区上创建文件系统。

mkfs.btrfs <device>
[root@localhost ~]# mkfs.btrfs /dev/xvdc1
Detected a SSD, turning off metadata duplication. Mkfs with -m dup if you want to force metadata duplication.
Btrfs v3.16.2
See http://btrfs.wiki.kernel.org for more information.
Turning ON incompat feature 'extref': increased hardlink limit per file to 65536
fs created label (null) on /dev/xvdc1
nodesize 16384 leafsize 16384 sectorsize 4096 size 1.00GiB

下一步是创建挂载点并安装文件系统。

[root@localhost ~]# mkdir /mnt/btrfs_mount
[root@localhost ~]# mount /dev/xvdc1 /mnt/btrfs_mount/

使用"df -h"命令验证挂载是否成功

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 14G 741M 13G 6% /
devtmpfs 493M 0 493M 0% /dev
tmpfs 494M 0 494M 0% /dev/shm
tmpfs 494M 14M 481M 3% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/xvdc1 1.0G 17M 899M 2% /mnt/btrfs_mount

一些有用的挂载选项

BTRFS提供了一些专用安装选项,我们可以用于控制其行为。
命名几个,

compress [=zlib|lzo|no]   -   To turn on data compression using zlib or LZO compression. While zlib method is faser, LZO offers better compression
autodefrag   - Enables auto defragmentation of the filesystem in the background.
nodatacow -  Turn off copy-on-write for newly created files.
nodatasum - Turn off data checksum creation for new files.

以下示例使用LZO压缩安装文件系统

[root@localhost ~]#mount -o compress=lzo /dev/xvdc1 /mnt/btrfs_mount

在BTRF中添加和卸下设备

可以将另一个设备添加到安装点。

btrfs device add <device> <mount point>

向BTRF添加设备

从"DF -H"输出中的注意事项上面,早期为1GB的文件系统的大小现在增加到2GB。

同样的方式,我们还可以删除一个或者多个添加的设备/卷:

btrfs device delete
[root@localhost ~]# btrfs device delete /dev/xvdd1 /mnt/btrfs_mount/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 14G 786M 13G 6% /
devtmpfs 493M 0 493M 0% /dev
tmpfs 494M 0 494M 0% /dev/shm
tmpfs 494M 51M 444M 11% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/xvdc1 1.0G 17M 899M 2% /mnt/btrfs_mount

添加或者删除设备后,可以在可用设备上重新分配文件系统中存在的数据和元数据。

btrfs filesystem balance <mount point>
[root@localhost ~]# btrfs filesystem balance /mnt/btrfs_mount
Done, had to relocate 4 out of 4 chunks

调整文件系统大小

如果底层设备有足够的空间来支持这些动作,则可以增加或者减少BTRFS文件系统的大小。

btrfs filesystem resize <new size> <mount point>

调整文件系统大小

压缩和碎片整理

BTRF允许压缩和碎片整理以分别增加文件系统容量和IO性能。

使用以下命令完成碎片整理:

btrfs filesystem defragment <mount point>
[root@localhost ~]# btrfs filesystem defragment /mnt/btrfs_mount

平行压缩和碎片整理,

btrfs filesystem defragment -c <mount point>
[root@localhost ~]# btrfs filesystem defragment -c /mnt/btrfs_mount

注意:为了使用"-c"选项,应在安装"某些有用的安装选项"部分中的指定时启用压缩。

还可以通过提供特定文件和目录来单独碎片整理文件和目录。

btrfs filesystem defragment -c <file-name> ......

子volumes,快照和配额

子伏和快照是BTRF的关键功能之一。
子VOLUME是POSIX文件命名空间。
它与LVM卷不同,其中卷是块设备。
我们可以将它们视为提供文件系统的限制视图的人,因为它们可以独立安装。
每个子Vumume都包含父文件系统下的相应目录。
我们可以进一步在此目录中创建其他目录,甚至是子域,但其行为与普通目录非常相似。

要创建子Vumume,请使用以下命令:

#btrfs subvolume create  <subvolume>
[root@localhost ~]# btrfs subvolume create /mnt/btrfs_mount/subvol1
Create subvolume '/mnt/btrfs_mount/subvol1'

列出可用的子伏

#btrfs subvolume list <mount point>
[root@localhost ~]# btrfs subvolume list /mnt/btrfs_mount/
ID 259 gen 38 top level 5 path subvol1

我们可以尝试将一些数据复制到新创建的子vhevolume中。

[root@localhost ~]# cp /etc/hosts /mnt/btrfs_mount/subvol1
[root@localhost ~]# ls -l /mnt/btrfs_mount/subvol1/
total 4
-rw-r--r-- 1 root root 158 Aug 6 09:44 hosts

让我们现在卸载主文件系统并仅挂载子化。

[root@localhost ~]# umount /mnt/btrfs_mount/
[root@localhost ~]# mount -o subvol=subvol1 /dev/xvdc1 /mnt
[root@localhost ~]# ls -l /mnt
total 4
-rw-r--r-- 1 root root 158 Aug 6 09:44 hosts

请注意,我们现在只看到子化内容。
超出此级别的任何内容都无法访问,从而提供限制视图。

来到快照,他们有助于拍摄子群的内容的快照。

#btrfs subvolume snapshot
[root@localhost ~]# btrfs subvolume snapshot /mnt/btrfs_mount /mnt/btrfs_mount/snapshot1
Create a snapshot of '/mnt/btrfs_mount' in '/mnt/btrfs_mount/snapshot1'
[root@localhost ~]# ls -l /mnt/btrfs_mount
total 16
drwxr-xr-x 1 root root 14 Aug 4 09:56 snapshot1
drwxr-xr-x 1 root root 10 Aug 6 09:44 subvol1

有趣的是,快照本身是一个子化。
我们可以在te上面的命令中使用'-r'选项来创建只读快照。

我们可以使用"btrfs subvol delete"命令删除任何子卷或者快照

[root@localhost ~]# btrfs subvol delete /mnt/btrfs_mount/subvol1
Transaction commit: none (default)
Delete subvolume '/mnt/btrfs_mount/subvol1'
[root@localhost ~]# btrfs subvol delete /mnt/btrfs_mount/snapshot1/
Transaction commit: none (default)
Delete subvolume '/mnt/btrfs_mount/snapshot1'
[root@localhost ~]# ls -l /mnt/btrfs_mount/
total 0

可以使用配额限制一个或者多个子v vlum,从消耗所有文件系统空间。

要使用此功能,我们需要在创建任何子vvolumes之前首先在文件系统上启用配额。

#btrfs quota enable <mount point>
[root@localhost ~]#btrfs quota enable /mnt/btrfs_mount

现在Subvolume可以限制:

#btrfs qgroup limit <size-limit> <subvolume-path>
[root@localhost ~]# btrfs subvolume create /mnt/btrfs_mount/svol1
Create subvolume '/mnt/btrfs_mount/svol1'
[root@localhost ~]# btrfs qgroup limit 500M /mnt/btrfs_mount/svol1/

其他有用的选项

btrfs filesystem df - Shows space usage information of a given mount point

BTRFS空间使用信息

btrfs filesystem show - Shows the filesystem version details
[root@localhost ~]# btrfs filesystem show /mnt/btrfs_mount/
Btrfs v3.16.2
btrfs scrub - Starts error checking on selected or all devices
[root@localhost ~]# btrfs scrub start /mnt/btrfs_mount/
scrub started on /mnt/btrfs_mount/, fsid 199b8fd8-95d4-49d5-b1b5-edc005f9bd48 (pid=20225)

使用"status"命令检查Crub的状态:

[root@localhost ~]# btrfs scrub status /mnt/btrfs_mount/
scrub status for 199b8fd8-95d4-49d5-b1b5-edc005f9bd48
scrub started at Thu Aug 6 10:12:22 2014 and finished after 0 seconds
total bytes scrubbed: 288.00KiB with 0 errors
btrfs restore [options]   - Restores files from a damaged filesystem without unmounting it.