translate done: 20201117 Getting started with btrfs for Linux.md

This commit is contained in:
刘超智 2021-05-28 21:55:36 +08:00
parent 44cac9fdcc
commit 861e301834
2 changed files with 238 additions and 250 deletions

View File

@ -1,250 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (Chao-zhi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Getting started with btrfs for Linux)
[#]: via: (https://opensource.com/article/20/11/btrfs-linux)
[#]: author: (Alan Formy-Duval https://opensource.com/users/alanfdoss)
Getting started with btrfs for Linux
======
The B-tree filesystem is a filesystem and volume manager rolled into
one. It offers a lot of promise for delivering an advanced filesystem
feature set to Linux.
![Filing cabinet for organization][1]
Btrfs has been available for Linux for several years, so you may already be familiar with it. If not, you may have questions about it, especially if you use Fedora Workstation (Btrfs is now its default filesystem). This article aims to help you get familiar with it and its advanced features, such as [copy-on-write][2] and [checksums][3].
Btrfs, short for B-Tree Filesystem, is actually a filesystem and volume manager rolled into one. It's often seen as a response to ZFS, introduced in Sun Microsystem's Solaris OS back in 2005, now largely replaced by an open source implementation called OpenZFS. Ubuntu Linux and FreeBSD often feature OpenZFS. Other examples with similar features are Red Hat's Stratis and the Linux Logical Volume Manager (LVM).
### Setup
To try Btrfs, I [downloaded][4] the Fedora 33 Workstation ISO file and installed it into a new virtual machine (VM). The installation process has not changed from previous versions. I did not customize any settings, including drive partitioning and formatting, to maintain an accurate "out of the box" setup for this tutorial. Once the VM was up and running, I installed and ran the GNOME Partition Editor ([GParted][5]) for a nice, factory-fresh view of the drive layout.
![GParted's view of Btrfs on Fedora 33 Workstation using GParted][6]
(Alan Formy-Duvall, [CC BY-SA 4.0][7])
From this point, it's not much different from what you're used to; in fact, you can use the system normally, and you might not even notice that the filesystem is Btrfs. However, having this new default enables you to leverage several cool features.
### Examine the Btrfs filesystem
I am not aware of any Btrfs-specific graphical tools, although some of its functions have been incorporated into existing disk-management tools.
From the command line, you can get a closer look at the Btrfs format:
```
# btrfs filesystem show
Label: 'fedora_localhost-live'  uuid: f2bb02f9-5c41-4c91-8eae-827a801ee58a
        Total devices 1 FS bytes used 6.36GiB
        devid    1 size 10.41GiB used 8.02GiB path /dev/vda3
```
### Change Btrfs labels
The first thing I noticed was the filesystem label set by the installer: `fedora_localhost-live`. This is inaccurate because it is now an installed system and no longer a [live CD][8]. So I changed it using the `btrfs filesystem label` command.
Changing a Btrfs filesystem label is simple:
```
# btrfs filesystem label /
fedora_localhost-live
# btrfs filesystem label / fedora33workstation
# btrfs filesystem label /
fedora33workstation
```
### Manage Btrfs subvolumes
A subvolume appears to be a standard directory that can be managed by Btrfs. There are several subvolumes on my new Fedora 33 Workstation:
```
# btrfs subvolume list /
ID 256 gen 2458 top level 5 path home
ID 258 gen 2461 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
```
Create a new subvolume using the `btrfs subvolume create` command, or delete a subvolume with `btrfs subvolume delete`:
```
# btrfs subvolume create /opt/foo
Create subvolume '/opt/foo'
# btrfs subvolume list /
ID 256 gen 2884 top level 5 path home
ID 258 gen 2888 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
ID 276 gen 2888 top level 258 path opt/foo
# btrfs subvolume delete /opt/foo
Delete subvolume (no-commit): '/opt/foo'
```
Subvolumes allow actions like setting a quota, taking a snapshot, and replicating to other locations and hosts. How can system administrators take advantage of these capabilities? How about for user home directories?
#### Add a user
As has been the case since olden times, adding a new user account creates a home directory for the account to use:
```
# useradd student1
# getent passwd student1
student1❌1006:1006::/home/student1:/bin/bash
# ls -l /home
drwx------. 1 student1 student1  80 Oct 29 00:21 student1
```
Traditionally, a user's home directory is a subdirectory of `/home`. Ownership and privileges are tailored to the owner, but there are no special functions for managing them. The enterprise server environment is another scenario. Often, a directory is reserved for use by a particular application and its user. You can take advantage of Btrfs to manage and apply constraints to these directories.
To accommodate Btrfs subvolumes as user homes, there is a new option to the `useradd` command: `--btrfs-subvolume-home`. Although the man pages have not been updated (as of this writing), you can see the option by running `useradd --help`. By passing this option when adding a new user, a new Btrfs subvolume will be created. It functions just like the `-d` option does for creating a regular directory:
```
# useradd --btrfs-subvolume-home student2
Create subvolume '/home/student2'
```
Verify the user with `getent passwd student2`, and it will appear normal. However, run the `btrfs subvolume` command to list subvolumes, and you will see something interesting: the new user's home directory!
```
# btrfs subvolume list /
ID 256 gen 2458 top level 5 path home
ID 258 gen 2461 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
ID 272 gen 2459 top level 256 path home/student2
```
Explore the second scenario of an enterprise server environment. Suppose you need to install a [WildFly][9] server in `/opt` and deploy a Java web application. Often, your first step is to create a `wildfly` user.  Do this using the new `--btrfs-subvolume-home` option along with the `-b` option to specify `/opt` as the base directory:
```
# useradd -b /opt --btrfs-subvolume-home wildfly
Create subvolume '/opt/wildfly'
```
Now, the `wildfly` user can log in and complete the installation in `/opt/wildfly`.
#### Delete a user
When you delete a user, sometimes you want to delete that user's files and the home directory at the same time. The `userdel` command has the `-r` option for this, and it also deletes Btrfs subvolumes:
```
# userdel -r student2
Delete subvolume (commit): '/home/student2'
```
#### Set disk-usage quotas
In one of my computer science classes, a student ran a C program that went out of control and wrote to the disk until the entire `/home` was filled on the department's Unix system! The server became unavailable until the admin killed the runaway process and cleared some space. The same is true for the scenario above; that Wildfly enterprise application will have a growing number of log files and content stores for its users. How can you prevent a server from grinding to a halt because the disk has filled up? Setting disk-usage constraints is a good idea. Fortunately, Btrfs supports this by way of quotas.
There are several steps required to configure quotas. The first step is to enable `quota` on the Btrfs filesystem:
```
`# btrfs quota enable /`
```
Make sure you know each subvolume's quota group (qgroup) ID number, which is displayed by the `btrfs subvolume list` command. Each subvolume needs an associated qgroup based on its ID number. This can be done on an individual basis with `btrfs qgroup create`, but, conveniently, the Btrfs wiki provides the following command to expedite creating qgroups for subvolumes on a filesystem:
```
`>btrfs subvolume list \<path> | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup destroy 0/{} \<path>`
```
In a freshly installed Fedora 33 workstation system, you are operating on the root filesystem path, `/`. Substitute `\<path>` with the root path:
```
`# btrfs subvolume list / | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup create 0/{} /`
```
Then run `btrfs quota rescan` and take a look at the new qgroups:
```
# btrfs quota rescan /
quota rescan started
# btrfs qgroup show /
qgroupid         rfer         excl
\--------         ----         ----
0/5          16.00KiB     16.00KiB
0/256       272.04MiB    272.04MiB
0/258         6.08GiB      6.08GiB
0/265        16.00KiB     16.00KiB
0/271        16.00KiB     16.00KiB
0/273        16.00KiB     16.00KiB
```
Now you can assign a quota to one of the qgroups, which, in turn, is applied to its associated subvolume. So, if you want to limit student3's home directory usage to 1GB, use the `btrfs qgroup limit` command:
```
`# btrfs qgroup limit 1G /home/student3`
```
Confirm the quota for the specific subvolume:
```
# btrfs qgroup show -reF /home/student3
qgroupid         rfer         excl     max_rfer     max_excl
\--------         ----         ----     --------     --------
0/271        16.00KiB     16.00KiB      1.00GiB         none
```
Slightly different options will show all qgroups and any quotas that are set:
```
# btrfs qgroup show -re /
qgroupid         rfer         excl     max_rfer     max_excl
\--------         ----         ----     --------     --------
0/5          16.00KiB     16.00KiB         none         none
0/256       272.04MiB    272.04MiB         none         none
0/258         6.08GiB      6.08GiB         none         none
0/265        16.00KiB     16.00KiB         none         none
0/271        16.00KiB     16.00KiB      1.00GiB         none
0/273        16.00KiB     16.00KiB         none         none
```
### Other features
These examples provide some idea of Btrfs' features. Run `btrfs --help` to see the full list of commands. Many other notable capabilities exist; for instance, snapshots and send/receive are two worth learning.
### Final thoughts
Btrfs offers a lot of promise for delivering an advanced filesystem feature set to Linux. It wasn't the first; I credit ZFS for my introduction to this type of filesystem some 15 years ago, but Btrfs is fully open source and unencumbered by patents.
I advise starting with a virtual machine or spare system if you want to explore this filesystem.
I would like to see some graphical management utilities produced for system administrators who like to operate in the GUI world. Fortunately, Btrfs has strong development activity, as evidenced by the Fedora project's decision to make it default on Workstation 33.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/btrfs-linux
作者:[Alan Formy-Duval][a]
选题:[lujun9972][b]
译者:[Chao-zhi](https://github.com/Chao-zhi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/alanfdoss
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/files_documents_organize_letter.png?itok=GTtiiabr (Filing cabinet for organization)
[2]: https://en.wikipedia.org/wiki/Copy-on-write
[3]: https://en.wikipedia.org/wiki/Checksum
[4]: https://getfedora.org/en/workstation/download/
[5]: https://gparted.org/
[6]: https://opensource.com/sites/default/files/uploads/gparted_btrfs.png (GParted's view of Btrfs on Fedora 33 Workstation using GParted)
[7]: https://creativecommons.org/licenses/by-sa/4.0/
[8]: https://en.wikipedia.org/wiki/Live_CD
[9]: https://www.wildfly.org/

View File

@ -0,0 +1,238 @@
[#]: collector: (lujun9972)
[#]: translator: (Chao-zhi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Getting started with btrfs for Linux)
[#]: via: (https://opensource.com/article/20/11/btrfs-linux)
[#]: author: (Alan Formy-Duval https://opensource.com/users/alanfdoss)
btrfs 文件系统入门
======
B-tree 文件系统融合了文件系统和卷管理器。它为 linux 操作系统提供了高级文件系统应当拥有的诸多不错的功能特性。
![Filing cabinet for organization][1]
好几年前 Btrfs 就已经可以在 Linux 中使用了,所以您可能已经熟悉它了。如果没有,你可能会遇到困难,尤其是如果你使用的是 Fedora 工作站 Btrfs 现在是它的默认文件系统)。本文旨在帮助您熟悉它及其高级功能,例如[写入时复制 ][2] 和 [checksums][3]。
BtrfsB-Tree Filesystem 的缩写,实际上是文件系统和卷管理器集成在一起的。它通常被视为对 ZFS 的回应ZFS 早在 2005 年就在 Sun Microsystem 的 Solaris 操作系统中引入,现在基本上被一个名为 OpenZFS 的开源实现所取代。Ubuntu 和 FreeBSD 常常使用 OpenZFS。其他具有类似特性的示例有 Red Hat 的 Stratis 和 Linux 逻辑卷管理器 LVM
## 安装
为了尝试 Btrfs我下载了 Fedora 33 工作站 [ISO 文件 ][4] 并将其安装到一个新的虚拟机 VM 中。安装过程与以前的版本没有更改。我没有自定义任何设置,包括驱动器分区和格式化,以保持本教程的准确“开箱即用”设置。当虚拟机启动并运行后,我安装并运行了 GNOME 分区编辑器 [GParted][5]),以获得一个良好的、工厂级的驱动器布局视图。
![GParted's view of Btrfs on Fedora 33 Workstation using GParted][6]
(Alan Formy-Duvall[CC BY-SA 4.0][7])
从安装这一点来说,与你以前所习惯的情况没什么不同;事实上,您可以正常使用该系统,甚至可能没有注意到文件系统是 Btrfs。然而拥有这个新的默认文件系统使您能够利用几个很酷的特性。
## 检查 Btrfs 文件系统
我暂时没有找到特定于 Btrfs 的图形工具,尽管它的一些功能已经被合并到现有的磁盘管理工具中。
在命令行中,您可以更仔细地查看 Btrfs 格式:
```
# btrfs filesystem show
Label: 'fedora_localhost-live'  uuid: f2bb02f9-5c41-4c91-8eae-827a801ee58a
        Total devices 1 FS bytes used 6.36GiB
        devid    1 size 10.41GiB used 8.02GiB path /dev/vda3
```
## 修改 Btrfs 标签
我首先注意到的是安装程序设置的文件系统标签:`fedora_localhost-live`。这是不准确的,因为它现在是一个已安装的系统,不再是 [livecd][8]。所以我使用 `btrfs filesystem label` 命令对其进行了更改。
修改 Btrfs 标签非常的简单:
```
# btrfs filesystem label /
fedora_localhost-live
# btrfs filesystem label / fedora33workstation
# btrfs filesystem label /
fedora33workstation
```
## 管理 Btrfs 子卷
子卷看起来是可以由 Btrfs 管理的标准目录。我的新 Fedora 33 工作站上有几个子卷:
```
# btrfs subvolume list /
ID 256 gen 2458 top level 5 path home
ID 258 gen 2461 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
```
使用 `btrfs subvolume Create` 命令创建新的子卷,或使用 `btrfs subvolume delete` 删除子卷:
```
# btrfs subvolume create /opt/foo
Create subvolume '/opt/foo'
# btrfs subvolume list /
ID 256 gen 2884 top level 5 path home
ID 258 gen 2888 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
ID 276 gen 2888 top level 258 path opt/foo
# btrfs subvolume delete /opt/foo
Delete subvolume (no-commit): '/opt/foo'
```
子卷允许设置配额、拍摄快照以及复制到其他位置和其他主机等操作。那么系统管理员如何利用这些功能?用户主目录又是如何操作的呢?
### 添加用户
就像从前一样,添加一个新的用户帐户会创建一个主目录供该帐户使用:
```
# useradd student1
# getent passwd student1
student1:x:1006:1006::/home/student1:/bin/bash
# ls -l /home
drwx------. 1 student1 student1 80 Oct 29 00:21 student1
```
传统上,用户的主目录是 `/home` 的子目录。所有权和操作权是为所有者定制的,但是没有管理它们的特殊功能。而企业服务器环境通常,一个目录是为特定的应用程序及其用户保留的。您可以利用 Btrfs 来管理这些目录并对其应用约束。
为了将 Btrfs 子卷作为用户主页,在 `useradd` 命令中有一个新选项:`--Btrfs-subvolume-home`。尽管手册页尚未更新(截至本文撰写之时),但您可以通过运行 `useradd --help` 来查看该选项。通过在添加新用户时传递此选项,将创建新的 Btrfs 子卷。它的功能与创建常规目录时的 `-d` 选项类似:
```
# useradd --btrfs-subvolume-home student2
Create subvolume '/home/student2'
```
使用 `getent passwd student2` 验证用户,它将显示为正常。但是,运行 `btrfs subvolume` 命令列出子卷,您将看到一些有趣的内容:新用户的主目录!
```
# btrfs subvolume list /
ID 256 gen 2458 top level 5 path home
ID 258 gen 2461 top level 5 path root
ID 265 gen 1593 top level 258 path var/lib/machines
ID 272 gen 2459 top level 256 path home/student2
```
探索企业服务器环境的第二个场景。假设您需要在 `/opt` 中安装一个 [WildFly][9] 服务器并部署一个 Java web 应用程序。通常,您的第一步是创建一个 `wildfly` 用户。使用新的 `--btrfs-subvolume-home` 选项和 `-b` 选项来指定 `/opt` 作为基本目录:
```
# useradd -b /opt --btrfs-subvolume-home wildfly
Create subvolume '/opt/wildfly'
```
于是,`wildfly` 用户可以使用了,并且主目录设置在了 `/opt/wildfly`
### 删除用户
删除用户时,有时需要同时删除该用户的文件和主目录。`userdel` 命令有 `-r` 选项,它可以同时删除 Btrfs 子卷:
```
# userdel -r student2
Delete subvolume (commit): '/home/student2'
```
### 设置磁盘使用配额
在我的一节计算机科学课上,一个学生运行了一个失控的 C 程序,然后写进了磁盘,将我们院的 Unix 系统上整个 `/home` 目录都填满了!在管理员终止失控进程并清除一些空间之前,服务器将无法使用。上述情况也是如此;那个 Wildfly 企业应用程序将为其用户提供越来越多的日志文件和内容存储。如何防止服务器因磁盘已满而死机设置磁盘使用限制是个好主意。幸运的是Btrfs 通过设置配额的方式支持这一点。
配置配额需要几个步骤。第一步是在 Btrfs 文件系统上启用 `quota`
```
`# btrfs quota enable /`
```
确保您知道每个子卷的配额组 qgroupID 号,该编号由 `btrfs subvolume list` 命令显示。每个子卷都需要基于 ID 号码来关联 qgroup。这可以通过 `btrfs qgroup create” 单独完成但是btrfs wiki 提供了以下命令来加快为文件系统上的子卷创建 qgroup
```
`>btrfs subvolume list \<path> | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup destroy 0/{} \<path>`
```
在新安装的 Fedora 33 工作站系统中,您在根文件系统路径上操作,`/`。用根路径替换 `\<path>`
```
`# btrfs subvolume list / | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup create 0/{} /`
```
然后运行 `btrfs quota rescan`,查看新的 qgroups
```
# btrfs quota rescan /
quota rescan started
# btrfs qgroup show /
qgroupid         rfer         excl
\--------         ----         ----
0/5          16.00KiB     16.00KiB
0/256       272.04MiB    272.04MiB
0/258         6.08GiB      6.08GiB
0/265        16.00KiB     16.00KiB
0/271        16.00KiB     16.00KiB
0/273        16.00KiB     16.00KiB
```
于是现在,您可以将配额分配给其中一个 qgroups然后将配额应用于其关联的子卷。因此如果要将 student3 的主目录使用限制为 1 GB请使用 `btrfs qgroup limit` 命令:
```
`# btrfs qgroup limit 1G /home/student3`
```
查看特定子卷的配额:
```
# btrfs qgroup show -reF /home/student3
qgroupid         rfer         excl     max_rfer     max_excl
\--------         ----         ----     --------     --------
0/271        16.00KiB     16.00KiB      1.00GiB         none
```
稍有不同的选项参数将显示所有 qgroups 和设置的所有配额:
```
# btrfs qgroup show -re /
qgroupid         rfer         excl     max_rfer     max_excl
\--------         ----         ----     --------     --------
0/5          16.00KiB     16.00KiB         none         none
0/256       272.04MiB    272.04MiB         none         none
0/258         6.08GiB      6.08GiB         none         none
0/265        16.00KiB     16.00KiB         none         none
0/271        16.00KiB     16.00KiB      1.00GiB         none
0/273        16.00KiB     16.00KiB         none         none
```
## 其他特性
这些例子提供了 Btrfs 特性的一些思考。运行 `btrfs --help` 查看命令的完整列表。还有许多其他值得注意的功能;例如,快照和发送/接收是两个值得学习的功能。
## 总结讨论
Btrfs 为向 Linux 提供高级文件系统特性集贡献了很多优点。这不是第一次;我知道 ZFS 在大约 15 年前引入了这种类型的文件系统,但是 Btrfs 是完全开源的,不受专利的限制。
如果您想探索这个文件系统,我建议从虚拟机或备用系统开始。
我想能够出现一些图形化的管理工具为那些喜欢用图形工具的系统管理员提供便利。幸运的是Btrfs 具有强大的开发活动Fedora 33 项目决定将其设置为工作站上的默认值就证明了这一点。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/btrfs-linux
作者:[Alan Formy-Duval][a]
选题:[lujun9972][b]
译者:[Chao-zhi](https://github.com/Chao-zhi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/alanfdoss
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/files_documents_organize_letter.png?itok=GTtiiabr (Filing cabinet for organization)
[2]: https://en.wikipedia.org/wiki/Copy-on-write
[3]: https://en.wikipedia.org/wiki/Checksum
[4]: https://getfedora.org/en/workstation/download/
[5]: https://gparted.org/
[6]: https://opensource.com/sites/default/files/uploads/gparted_btrfs.png (GParted's view of Btrfs on Fedora 33 Workstation using GParted)
[7]: https://creativecommons.org/licenses/by-sa/4.0/
[8]: https://en.wikipedia.org/wiki/Live_CD
[9]: https://www.wildfly.org/