在Linux中检测配额的块大小

时间:2023-01-26 00:36:48

The limit placed on disk quota in Linux is counted in blocks. However, I found no reliable way to determine the block size. Tutorials I found refer to block size as 512 bytes, and sometimes as 1024 bytes.

Linux中磁盘配额的限制以块为单位计算。但是,我发现没有可靠的方法来确定块大小。我发现的教程将块大小称为512字节,有时称为1024字节。

I got confused reading a post on LinuxForum.org for what a block size really means. So I tried to find that meaning in the context of quota.

我很困惑在LinuxForum.org上阅读一篇关于块大小真正含义的帖子。所以我试图在配额的背景下找到这个含义。

I found a "Determine the block size on hard disk filesystem for disk quota" tip on NixCraft, that suggested the command:

我在NixCraft上找到了“确定磁盘配额的硬盘文件系统上的块大小”提示,提示命令:

dumpe2fs /dev/sdXN | grep -i 'Block size'

or

blockdev --getbsz /dev/sdXN

But on my system those commands returned 4096, and when I checked the real quota block size on the same system, I got a block size of 1024 bytes.

但是在我的系统上,这些命令返回4096,当我在同一系统上检查实际配额块大小时,我得到了1024字节的块大小。

Is there a scriptable way to determine the quota block size on a device, short of creating a known sized file, and checking it's quota usage?

是否有一种可编写脚本的方法来确定设备上的配额块大小,而不是创建一个已知大小的文件,并检查它的配额使用情况?

1 个解决方案

#1


7  

The filesystem blocksize and the quota blocksize are potentially different. The quota blocksize is given by the BLOCK_SIZE macro defined in <sys/mount.h> (/usr/include/sys/mount.h):

文件系统块大小和配额块大小可能不同。配额块大小由 (/usr/include/sys/mount.h)中定义的BLOCK_SIZE宏给出:

#ifndef _SYS_MOUNT_H
#define _SYS_MOUNT_H    1

#include <features.h>
#include <sys/ioctl.h>

#define BLOCK_SIZE      1024
#define BLOCK_SIZE_BITS 10
...

The filesystem blocksize for a given filesystem is returned by the statvfs call:

statvfs调用返回给定文件系统的文件系统块大小:

#include <stdio.h>
#include <sys/statvfs.h>

int main(int argc, char *argv[])
{
    char *fn;
    struct statvfs vfs;

    if (argc > 1)
        fn = argv[1];
    else
        fn = argv[0];

    if (statvfs(fn, &vfs))
    {
        perror("statvfs");
        return 1;
    }

    printf("(%s) bsize: %lu\n", fn, vfs.f_bsize);

    return 0;
}

The <sys/quota.h> header includes a convenience macro to convert filesystem blocks to disk quota blocks:

头包含一个便捷宏,用于将文件系统块转换为磁盘配额块:

/*
 * Convert count of filesystem blocks to diskquota blocks, meant
 * for filesystems where i_blksize != BLOCK_SIZE
 */
#define fs_to_dq_blocks(num, blksize) (((num) * (blksize)) / BLOCK_SIZE)

#1


7  

The filesystem blocksize and the quota blocksize are potentially different. The quota blocksize is given by the BLOCK_SIZE macro defined in <sys/mount.h> (/usr/include/sys/mount.h):

文件系统块大小和配额块大小可能不同。配额块大小由 (/usr/include/sys/mount.h)中定义的BLOCK_SIZE宏给出:

#ifndef _SYS_MOUNT_H
#define _SYS_MOUNT_H    1

#include <features.h>
#include <sys/ioctl.h>

#define BLOCK_SIZE      1024
#define BLOCK_SIZE_BITS 10
...

The filesystem blocksize for a given filesystem is returned by the statvfs call:

statvfs调用返回给定文件系统的文件系统块大小:

#include <stdio.h>
#include <sys/statvfs.h>

int main(int argc, char *argv[])
{
    char *fn;
    struct statvfs vfs;

    if (argc > 1)
        fn = argv[1];
    else
        fn = argv[0];

    if (statvfs(fn, &vfs))
    {
        perror("statvfs");
        return 1;
    }

    printf("(%s) bsize: %lu\n", fn, vfs.f_bsize);

    return 0;
}

The <sys/quota.h> header includes a convenience macro to convert filesystem blocks to disk quota blocks:

头包含一个便捷宏,用于将文件系统块转换为磁盘配额块:

/*
 * Convert count of filesystem blocks to diskquota blocks, meant
 * for filesystems where i_blksize != BLOCK_SIZE
 */
#define fs_to_dq_blocks(num, blksize) (((num) * (blksize)) / BLOCK_SIZE)