Does ZFS overwrite folders if you create a dataset with the same name?

First up: you’re fine; all you did was mount a filesystem onto a non-empty folder. This does not destroy the contents of the non-empty folder on the root filesystem, regardless of the filesystem type you mount.

Second up: you find out by experimenting! So let’s experiment. First, we’ll set up a test pool using a sparse file, and make a test folder and file on it:

root@elden:/tmp# truncate -s 1G temppool.raw
root@elden:/tmp# zpool create temppool /tmp/temppool.raw
root@elden:/tmp# cd /temppool
root@elden:/temppool# mkdir testfolder
root@elden:/temppool# touch testfolder/veryimportantfile

Now that we’ve got our test environment, let’s create a dataset in such a way that it mounts on top of our testfolder:

root@elden:/temppool# zfs create temppool/testfolder
root@elden:/temppool# ls testfolder

As you can see, when we created temppool/testfolder as a dataset, it mounted on top of /temppool/testfolder on the actual filesystem, since our veryimportantfile isn’t showing up anymore. How do we make sure veryimportantfile still exists, and regain access to it?

root@elden:/temppool# zfs umount temppool/testfolder
root@elden:/temppool# ls testfolder
veryimportantfile

There it is! veryimportantfile was there all along, we just had to unmount the filesystem that was hiding it. Now we can change the mountpoint of temppool/testfolder to somewhere less inconvenient, or even just destroy it entirely if we don’t need it.

Finally, always remember to clean up your toys when you’re done playing in test pools:

root@elden:/temppool# cd /tmp
root@elden:/tmp# zpool destroy temppool
root@elden:/tmp# rm temppool.raw
2 Likes