Skip to content

Commit

Permalink
feat: btrfs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan4yin committed Nov 28, 2023
1 parent 08a7d0f commit 9b11456
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions filesystem/btrfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,97 @@ BTRFS 的主要特性:

- [Btrfs - Arch Wiki](https://wiki.archlinuxcn.org/zh-hans/Btrfs)

With Btrfs, it's also easy to install and manage multiple Linux distributions on the same disk, just use different subvolumes for each distribution.

## Take a snapshot of a subvolume

```bash
# create a readonly snapshot of the subvolume
btrfs subvolume snapshot -r /home /snapshots/@home-2021-01-01

# restore the snapshot
## 1. delete the current subvolume
btrfs subvolume delete /home
## 2. create a new subvolume with the same name as the deleted one, using the snapshot as the source
btrfs subvolume snapshot /snapshots/@home-2021-01-01 /home

# transfer the snapshot and restore it on another machine
btrfs send /snapshots/@home-2021-01-01 | zstd | ssh [email protected] 'zstd -d | btrfs receive /home'
```

## Deduplication 去重功能

Btrfs 本身支持去重

第三方工具:

- [duperemove](https://github.com/markfasheh/duperemove)

## Usage

```bash
# format the root partition with btrfs and label it
mkfs.btrfs -L crypted-nixos /dev/mapper/crypted-nixos

# mount the root partition and create subvolumes
mount /dev/mapper/crypted-nixos /mnt
btrfs subvolume create /mnt/@root
btrfs subvolume create /mnt/@home
btrfs subvolume create /mnt/@nix
btrfs subvolume create /mnt/@swap
umount /mnt

# Remount the root partition with the subvolumes you just created
#
# Enable zstd compression to:
# 1. Reduce the read/write operations, which helps to:
# 1. Extend the life of the SSD.
# 2. improve the performance of disks with low IOPS / RW throughput, such as HDD and SATA SSD.
# 2. Save the disk space.
mount -o compress-force=zstd:1,subvol=@root /dev/mapper/crypted-nixos /mnt
mkdir /mnt/{home,nix,swap,boot}
mount -o compress-force=zstd:1,subvol=@home /dev/mapper/crypted-nixos /mnt/home
mount -o compress-force=zstd:1,noatime,subvol=@nix /dev/mapper/crypted-nixos /mnt/nix


mount -o subvol=@swap /dev/mapper/crypted-nixos /mnt/swap
# disable CoW / compression on the swap subvolume,
# because the linux kernel requires that swapfile must not be compressed or have copy-on-write(CoW) enabled.
chattr -R +C -m /mnt/swap
# put the swapfile on the swap subvolume
cd /mnt/swap
truncate -s 0 swapfile
fallocate -l 96G wapfile
chmod 600 wapfile
mkswap swapfile
# check whether the swap subvolume has CoW disabled
# the output of `lsattr` for the swap subvolume should be:
# ---------------C------ /swap/swapfile
# if not, delete the swapfile, and rerun the commands above.

# This is an alternative way to create the swapfile on the swap subvolume:
btrfs filesystem mkswapfile --size 96g --uuid clear /swap/swapfile

lsattr /mnt/swap
# mount the swapfile as swap area
swapon swapfile
```

## FAQ

### How to create a subvolume on top level when another subvolume is mounted on top level?

```bash
# get the uuid of the root partition
lsblk -af

# mount the root partition with the uuid
mount /dev/disk/by-uuid/<UUID> /mnt

# create a subvolume on top level
btrfs subvolume create /mnt/@snapshots
```




0 comments on commit 9b11456

Please sign in to comment.