User Tools

Site Tools


linux:btrfs:error-checks

BTRFS Error Checks

To perform an error check on a Btrfs filesystem, you can use the btrfs check command, which is designed for checking and repairing Btrfs filesystems. Here’s how to perform an error check:

1. Check the Btrfs Filesystem without Making Changes:

Before performing any modifications, it's always good to run the check in a read-only mode to identify any issues.

sudo btrfs check --readonly /dev/sdX
  • Replace /dev/sdX with the actual device where your Btrfs filesystem is located (e.g., /dev/sda1).
  • This command will scan for errors but won’t make any changes.

2. Check and Repair the Btrfs Filesystem (if needed):

If you want to perform repairs, you can use the --repair option. Be cautious with this option, as it can result in data loss.

sudo btrfs check --repair /dev/sdX

3. Checking a Mounted Btrfs Filesystem:

You can also check for errors in a mounted Btrfs filesystem by using the btrfs scrub command. This scrubs the filesystem for data inconsistencies:

sudo btrfs scrub start /mountpoint
  • Replace /mountpoint with the actual mount point of the Btrfs filesystem (e.g., /mnt).
  • This process runs in the background, checking the data and metadata blocks.
  • To monitor the progress of a scrub operation, run:
sudo btrfs scrub status /mountpoint

4. Log Scrub Results:

After a scrub completes, you can view detailed information about any errors found or fixed using the status command mentioned earlier.

5. Repairing Specific Issues:

If you're dealing with specific issues like a corrupt superblock or other particular problems, there are more advanced options, but these should be used with caution and preferably after backing up data.

What Does `btrfs scrub` Do?

The btrfs scrub command in Btrfs is used to verify data integrity by checking for any inconsistencies between data and metadata on disk. Here's a more detailed breakdown of what it does:

  • Data Integrity Check: The scrub operation systematically reads all data and metadata blocks of the Btrfs filesystem, verifying that the content matches the checksums that were stored when the data was written. This ensures that the data hasn't been corrupted since it was last written.
    - Error Detection: If the scrub finds any blocks that don’t match their checksum (i.e., corrupted data), it logs these errors. If redundancy (like a RAID setup) is used, it can attempt to fix the issue by reading the same data from another redundant copy (e.g., from a mirrored disk in RAID 1 or RAID 10) and automatically repairing the corrupted data.
  • Involves All Devices: On multi-device setups, like Btrfs RAID configurations, the scrub runs across all devices, checking for issues in any of the mirrored or striped copies of the data.

Key Points:

1. Non-intrusive: The scrub operation is safe to run while the filesystem is mounted and in use, as it doesn’t alter the data itself unless an error is detected and corrected (in RAID setups). 2. Runs in Background: It runs in the background, allowing the system to continue operating normally. You can check its progress using the status option. 3. Finds and Fixes Silent Corruption: Btrfs has built-in checksumming for both metadata and data. A scrub operation ensures that any silent data corruption (e.g., bit rot) is detected and, if possible, corrected.

How Scrub Works in RAID:

- RAID 1 (mirroring): If a block’s checksum doesn’t match the stored checksum, Btrfs will compare it with the mirrored block from the other device. If the other copy is correct, it will be used to repair the corrupted block.

- RAID 5/6 (striping with parity): If a block is corrupted, the parity blocks are used to reconstruct the correct data and repair the error.

Commands for Scrubbing

  • Start Scrub:
sudo btrfs scrub start /mountpoint

This begins the scrub operation on the specified Btrfs filesystem.

  • Check Scrub Status:
sudo btrfs scrub status /mountpoint

This shows the current progress of the scrub and any errors that have been found or repaired.

  • Cancel Scrub (if needed): sudo btrfs scrub cancel /mountpoint

This cancels the ongoing scrub operation.

Output Example of Scrub Status:

After completing a scrub, you can see the results with:

sudo btrfs scrub status /mountpoint

Example output:

scrub status for btrfs filesystem /mnt/volume
scrub started at Sun Sep 29 11:27:53 2024 and finished after 02:11:08
total bytes scrubbed: 500.00GiB with 0 errors

In this case, no errors were detected during the scrub of a 500 GiB filesystem.

When to Run `btrfs scrub`?

- Periodically: Regular scrubs are recommended to ensure data integrity. Many users run it monthly or quarterly. - Before Backups: It's a good practice to scrub the filesystem before taking backups to ensure that no corrupted data gets backed up. - After Power Failures or Hardware Issues: If there’s a concern about potential disk corruption (e.g., after a power failure), running a scrub can help detect any issues.

When Scrub Doesn’t Repair:

- If you're not using RAID or some form of data redundancy, scrub can detect corruption but will not be able to repair it, as there’s no redundant data to fall back on.

In short, btrfs scrub is a powerful tool for verifying and maintaining the integrity of data on a Btrfs filesystem, especially in scenarios where data redundancy (like RAID) is involved.

linux/btrfs/error-checks.txt · Last modified: by luci4