jceel / py-libzfs

Python libzfs bindings
16 stars 8 forks source link

[feature] Destroy dataset recursively #13

Open mexicarne opened 8 years ago

mexicarne commented 8 years ago

Hello,

if I understand correctly, right now there is no possibility to remove dataset with all it snapshots in one run. I'm trying something like that:

    zfs = libzfs.ZFS()
    ds = zfs.get_dataset('dataset/with/snapshots')
    ds.umount(force=True)
    ds.delete()

And I got 'dataset is busy' exception.

delete() for dataset doesn't support recursive argument, so we have no direct equivalent for zfs destroy -r dataset/with/snapshots. Would you please provide support for this? Or I getting something wrong?

jceel commented 8 years ago

Hi,

Yes, it's not supported as a single call at the moment. However, you can trivially implement it by leveraging ds.dependents iterator:

>>> import libzfs
>>> z = libzfs.ZFS()
>>> ds = z.get_dataset('tank')
>>> ds
<libzfs.ZFSDataset name 'tank' type 'FILESYSTEM'>
>>> list(ds.dependents)
[<libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0905'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0908'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0911'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0906'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0910'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0909'>, <libzfs.ZFSSnapshot name 'tank/foo@auto-20160420.0907'>, <libzfs.ZFSDataset name 'tank/foo' type 'FILESYSTEM'>, <libzfs.ZFSDataset name 'tank/bar' type 'FILESYSTEM'>, <libzfs.ZFSDataset name 'tank/nfs' type 'FILESYSTEM'>]
>>>

Dependencies list is already sorted in the way that allows you to just iterate over it and issue .delete() for each object.

mexicarne commented 8 years ago

Thanks, will try. Can we please keep this issue open as a feature request? We have recursive argument for ZFSSnapshot's destroy() method, and it looks like something similar can be done for ZFSDataset's delete() method.

mexicarne commented 8 years ago

Sadly, dependents iterator not in FreeBSD port yet. I asked @williambr already to update the port.

AttributeError: 'libzfs.ZFSDataset' object has no attribute 'dependents'
jordanhubbard commented 8 years ago

Is this something we still need to do? Just checking back as this issue is a bit old now..

mexicarne commented 7 years ago

Yes, I think that delete() call of Dataset object need to have recursive argument out of the box like we have for ZFSSnapshot's destroy() call. I'm not sure I can do it myself, so just prefer it to just hang here so @jceel or somebody else can add this feature.