Script to fill the file system mount with empty files to desired size

We are regularly using for our testing, where we are manually filling up the mount with desired size with following command

dd if=/dev/zero of=file_2GB bs=2048000 count=2000

We are planning to automate the task where taking input for % [(say 70% of mount size)]of size as one input and the name of the file system mount as other input. Could you please help me on this.

Thanks,
Chandra

threshold=80
FStoFill=/tmp
set -- $(df -k $FStoFill | tail -1)
dd if=/dev/zero of=$FStoFill/zerofile.$$ bs=1k count=$((($threshold*$2/100)-$3))

But the dd command above may be set up to be faster (search for the msg posted by Alister )

The idea was to use /dev/null instead, as well as seek to avoid a bunch of useless zero writting so something like :

dd if=/dev/null of=dummy seek=37580963840 bs=1

Just tweak it in a way you can replace the dd if=/dev/zero... of the first code with the more optimized dd if=/dev/null ... command

1 Like

Hello,

I tried to run the below code and getting the following error. could you please help.

dd: bad arg -5211234
Usage: dd [if=InputFile] [of=OutputFile] [cbs=Number] [fskip=Number]
[skip=Number] [seek=Number] [count=Number] [bs=Number] [span=yes|no]
[ibs=Number] [obs=Number] [files=Number] [conv=Parameter[, ...]]

Here is the code that i am using.

#!/bin/ksh
threshold=20
FStoFill=/tmp
set -- $(df -k $FStoFill | tail -1)
dd if=/dev/zero of=$FStoFill/zerofile.$$ bs=1 count=$((($threshold*$2/100)-$3))

What arguments are you running the script with?

what output does

df -k yourfilesystem | tail -1

give ?

Maybe this will be more reliable :

threshold=70
FStoFill=/tmp
set -- $(df -k $FStoFill | tail -1 | awk '{$1=$1;print $(NF-4),$(NF-3)}')
dd if=/dev/zero of=$FStoFill/dummy bs=1K count=$((($threshold*$1/100)-$2-1064))

Keep some space of 1064K in case threshold is set at 100% (i dont know why this 1064 number but it worked fine so...).
You should add some test on the threshold : (should not be set higher than 100%, and should not be set lower than current use)

1 Like

Here is the output i am getting for the below command

 df -k /tmp | tail -1
/dev/hd3          5767168   5557264    4%      811     1% /tmp

Please let me know wt are the expected parameters. i was running with out parameters as the threshold and filesystem has been added in the script.

Thanks,
Chandra

In :

count=$((($threshold*$1/100)-$2-1064))

$1 is supposed to refer to the total number of 1K block
$2 is supposed to refer to the number of block currently in use

So you should adapt the code to make the right calculation:

set -- $(df -k $FStoFill | tail -1 | awk '{$1=$1;print $(NF-5),$(NF-5)-$(NF-4)}')
dd if=/dev/zero of=$FStoFill/dummy bs=1K count=$((($threshold*$1/100)-$2-1064))
1 Like

Might be simpler to let awk do the calculation, and let the script's input parameters be used in the normal sense.
This code looks longer, because I added lots of checking, but the functional part is more concise.

usage() { 
  echo "Usage: $0 mountpoint threshold "
  echo "   where threshold is 1 to 100"
}

# $1 is the mountpoint
# $2 is the threshold (whole number, 1 to 100)

if [ ! -d $1 ]; then
  usage; exit 2;
fi
if [ $2 -lt 1 -o $2 -gt 100 ] ;then
  usage; exit 2;
fi

df -k $1 |
 awk -v t=$2     'NR==2  { print int($4 * (t-int($5))/100 ); }'  |
{
# The pipe-brace here in ksh isn't necessary, but in other sh-based shells, it is.
# 
read blocks
if [ -z $blocks -o $blocks -le 0 ] ; then
   echo "Error: Disk is already filled above threshold!"
   exit 1
else
   dd if=/dev/zero of=$1/.fillfile.$$ bs=1k count=$blocks
   exit $?
fi
}

I gave this a test under Darwin with bash.

You could make a really simple script with this:

df -k $1 |  awk -v t=$2  'NR==2  { print int($4 * (t-int($5))/100 ); }'  |
{ read blocks;  dd if=/dev/zero of=$1/.fillfile.$$ bs=1k count=$blocks }

If blocks is negative (your threshold is too low), dd will report an error. If it's 0, it will essentially do nothing (but still create the file).

You could do it for ALL mounted filesystems with this modification:

df -k |  awk -v t=$2  'NR>1  { print $NF,int($4 * (t-int($5))/100 ); }'  |
while read mountpoint blocks;  dd if=/dev/zero of=$mountpoint/.fillfile.$$ bs=1k count=$blocks ; done
1 Like

Thank you all..This thread can closed now!!