LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to break copy process when folder size reaches its maximum (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-break-copy-process-when-folder-size-reaches-its-maximum-869591/)

dinakumar12 03-19-2011 07:02 AM

how to break copy process when folder size reaches its maximum
 
Hi all,

I have written a script.

#/bin/bash
a=`du -sh /root/samplefolder | cut -f1`
echo $a > testfile
sed -i 's/M//g' testfile
b=`cut -f1 testfile`
if [ $b -ge 20 ]
then
echo size of samplefolder reaches 20M so cannot copy files to that folder
chattr +i /root/samplefolder
else
echo size for samplefolder is lessthan 20M
fi

The aim of this script is, when the folder reaches 20M then attributes will be set to that particular folder so that no newfiles and folders cannot be created or copied to that samplefolder.

i run the script every second with the help of LQ member suggestion.

whenever i copy a file morethan 20M to that folder its getting copied fully and then the attributes were applied.

But i dont want this to happen, when the folder reaches its maximum current write operation to that folder should be stopped automatically with a error.

How to do this.

Kindky post your suggestions.


Thanks in advance,
Dinesh.

tredegar 03-19-2011 08:42 AM

I think you are going about this the wrong way.

What you can do is create and mount a filesystem of size 20MB.
You can then copy files to it until it reaches 20MB and then the last operation will fail because "no space left on device".

This is more efficient than running a script every second.

As root:

Code:

# create a 20MB file
dd if=/dev/zero of=vfs bs=1M count=20
# make a filesystem, with no reserved space, in that file
mkfs.ext3 -m 0  vfs
# create a mountpoint
mkdir /mnt/max20M
# mount the file
mount -t ext3 -o loop vfs /mnt/max20M
# let anyone access it
chmod 777 /mnt/max20M

Now you have your "folder" at /mnt/max20M that cannot hold more than 20MB of files. Actually the number will be a little less than 20MB because the filesystem itself takes up some of that space. I'll let you work out how to adjust the dd command if you want exactly 20MB of storage space.

eSelix 03-19-2011 08:53 AM

Or use some quota tool, it was created for that purpose.

dinakumar12 03-19-2011 10:28 AM

hi tredegar,

If i need to extend the size can i do that with out un mounting. If possible kindly post me the code.

tredegar 03-19-2011 10:45 AM

Quote:

If i need to extend the size can i do that with out un mounting.
No, you can't.

dinakumar12 03-19-2011 10:48 AM

thanks tredegar for your suggestion.


All times are GMT -5. The time now is 08:06 PM.