Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

btrfs Filesystem Administration in Linux

Tech Sep 14 1

btrfs Filesystem Overview

btrfs is a modern copy-on-write (CoW) filesystem designed for Linux that offers several advanced capabilities:

Multi-Device Support: btrfs can span multiple underlying storage devices and supports various RAID configurations with online device addition, removal, and modification.

Copy-on-Write Mechanism: When modifying files, btrfs does not update data in-place. Instead, it copies the data first, makes changes to the copy, then redirects pointers from the old location to the new location. This ensures data integrity and enables efficient snapshots.

Checksum Verification: Both data and metadata are protected with checksums, enabling automatic detection of data corruption.

Subvolumes: btrfs supports logical filesystem subdivisions that can be mounted and managed independently.

Snapshot Support: The filesystem supports creating snapshots, including snapshots of snapshots, enabling efficient backup solutions.

Transparent Compression: Data is automatically compressed during writes and decompressed during reads, improving storage efficiency without application awareness.

Creating a btrfs Filesystem

The mkfs.btrfs command initializes the filesystem. Key options include:

  • -L 'LABEL': Sets the filesystem label
  • -d <type>: Specifies data RAID profile (raid0, raid1, raid5, raid6, raid10, single)
  • -m <profile>: Specifies metadata RAID profile (same options as data)

Viewing Filesystem Information

To display all btrf filesystems on the system:

btrfs filesystem show

Mounting btrfs Filesystems

mount -t btrfs /dev/sdb /mnt/data

Enabling Transparent Compression

Compression can be enabled during mount using the compress option:

mount -o compress=lzo /dev/sdb /mnt/data

Supported algorithms are lzo (faster) and zlib (better compression ratio).

Resizing Filesystem Capacity

To grow a filesystem to maximum available space:

btrfs filesystem resize max /mnt/data

To shrink by a specific amount:

btrfs filesystem resize -10G /mnt/data

Managing Devices in a Filesystem

Adding a new device to an existing filesystem:

btrfs device add /dev/sdd /mnt/data

Removing a device (data migrates to remaining devices):

btrfs device delete /dev/sdd /mnt/data

Data Balance Operations

After adding or removing devices, redistribute data across all devices:

btrfs balance start /mnt/data

Converting RAID Levels

Check current storage allocation:

btrfs filesystem df /mnt/data

Convert data or metadata RAID profiles. For example, converting metadata to RAID5 (requires at least 3 devices):

btrfs balance start -mconvert=raid5 /mnt/data

Convert data profile to RAID1:

btrfs balance start -dconvert=raid1 /mnt/data

Note: Ensure sufficient devices exist for the target RAID level before conversion.

Working with Subvolumes

Subvolumes provide logical partitioning within a btrfs filesystem with out requiring separate partitions.

Create two subvolumes:

btrfs subvolume create /mnt/data/vol_archive
btrfs subvolume create /mnt/data/vol_backups

List existing subvolumes:

btrfs subvolume list /mnt/data

Mount a specific subvolume to a different location:

umount /mnt/data
mount -o subvol=vol_archive /dev/sdb /archive

Verify subvolume isolation by creating a file in one subvolume and confirming it doesn't appear in another:

cp /etc/hosts /archive/
umount /archive
mount /dev/sdb /mnt/data
ls /mnt/data/vol_backups

Delete a subvolume:

btrfs subvolume delete /mnt/data/vol_backups

Creating Snapshots

Snapshots capture the state of a subvolume at creation time. They are writable and space-efficient due to CoW mechanics.

Create a snapshot of an existing subvolume:

btrfs subvolume snapshot /mnt/data/vol_archive /mnt/data/archive_backup

Verify snapshot independence by modifying the source and confirming the snapshot remains unchanged:

cd /mnt/data/vol_archive
touch inventory.log
ls /mnt/data/archive_backup

List all subvolumes and snapshots:

btrfs subvolume list /mnt/data

Practical Example

Create a multi-device btrfs filesystem with RAID1 metadata:

mkfs.btrfs -L production_data -d single -m raid1 /dev/sdb /dev/sdc

Mount with compression enabled:

mount -o compress=zlib /dev/sdb /mnt/prod

Add a third device and rebalance:

btrfs device add /dev/sdd /mnt/prod
btrfs balance start /mnt/prod

Create subvolumes for different departments:

btrfs subvolume create /mnt/prod/engineering
btrfs subvolume create /mnt/prod/marketing

Create weekly snapshots:

btrfs subvolume snapshot /mnt/prod/engineering /mnt/prod/engineering_snap_$(date +%Y%m%d)
Tags: btrfs

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.