Skip to content

Instantly share code, notes, and snippets.

@alekratz
Created August 13, 2020 22:44
Show Gist options
  • Save alekratz/998731fb8051eef559dda70b137514af to your computer and use it in GitHub Desktop.
Save alekratz/998731fb8051eef559dda70b137514af to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create some randomly-populated files of varying sizes in the given directory,
# up to a maximum summed size.
min=512
max=$((1024 * 1024))
size=$((1024 * 1024 * 1024))
location="$PWD"
sleep_between=0
#count=1024
verbose=0
usage() {
echo "usage: $0 [OPTIONS] [LOCATION]"
echo
echo "The LOCATION positional argument is used to determine where to create"
echo "these files. Default is the current working directory."
echo
echo "options:"
echo
echo "--min X minimum number of bytes per file (default: $min)"
echo "--max X maximum number of bytes per file (default: $max)"
echo "--size X maximum apparent size allowed of the given location (default: $size)"
echo "--sleep X sleep for X seconds between creating each file (default: $sleep_between)"
#echo "--count X maximum number of files allowed in the location (default: $count)"
echo "-v,--verbose verbose output"
}
dirsize() {
local loc
loc="$1"
# -x is "one filesystem" flag
du -b -x -s "$loc" 2>/dev/null | cut -f 1
}
dehumanize() {
awk '/[0-9]$/{print $1;next};/[gG]/{printf "%u\n", $1*(1024*1024*1024);next;}/[mM]$/{printf "%u\n", $1*(1024*1024);next};/[kK]$/{printf "%u\n", $1*1024;next}' <<< "$1"
}
#filecount() {
# local loc
# loc="$1"
# find "$loc" -type f -maxdepth 1 2>/dev/null | wc -l
#}
percent() {
local loc
local size
loc="$1"
size="$2"
printf "%0.2f%%\r" "$(bc -l <<< "($(dirsize "$loc") / $size) * 100")"
}
options=$(getopt -o v -q --long min:,max:,size:,sleep:,verbose -- "$@")
if [[ "$?" != 0 ]]; then
usage
exit 1
fi
eval set -- "$options"
while true; do
case "$1" in
--max)
shift
max="$(dehumanize "$1")"
;;
--min)
shift
min="$(dehumanize "$1")"
;;
--size)
shift
size="$(dehumanize "$1")"
;;
--sleep)
shift
sleep_between="$1"
;;
#--count)
# shift
# count="$1"
# ;;
--verbose|-v)
verbose=1
;;
--)
shift
[[ ! -z "$1" ]] && location="$1"
break
;;
esac
shift
done
# Check args, make sure everything makes sense
if (( "$max" < "$min" )); then
echo "Max size specified is less than min"
exit 1
fi
if [[ ! -d "$location" ]]; then
echo "$location is not a directory"
exit 1
fi
if (( $verbose )); then
echo "LOCATION : $location"
echo "MAX : $max"
echo "MIN : $min"
echo "SIZE : $size"
echo "SLEEP : $sleep_between"
#echo "COUNT : $count"
fi
echo "Creating files in $location allowing the usage on the filesystem to not exceed $size bytes"
while (( "$(dirsize "$location")" < "$size" || "$size" < 0 )); do
filesize="$(shuf -i "$min"-"$max" -n 1)"
filename="$(mktemp -p "$location")"
if (( $verbose )); then
echo "Creating $filename with $filesize ($(numfmt --to=iec-i --suffix=B $filesize)) random bytes"
elif (( $size < 0 )); then
printf "%s\r" "$(du -sh "$location" | cut -f 1)"
else
percent "$location" "$size"
fi
dd if=/dev/random of="$filename" bs="$filesize" count=1 2>/dev/null
sleep "$sleep_between"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment