User:L29Ah/Reflink-based snapshots for fun and profit

From Gentoo Wiki
Jump to:navigation Jump to:search

I figured filesystems without native snapshotting support, but having the "reflink" functionality (at the time of writing of the article it's XFS, CIFS, NFS, OCFS and bcachefs), still can provide "snapshots" immensely useful for major Gentoo system development (e.g. trying out new toolchains, libraries and so on) in case you don't feel like running more radical and all-encompassing filesystems like ZFS and BTRFS.

This script creates a new "snapshot" of the currently running system that you can chroot into and break things at your leisure (well, except the kernel-wide `/dev` and friends that handle your hardware and you don't generally want to break):

FILE reflink-chroot
#!/bin/bash
grab () {
	cp -ar --reflink=always "/$1" "$path/$1"
}
set -x
set -e
path=/opt/chroots/gentoo_reflink
binds='
	var/paludis/repositories/gentoo/distfiles
	var/paludis/repositories/gentoo/packages
	var/lib/layman
'
if ! [ -e "$path" ]; then
	mkdir -p "$path"
	cd "$path"
	mkdir dev proc tmp sys run
	chmod 1777 tmp
	grab bin
	grab etc
	grab lib
	grab lib32
	grab lib64
	grab libexec
	grab sbin
	grab usr
	grab var
	for d in $binds; do
		rm -rf "$d/*"
	done
fi
mount -o rbind /dev dev
mount -t proc proc proc
mount -t sysfs sys sys
mount -t tmpfs run run
for d in $binds; do
	mount -o rbind /"$d" "$d"
done