btrfs-snap.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. btrfs-snap.py: source target newname
  3. creates a exactly named snapshots and bails out if they exist
  4. """
  5. import argparse
  6. import fcntl
  7. import os
  8. import sys
  9. import cffi
  10. ffi = cffi.FFI()
  11. ffi.cdef("""
  12. #define BTRFS_IOC_SNAP_CREATE_V2 ...
  13. struct btrfs_ioctl_vol_args_v2 {
  14. int64_t fd;
  15. char name[];
  16. ...;
  17. };
  18. """)
  19. ffi.set_source("_btrfs_cffi", "#include <btrfs/ioctl.h>")
  20. ffi.compile()
  21. # ____________________________________________________________
  22. from _btrfs_cffi import ffi, lib
  23. parser = argparse.ArgumentParser(usage=__doc__.strip())
  24. parser.add_argument('source', help='source subvolume')
  25. parser.add_argument('target', help='target directory')
  26. parser.add_argument('newname', help='name of the new snapshot')
  27. opts = parser.parse_args()
  28. source = os.open(opts.source, os.O_DIRECTORY)
  29. target = os.open(opts.target, os.O_DIRECTORY)
  30. args = ffi.new('struct btrfs_ioctl_vol_args_v2 *')
  31. args.name = opts.newname
  32. args.fd = source
  33. args_buffer = ffi.buffer(args)
  34. try:
  35. fcntl.ioctl(target, lib.BTRFS_IOC_SNAP_CREATE_V2, args_buffer)
  36. except IOError as e:
  37. print e
  38. sys.exit(1)