# S.Chudley (www.chudley.me) : Script to fill up a drive, leaving a given number of bytes free my $leave = $ARGV[0]; if ($leave eq "") { print "Specify amount of space to leave (as bytes).\n"; exit(1); } print "Filling buffer...\n"; $block_size = 1024 * 1024; my $data_buffer = ""; for (my $i=0;$i!=$block_size;$i++) { $data_buffer .= " "; } if ($leave >= 2000 * $block_size) { print "Can't leave more than ".(2000 * $block_size / 1024)." kb free!\n"; exit(1); } my $free_space = &get_free(); print "Drive has ".($free_space/1024)." kb free...\n"; if ($free_space >= 2097152000) { print "Making a big file (2048 MB) called file.big..."; &make_file("file.big", 2000 * $block_size); print " Done.\n"; } $free_space = &get_free(); print "Drive now has ".($free_space/1024)." kb free...\n"; if ($free_space <= $leave) { print "Arn't $leave bytes free anyway!\n"; exit(1); } my $multi = int($free_space / (2000 * $block_size)); print "Spinning out $multi copies of this file (total ".(2000 * 1024 * $multi)." kb)...\n"; for (my $i=0;$i!=$multi;$i++) { `copy file.big file.$i.big`; if ($? != 0) { print "Oops? File write failed?\n"; exit(1); } my $pc = ($i / $multi) * 100; $pc = substr($pc, 0, 5); print " Created ".($i + 1)." files [".$pc." %]\n"; } $free_space = &get_free(); print "Drive now has ".($free_space/1024)." kb free...\n"; my $remains = $free_space - $leave; print "Consuming up remaining ".($remains / 1024)." kbs... "; &make_file("remaining.big", $remains); print "Done\n"; $free_space = &get_free(); print "Drive now has ".($free_space/1024)." kb free...\n"; if ($free_space ne $leave) { print "Erm? Something went wrong\n"; exit(1); } exit(0); sub make_file { my ($name, $size) = @_; open(OUT,">$name") or die "Can't create file '$name'!\n"; my $done = 0; my $blocks = int($size / $block_size); while ($done < ($blocks * $block_size)) { print OUT $data_buffer or die "Oops? Write failed in make_file()?"; $done += $block_size; } for (my $i=$done;$i!=$size;$i++) { print OUT " " or die "Oops? Write failed in make_file()?"; } close(OUT); } sub get_free { my @free_txt = `dir /-C`; my $free = 0; foreach (@free_txt) { if ($_ =~ m/\s+([0-9,]+)\s+bytes\s+free/) { $free = $1; $free =~ s/,//g; } } return $free; }