{
    "content": [
        {
            "type": "text",
            "text": "# btrfs-device(8) (man)\n\n## TLDR\n\n> Manage devices in a btrfs filesystem.\n\n- Add one or more devices to a btrfs filesystem:\n  `sudo btrfs {{d|device}} {{a|add}} {{path/to/block_device1 path/to/block_device2 ...}} {{path/to/btrfs_filesystem}}`\n- Remove a device from a btrfs filesystem:\n  `sudo btrfs {{d|device}} {{rem|remove}} {{path/to/device1|device_id1 path/to/device2|device_id2 ...}}`\n- Display error statistics:\n  `sudo btrfs {{d|device}} {{st|stats}} {{path/to/btrfs_filesystem}}`\n- Scan all disks and inform the kernel of all detected btrfs filesystems:\n  `sudo btrfs {{d|device}} {{sc|scan}} {{-d|--all-devices}}`\n- Display detailed per-disk allocation statistics:\n  `sudo btrfs {{d|device}} {{u|usage}} {{path/to/btrfs_filesystem}}`\n\n*Source: tldr-pages*\n\n---\n\n**Summary:** btrfs-device - manage devices of btrfs filesystems\n\n**Synopsis:** btrfs device <subcommand> <args>\n\n## See Also\n\n- mkfs.btrfs(8)\n- btrfs-replace(8)\n- btrfs-balance(8)\n- BTRFS-DEVICE(8)\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (2 lines)\n- **DESCRIPTION** (2 lines)\n- **DEVICE MANAGEMENT** (29 lines)\n- **SUBCOMMAND** (167 lines)\n- **TYPICAL USECASES** (67 lines)\n- **DEVICE STATS** (33 lines)\n- **EXIT STATUS** (6 lines)\n- **AVAILABILITY** (3 lines)\n- **SEE ALSO** (5 lines)\n\n## Full Content\n\n### NAME\n\nbtrfs-device - manage devices of btrfs filesystems\n\n### SYNOPSIS\n\nbtrfs device <subcommand> <args>\n\n### DESCRIPTION\n\nThe btrfs device command group is used to manage devices of the btrfs filesystems.\n\n### DEVICE MANAGEMENT\n\nBtrfs filesystem can be created on top of single or multiple block devices. Data and metadata\nare organized in allocation profiles with various redundancy policies. There’s some\nsimilarity with traditional RAID levels, but this could be confusing to users familiar with\nthe traditional meaning. Due to the similarity, the RAID terminology is widely used in the\ndocumentation. See mkfs.btrfs(8) for more details and the exact profile capabilities and\nconstraints.\n\nThe device management works on a mounted filesystem. Devices can be added, removed or\nreplaced, by commands provided by btrfs device and btrfs replace.\n\nThe profiles can be also changed, provided there’s enough workspace to do the conversion,\nusing the btrfs balance command and namely the filter convert.\n\nType\nThe block group profile type is the main distinction of the information stored on the\nblock device. User data are called Data, the internal data structures managed by\nfilesystem are Metadata and System.\n\nProfile\nA profile describes an allocation policy based on the redundancy/replication constraints\nin connection with the number of devices. The profile applies to data and metadata block\ngroups separately. Eg.  single, RAID1.\n\nRAID level\nWhere applicable, the level refers to a profile that matches constraints of the standard\nRAID levels. At the moment the supported ones are: RAID0, RAID1, RAID10, RAID5 and RAID6.\n\nSee the section TYPICAL USECASES for some examples.\n\n### SUBCOMMAND\n\nadd [-Kf] <device> [<device>...] <path>\nAdd device(s) to the filesystem identified by <path>.\n\nIf applicable, a whole device discard (TRIM) operation is performed prior to adding the\ndevice. A device with existing filesystem detected by blkid(8) will prevent device\naddition and has to be forced. Alternatively the filesystem can be wiped from the device\nusing eg. the wipefs(8) tool.\n\nThe operation is instant and does not affect existing data. The operation merely adds the\ndevice to the filesystem structures and creates some block groups headers.\n\nOptions\n\n-K|--nodiscard\ndo not perform discard (TRIM) by default\n\n-f|--force\nforce overwrite of existing filesystem on the given disk(s)\n\n--enqueue\nwait if there’s another exclusive operation running, otherwise continue\n\nremove [options] <device>|<devid> [<device>|<devid>...] <path>\nRemove device(s) from a filesystem identified by <path>\n\nDevice removal must satisfy the profile constraints, otherwise the command fails. The\nfilesystem must be converted to profile(s) that would allow the removal. This can\ntypically happen when going down from 2 devices to 1 and using the RAID1 profile. See the\nTYPICAL USECASES section below.\n\nThe operation can take long as it needs to move all data from the device.\n\nIt is possible to delete the device that was used to mount the filesystem. The device\nentry in the mount table will be replaced by another device name with the lowest device\nid.\n\nIf the filesystem is mounted in degraded mode (-o degraded), special term missing can be\nused for device. In that case, the first device that is described by the filesystem\nmetadata, but not present at the mount time will be removed.\n\nNote\nIn most cases, there is only one missing device in degraded mode, otherwise mount\nfails. If there are two or more devices missing (e.g. possible in RAID6), you need\nspecify missing as many times as the number of missing devices to remove all of them.\nOptions\n\n--enqueue\nwait if there’s another exclusive operation running, otherwise continue\n\ndelete <device>|<devid> [<device>|<devid>...] <path>\nAlias of remove kept for backward compatibility\n\nready <device>\nWait until all devices of a multiple-device filesystem are scanned and registered within\nthe kernel module. This is to provide a way for automatic filesystem mounting tools to\nwait before the mount can start. The device scan is only one of the preconditions and the\nmount can fail for other reasons. Normal users usually do not need this command and may\nsafely ignore it.\n\nscan [options] [<device> [<device>...]]\nScan devices for a btrfs filesystem and register them with the kernel module. This allows\nmounting multiple-device filesystem by specifying just one from the whole group.\n\nIf no devices are passed, all block devices that blkid reports to contain btrfs are\nscanned.\n\nThe options --all-devices or -d can be used as a fallback in case blkid is not available.\nIf used, behavior is the same as if no devices are passed.\n\nThe command can be run repeatedly. Devices that have been already registered remain as\nsuch. Reloading the kernel module will drop this information. There’s an alternative way\nof mounting multiple-device filesystem without the need for prior scanning. See the mount\noption device.\n\nOptions\n\n-d|--all-devices\nEnumerate and register all devices, use as a fallback in case blkid is not available.\n\n-u|--forget\nUnregister a given device or all stale devices if no path is given, the device must\nbe unmounted otherwise it’s an error.\n\nstats [options] <path>|<device>\nRead and print the device IO error statistics for all devices of the given filesystem\nidentified by <path> or for a single <device>. The filesystem must be mounted. See\nsection DEVICE STATS for more information about the reported statistics and the meaning.\n\nOptions\n\n-z|--reset\nPrint the stats and reset the values to zero afterwards.\n\n-c|--check\nCheck if the stats are all zeros and return 0 if it is so. Set bit 6 of the return\ncode if any of the statistics is no-zero. The error values is 65 if reading stats\nfrom at least one device failed, otherwise it’s 64.\n\nusage [options] <path> [<path>...]\nShow detailed information about internal allocations on devices.\n\nThe level of detail can differ if the command is run under a regular or the root user\n(due to use of restricted ioctls). The first example below is for normal user (warning\nincluded) and the next one with root on the same filesystem:\n\nWARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root\n/dev/sdc1, ID: 1\nDevice size:           931.51GiB\nDevice slack:              0.00B\nUnallocated:           931.51GiB\n\n/dev/sdc1, ID: 1\nDevice size:           931.51GiB\nDevice slack:              0.00B\nData,single:           641.00GiB\nData,RAID0/3:            1.00GiB\nMetadata,single:        19.00GiB\nSystem,single:          32.00MiB\nUnallocated:           271.48GiB\n\n•   Device size — size of the device as seen by the filesystem (may be different than\nactual device size)\n\n•   Device slack — portion of device not used by the filesystem but still available in\nthe physical space provided by the device, eg. after a device shrink\n\n•   Data,single, Metadata,single, System,single — in general, list of block group type\n(Data, Metadata, System) and profile (single, RAID1, ...) allocated on the device\n\n•   Data,RAID0/3 — in particular, striped profiles RAID0/RAID10/RAID5/RAID6 with the\nnumber of devices on which the stripes are allocated, multiple occurrences of the\nsame profile can appear in case a new device has been added and all new available\nstripes have been used for writes\n\n•   Unallocated — remaining space that the filesystem can still use for new block groups\n\nOptions\n\n-b|--raw\nraw numbers in bytes, without the B suffix\n\n-h|--human-readable\nprint human friendly numbers, base 1024, this is the default\n\n-H\nprint human friendly numbers, base 1000\n\n--iec\nselect the 1024 base for the following options, according to the IEC standard\n\n--si\nselect the 1000 base for the following options, according to the SI standard\n\n-k|--kbytes\nshow sizes in KiB, or kB with --si\n\n-m|--mbytes\nshow sizes in MiB, or MB with --si\n\n-g|--gbytes\nshow sizes in GiB, or GB with --si\n\n-t|--tbytes\nshow sizes in TiB, or TB with --si\n\nIf conflicting options are passed, the last one takes precedence.\n\n### TYPICAL USECASES\n\nSTARTING WITH A SINGLE-DEVICE FILESYSTEM\nAssume we’ve created a filesystem on a block device /dev/sda with profile single/single\n(data/metadata), the device size is 50GiB and we’ve used the whole device for the filesystem.\nThe mount point is /mnt.\n\nThe amount of data stored is 16GiB, metadata have allocated 2GiB.\n\nADD NEW DEVICE\nWe want to increase the total size of the filesystem and keep the profiles. The size of\nthe new device /dev/sdb is 100GiB.\n\n$ btrfs device add /dev/sdb /mnt\n\nThe amount of free data space increases by less than 100GiB, some space is allocated for\nmetadata.\n\nCONVERT TO RAID1\nNow we want to increase the redundancy level of both data and metadata, but we’ll do that\nin steps. Note, that the device sizes are not equal and we’ll use that to show the\ncapabilities of split data/metadata and independent profiles.\n\nThe constraint for RAID1 gives us at most 50GiB of usable space and exactly 2 copies will\nbe stored on the devices.\n\nFirst we’ll convert the metadata. As the metadata occupy less than 50GiB and there’s\nenough workspace for the conversion process, we can do:\n\n$ btrfs balance start -mconvert=raid1 /mnt\n\nThis operation can take a while, because all metadata have to be moved and all block\npointers updated. Depending on the physical locations of the old and new blocks, the disk\nseeking is the key factor affecting performance.\n\nYou’ll note that the system block group has been also converted to RAID1, this normally\nhappens as the system block group also holds metadata (the physical to logical mappings).\n\nWhat changed:\n\n•   available data space decreased by 3GiB, usable roughly (50 - 3) + (100 - 3) = 144 GiB\n\n•   metadata redundancy increased\n\nIOW, the unequal device sizes allow for combined space for data yet improved redundancy\nfor metadata. If we decide to increase redundancy of data as well, we’re going to lose\n50GiB of the second device for obvious reasons.\n\n$ btrfs balance start -dconvert=raid1 /mnt\n\nThe balance process needs some workspace (ie. a free device space without any data or\nmetadata block groups) so the command could fail if there’s too much data or the block\ngroups occupy the whole first device.\n\nThe device size of /dev/sdb as seen by the filesystem remains unchanged, but the logical\nspace from 50-100GiB will be unused.\n\nREMOVE DEVICE\nDevice removal must satisfy the profile constraints, otherwise the command fails. For\nexample:\n\n$ btrfs device remove /dev/sda /mnt\nERROR: error removing device '/dev/sda': unable to go below two devices on raid1\n\nIn order to remove a device, you need to convert the profile in this case:\n\n$ btrfs balance start -mconvert=dup -dconvert=single /mnt\n$ btrfs device remove /dev/sda /mnt\n\n### DEVICE STATS\n\nThe device stats keep persistent record of several error classes related to doing IO. The\ncurrent values are printed at mount time and updated during filesystem lifetime or from a\nscrub run.\n\n$ btrfs device stats /dev/sda3\n[/dev/sda3].writeioerrs   0\n[/dev/sda3].readioerrs    0\n[/dev/sda3].flushioerrs   0\n[/dev/sda3].corruptionerrs 0\n[/dev/sda3].generationerrs 0\n\nwriteioerrs\nFailed writes to the block devices, means that the layers beneath the filesystem were not\nable to satisfy the write request.\n\nreadioerrors\nRead request analogy to writeioerrs.\n\nflushioerrs\nNumber of failed writes with the FLUSH flag set. The flushing is a method of forcing a\nparticular order between write requests and is crucial for implementing crash\nconsistency. In case of btrfs, all the metadata blocks must be permanently stored on the\nblock device before the superblock is written.\n\ncorruptionerrs\nA block checksum mismatched or a corrupted metadata header was found.\n\ngenerationerrs\nThe block generation does not match the expected value (eg. stored in the parent node).\n\nSince kernel 5.14 the device stats are also available in textual form in\n/sys/fs/btrfs/FSID/devinfo/DEVID/errorstats.\n\n### EXIT STATUS\n\nbtrfs device returns a zero exit status if it succeeds. Non zero is returned in case of\nfailure.\n\nIf the -s option is used, btrfs device stats will add 64 to the exit status if any of the\nerror counters is non-zero.\n\n### AVAILABILITY\n\nbtrfs is part of btrfs-progs. Please refer to the btrfs wiki http://btrfs.wiki.kernel.org for\nfurther details.\n\n### SEE ALSO\n\nmkfs.btrfs(8), btrfs-replace(8), btrfs-balance(8)\n\n\n\nBtrfs v5.16.2                                02/16/2022                              BTRFS-DEVICE(8)\n\n"
        }
    ],
    "structuredContent": {
        "command": "btrfs-device",
        "section": "8",
        "mode": "man",
        "summary": "btrfs-device - manage devices of btrfs filesystems",
        "synopsis": "btrfs device <subcommand> <args>",
        "tldr_summary": "Manage devices in a btrfs filesystem.",
        "tldr_examples": [
            {
                "description": "Add one or more devices to a btrfs filesystem",
                "command": "sudo btrfs {{d|device}} {{a|add}} {{path/to/block_device1 path/to/block_device2 ...}} {{path/to/btrfs_filesystem}}"
            },
            {
                "description": "Remove a device from a btrfs filesystem",
                "command": "sudo btrfs {{d|device}} {{rem|remove}} {{path/to/device1|device_id1 path/to/device2|device_id2 ...}}"
            },
            {
                "description": "Display error statistics",
                "command": "sudo btrfs {{d|device}} {{st|stats}} {{path/to/btrfs_filesystem}}"
            },
            {
                "description": "Scan all disks and inform the kernel of all detected btrfs filesystems",
                "command": "sudo btrfs {{d|device}} {{sc|scan}} {{-d|--all-devices}}"
            },
            {
                "description": "Display detailed per-disk allocation statistics",
                "command": "sudo btrfs {{d|device}} {{u|usage}} {{path/to/btrfs_filesystem}}"
            }
        ],
        "tldr_source": "official",
        "flags": [],
        "examples": [],
        "see_also": [
            {
                "name": "mkfs.btrfs",
                "section": "8",
                "url": "https://www.chedong.com/phpMan.php/man/mkfs.btrfs/8/json"
            },
            {
                "name": "btrfs-replace",
                "section": "8",
                "url": "https://www.chedong.com/phpMan.php/man/btrfs-replace/8/json"
            },
            {
                "name": "btrfs-balance",
                "section": "8",
                "url": "https://www.chedong.com/phpMan.php/man/btrfs-balance/8/json"
            },
            {
                "name": "BTRFS-DEVICE",
                "section": "8",
                "url": "https://www.chedong.com/phpMan.php/man/BTRFS-DEVICE/8/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DEVICE MANAGEMENT",
                "lines": 29,
                "subsections": []
            },
            {
                "name": "SUBCOMMAND",
                "lines": 167,
                "subsections": []
            },
            {
                "name": "TYPICAL USECASES",
                "lines": 67,
                "subsections": []
            },
            {
                "name": "DEVICE STATS",
                "lines": 33,
                "subsections": []
            },
            {
                "name": "EXIT STATUS",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "AVAILABILITY",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 5,
                "subsections": []
            }
        ]
    }
}