From 9b11456bb254e84e07000ae574ae363daa1fe268 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Tue, 28 Nov 2023 21:23:52 +0800 Subject: [PATCH] feat: btrfs --- filesystem/btrfs.md | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/filesystem/btrfs.md b/filesystem/btrfs.md index 15073616..9ea77be7 100644 --- a/filesystem/btrfs.md +++ b/filesystem/btrfs.md @@ -9,6 +9,24 @@ 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 root@192.168.178.110 'zstd -d | btrfs receive /home' +``` + ## Deduplication 去重功能 Btrfs 本身支持去重 @@ -16,3 +34,72 @@ 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/ /mnt + +# create a subvolume on top level +btrfs subvolume create /mnt/@snapshots +``` + + + +