You cloned a dataset to try something risky. The risky thing worked. Now the clone is the real one, the original is the stale one, and zfs destroy on the original tells you to go to hell. The answer is zfs promote, and it is one of those commands that makes sense the instant you understand what a clone actually is.
A clone is a child of a snapshot
zfs snapshot tank/app@pre-upgrade
zfs clone tank/app@pre-upgrade tank/app-test
zfs get origin tank/app-test
The clone shares every block with the snapshot until either side writes. That is why it costs almost nothing, and it is the same trick behind clone-per-jail roots. But the clone’s origin property now points at tank/app@pre-upgrade, and the snapshot cannot be destroyed while something depends on it. Neither can the dataset that owns the snapshot, because destroying the dataset destroys its snapshots:
zfs destroy tank/app
cannot destroy 'tank/app': filesystem has dependent clones
use '-R' to destroy the following datasets:
tank/app-test
Do not reach for -R here. It does exactly what it says, and it will take your good clone with it.
What promote does
zfs promote tank/app-test
Promote flips the dependency. The origin snapshot, and every snapshot older than it, is moved from the original dataset to the clone. The original becomes a clone of a snapshot that now lives under tank/app-test. Same blocks, no data copied, finishes in about the time it takes to type it. Afterwards:
zfs get origin tank/app tank/app-test
tank/app-test has no origin. tank/app has tank/app-test@pre-upgrade. Now the old one can go:
zfs destroy tank/app
That works, because nothing depends on it. Check zfs list -t snapshot -o name,clones tank/app-test first if you want to see who still hangs off what; the clones property on a snapshot lists its dependents.
The swap workflow
This is the test-then-cut-over pattern I use for upgrades on datasets where a plain rollback would be too blunt:
zfs snapshot tank/app@pre-upgrade
zfs clone tank/app@pre-upgrade tank/app-new
# point the test instance at tank/app-new, break things, fix things
zfs promote tank/app-new
zfs rename tank/app tank/app-old
zfs rename tank/app-new tank/app
Mountpoints follow dataset names unless you set them explicitly, so check zfs get mountpoint before renaming and stop the service first. Keep app-old around until you are sure. When you are, destroy it. If you are not, you still have a way back, which is the whole point. For the whole-system version of this idea, boot environments with bectl are built on exactly these clone and promote mechanics.
Where it bites
- Snapshot names must not collide. Promote moves snapshots between datasets, so if the clone already has a snapshot with the same name as one being moved, it refuses. Name your snapshots with dates or purposes, not
snap1. - Space accounting shifts. The space charged for the shared snapshots moves with them. After a promote,
usedbysnapshotson both sides changes and people think something leaked. It did not. Usezfs list -o spaceand read reclaiming space with a destroy dry run before you start deleting. - Replication notices. If you send incrementally to a replica, promoting changes which dataset owns the snapshot you were sending from. Take a fresh snapshot and expect to resync deliberately rather than hoping the incremental chain survived. Bookmarks and holds are your friend there.
- Clones of clones chain. Promote only reverses one link at a time. A long tree of clones needs thought before you start, not after.
I spent years treating clones as disposable and then being baffled when they refused to be disposed of. The origin snapshot is a real dependency, and promote is how you tell the filesystem which side is the parent now. More in the ZFS and storage hub and the FreeBSD and OpenBSD hub.
