From: Milind Changire Date: Thu, 9 May 2019 06:25:05 +0000 (+0530) Subject: cephfs-shell: add quota management X-Git-Tag: v14.2.2~23^2~20 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=b53c43e0784a1553c10789e434e837afefe4f8e1;p=ceph.git cephfs-shell: add quota management Quotas can be managed by: $ quota get dir $ quota set {--max_files MAX_FILES} {--max_bytes MAX_BYTES} dir Fixes: http://tracker.ceph.com/issues/39165 Signed-off-by: Milind Changire (cherry picked from commit d187e9b0a917b828530bda89866d6a4867e9f9ff) --- diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 32c4f0840fe67..a4d792d46023b 100644 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -1124,6 +1124,78 @@ sub-directories, files') self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr(to_bytes( dir_), 'ceph.dir.rbytes').decode('utf-8'))), '.' + dir_)) + quota_parser = argparse.ArgumentParser( + description='Quota management for a Directory') + quota_parser.add_argument('op', choices=['get', 'set'], + help='Quota operation type.') + quota_parser.add_argument('dir', type=str, help='Name of the directory.') + quota_parser.add_argument('--max_bytes', type=int, default=-1, nargs='?', + help='Max cumulative size of the data under ' + 'this directory.') + quota_parser.add_argument('--max_files', type=int, default=-1, nargs='?', + help='Total number of files under this ' + 'directory tree.') + + @with_argparser(quota_parser) + def do_quota(self, args): + """ + Quota management. + """ + if not is_dir_exists(args.dir): + self.poutput("error: no such directory '%s'" % args.dir) + return + + if args.op == 'set': + if (args.max_bytes == -1) and (args.max_files == -1): + self.poutput('please specify either --max_bytes or ' + '--max_files or both') + return + + if args.max_bytes >= 0: + max_bytes = to_bytes(str(args.max_bytes)) + try: + cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_bytes', + max_bytes, len(max_bytes), + os.XATTR_CREATE) + self.poutput('max_bytes set to %d' % args.max_bytes) + except: + cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_bytes', + max_bytes, len(max_bytes), + os.XATTR_REPLACE) + self.poutput('max_bytes reset to %d' % args.max_bytes) + + if args.max_files >= 0: + max_files = to_bytes(str(args.max_files)) + try: + cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_files', + max_files, len(max_files), + os.XATTR_CREATE) + self.poutput('max_files set to %d' % args.max_files) + except: + cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_files', + max_files, len(max_files), + os.XATTR_REPLACE) + self.poutput('max_files reset to %d' % args.max_files) + elif args.op == 'get': + max_bytes = '0' + max_files = '0' + try: + max_bytes = cephfs.getxattr(to_bytes(args.dir), + 'ceph.quota.max_bytes') + self.poutput('max_bytes: %s' % max_bytes) + except: + self.poutput('max_bytes is not set') + pass + + try: + max_files = cephfs.getxattr(to_bytes(args.dir), + 'ceph.quota.max_files') + self.poutput('max_files: %s' % max_files) + except: + self.poutput('max_files is not set') + pass + + def do_help(self, line): """ Get details about a command.