btrfs-snap.py 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. v = ffi.verify("#include <btrfs/ioctl.h>")
  20. parser = argparse.ArgumentParser(usage=__doc__.strip())
  21. parser.add_argument('source', help='source subvolume')
  22. parser.add_argument('target', help='target directory')
  23. parser.add_argument('newname', help='name of the new snapshot')
  24. opts = parser.parse_args()
  25. source = os.open(opts.source, os.O_DIRECTORY)
  26. target = os.open(opts.target, os.O_DIRECTORY)
  27. args = ffi.new('struct btrfs_ioctl_vol_args_v2 *')
  28. args.name = opts.newname
  29. args.fd = source
  30. args_buffer = ffi.buffer(args)
  31. try:
  32. fcntl.ioctl(target, v.BTRFS_IOC_SNAP_CREATE_V2, args_buffer)
  33. except IOError as e:
  34. print e
  35. sys.exit(1)