#!/usr/bin/perl use strict; use File::Which; my $exe_path = which 'parted'; if(!$exe_path) { print "This program requires the parted program to be installed and in your path\n"; exit(-1); } $exe_path = which 'hdparm'; if(!$exe_path) { print "This program requires the hdparm program to be installed and in your path\n"; exit(-1); } if(!$ARGV[0] || ! -b $ARGV[0]) { print "Usage is:\n"; print " $0 /dev/sdX\n"; exit(-1); } open INFO,"parted $ARGV[0] print|" or die "Error running parted $ARGV[0] print: $!\n"; print "ARE YOU SURE YOU WANT TO ERASE THE FOLLOWING DRIVE:\n\n"; while() { print; } close INFO; print "\nTYPE \"YES\" TO COMPLETELY ERASE THE DRIVE SHOWN ABOVE:\n"; my $reply=; chomp $reply; if($reply ne "YES") { print "\nAborting...\n"; exit(-1); } open INFO,"hdparm -I $ARGV[0]|" or die "Error running hdparm -I $ARGV[0]: $!\n"; my $sectors; while(my $line=) { chomp $line; if ($line =~ /LBA48 user addressable sectors:\s+(\d+)/) { $sectors=$1; } } close INFO; if(!$sectors) { print "Unable to determine the number of sectors\n"; exit(-1); } open MOUNTS,"/proc/mounts" or die "Error opening /proc/mounts: $!\n"; while(my $line=) { if ($line =~ /^$ARGV[0]/) { print "Error - possible mounted disk detected:\n"; print $line; exit(-1); } } print "Issuing TRIM commands\n"; open TRIM,"|hdparm --trim-sector-ranges-stdin --please-destroy-my-drive $ARGV[0]" or die "Unable to run hdparm --trim-sector-ranges-stdin --please-destroy-my-drive $ARGV[0]: $!\n"; for(my $i=0; $i < $sectors; $i+=65535) { my $length=$sectors-$i; $length=65535 if $length > 65535; print TRIM "$i:$length\n"; } close TRIM; print "Syncing\n"; system("sync");