quotactl的capi的简单python包装器
PyQuota的Python项目详细描述
pyquota
pyquota是一个简单的python包装器,用于quotactl的c api。
支持的内核版本:>=2.4.22, <5
。
C API中支持的命令:
Q_QUOTAON
Q_QUOTAOFF
Q_GETQUOTA
Q_GETNEXTQUOTA
(需要内核>;=4.6)Q_SETQUOTA
Q_GETINFO
Q_SETINFO
Q_GETFMT
Q_SYNC
目前,由于缺少文档和测试环境,不支持任何用于xfs文件系统的命令,例如Q_XQUOTAON
。
安装
pip install pyquota
用法
对于上面列出的每个受支持的命令,此包提供3个python方法,对应于用户配额、组配额和项目配额的操作。项目配额方法要求内核>;=4.1。
为了便于说明,这里只提供用户配额方法的示例。若要使用组/项目配额方法,只需将方法名称中的“user”替换为“group”或“project”。
# Import packageimportpyquotaaspq# Turn on user quota for a filesystempq.user_quota_on("/dev/sda1",pq.QFMT_VFS_V0,"/aquota.user")# device path, quota format, quota file path # quota format can be either pq.QFMT_VFS_OLD, pq.QFMT_VFS_V0 or pq.QFMT_VFS_V1.# Turn off user quota for a filesystempq.user_quota_off("/dev/sda1")# Get quota of a user on a filesystemquota=pq.get_user_quota("/dev/sda1",1000)# 1000 is the uid, returns a tuple of 8 integersblock_hard_limit=quota[0]# unit: disk quota block (1024 Bytes)block_soft_limit=quota[1]# unit: disk quota block (1024 Bytes)block_current=quota[2]# unit: block (1 Byte)inode_hard_limit=quota[3]inode_soft_limit=quota[4]inode_current=quota[5]block_time=quota[6]# time limit for excessive disk useinode_time=quota[7]# time limit for excessive files# Get quota of the next user, whose ID is greater than or equal to the specified ID, on a filesystemquota=pq.get_next_user_quota("/dev/sda1",1000)# returns a tuple of 9 integers. # The first 8 integers are the same as the result of pg.get_user_quota while the last integer is the user id. uid=quota[8]# Set quota of a user on a filesystempq.set_user_quota("/dev/sda1",1000,102400,92160,0,0)# hard block limit 100MB, soft block limit 90MB, no inode limits # Get information about the user quotafile for a filesysteminfo=pq.get_user_quota_info("/dev/sda1")# returns a tuple of 3 integersblock_grace=info[0]# time before block soft limit becomes hard limit. (unit: second)inode_grace=info[1]# time before inode soft limit becomes hard limit. (unit: second)flags=info[2]# flags for quotafileis_root_squash_enabled=bool(flags&pq.DQF_ROOT_SQUASH)is_stored_in_system_file=bool(flags&pq.DQF_SYS_FILE)# Set information about the user quotafile for a filesystempq.set_user_quota_info("/dev/sda1",604800,604800,0)# set both block grace and inode grace to 1 week (7*24*60*60), set flags as empty # Get quota format used for user quotas on a filesystemfmt=pq.get_user_quota_format("/dev/sda1")# returns an integer # fmt should be either pq.QFMT_VFS_OLD, pq.QFMT_VFS_V0 or pq.QFMT_VFS_V1# Update the on-disk copy of user quota usages for a filesystempq.sync_user_quotas("/dev/sda1")# Update the on-disk copy of user quota usages for all filesystems with active quotaspq.sync_user_quotas(None)
由于这个包只是C API的包装,它几乎保持了原始的风格和输入/输出格式。因此,如果您想了解这些命令的具体功能、参数的含义和返回值的含义,请阅读man page。
错误消息
来自C API的任何内部错误都将转换为一个pyquota.APIError
实例,该实例具有符合ERRORS section in the man page的文本描述。