#!/bin/bash
# ---
# this will write 0s across a drive at random locations (using dd)
# you specify the details in the variables location
# ---
# fill out these variables & copy paste script & it will not hurt (it will not do the writes)
# For security purposes:L you will see the dd commands output (but they will not run)
# To run them just select and copy the dd commands, and paste them back into the shell to run them
# ---
# You could also run the script and save it to a file
# first edit ddestroy.sh to have the correct variables, then save and exit, and run the code like this:
# $ ./ddestroy.sh > ddcommands
# this will save all of the dd commands (and comments of the output) into the ddcommands file
# now we just run the ddcommands file to execute those commands
# since the output ddcommands, will not have +x bit, you can just run it with the bash "source" commands
# $ source ddcommnds
(#####################
### SET VARIABLES ###
### ALL IN MiBs #####
### Megabytes #######
#####################
DISK=sda
START_LOCATION=0
MAX_SIZE_OF_A_WRITE=1000
MIN_NUM_OF_WRITES_TO_DISK=24
## example:
## DISK=sda
## START_LOCATION=0
## MAX_SIZE_OF_A_WRITE=1000
## MIN_NUM_OF_WRITES_TO_DISK=12
## This will write to disk sda, from the beginning due to the 0 START_LOCATION
## 1 gig writes due to MAX_SIZE_OF_A_WRITE (max, but they will vary in size)
## at about 12 locations due to MIN_NUM_OF_WRITES_TO_DISK
## but since thats random too, it might be more locations)
######################
### start of code ####
######################
SIZE=`cat /sys/block/$DISK/size`
echo "# Size of disk '$DISK' is $SIZE sectors"
SIZE_MB=`echo $SIZE | awk '{printf("%d",$1/2/1024)}'`
echo "# Size of disk '$DISK' is $SIZE_MB megabytes"
### INPUT TO AWK: start location, write size max (but they all are random), max size of drive - all in MB, name of disk, min_number_of_writes_to_disk
echo "$START_LOCATION $MAX_SIZE_OF_A_WRITE $SIZE_MB $DISK $MIN_NUM_OF_WRITES_TO_DISK" | awk '{
srand();
start=int($1);
wrmax=int($2);
ms=int($3);
name_of_drive=$4;
cu=start;
range1=ms-start;
min_number_of_writes=$5;
random_division=min_number_of_writes/2;
print "### WRITING AT RANDOM LOCATIONS (all units in MiB) ###"
print "# - start_location: " cu;
print "# - size_of_disk: " ms;
print "# - max_random_size_write: " wrmax;
print "# - min_number_of_total_writes: " min_number_of_writes;
print "# - drive: " name_of_drive;
i=1;
while (cu<ms)
{
wr=int(wrmax*rand())+1; # random write amount
printf("# write number : %d - current: %d", i, cu);
ri=int(rand()*(range1/random_division));
printf(", skipping: %d, writing: %d\n", ri, wr);
printf("dd if=/dev/zero of=/dev/%s bs=1M skip=%d count=%d\n", name_of_drive, cu, wr);
cu=cu+ri+wr;
i=i+1;
}}')