e99b Fast and easy delete of partition and filesystem data - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old September 10th, 2011, 18:09
Sebulon's Avatar
Sebulon Sebulon is offline
Member
 
Join Date: Nov 2010
Location: Uppsala, Sweden
Posts: 558
Thanks: 24
Thanked 93 Times in 78 Posts
Default Fast and easy delete of partition and filesystem data

I've also posted this in another thread but thought it be best to start a new post about it since it is more of a general purpose tool. Been tinkering on this for a bit and I am now ready to present to everyone:

cleandrives:
Code:
#!/bin/sh

if [ -z "$1" ]
  then
    echo "Usage: `basename $0` drive1 drive2 ..."
  exit
fi

drives="$*"

verifydrives()
  {
    for drive in $drives
      do
        if [ `ls -l /dev/ | grep -w $drive | wc -l` = "0" ]
          then
            echo "Drive $drive does not exist. Aborting."
            exit
          else
            echo "Drive $drive verified."
        fi
      done
  }

seeksector()
  {
    blocksize=`dmesg | grep -w $drive | grep -oe '[0-9]\{7,\}'`
    mbsize=`echo "$blocksize / 2048" | bc`
    echo "$mbsize - 10" | bc
  }

cleandrives()
  {
    for drive in $drives
      do
        dd if=/dev/zero of=/dev/$drive bs=1M count=10 >/dev/null 2>&1
        dd if=/dev/zero of=/dev/$drive bs=1M count=10 seek=`seeksector $drive` >/dev/null 2>&1
    done
  }

verifydrives $drives

echo ""
echo "This will irreversibly destroy partition- and filesystem data on drive(s):"
echo "$drives"
echo ""
echo "USE WITH EXTREME CAUTION!"
read -r -p 'Do you confirm "yes/no": ' choice
  case "$choice" in
    yes) cleandrives $drives
         echo ""
         echo "Drive(s) cleaned."  ;;
     no) echo ""
         echo "Cleaning cancelled."; break ;;
      *) echo ""
         echo "Cleaning cancelled."; break ;;
  esac
This script verifies and then erases the first and last 10MB of every hard drive you´ve confirmed. If you find yourself in a situation where you don't want to wait for a complete dd over a 1 or 2 or even 3TB drive, or if you want to move a hard drive from one system to another and don´t want the new system to be "confused" by old partitioning- and filesystem data, then this command makes that cleaning fast and easy. Best part is that it works on any *nix system out there, not just FreeBSD. Enjoy!

/Sebulon

Last edited by Sebulon; February 21st, 2012 at 19:51. Reason: Optimized seeksector for cross-platform, silenced dd and now using M for bs
Reply With Quote
  #2  
Old September 10th, 2011, 19:00
nekoexmachina's Avatar
nekoexmachina nekoexmachina is offline
Member
 
Join Date: Jan 2010
Location: Russia
Posts: 310
Thanks: 66
Thanked 21 Times in 21 Posts
Default

Code:
        if [ `ls -l /dev/ | grep $drive | wc -l` = "0" ]
          then
            echo "Drive $drive does not exist. Aborting."
            exit
          else
            echo "Drive $drive verified."
        fi

Better check if it exist with file /dev/$drive
or if [ -x /dev/$drive]
Cause grep will say yes, it is if i will run your script like $ script.sh da if there will be da0 or da1 or something.

Last edited by DutchDaemon; September 11th, 2011 at 02:04.
Reply With Quote
The Following User Says Thank You to nekoexmachina For This Useful Post:
Sebulon (September 11th, 2011)
  #3  
Old September 10th, 2011, 19:09
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,701
Thanks: 429
Thanked 1,757 Times in 1,456 Posts
Default

Some general thoughts...

Automating this makes me a little queasy. The code should be very, very picky. Easy to safely quit and hard to actually wipe a disk. Showing the user information about the device they're about to destroy, like with file -s, could be useful.

Along those lines, a more suggestive confirm choice would help alert the user they're about to do something serious. Maybe "destroy disks" instead of "yes". "no" isn't needed, it's the default choice.

To erase MBR and/or GPT, the first and last 35 blocks (17K) of the drive is enough. The first 35 blocks alone is probably adequate.

bs=1m breaks on Linux dd(1), which is case-sensitive on block size (M for meg, K for k). A buffer that large really isn't needed, 64K is plenty.

The GEOM safety will prevent writes to important parts of the disk like the MBR. It can be disabled by setting sysctl kern.geom.debugflags=16.
Reply With Quote
The Following User Says Thank You to wblock@ For This Useful Post:
Sebulon (September 11th, 2011)
  #4  
Old September 11th, 2011, 20:26
Sebulon's Avatar
Sebulon Sebulon is offline
Member
 
Join Date: Nov 2010
Location: Uppsala, Sweden
Posts: 558
Thanks: 24
Thanked 93 Times in 78 Posts
Default

Thank you both for your inputs, they are now corrected.
Code:
if [ -x /dev/$drive]
Wow, that easy huh? I´m trying to remember if I didn´t try it just like that but it didn´t work and stopped and kept saying "not permitted" something or other...and that´s why I went that other way instead.

Would any of you happen to know if the default block-size of dd is the same everywhere; 512b? Cause it´s kinda important for this command to be accurate on other platforms as well. If not, any ideas on how to improve?

Just nuking the beginning of a drive has been unsuccessful for me, at least with ZFS, it can still find the backups at the end. I have also read from at least three different people on this forum that it´s safest to wipe 10MB for good measure. Just for partition data, fine, but this aims to wipe filesystem data as well, so that´s why it is that way.

It´s not this tool´s job to know whether you have set the geom.debugflags to 16. Everyone should know to do that beforehand.
Besides, it´s very FreeBSD-specific. Also:
Quote:
Originally Posted by gkontos View Post
sysctl kern.geom.debugflags=16 is being used only if the disk is currently mounted in read/write mode. Something just to protect you from accidentally messing up.
If the drives you doing this on are mounted read/write, you are doing something wrong

/Sebulon
Reply With Quote
  #5  
Old September 12th, 2011, 09:40
Sebulon's Avatar
Sebulon Sebulon is offline
Member
 
Join Date: Nov 2010
Location: Uppsala, Sweden
Posts: 558
Thanks: 24
Thanked 93 Times in 78 Posts
Default

Learned something new about dd today. If you seek and have specified bs, it will use that unit to seek with. It came to me while trying to use bs=1M, while seeking to the end of the drive.

Writing 10MB with bs=1M took 0.05 secs
Writing 10MB with bs=512B took 7.9 secs

So I wanted to change the second dd to use bs=1M, but it refused, because I used the total number of blocks to seek with. So I had work out a way to use dmesg, and retrieve the size in MB, minus 10. And it had to work cross-platform. Well, now it does I have tested this on both FreeBSD and Ubuntu and it works.

Total time of cleaning one drive: 0.1 second.

/Sebulon
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which partition table & filesystem work best with FreeBSD PPC through USB? aurora72 Peripheral Hardware 0 September 5th, 2011 01:58
Easy upgrade? demonfire Installing & Upgrading 1 June 27th, 2011 07:51
Automount on WM (The easy way) :D sk8harddiefast Peripheral Hardware 0 December 13th, 2010 14:47
[Solved] delete a very large file from UFS partition m4rtin General 13 July 16th, 2010 02:35
ZFS: df shows data usage when filesystem is empty mcj General 0 May 22nd, 2010 02:16


All times are GMT +1. The time now is 20:43.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0