Following [[systemd]] unit file will run the script every time the system manages to successfully boot into ''multi-user.target'':

Following [[systemd]] unit file will run the script every time the system manages to successfully boot into ''multi-user.target'':

Line 154:

Line 153:

=== Booting into snapshots ===

=== Booting into snapshots ===

−

In order to boot into a subvolume the ''rootflags=subvol='' option has to be used on the kernel line. The ''subvol='' mount options in {{ic|/etc/[[fstab]]}} of the snapshot to boot into also have to be specified correctly (note: if using the example [[Btrfs - Tips and tricks#Snapshot script|script above]], the {{ic|/etc/[[fstab]]}} of the snapshot of the root subvolume is set automatically).

+

In order to boot into a subvolume the ''rootflags=subvol='' option has to be used on the kernel line. The ''subvol='' mount options in {{ic|/etc/fstab}} of the snapshot to boot into also have to be specified correctly.

+

{{Note|If using the example [[Btrfs - Tips and tricks#Snapshot script|script above]], the {{ic|/etc/fstab}} of the snapshot of the root subvolume is set automatically.}}

Contents

Snapshots

Automatic snapshots on each boot

It is possible to automatically save the then current state of a btrfs subvolume during system boot. Two files are needed to accomplish this, a script which snapshots btrfs subvolumes and a systemd unit file to run the script during the boot process.

Requirements

The root filesystem must have been installed into its own dedicated btrfs subvolume and the btrfs-root where all subvolumes reside has to be accessible.

Snapshot script

An example script to snapshot the btrfs subvolumes mounted under / is listed further below. The script will automatically detect the names of all subvolumes mounted under / and create snapshots of the same names under a directory specified by the caller.

What the script does, is to delete all snapshots from the state the system was in at last boot, make new snapshots, and alter the /etc/fstab file of the snapshot of the root subvolume to allow it to be booted without manual configuration.

The name of the btrfs root subvolume as specified in /etc/fstab. E. g. __current/ROOT,

The path where the newly created snapshots will reside without its 1st parameter portion. E. g. __snapshot/__state_at_last_successful_boot (if the actual path is /run/btrfs-root/__snapshot/__state_at_last_successful_boot)

Warning: This script will delete all snapshots of the same names as the regular subvolume names in the path its third parameter is pointing to. Be careful that the 3rd parameter is not pointing at the place where your subvolumes reside in --- In this example, /run/btrfs-root/__current as the 3rd parameter would be incorrect and lead to data loss.

Booting into snapshots

In order to boot into a subvolume the rootflags=subvol= option has to be used on the kernel line. The subvol= mount options in /etc/fstab of the snapshot to boot into also have to be specified correctly.

Note: If using the example script above, the /etc/fstab of the snapshot of the root subvolume is set automatically.