After spending hours and hours i cannot manage to understand btrfs enough to use it the way i want.
In the end my goal is simple: Having a clean fail-proof solution for os upgrades. So i've exactly 3 things i need to do: Creating snapshot, Rollback to a snapshot and discarding a snapshot (as the upgrade was successfull).
Creating system snapshots:
btrfs subvolume snapshot @ /btrfs/@-before-upgrade
At first this command should create a snapshot called "@-before-upgrade" which is my current "/". Everything which writes to disk afterwards will not be inside that snapshot. This gives me the first problem: What if a running process was writing to a file or a service has data in memory which has to be stored? Is there any way to create a snapshot which will be used as "@" - "/" after a reboot?
Discard Snapshot:
If an upgrade was successfull, the snapshot can be safely deleted. If im not mistaken the correct command should be this:
btrfs subvolume delete /btrfs/@-before-upgrade
Rollback to snapshot:
If an upgrade was unsuccessfull, i need to revert to the state when i made the snapshot. So in the end after managing the first part, it was simply like a reboot instead of a power loss. So i need to swap the snapshot names, then reboot, and then remove the broken one. Question about that: After swapping them, will changes still be written in the now called "@-broken" snapshot? Or will everything suddenly keep writing into the new/old "@"? If so, is there any way like in above question to apply it after a reboot?
mv /btrfs/@ /btrfs/@-broken
mv /btrfs/@-before-upgrade /btrfs/@
reboot
btrfs subvolume delete /btrfs/@-broken
Thanks in advance for all the help and feedback.
After making the snapshot, any files that are in the middle of being written will still be half written in the snapshot, and the new writes to go the original volume. This can cause corruption of the file, just like a crash or loss of power. If you are running applications that can't cope with a sudden power loss, then shut them down ( maybe boot into rescue mode ) when making the snapshot. When moving the snapshots around, the change does not take effect until you reboot, so anything written after the move is still in the snapshot, which you will discard later.
FYI, the
apt-btrfs-snapshot
package provides scripts for managing all of this and automaitcally creates snapshots for you whenever you run apt to install/remove/upgrade packages.