Convert existing ext4 filesystem to btrfs
What I want to achieve is getting btrfs features like snapshots, compression and deduplication
The problem is, my system already running and containing data, filesystem is ext4 under linux lvm. So I will convert existing ext4 filesystem to btrfs, keeping lvm partition, and only /home will be converted
Converting to btrfs in-place
LVM partition:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 123M 0 part /boot/efi
├─sda2 8:2 0 390.6G 0 part
│ ├─VGVG-Fedora--30--root 253:0 0 60G 0 lvm /
│ ├─VGVG-Archive 253:1 0 25.6G 0 lvm
│ └─VGVG-Home3rd 253:2 0 200G 0 lvm /home
First, go to rescue mode and unmount /home , or boot into single-user mode
# systemctl rescue
# umount /home
Fix filesystem issue:
# fsck.ext4 -fv /dev/mapper/VGVG-Home3rd
When filesystem is clean for error, now convert to btrfs:
# btrfs-convert /dev/mapper/VGVG-Home3rd
Need about 10 minutes for 150 GB data
Continue to mount, when no error found
# mount /dev/mapper/VGVG-Home3rd /mnt/
# cd /mnt
# btrfs subvolume list -t /mnt/
I want to create subvolume: home like anaconda creates by default:
# cd /mnt
# btrfs subvolume snapshot /mnt/ /mnt/home_orig
# btrfs subvolume create home
# btrfs subvolume list -t /mnt/
Copy user's folder to home subvolume:
# mv yourusername USER2 USER3 ./home/
# ls -alh /mnt/home
Now edit /etc/fstab like this:
/dev/VGVG/Home3rd /home btrfs subvol=home,noatime,compress=zstd 1 2
Try to mount /home:
# mount /home
# ls -alh /home
If it looks okay, /home/yourusername files are there, delete home_orig snapshot and reboot
# btrfs subvolume delete /mnt/home_orig/
# reboot
After reboot, login and check again by running firefox, thunderbird, etc. if everything is good with btrfs
Snapshot Tools
Then install snapper, for automatic snapshot and cleanup
# dnf install snapper
# snapper -c home create-config /home/
# systemctl enable snapper-cleanup.timer
# systemctl enable snapper-timeline.timer
# semanage permissive -a snapperd_t
Edit /etc/snapper/configs/home :
ALLOW_USERS="yourusername"
SYNC_ACL="yes"
TIMELINE_LIMIT_HOURLY="10"
TIMELINE_LIMIT_DAILY="5"
TIMELINE_LIMIT_WEEKLY="4"
TIMELINE_LIMIT_MONTHLY="3"
TIMELINE_LIMIT_YEARLY="1"
Then install snapper-gui from github
Deduplicate
Install rmlint which support btrfs for very fast deduplicate
# dnf install rmlint
# rmlint --types="duplicates" --config=sh:handler=clone -s 512K -g -x /home
Check the script and run (will take very long time):
# ./rmlint.sh
Clean up and defragment
Mount top level volume to /mnt
# mount /dev/VGVG/Home3rd /mnt/
# btrfs subvolume show /mnt/
# btrfs filesystem df -h /mnt/
# btrfs filesystem usage /mnt/
# df -h /mnt/
Delete old ext4 snapshot, and notice used Data changed
# btrfs subvolume delete /mnt/ext2_saved
# btrfs filesystem df -h /mnt/
# df -h /mnt/
Defragment and Compressing
Mount top level volume to /mnt # mount /dev/VGVG/Home3rd /mnt/
# btrfs filesystem df -h /mnt/
Delete old snapshot first (be careful!)
# btrfs subvolume list -ts /mnt
# ls -ld /mnt/home/.snapshots/*/snapshot
# btrfs subvolume delete /mnt/home/.snapshots/*/snapshot
Do defragment process with compressing:
# btrfs filesystem defragment -v -r -f -t 32M -czstd /mnt/home
While defragment you can see used Data is decreasing :
# btrfs filesystem df -h /mnt/
Compacting
Mount top level volume to /mnt
# mount /dev/VGVG/Home3rd /mnt/
# btrfs filesystem df -h /mnt/
Do balancing
# btrfs balance start -m /mnt
Understanding disk usage
Mount top level volume to /mnt:
# mount /dev/VGVG/Home3rd /mnt/
Show disk usage (example output):
# btrfs filesystem usage /mnt/
Overall:
Device size: 200.00GiB
Device allocated: 119.64GiB
Device unallocated: 80.36GiB
Device missing: 0.00B
Used: 57.37GiB
Free (estimated): 141.70GiB (min: 141.70GiB)
Free (statfs, df): 141.69GiB
Data ratio: 1.00
Metadata ratio: 1.00
Global reserve: 165.70MiB (used: 0.00B)
Multiple profiles: no
Data,single: Size:115.61GiB, Used:54.27GiB (46.95%)
/dev/mapper/VGVG-Home3rd 115.61GiB
Metadata,single: Size:4.00GiB, Used:3.09GiB (77.37%)
/dev/mapper/VGVG-Home3rd 4.00GiB
System,single: Size:32.00MiB, Used:48.00KiB (0.15%)
/dev/mapper/VGVG-Home3rd 32.00MiB
Unallocated:
/dev/mapper/VGVG-Home3rd 80.36GiB
Device size: physical partition size
Used: real disk used, after compressed and deduplicated
Free: estimated available disk space
Unallocated: unallocated disk space, will be automatically allocated when needed
Ref: https://fedoramagazine.org/convert-your-filesystem-to-btrfs/
Ref: https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs-convert
Comments