Snapshot the system zpool from within the initramfs

Bug #1639500 reported by Sam Van den Eynde
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
zfs-linux (Ubuntu)
New
Wishlist
Unassigned

Bug Description

Idea/enhancement request.

I wrote a small initramfs script to snapshot the zpool when booting off zfs in Ubuntu. As the pool is not actively used at that moment, it is in the perfect consistent state to take snapshots. Perhaps this is functionality more people are interested in?

To use it, you do 2 things:
- put the script in /etc/initramfs-tools/scripts/local-bottom and update the initrd (update-initramfs -ck all)
- add a boot parameter in the form of ZFSSNAP=xx in /etc/default/grub and run update-grub. xx has to be an integer

As I did not want to tamper with the initrd too much, it does not require any additional tools in the initrd image. However it uses the zfs-auto-snapshot syntax using "boot" as identifier instead of hourly/daily/weekly/monthly/yearly.

As mentioned above you trigger the script when adding ZFSSNAP=xx as a grub parameter, where xx is an integer. The script will keep the last xx days that contain valid "boot" snapshots, and delete older ones. The number of snapshots on a single day is irrelevant.

The script goes in /etc/initramfs-tools/scripts/local-bottom, so runs right after the root zpool is imported. Snapshot queries are done only on the top level of the pool ("-d 1") to avoid the script slowing down the boot process too much although this means orphans are possible if the cleaning operation is interrupted (system reset for example).

If this kind of functionality would be supported by the systemd import script, other zpools on the system could be snapshotted at import time as well. I considered importing my other zpools in the initramfs stage temporarily just for snapshotting them, but then decided to keep it all simple and refrain from that. Only a zpool that is defined on the kernel command line for the rootfs (root=ZFS=zpool/rootfs) will be taken into account.

So basically, if you add ZFSSNAP=4 to grub and put the script in the initrd image, you will end up with snapshots in the form 'dataset@zfs-auto-snap_boot-YYYY-MM-DD-HHMM' for MYPOOL and all recursive datasets on MYPOOL that don't have com.sun:auto-snapshot set to false. You will find these snapshots for every time you have rebooted your machine but only the last 4 days (re)boots occured.

Disclaimer: I am not a developer so the script might not be a piece of art. It does the job though.

Tags: patch
Revision history for this message
Sam Van den Eynde (samvde) wrote :

For the additional pools one might have, I adapted the script so it snapshots everything else.

To trigger the script I added a new service zfs-import-scan.service, and I adapted the existing zfs-mount.service to include it. This way additional zpools will be snapshotted right after being imported, and before mounting takes place. The script doing the work is called zfs-systemd-snap.

I keep both the initramfs and systemd solution in place on my systems because the rootfs has been pivoted when systemd runs and the whole goal of this is to snapshot zpools without them being written to. I did make sure not to snapshot a zpool twice.

To test the additional systemd stuff:
- add zfs-systemd-snap to /etc/systemd
- add zfs-import-scan.service to /etc/systemd/system and run "systemctl enable zfs-import-scan.service"
- add zfs-mount.service to /etc/systemd/system and run "systemctl enable zfs-mount.service"

That's basically it, it should run based on the same GRUB parameter mentioned before behaving the same way.

Disclaimer: when executing the steps above you are effectively overriding the default zfs-mount.service script provided by the Ubuntu-provided zfs packages. If the unit file gets updated in the future, this will no longer be reflected on your system.

Revision history for this message
Sam Van den Eynde (samvde) wrote :
Revision history for this message
Sam Van den Eynde (samvde) wrote :
Revision history for this message
Sam Van den Eynde (samvde) wrote :

Small fixes done.

Revision history for this message
Sam Van den Eynde (samvde) wrote :

Small fixes done.

Aron Xu (happyaron)
Changed in zfs-linux (Ubuntu):
importance: Undecided → Wishlist
tags: added: patch
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.