0

I want to enable size restrictions, which means I want to resize few folders for users like Desktop, Downloads or Music folders. If a user exceeded the memory then it shouldn't overwrite and gets the warning message.

How to resize the folders through Ansible ? It's for many hosts and directly on the system with root permission. I want to do this on Ubuntu 14.04 Desktop edition.

  • Probably the easiest way is to set a quota on their home directories and let them figure out what they want to save where, rather than monitoring all the sub directories. digitalocean.com/community/tutorials/… and thegeekstuff.com/2010/07/disk-quota – Panther Jun 2 '18 at 21:31
  • Thanks for your reply @Panther, i have one more doubt, if I will implement quota for the home directory will that impact to the hidden folders ( like if someone uses thunderbird for mailing and the file location is /home/.thunderbird which is hidden ) if yes then how to figure out not to impact the hidden folders – Rajendra Jun 2 '18 at 22:19
  • Yes it will affect hidden directories. Look, you can either set a quota per user per directory (lots of work) or set a reasonable quota for all of home (much less work). Hidden directories should not be that large . – Panther Jun 2 '18 at 23:42
  • I can understand that qquota per user per directory is a big headache, actually, my users are using thunderbird and few more directories about 20/30 GBs. so I don't want to restrict any of the hidden directories, I just want to restrict few directories with lesser memory. Please tell me if its possible@Panther – Rajendra Jun 3 '18 at 19:02
1

Based on the accepted answer at How to set limit on directory size in Linux? by Sergei Nikulov and the original tutorial, I've put together a script which can create a quota-limited directory on demand.

The script operates on the premise of creating a loop device with specific size and filesystem, and mounting the loop device at the user-defined directory.

Script

Also available on GitHub. Further development will be done there.

#!/usr/bin/env bash
# Author: Serg Kolo
# Date: June 1, 2018
# Written for: https://askubuntu.com/q/1043035/295286
# Based on: https://www.linuxquestions.org/questions/linux-server-73/directory-quota-601140/

set -e

print_usage(){

cat <<EOF
Usage: sudo mklimdir.sh -m <Mountpoint Directory> -f <Filesystem> -s <INT>

-m directory
-f filesystem type (one of supported by mke2fs)
-s size in bytes
-h this message

Exit statuses:
0:
1: Invalid option
2: Missing argument
3: No args
4: root privillege required
EOF
} > /dev/stderr

parse_args(){
    #set -x

    option_handler(){

        case ${opt} in
            m) mountpoint=$( realpath -e "${OPTARG}" );;
            s) size=${OPTARG} ;;
            h) print_usage; exit 0 ;;
            f) mkfs_cmd=mkfs."${OPTARG}" ;;
            \?) echo ">>>Invalid option: -$OPTARG" > /dev/stderr; exit 1;;
            \:) echo ">>>Missing argument to -${OPTARG}" > /dev/stderr; exit 2;;
        esac
    }

    local OPTIND opt
    getopts "m:s:f:h" opt || { echo "No args passed">/dev/stderr;print_usage;exit 3;}
    option_handler 
    while getopts "m:s:f:h" opt; do
         option_handler
    done
    shift $((OPTIND-1))

}


main(){

    if [ $EUID -ne 0 ]; then
        echo ">>> Please run the script with sudo/as root" > /dev/stderr
        exit 4
    fi

    local mountpoint=""
    local size=0
    local mkfs_cmd

    parse_args "$@"
    quota_fs=/"${mountpoint//\//_}"_"$(date +%s)".quota
    dd if=/dev/zero of="$quota_fs" count=1 bs="$size"
    "$mkfs_cmd" "$quota_fs"
    mount -o loop,rw,usrquota,grpquota "$quota_fs" "$mountpoint"

    chown $SUDO_USER:$SUDO_USER "$mountpoint"

}

main "$@"

Usage

There's 3 required flags:

  • -m for mountpoint, aka the directory which you want to limit
  • -s the size to which you want to limit in bytes
  • -f filesystem. If you're not sure what this should be, just stick with ext4 or ext3

Here's an example of how I used this script to create ./quoted_dir limited to 1 MiB (that is 10242 in Binary Prefix )

sudo ./mklimdir.sh -m ./quoted_dir/ -s $((1024*1024)) -f ext4

I've tested the script with while true; do cat /etc/passwd >> ./quoted_dir/passwd; sleep 1; done type of loop, which would append contents of /etc/passwd to the file inside the quote-limited directory. The cat eventually errored out with:

cat: write error: No space left on device

and the file writing stopped at 909KiB which is just a little short of 1 MiB and would prevent exceeding the limits within the directory.

Further development

For permanent effect, the loop device created by the script should be added to /etc/fstab. This may be added later on GitHub. The script operates on a single directory, however this script can be used within another script for creating multiple limited directories, so it is flexible enough.

  • Just for my understanding: this means when I want 20 directories with quotas then I'll need 20 filesystems and they pre-occupy 20×$size, i.e. the quota is also the guaranteed size, not just the max. size, right? – PerlDuck Jun 3 '18 at 10:00
  • @PerlDuck Yes, It means 20 filesystems per each folder. I'm not sure what do you mean about guaranteed vs max.size, though. – Sergiy Kolodyazhnyy Jun 3 '18 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.