#!/usr/bin/perl -w
use strict;

# zero-gpt.pl
# by Bill McGonigle <bill@bfccomputing.com>
# license is GPLv2+ or WTFPL

my $BLOCK_SIZE = 512; # bytes
my $GPT_SIZE = 34; # 512b blocks http://en.wikipedia.org/wiki/GUID_Partition_Table

my $device = shift;
if ( $device =~ m/^\/dev\/(.*)/ ) {
    $device = $1;
}

if ( !-b "/dev/$device" ) {
    die "$device is NOT a block device\n";
}

# this is in 512b blocks (is this stable for 4k block drives?)
my $size_file = "/sys/block/$device/size";

if ( !-f $size_file ) {
    die ("can't find size file - do you have sysfs mounted?");
}

open SIZEFILE, $size_file;
my $size = <SIZEFILE>;
close SIZEFILE;

my $size_less_gpt = $size - $GPT_SIZE;

print "Sanity check these values before executing (just printing here)\n";

print "/bin/dd if=/dev/zero of=/dev/$device bs=$BLOCK_SIZE count=$GPT_SIZE\n";
print "/bin/dd if=/dev/zero of=/dev/$device bs=$BLOCK_SIZE count=$GPT_SIZE skip=$size_less_gpt\n";
