def put(self, request, sname): with self._handle_exception(request): share = self._validate_share(request, sname) if ('size' in request.data): new_size = self._validate_share_size(request, share.pool) disk = Disk.objects.filter(pool=share.pool)[0] qid = qgroup_id(share.pool, share.subvol_name) cur_rusage, cur_eusage = share_usage(share.pool, qid) if (new_size < cur_rusage): e_msg = ('Unable to resize because requested new size(%dKB) ' 'is less than current usage(%dKB) of the share.' % (new_size, cur_rusage)) handle_exception(Exception(e_msg), request) update_quota(share.pool, share.pqgroup, new_size * 1024) share.size = new_size if ('compression' in request.data): new_compression = self._validate_compression(request) if (share.compression_algo != new_compression): share.compression_algo = new_compression mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (new_compression == 'no'): new_compression = '' set_property(mnt_pt, 'compression', new_compression) share.save() return Response(ShareSerializer(share).data)
def put(self, request, sname): with self._handle_exception(request): share = self._validate_share(request, sname) if ('size' in request.data): new_size = self._validate_share_size(request, share.pool) disk = Disk.objects.filter(pool=share.pool)[0] qid = qgroup_id(share.pool, share.subvol_name) cur_rusage, cur_eusage = share_usage(share.pool, qid) if (new_size < cur_rusage): e_msg = ( 'Unable to resize because requested new size(%dKB) ' 'is less than current usage(%dKB) of the share.' % (new_size, cur_rusage)) handle_exception(Exception(e_msg), request) update_quota(share.pool, share.pqgroup, new_size * 1024) share.size = new_size if ('compression' in request.data): new_compression = self._validate_compression(request) if (share.compression_algo != new_compression): share.compression_algo = new_compression mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (new_compression == 'no'): new_compression = '' set_property(mnt_pt, 'compression', new_compression) share.save() return Response(ShareSerializer(share).data)
def post(self, request): with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ( 'A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool, disk.name, sname) qid = qgroup_id(pool, disk.name, sname) update_quota(pool, disk.name, qid, size * 1024) s = Share(pool=pool, qgroup=qid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): disk = Disk.objects.filter(pool=pool)[0].name mount_share(s, disk, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool, disk.name, sname) qid = qgroup_id(pool, disk.name, sname) update_quota(pool, disk.name, qid, size * 1024) s = Share(pool=pool, qgroup=qgroup_id, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): disk = Disk.objects.filter(pool=pool)[0].name mount_share(s, disk, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def put(self, request, sid): with self._handle_exception(request): share = self._validate_share(request, sid) if ('size' in request.data): new_size = self._validate_share_size(request, share.pool) qid = qgroup_id(share.pool, share.subvol_name) cur_rusage, cur_eusage = volume_usage(share.pool, qid) if (new_size < cur_rusage): e_msg = ('Unable to resize because requested new ' 'size {} KB is less than current usage {} KB ' 'of the share.').format(new_size, cur_rusage) handle_exception(Exception(e_msg), request) # quota maintenance if share.pool.quotas_enabled: # Only try create / update quotas if they are enabled, # pqgroup of PQGROUP_DEFAULT (-1/-1) indicates no pqgroup, # ie quotas were disabled when update was requested. if share.pqgroup == PQGROUP_DEFAULT or \ not share.pqgroup_exist: # if quotas were disabled or pqgroup non-existent. share.pqgroup = qgroup_create(share.pool) share.save() if share.pqgroup is not PQGROUP_DEFAULT: # Only update quota and assign if now non default as # default can also indicate Read-only fs at this point. update_quota(share.pool, share.pqgroup, new_size * 1024) share_pqgroup_assign(share.pqgroup, share) else: # Our pool's quotas are disabled so reset pqgroup to -1/-1. if share.pqgroup != PQGROUP_DEFAULT: # Only reset if necessary share.pqgroup = PQGROUP_DEFAULT share.save() share.size = new_size if ('compression' in request.data): new_compression = self._validate_compression(request) if (share.compression_algo != new_compression): share.compression_algo = new_compression mnt_pt = '%s%s' % (settings.MNT_PT, share.name) if (new_compression == 'no'): new_compression = '' set_property(mnt_pt, 'compression', new_compression) share.save() return Response(ShareSerializer(share).data)
def btrfs_share_scan(): pools = btrfs_pool_scan() shares = [] for pool in pools: info = shares_info(pool) for i in range(len(info.keys())): share = {} share["name"] = info.keys()[i] share["subvol_name"] = info.keys()[i] share["group"] = info[info.keys()[i]] share["pool"] = pool qid = qgroup_id(pool, share["subvol_name"]) cur_rusage, cur_eusage = share_usage(pool, qid) share["rusage"] = cur_rusage share["eusage"] = cur_eusage share["pqgroup"] = "xxx" # print qgroup_info(pool) shares.append(share) return shares
def btrfs_add_share(share): pools = btrfs_pool_scan() share_s = {} for pool in pools: if pool["name"] == share["pool"]: pqid = qgroup_create(pool) add_share(pool, share["sname"], pqid) qid = qgroup_id(pool, share["sname"]) update_quota(pool, pqid, share["size"] * 1024) mnt_pt = '%s%s' % (MNT_PT, share["sname"]) replica = False share_s["name"] = share["sname"] share_s["subvol_name"] = share["sname"] share_s["size"] = share["size"] share_s["qgroup"] = qid share_s["pqgroup"] = pqid share_s["pool"] = pool share_s["replica"] = replica share_s["compression_algo"] = share["compression"] mount_share(share_s, mnt_pt) return share_s
def post(self, request): # qgroup notes for shares. we need to create a qgroup prior to share # creation. qgroup ids 0/<subvol_id> automatically get created when a # subvolume(i.e., a Share or a Snapshot) is created. So let's create a # new qgroup: 2015/<some_number> whenever a Share is # created. <some_number> starts from 1 and is incremented as more # Shares are created. So, for the very first Share in a pool, it's # qgroup will be 1/1. 2015 is arbitrarily chose. # Before creating a new Share, we create the qgroup for it. And during # it's creation, we assign this qgroup to it. During it's creation a # 0/x qgroup will automatically be created, but it will become the # child of our explicitly-created qgroup(2015/x). # We will set the qgroup limit on our qgroup and it will enforce the # quota on every subvolume(i.e., Share and Snapshot) in that qgroup. # When a Share is deleted, we need to destroy two qgroups. One is it's # auto 0/x qgroup and the other is our explicitly-created 2015/y # qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = 'Pool ({}) does not exist.'.format(pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Invalid characters in share name. Following are ' 'allowed: letter(a-z or A-Z), digit(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): # btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters.') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): # Note e_msg is consumed by replication/util.py create_share() e_msg = ('Share ({}) already exists. Choose a ' 'different name.').format(sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A pool with this name ({}) exists. Share ' 'and pool names must be distinct. Choose ' 'a different name.').format(sname) handle_exception(Exception(e_msg), request) replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): # TODO: confirm this 'type' call works as format parameter. e_msg = ('Replica must be a boolean, ' 'not ({}).').format(type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) # The following pool.save() was informed by test_share.py pool.save() s.save() if pqid is not PQGROUP_DEFAULT: update_quota(pool, pqid, size * 1024) share_pqgroup_assign(pqid, s) mnt_pt = '%s%s' % (settings.MNT_PT, sname) if not s.is_mounted: mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): #qgroup notes for shares. we need to create a qgroup prior to share #creation. qgroup ids 0/<subvol_id> automatically get created when a #subvolume(i.e., a Share or a Snapshot) is created. So let's create a #new qgroup: 2015/<some_number> whenever a Share is #created. <some_number> starts from 1 and is incremented as more Shares #are created. So, for the very first Share in a pool, it's qgroup will #be 1/1. 2015 is arbitrarily chose. #Before creating a new Share, we create the qgroup for it. And during #it's creation, we assign this qgroup to it. During it's creation a 0/x #qgroup will automatically be created, but it will become the child of #our explicitly-created qgroup(2015/x). #We will set the qgroup limit on our qgroup and it will enforce the #quota on every subvolume(i.e., Share and Snapshot) in that qgroup. #When a Share is deleted, we need to destroy two qgroups. One is it's #auto 0/x qgroup and the other is our explicitly-created 2015/y qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): #btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) update_quota(pool, pqid, size * 1024) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): #qgroup notes for shares. we need to create a qgroup prior to share #creation. qgroup ids 0/<subvol_id> automatically get created when a #subvolume(i.e., a Share or a Snapshot) is created. So let's create a #new qgroup: 2015/<some_number> whenever a Share is #created. <some_number> starts from 1 and is incremented as more Shares #are created. So, for the very first Share in a pool, it's qgroup will #be 1/1. 2015 is arbitrarily chose. #Before creating a new Share, we create the qgroup for it. And during #it's creation, we assign this qgroup to it. During it's creation a 0/x #qgroup will automatically be created, but it will become the child of #our explicitly-created qgroup(2015/x). #We will set the qgroup limit on our qgroup and it will enforce the #quota on every subvolume(i.e., Share and Snapshot) in that qgroup. #When a Share is deleted, we need to destroy two qgroups. One is it's #auto 0/x qgroup and the other is our explicitly-created 2015/y qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): #btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ( 'A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) update_quota(pool, pqid, size * 1024) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)