Balance 平衡 

The primary purpose of the balance feature is to spread block groups across all devices so they match constraints defined by the respective profiles. See mkfs.btrfs(8) section PROFILES for more details. The scope of the balancing process can be further tuned by use of filters that can select the block groups to process. Balance works only on a mounted filesystem. Extent sharing is preserved and reflinks are not broken. Files are not defragmented nor recompressed, file extents are preserved but the physical location on devices will change.
平衡功能的主要目的是将块组分布到所有设备上,以便它们符合各自配置文件定义的约束条件。有关更多详细信息,请参阅 mkfs.btrfs(8) 章节 PROFILES。平衡过程的范围可以通过使用可选择要处理的块组的过滤器进一步调整。平衡仅适用于已挂载的文件系统。范围共享得到保留,重链接不会被破坏。文件不会被碎片化或重新压缩,文件范围得到保留,但设备上的物理位置会发生变化。

The balance operation is cancellable by the user. The on-disk state of the filesystem is always consistent so an unexpected interruption (e.g. system crash, reboot) does not corrupt the filesystem. The progress of the balance operation is temporarily stored as an internal state and will be resumed upon mount, unless the mount option skip_balance is specified.
用户可以取消平衡操作。文件系统的磁盘状态始终保持一致,因此意外中断(例如系统崩溃、重新启动)不会损坏文件系统。平衡操作的进度将暂时存储为内部状态,并将在挂载时恢复,除非指定了挂载选项 skip_balance。

Warning 警告

Running balance without filters will take a lot of time as it basically move data/metadata from the whole filesystem and needs to update all block pointers.
没有过滤器的运行平衡将花费大量时间,因为它基本上会从整个文件系统移动数据/元数据,并需要更新所有块指针。

The filters can be used to perform following actions:
过滤器可用于执行以下操作:

  • convert block group profiles (filter convert)
    转换块组配置文件(过滤器转换)

  • make block group usage more compact (filter usage)
    使块组使用更紧凑(过滤器使用)

  • perform actions only on a given device (filters devid, drange)
    仅在给定设备上执行操作(过滤器 devid、drange)

The filters can be applied to a combination of block group types (data, metadata, system). Note that changing only the system type needs the force option. Otherwise system gets automatically converted whenever metadata profile is converted.
过滤器可以应用于块组类型的组合(数据、元数据、系统)。请注意,仅更改系统类型需要 force 选项。否则,每当元数据配置文件转换时,系统会自动转换。

When metadata redundancy is reduced (e.g. from RAID1 to single) the force option is also required and it is noted in system log.
当元数据冗余减少时(例如从 RAID1 到单个),也需要 force 选项,并在系统日志中进行了记录。

Note 注意

The balance operation needs enough work space, i.e. space that is completely unused in the filesystem, otherwise this may lead to ENOSPC reports. See the section ENOSPC for more details.
平衡操作需要足够的工作空间,即文件系统中完全未使用的空间,否则可能导致 ENOSPC 报告。有关更多详细信息,请参阅 ENOSPC 部分。

Compatibility 兼容性 

Note 注意

The balance subcommand also exists under the btrfs filesystem namespace. This still works for backward compatibility but is deprecated and should not be used any more.
平衡子命令也存在于 btrfs 文件系统命名空间下。这仍然适用于向后兼容性,但已被弃用,不应再使用。

Note 注意

A short syntax btrfs balance <path> works due to backward compatibility but is deprecated and should not be used any more. Use btrfs balance start command instead.
由于向后兼容性,短语法 btrfs balance <path> 仍然有效,但已被弃用,不应再使用。请改用 btrfs balance start 命令。

Performance implications
性能影响 

Balancing operations are very IO intensive and can also be quite CPU intensive, impacting other ongoing filesystem operations. Typically large amounts of data are copied from one location to another, with corresponding metadata updates.
平衡操作非常依赖 IO,并且可能会相当依赖 CPU,影响其他正在进行的文件系统操作。通常会从一个位置复制大量数据到另一个位置,并进行相应的元数据更新。

Depending upon the block group layout, it can also be seek heavy. Performance on rotational devices is noticeably worse compared to SSDs or fast arrays.
根据块组布局的不同,也可能会有较多的寻道操作。在旋转设备上的性能明显比 SSD 或快速阵列差。

Filters 过滤器 

From kernel 3.3 onwards, BTRFS balance can limit its action to a subset of the whole filesystem, and can be used to change the replication configuration (e.g. convert data from single to RAID1).
从内核 3.3 开始,BTRFS 平衡可以将其操作限制在整个文件系统的子集上,并可用于更改复制配置(例如,将数据从 single 转换为 RAID1 )。

Balance can be limited to a block group profile with the following options:
平衡可以限制为具有以下选项的块组配置文件:

  • -d for data block groups
    -d 用于数据块组

  • -m for metadata block groups (also implicitly applies to -s)
    元数据块组的 -m (也隐式适用于 -s)

  • -s for system block groups
    系统块组的 -s

The options have an optional parameter which means that the parameter must start right after the option without a space (this is mandatory getopt syntax), like -dusage=10. Options for all block group types can be specified in one command.
选项有一个可选参数,这意味着参数必须紧跟在选项后面,不能有空格(这是强制的 getopt 语法),例如 -dusage=10 。所有块组类型的选项可以在一个命令中指定。

A filter has the following structure: filter[=params][,filter=...]
过滤器具有以下结构: filter[=params][,filter=...]

To combine multiple filters use ,, without spaces. Example: -dconvert=raid1,soft
要组合多个过滤器,请使用 , ,不要留空格。示例: -dconvert=raid1,soft

BTRFS can have different profiles on a single device or the same profile on multiple device.
BTRFS 可以在单个设备上具有不同的配置文件,也可以在多个设备上具有相同的配置文件。

The main reason why you want to have different profiles for data and metadata is to provide additional protection of the filesystem’s metadata when devices fail, since a single sector of unrecoverable metadata will break the filesystem, while a single sector of lost data can be trivially recovered by deleting the broken file.
您希望为数据和元数据设置不同配置文件的主要原因是在设备故障时提供对文件系统元数据的额外保护,因为一个无法恢复的元数据扇区将破坏文件系统,而一个丢失的数据扇区可以通过删除损坏的文件来轻松恢复。

Before changing profiles, make sure there is enough unallocated space on existing drives to create new metadata block groups (for filesystems over 50GiB, this is 1GB * (number_of_devices + 2)).
在更改配置文件之前,请确保现有驱动器上有足够的未分配空间来创建新的元数据块组(对于超过 50GiB 的文件系统,这是 1GB * (number_of_devices + 2))

Default profiles on BTRFS are:
BTRFS 上的默认配置文件为:

  • data: single 数据: single

  • metadata: 元数据:
    • single devices: dup 单个设备: dup

    • multiple devices: raid1 多个设备: raid1

The available filter types are:
可用的过滤器类型为:

Filter types 过滤器类型 

profiles=<profiles> 档案=<profiles>

Balances only block groups with the given profiles. Parameters are a list of profile names separated by | (pipe).
仅块组与给定档案匹配。参数是由 | (竖线)分隔的档案名称列表。

usage=<percent>, usage=<range>
使用率=<percent>,使用率=<range>

Balances only block groups with usage under the given percentage. The value of 0 is allowed and will clean up completely unused block groups, this should not require any new work space allocated. You may want to use usage=0 in case balance is returning ENOSPC and your filesystem is not too full.
仅块组使用率低于给定百分比的余额。允许值为 0,并将完全清除未使用的块组,这不应需要分配任何新的工作空间。如果平衡返回 ENOSPC 并且您的文件系统没有太满,您可能希望使用 usage=0。

The argument may be a single value or a range. The single value N means at most N percent used, equivalent to ..N range syntax. Kernels prior to 4.4 accept only the single value format. The minimum range boundary is inclusive, maximum is exclusive.
参数可以是单个值或范围。单个值 N 表示最多使用 N 百分比,等同于 ..N 范围语法。4.4 之前的内核仅接受单个值格式。最小范围边界是包含的,最大范围边界是排他的。

devid=<id>

Balances only block groups which have at least one chunk on the given device. To list devices with ids use btrfs filesystem show.
仅均衡具有给定设备上至少一个块的块组。要列出带有 ID 的设备,请使用 btrfs 文件系统显示。

drange=<range>

Balance only block groups which overlap with the given byte range on any device. Use in conjunction with devid to filter on a specific device. The parameter is a range specified as start..end.
仅均衡与任何设备上给定字节范围重叠的块组。与 devid 结合使用以过滤特定设备上的内容。该参数是一个指定为 start..end 的范围。

vrange=<range> vrange=<范围>

Balance only block groups which overlap with the given byte range in the filesystem’s internal virtual address space. This is the address space that most reports from btrfs in the kernel log use. The parameter is a range specified as start..end.
仅平衡与文件系统内部虚拟地址空间中给定字节范围重叠的块组。这是内核日志中大多数来自 btrfs 的报告使用的地址空间。该参数是一个指定为 start..end 的范围。

convert=<profile> convert=<配置文件>

Convert each selected block group to the given profile name identified by parameters.
将每个选定的块组转换为由参数标识的给定配置文件名。

Note 注意

Starting with kernel 4.5, the data chunks can be converted to/from the DUP profile on a single device.
从内核 4.5 开始,可以在单个设备上将 data 块转换为/ DUP 配置文件。

Note 注意

Starting with kernel 4.6, all profiles can be converted to/from DUP on multi-device filesystems.
从内核 4.6 开始,所有配置文件都可以在多设备文件系统上与 DUP 之间进行转换。

limit=<number>, limit=<range>
限制=<数字>,限制=<范围>

Process only given number of chunks, after all filters are applied. This can be used to specifically target a chunk in connection with other filters (drange, vrange) or just simply limit the amount of work done by a single balance run.
在应用所有过滤器后,仅处理给定数量的块。这可以用于针对与其他过滤器( drangevrange )连接的块,或者仅仅限制单个平衡运行所做的工作量。

The argument may be a single value or a range. The single value N means at most N chunks, equivalent to ..N range syntax. Kernels prior to 4.4 accept only the single value format. The range minimum and maximum are inclusive.
参数可以是单个值或范围。单个值 N 表示最多 N 个块,等同于 ..N 范围语法。4.4 之前的内核仅接受单个值格式。范围的最小值和最大值都包含在内。

stripes=<range> 条纹=<range>

Balance only block groups which have the given number of stripes. The parameter is a range specified as start..end. Makes sense for block group profiles that utilize striping, i.e. RAID0/10/5/6. The range minimum and maximum are inclusive.
仅平衡具有给定条纹数量的块组。该参数是一个范围,指定为 start..end 。适用于利用条带化的块组配置文件,即 RAID0/10/5/6。范围的最小值和最大值都包含在内。

soft 

Takes no parameters. Only has meaning when converting between profiles, or When doing convert from one profile to another and soft mode is on, chunks that already have the target profile are left untouched. This is useful e.g. when half of the filesystem was converted earlier but got cancelled.
不接受参数。仅在转换配置文件之间或在从一个配置文件转换到另一个配置文件且软模式打开时才有意义,已经具有目标配置文件的块将保持不变。例如,当文件系统的一半在之前转换但被取消时,这是有用的。

The soft mode switch is (like every other filter) per-type. For example, this means that we can convert metadata chunks the “hard” way while converting data chunks selectively with soft switch.
软模式开关(像每个其他过滤器一样)是按类型的。例如,这意味着我们可以以“硬”方式转换元数据块,同时使用软开关有选择地转换数据块。

Profile names, used in profiles and convert are one of:
profilesconvert 中使用的配置文件名称之一为:

  • raid0

  • raid1

  • raid1c3

  • raid1c4

  • raid10

  • raid5

  • raid6

  • dup

  • single

The mixed data/metadata profiles can be converted in the same way, but conversion between mixed and non-mixed is not implemented. For the constraints of the profiles please refer to mkfs.btrfs(8) section PROFILES.
混合数据/元数据配置文件可以以相同方式转换,但混合和非混合之间的转换尚未实现。有关配置文件的约束,请参阅 mkfs.btrfs(8) 章节 PROFILES。

Examples 示例 

Adding new device 添加新设备 

The unallocated space requirements depend on the selected storage profiles. The requirements for the storage profile must be met for the selected for both data and metadata (e.g. if you have single data and RAID1 metadata, the stricter RAID1 requirements must be met or the filesystem may run out of metadata space and go read-only).
未分配空间的需求取决于所选的存储配置文件。必须满足所选的存储配置文件的要求,用于选择数据和元数据(例如,如果您有单个数据和 RAID1 元数据,则必须满足更严格的 RAID1 要求,否则文件系统可能会耗尽元数据空间并变为只读)。

Before adding a drive, make sure there is enough unallocated space on existing drives to create new metadata block groups (for filesystems over 50GB, this is 1GB * (number_of_devices + 2)).
在添加驱动器之前,请确保现有驱动器上有足够的未分配空间来创建新的元数据块组(对于超过 50GB 的文件系统,这是 1GB *(设备数量+2))。

If using a striped profile (raid0, raid10, raid5, or raid6), then do a full data balance of all data after adding a drive. If adding multiple drives at once, do a full data balance after adding the last one.
如果使用条带配置文件(raid0、raid10、raid5 或 raid6),则在添加驱动器后对所有数据进行完整的数据平衡。如果一次添加多个驱动器,请在添加最后一个驱动器后进行完整的数据平衡。

btrfs balance start -v --full-balance mnt/

If the balance is interrupted, it can be restarted using the stripes filter (i.e. -dstripes=1..N where N is the previous size of the array before the new device was added) as long as all devices are the same size. If the device sizes are different, a specialized userspace balance tool is required. The data balance must be completed before adding any new devices or increasing the size of existing ones.
如果平衡被中断,可以使用条带过滤器重新启动(即 -dstripes=1..N,其中 N 是在添加新设备之前数组的先前大小),只要所有设备大小相同。如果设备大小不同,则需要使用专门的用户空间平衡工具。在添加任何新设备或增加现有设备的大小之前,必须完成数据平衡。

# For going from 4 disk to 5 disks, in Raid 5
btrfs balance start -v -dstripes=1..4 mnt/

If you are not using a striped profile now, but intend to convert to a striped profile in the future, always perform a full data balance after adding drives or replacing existing drives with larger ones. The stock btrfs balance tool cannot cope with special cases on filesystems with striped raid profiles, and will paint itself into a corner that will require custom userspace balancing tools to recover if you try.
如果您现在没有使用条带配置文件,但打算将来转换为条带配置文件,请在添加驱动器或用更大的驱动器替换现有驱动器后始终执行完整的数据平衡。标准的 btrfs 平衡工具无法处理带有条带 RAID 配置文件的文件系统上的特殊情况,如果尝试,将陷入困境,需要自定义用户空间平衡工具来恢复。

To watch one can use the following:
要查看,可以使用以下内容:

watch "btrfs filesystem usage -T mnt/; btrfs balance status mnt/"

Convert RAID1 after mkfs with defaults
在使用默认设置进行 mkfs 后转换 RAID1

If you forgot to set the block group profile when creating the volume, run the following command:
如果在创建卷时忘记设置块组配置文件,请运行以下命令:

btrfs balance start -v convert=raid1,soft mnt/

This will convert all remaining profiles that are not yet raid1.
这将转换所有尚未为 raid1 的剩余配置文件。

Convert data to RAID10 with RAID1C4 for metadata
使用 RAID1C4 将数据转换为 RAID10 以用于元数据

If you a have multi device setup, or you’d like to have different profiles on a single disk, e.g. RAID10 for data and RAID1C4 for metadata and system:
如果您有多设备设置,或者希望在单个磁盘上拥有不同的配置文件,例如数据使用 RAID10,元数据和系统使用 RAID1C4:

btrfs balance start -v -mconvert=raid1C4,soft -dconvert=raid10,soft mnt/

Compact under used chunks
压缩未使用的块

If the data chunks are not balanced and used only partially, the usage filter can be used to make them more compact:
如果数据块不平衡且仅部分使用,则可以使用使用过滤器使其更紧凑:

btrfs balance start -v -dusage=10 mnt/

If the percent starts from a small number, like 5 or 10, the chunks will be processed relatively quickly and will make more space available. Increasing the percentage can then make more chunks compact by relocating the data.
如果百分比从一个较小的数字开始,比如 5 或 10,那么数据块将被相对快速地处理,并将提供更多可用空间。增加百分比可以通过重新定位数据使更多数据块变得更紧凑。

Chunks utilized up to 50% can be relocated to other chunks while still freeing the space. With utilization higher than 50% the chunks will be basically only moved on the devices. The actual chunk layout may help to coalesce the free space but this is a secondary effect.
利用率高达 50% 的数据块可以被重新定位到其他数据块,同时仍然释放空间。当利用率高于 50% 时,数据块基本上只会在设备上移动。实际的数据块布局可能有助于合并空闲空间,但这只是一个次要影响。

for USAGE in {10..50..10} do
    btrfs balance start -v -dusage=$USAGE mnt/
done

Fix incomplete balance 修复不完整的平衡 

If the balance is interrupted (due to reboot or cancelled) during conversion to RAID1. The following command will skip all RAID1 chunks that have been already converted and continue with what’s left to convert. Note that an interrupted conversion may leave the last chunk under utilized.
如果在转换为 RAID1 期间平衡被中断(由于重新启动或取消),以下命令将跳过已经转换的所有 RAID1 块,并继续转换剩下的部分。请注意,中断的转换可能导致最后一个块被低效利用。

btrfs balance start convert=raid1,soft mnt/