e99b
![]() |
|
|
|
|
|||||||
| Userland Programming & Scripting C, Shell, Perl, Sed & Awk |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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
/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 |
|
#2
|
||||
|
||||
|
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. |
| The Following User Says Thank You to nekoexmachina For This Useful Post: | ||
Sebulon (September 11th, 2011) | ||
|
#3
|
||||
|
||||
|
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. |
| The Following User Says Thank You to wblock@ For This Useful Post: | ||
Sebulon (September 11th, 2011) | ||
|
#4
|
||||
|
||||
|
Thank you both for your inputs, they are now corrected.
Code:
if [ -x /dev/$drive] 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:
![]() /Sebulon |
|
#5
|
||||
|
||||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |