hackbyte

randfs.pl from http://home.ircnet.de/cru/luks/dist/randfs

Oct 2nd, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.90 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # from http://home.ircnet.de/cru/luks/dist/randfs
  4. #
  5.  
  6. use strict;
  7. use warnings;
  8. use IO::File;
  9.  
  10. $|=1;
  11.  
  12. my $file;
  13.  
  14. sub randomString(){
  15.     my $fetch=(int(rand(512))+512)*16384;
  16.     my $data='';
  17.     my $packet;
  18.     my $fh=new IO::File('/dev/urandom','r')
  19.         || die("Unable to open /dev/urandom: $!\n");
  20.     binmode($fh);
  21.     while(length($data) < $fetch){
  22.         printf("\rGenerating %d bytes urandom string... %d%%",$fetch,(length($data)*100/$fetch));
  23.         read($fh,$packet,8192);
  24.         $data.=$packet;
  25.     }
  26.     $fh->close();
  27.     printf("\rGenerating %d bytes urandom string... done\n",$fetch);
  28.     return($data);
  29. }
  30.  
  31. sub fillDisk($){
  32.     my $string=shift;
  33.     my $written=0;
  34.     my $size=length($string);
  35.     my $started=time;
  36.     my $mbytes;
  37.     $SIG{TERM}=$SIG{INT}=sub{
  38.             print("\n");
  39.             removeFile();
  40.             exit(1);
  41.         };
  42.     my $fh=new IO::File($file,'w')
  43.         || die("Unable to open $file: $!\n");
  44.     binmode($fh);
  45.     while(print($fh $string)){
  46.         $written+=$size;
  47.         $mbytes=$written/1048576;
  48.         printf("\rWriting growing file to disk... %d MB (%d MB/s)   ",$mbytes,($mbytes/((time-$started)||1)));
  49.     }
  50.     $fh->close();
  51.     print("\n");
  52. }
  53.  
  54. sub syncDisk(){
  55.     print('Writing cached data to disk... ');
  56.     system('/bin/sync');
  57.     print("done\n");
  58. }
  59.  
  60. sub removeFile(){
  61.     print('Removing the growing file from disk... ');
  62.     unlink($file);
  63.     system('/bin/sync');
  64.     print("done\n");
  65. }
  66.  
  67. sub showUsage(){
  68. print(<<__EOF);
  69. This programm will generate a urandom string of 8 to 16 MBytes,
  70. repeatedly write this into the given file until writing becomes impossible
  71. and remove the file from disk.
  72.  
  73. The given file must NOT exist!
  74.  
  75. Usage: $0 <filename>
  76.  
  77. __EOF
  78.     exit(1);
  79. }
  80.  
  81. sub main(){
  82.     $file=$ARGV[0];
  83.     print("Randomize Free Space, (C)2006 Veit Wahlich <cru at zodia dot de>\n\n");
  84.     unless(defined($file) && $file=~/^[^\-]/ && not(-e $file)){
  85.         showUsage();
  86.     }
  87.     fillDisk(randomString());
  88.     syncDisk();
  89.     removeFile();
  90.     exit(0);
  91. }
  92.  
  93. main();
  94. 1;
Add Comment
Please, Sign In to add comment