定额分配硬盘空间

由于大家共享实验室的服务器,如果每个人都任意占用硬盘空间,多大的硬盘空间肯定都是不够用的(just like the first harddisk),所以必需在共享的服务器上建立磁盘定额分配策略。

安装

sudo apt-get install quota

配置

编辑/etc/fstab,在所要配置quota的磁盘分区中添加usrquota, grpquota选项。

/dev/sdb1   /mnt/hdd2   ext4    defaults,usrquota,grpquota 1  2

重新挂载该磁盘分区:

mount -o remount /dev/sdb1 /mnt/hdd2

在配置quota的磁盘分区的根目录中建立aquota.group, aquota.user, quota.group, quota.user文件

touch aquota.group aquota.user quota.group quota.user

执行quotacheck扫描

sudo quotacheck -avug
quotacheck: Cannot find filesystem to check or filesystem not mounted with quota option.

这一步容易出现上面的问题,所以这里可以强制执行quotacheck扫描:

sudo quotacheck -cfmvF vfsv0 /mnt/hdd2
quotacheck: Scanning /dev/sdb1 [/mnt/hdd2]
|/-|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\
.
.
|/-|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\
done
quotacheck: Checked 60202 directories and 495168 files

开启quota服务

sudo quotaon -av /mnt/hdd2

关闭可以用:

sudo quotaoff -av /mnt/hdd2

用户配置策略

这里我写了个脚本,根据.miduserdb和.supuserdb中的用户名称来对每个用户进行分配硬盘额度。

#!/bin/bash

quota_init() {
    awk -F: '{if($3>500) print $1>"./tmpfile"}' /etc/passwd
    for user in $(cat "./tmpfile")
    do
        miduser_flag=0
        for miduser in $(cat "/mnt/hdd2/yanan/.miduserdb")
        do
            if [ $user = $miduser ]; then
                miduser_flag=1
            fi
        done

        supuser_flag=0
        for supuser in $(cat "/mnt/hdd2/yanan/.supuserdb")
        do
            if [ $user = $supuser ]; then
                supuser_flag=1
            fi
        done

        if [ "$supuser_flag" -eq 1 ]; then
            echo $user is a super user, deal with it
            sudo setquota $user 500000000 520000000 0 0 /mnt/hdd2
        elif [ "$miduser_flag" -eq 1 ]; then
            echo $user is a miduser, no deal with it
            sudo setquota $user 300000000 320000000 0 0 /mnt/hdd2
        else
            echo $user is a general user, deal with it
            sudo setquota $user 100000000 120000000 0 0 /mnt/hdd2
        fi
    done
}

quota_help() {
    cat <<EOF
Usage: `basename $0` replace hexo with parameter "-d" and "-g".
options:
    -d display the info
    -i init the quota
    -h display this help menu
EOF
}

quota_info() {
    sudo repquota /mnt/hdd2
}

while getopts di option; do
    case "$option" in
        d) quota_info;;
        i) quota_init;;
        \?) quota_help
            exit 1;;
    esac
done

rm -rf ./tmpfile

其中.supuserdb中记录中级用户的用户名,配额为:500G
.miduserdb中记录中级用户的用户名,配额为:300G
其他用户为普通用户,配额为:100G

QA

QA1

Q. 为已存在的用户分配配额后删除该用户,在repquota报告中认为以之前用户ID的形式显示之前的配额。

A. 可以采用如下命令找到已删除用户的所有文件,删除掉即可:

find ./ -user delusr_id

QA2

Q. 磁盘定额配置中存在soft/hard配置之分,他们的区别,如果soft<用户所用磁盘<hard会如何?

A. 磁盘分配hard是绝对无法越过的磁盘占用,而磁盘占用超过soft则会为用户提出警告,在约定时间(默认7days)未能降到soft以下的则soft等同于hard,无法再继续创建文件,占用更多的空间。