Are Ubuntu ZFS root partition snapshots safe for system rollback?

Thinking ZFS had my back, yesterday I started messing with Gnome, Compiz, and dconf editor. After several hours, Gnome sure looked good with all sorts of addins, but after a reboot, my (complex, 3 monitor) setup went berserk, with Wayland very unhappy. So I figured I'd just go back to my Grub snapshot history to early that evening before the tinkering started (cos apt had made a few snapshots then).

Select older root snapshot (system and user), Reboot, seems okay. But then.... sudo apt update && sudo apt upgrade .... whole system gets confused. Starts messing with kernels and graphic driver updates. Apt was very unhappy. Very strange. And smashes the whole display driver setup, to the extent I couldn't recover the system and spent a half day today reinstalling 20.04. This time with ext 4 (though I was quite happy with ZFS up to now).

So I'm wondering. Are ZFS snapshots really recommended for "rolling back" the OS on a root partition? Or is it really good only for a data drive or a home directory. Does apt get confused in some way ie can this kind of rollback create unrecoverably inconsistent states?


Solution 1:

ZFS is save in that sense. BUT the current implementation in Ubuntu seems buggy. What it currently does is it clones snapshots under a new name and then mounts them on top of the old ones. This also created several issues for me after such a "restore". I really hope they fix that. For now I am using a zfs restore instead. This rolls back the filesystem completely. The only downside is that you loose all the data in the meantime, which in my case is not needed anyway. The good news about this is that it frees up all the space, which the ubuntu method does not.

For that the first step is to boot from a Ubuntu Live CD.

Then you need to import your zpool:

sudo zpool import rpool

List all snapshots and find out which one you want to restore.

zfs list -r -t snapshot -o name,creation

Now we make a list of all datasets that need to be restored

# name of snapshot to restore
export SNAPSHOT=autozsys_88hby6

# we restore all datasets below this one
#   restore everything: rpool
#   restore system files only: rpool/ROOT
#   restore user files only: rpool/USERDATA
export DATASET_ROOT=rpool

# collect list of datasets
export DATASETS=$(zfs list -r -t snapshot -o name $DATASET_ROOT | grep $SNAPSHOT)
for i in $DATASETS; do echo "$i"; done

Inspect this list carefully, as any data will be deleted that was changed after the snapshot.

WARNING: ZFS can only rollback to a snapshot that are the most recent one. In order to do so, all intermediate snapshots and bookmarks must be destroyed as well as any clones of those snapshots. We do this by specifying the -rR option.

Now we need to execute rollback for each of these snapshots.

for i in $DATASETS; do sudo zfs rollback -rR $i ; done

At this point you can reboot the system and you have a rolled back system without any clutter.

With this method it is a good practice to create a snapshot with an easy identifiable name before attempting some risky operations. To do that just do at any time:

sudo zfs snapshot -r rpool@SNAP1_BEFORE_DCONF

Note The rollback can technically also be done on a live system. However that might not be a good idea, depending on what was changed.