I am trying to create large dummy files on a drive using dd. I am currently doing this:
#!/bin/bash
writeFile(){ #$1 - destination directory/filename, $2 - source filepath $3 - blocksize, $4 - blockcount $5 - log file name
if [ "$#" -ne 5 ]; then
echo "Bad number of args - Should be 4, not $#"
return 1;
fi
dest_filepath=$1
src_filepath=$2
block_size=$3
block_count=$4
log_file=$5
int_regex='^[0-9]+$'
file_size=$(($block_size * $block_count))
src_file_size=`ls -l $src_filepath | awk '{print $5}'`
full_iter=0
while [[ $file_size -ge $src_file_size ]]; do
file_size=$((file_size - $src_file_size))
full_iter=$((full_iter + 1))
done
section_block_count=$(($src_file_size / $block_size))
echo $section_block_count $block_size
topping_off_block_count=$(($file_size / $block_size))
dest_dir=$(dirname $dest_filepath)
if [ -d "$dest_dir" ] && [ -r $src_filepath ] && [[ $block_size =~ $int_regex ]] && [[ $block_count =~ $int_regex ]]; then
data_written=0
for (( i=0 ; i < $full_iter ; i=$((i+1)) )); do
(time dd of=$dest_filepath if=$src_filepath bs=$block_size count=$section_block_count seek=$data_written) >> $log_file 2>&1 #Output going to external file
data_written=$(($data_written + $src_file_size +1 ))
echo $data_written
done
if [[ $file_size -gt 0 ]]; then
(time dd of=$dest_filepath if=$src_filepath bs=$block_size count=$topping_off_block_count seek=$data_written) >> $log_file 2>&1 & #Output going to external file
fi
return 0;
fi
return 1;
}
However, this isn't working, as it's either only writing from the src_filepath
once, or writing over the same part of the file multiple times, I don't know how to find out the difference. In this particular case, what I'm doing is writing from a 256MB file 4 times to create a single 1GB file, but I want to keep it generic so that I can write any size from and to.
The aim is to fragment a hard drive, and measure the output of dd (rate of transfer specifically) and the time it took.
I am on an embedded system with limited functionality, and the OS is a very but down version of linux using busybox.
How do I alter this so that it will write the correct size file?
cat
the file? Something likefor i in a b c d; do cat $file1 >> $file2; done
? You seem to have chosen an extremely complex way to get this done, what is your actual objective? – terdon Oct 28 '14 at 15:34conv=notrunc
to thedd
lines. – Mark Plotnick Oct 28 '14 at 16:00conv
-.- – Yann Oct 28 '14 at 16:04bash
but it should also be mentioned in the question since you have no shebang line). – terdon Oct 28 '14 at 16:23src_filepath
only once, or writing over the same part of the file multiple times, I don't know how to find out the difference.” It’s writing over the same part of the file multiple times. You can debug things like this by inserting aset -x
command before any statements whose execution you want to monitor. See How to debug a bash script? – G-Man Says 'Reinstate Monica' Oct 28 '14 at 16:32seek=
option todd
, if your version supports it. (3) Rather thandd of=$dest_filepath … >> $log_file 2>&1
, dodd … >> $dest_filepath 2>> $log_file
. (4) A bit of general advice: always quote your shell variable references (e.g.,"$dest_filepath"
and"$log_file"
) unless you have a good reason not to, and you’re sure you know what you’re doing. – G-Man Says 'Reinstate Monica' Oct 28 '14 at 16:33seek
. I'm not sure I follow your third point, and it's getting a little long for a comment, why not explain it in an answer? – Yann Oct 28 '14 at 16:37conv=notrunc
,dd
will truncate the output file every time it's run. Can you get a traditionaldd
executable for your system? – Mark Plotnick Oct 28 '14 at 16:58dd <<IN >file\n$(cat file file file file file)\nIN
. If you have atee
at your disposal which will handle-
args, thentee <file - - - -
would work well there too. – mikeserv Dec 04 '14 at 11:54