Skip to main content
tiCrypt

Linux Images: BTRFS Drive Snapshots

A snapshot is a read-only copy of a drive at one point in time. With the setup on this page, BTRFS drives keep snapshots on a schedule, so files that are deleted or overwritten by mistake can be copied back.

tiCrypt does not take these snapshots itself. A script in the VM image runs every hour, and two small files at the top of each drive say how often to take a snapshot and how many to keep. The settings, the snapshots and their log are stored on the drive, so they follow it into any VM whose image has the script.

note

These are not the snapshot shown in the Snapshot field of a drive's details. That one is kept by tiCrypt on the VM host, and Super-admins merge it with Merge snapshots. The snapshots on this page are inside the drive's file system, and only a VM can see them.

Requirements​

  • A kernel with btrfs. AlmaLinux 10.1 and later, and Fedora, have it. Images built with the image builder from AlmaLinux 10 already contain the other packages the script needs, btrfs-progs and cronie.
  • The stock kernels of Rocky Linux 9 and 10, AlmaLinux 9 and RHEL have no btrfs. VMs on those images cannot format or mount BTRFS drives at all, so do not list btrfs in their supported drive formats.
  • Root in the VM to turn snapshots on for a drive (step 3). By default no one has sudo in a tiCrypt VM. Log in as root on the VM's console, or turn on adminAccess (the VM owner gets sudo) or enableManagerSudo (VM managers get sudo) in the image's controller.toml. See Controller Configuration.

1. Add the Script to the Image​

Save the following as btrfs-snapshots.sh. Run as root inside an image, it installs the snapshot script as /usr/local/sbin/btrfs_snapshot.sh and runs it at the top of every hour from /etc/cron.d/btrfs_snapshot. It stops with an error if the image's kernel has no btrfs.

#!/bin/bash
# Sets up hourly snapshots of tiCrypt BTRFS drives. Run as root inside the image.
set -euo pipefail

# tiCrypt can only format and mount BTRFS drives if the image's kernel has btrfs.
if ! find /lib/modules -name 'btrfs.ko*' 2>/dev/null | grep -q . &&
! grep -qs '/btrfs\.ko' /lib/modules/*/modules.builtin; then
echo "This image's kernel has no btrfs support, so it cannot use BTRFS drives." >&2
exit 1
fi

dnf install -y btrfs-progs cronie
systemctl enable crond

cat > /usr/local/sbin/btrfs_snapshot.sh <<'SCRIPT'
#!/bin/bash
# Takes and removes read-only snapshots of tiCrypt BTRFS drives.
# Runs every hour from /etc/cron.d/btrfs_snapshot. Each drive is set up by two
# files at its top (for example /drives/drive2):
# .snapshot_interval hours between snapshots, 1 to 24
# .snapshot_retention how many of the newest snapshots to keep, 0 or more
# A drive with neither file is left alone.

BTRFS=/usr/sbin/btrfs
NAME=$(date +%Y%m%d-%H%M)
HOUR=$((10#$(date +%H)))

# Reads a whole number from a file; prints nothing if the file holds anything else.
read_number() {
local value
value=$(tr -d '[:space:]' < "$1")
[[ $value =~ ^[0-9]+$ ]] && echo $((10#$value))
}

# Only drives that tiCrypt mounted read-write as BTRFS. This skips other
# formats, read-only attachments, empty drive folders and, on Slurm worker
# nodes, the controller's drives mounted over NFS.
for DRIVE in $(findmnt -rn -t btrfs -O rw -o TARGET | grep -E '^/drives/drive[0-9]+$'); do
[ -f "$DRIVE/.snapshot_interval" ] || [ -f "$DRIVE/.snapshot_retention" ] || continue
echo "Processing $DRIVE ..."
LOG=$DRIVE/.snapshot.log
SNAPSHOTS=$DRIVE/.snapshot

# Snapshots keep the owners and permissions files had when they were taken,
# so only root may open them.
mkdir -p "$SNAPSHOTS"
chown root:root "$SNAPSHOTS"
chmod 700 "$SNAPSHOTS"

# Take a snapshot
if [ -f "$DRIVE/.snapshot_interval" ]; then
INTERVAL=$(read_number "$DRIVE/.snapshot_interval")
if [ -z "$INTERVAL" ] || [ "$INTERVAL" -lt 1 ] || [ "$INTERVAL" -gt 24 ]; then
echo "$(date) .snapshot_interval must be a whole number from 1 to 24, snapshot not taken" >> "$LOG"
elif [ $((HOUR % INTERVAL)) -eq 0 ]; then
echo "$(date) Taking snapshot at: $SNAPSHOTS/$NAME" >> "$LOG"
"$BTRFS" subvolume snapshot -r "$DRIVE" "$SNAPSHOTS/$NAME" >> "$LOG" 2>&1
fi
fi

# Remove the oldest snapshots
if [ -f "$DRIVE/.snapshot_retention" ]; then
RETENTION=$(read_number "$DRIVE/.snapshot_retention")
if [ -z "$RETENTION" ]; then
echo "$(date) .snapshot_retention must be a whole number, 0 or more, no snapshots removed" >> "$LOG"
else
# Only the snapshots this script took (named like 20261201-1300)
for SNAPSHOT in $(find "$SNAPSHOTS" -mindepth 1 -maxdepth 1 -type d -regextype posix-extended -regex '.*/[0-9]{8}-[0-9]{4}' | sort | head -n -"$RETENTION"); do
echo "$(date) Removing $SNAPSHOT" >> "$LOG"
"$BTRFS" subvolume delete "$SNAPSHOT" >> "$LOG" 2>&1
done
fi
fi
done
SCRIPT
chmod 755 /usr/local/sbin/btrfs_snapshot.sh

cat > /etc/cron.d/btrfs_snapshot <<'CRON'
0 * * * * root /usr/local/sbin/btrfs_snapshot.sh > /var/log/btrfs_snapshot.log 2>&1
CRON
chmod 644 /etc/cron.d/btrfs_snapshot

With the image builder, put the file next to image.conf, list it in scripts, raise version and build. Use an AlmaLinux 10.1 or later base image.

scripts=( "btrfs-snapshots.sh" )

For other images, run it as root inside the image before you register it, for example with virt-customize -a <new image>.qcow2 --run btrfs-snapshots.sh on the VM host.

caution

Only change a new image file that no VM uses yet. Every VM's boot drive is built on top of its image file, so changing an image in use breaks those VMs. Build or copy a new image instead.

2. Allow BTRFS Drives on the Image​

When you register the image in VM Images, add btrfs to Supported Drive Formats, next to ext4 and xfs. For an image that is already registered, see Edit the Supported Drive Format of a VM Image.

Users then choose BTRFS (Linux) as the format when they create a drive. A drive's format cannot be changed later, so existing EXT4 drives stay without snapshots.

3. Turn On Snapshots for a Drive​

A drive has no snapshots until it has the settings files. In a VM that has the drive attached read-write, write the two files at the top of the drive as root. Drives are mounted at /drives/driveN, the home drive at /drives/drive1, and /drives/<Drive name> links to each one, so ls -l /drives shows which is which.

echo 4 | sudo tee /drives/drive2/.snapshot_interval
echo 42 | sudo tee /drives/drive2/.snapshot_retention

This example takes a snapshot every four hours and keeps the newest 42, which is seven days. The first snapshot is taken at the next hour that matches the interval.

FileContents
.snapshot_intervalA whole number from 1 to 24: the hours between snapshots. Delete the file to stop taking snapshots.
.snapshot_retentionA whole number, 0 or more: how many of the newest snapshots to keep. Older ones are removed every hour. 0 removes them all, including one taken in the same hour. Without this file no snapshot is ever removed, and the drive fills up.

A snapshot is taken at every hour that the interval divides evenly, counting from midnight. Hours are in the VM's time zone, which is UTC on images built from the AlmaLinux cloud images.

.snapshot_intervalSnapshots at
1Every hour
200:00, 02:00, 04:00 and so on
400:00, 04:00, 08:00, 12:00, 16:00, 20:00
600:00, 06:00, 12:00, 18:00
800:00, 08:00, 16:00
1200:00, 12:00
2400:00

Use 1, 2, 3, 4, 6, 8, 12 or 24 for evenly spaced snapshots. Other numbers start again at midnight: 5 gives 00:00, 05:00, 10:00, 15:00 and 20:00, and 15 gives 00:00 and 15:00.

The settings stay with the drive. It gets snapshots in any VM whose image has the script, whichever /drives/driveN it is mounted at, and none in a VM whose image does not.

Where Snapshots Are Kept​

Each snapshot is a read-only folder in .snapshot at the top of the drive, named after the time it was taken, for example /drives/drive2/.snapshot/20261201-1600. It holds the whole drive as it was then, including the user home folders and, on the home drive, the VM's user database in .ticrypt. Earlier snapshots show up in it as empty folders.

  • Only root can open .snapshot. The script sets it to mode 700 on every run. A snapshot keeps the owners and permissions files had when it was taken, so if users could open it, someone removed from an access directory could still read its old files until the snapshot is removed.
  • Snapshots use space on the drive. They share unchanged data with the drive, but a deleted or changed file keeps its old data until every snapshot that holds it is removed. If the drive fills up, lower .snapshot_retention or delete snapshots. sudo btrfs filesystem usage /drives/drive2 shows the space in use.
  • Do not make the top of the drive an access directory. tiCrypt applies an access directory's owner and permissions to everything inside it, and it cannot change read-only snapshots, so applying them fails. Access directories on folders below the top are not affected.
  • A read-only attachment takes no snapshots. Root in that VM can still read the existing ones.

Restore Files​

As root, find the snapshot and copy the files back:

sudo ls /drives/drive2/.snapshot/
sudo ls /drives/drive2/.snapshot/20261201-1600/projects/
sudo cp -a --reflink=always /drives/drive2/.snapshot/20261201-1600/projects/report.docx /drives/drive2/projects/

--reflink=always makes the copy without using more space on the drive. -a keeps the owner and permissions the file had when the snapshot was taken. If access to the folder has changed since, check them with ls -l and getfacl.

Copy back files and folders, never the whole drive:

  • Do not copy .ticrypt back. On the home drive it holds the VM's user database, and an old copy would undo later changes to users and access.
  • Do not run btrfs subvolume set-default on a drive. tiCrypt mounts the drive's default subvolume, so the drive would then open as the read-only snapshot.

Delete Snapshots​

sudo btrfs subvolume delete /drives/drive2/.snapshot/20261201-1600

The drive gets the space back shortly afterwards. The hourly run only removes snapshots it took itself, named like 20261201-1600. Snapshots you take by hand under other names stay until you delete them.

Logs​

LogContents
/var/log/btrfs_snapshot.logOutput of the latest hourly run: the drives it processed and any errors.
.snapshot.log at the top of each driveEvery snapshot taken and removed on that drive, and settings it could not read. It stays with the drive.