Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to erase a Hard Disk Drive

This is an odd question, but here it goes. I would like to write a program to flash my external hard drive with 1s and then 0s to clean it completely so I can sell it. Now, why do I want to write my own software instead of just using DBAN?

  1. From what I understand, DBAN deletes all hard drives it detects. I only want to clean my external.

  2. DBAN flashes seven times. I feel this is a little excessive for my purposes, since my external contains no illegal material nor credit card information.

  3. To be honest, I actually am kind of curious as to how firmware works.

Google didn't turn up too much (stupid Adobe Flash). I would prefer doing this in C/C++, but other languages work to, including assembly.

like image 583
John Morgan Avatar asked Nov 15 '12 02:11

John Morgan


People also ask

How do I completely erase a hard drive?

In Windows 10, open Settings > Update & security > Recovery, and then click the Get Started button. When asked what you want to erase, select Remove everything. Choose the Local reinstall option to reinstall Windows from your computer. Click Change Settings and turn on the switch next to Wipe the drive.

How do I wipe my hard drive before recycling?

In Windows 10, go to Settings > Update & security > Recovery. Click the Get started button under the Reset this PC section. You are then asked if you wish to remove apps and settings but keep your personal files or remove everything. Choose the option for Remove everything.

Does erasing a hard drive delete everything?

Otherwise, formatting a hard drive using the quick format option (or an earlier version of Windows) will not actually erase all of its stored data. When you choose to format a hard drive, you are essentially only removing the pointers to the data as the partition table is either cleared or rebuilt.


1 Answers

Well, doing it in C is rather easy. First, you open the appropriate device file in write mode:

int fd = open("/dev/sdc", O_WRONLY);

and you simply write() 512 byte chunks to it until you can write no more. Newer disks use 4096 byte sectors instead, but the OS usually treats them as if they have 512 byte sectors, so 512 is the safest value. Here's a C program that does exactly this:

(Note: Be very careful to choose the correct /dev device file, or else you're going to wipe out the wrong disk!)

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
    int fd = open("/dev/sdd", O_WRONLY);
    if (fd < 0) {
        fprintf(stderr, "Error opening device file.\n");
        return EXIT_FAILURE;
    }

    // Write 0's all over the disk, in chunks of 512 bytes.
    char* zeros = calloc(1, 512);
    ssize_t written, total = 0;
    do {
        total += written = write(fd, zeros, 512);
        printf("\rBytes written: %ld", total);
    } while (written == 512);
    printf("\nDone!\n");

    close(fd);
    free(zeros);
    return 0;
}

You might get a speed-up if you remove the printf(), though it's kind of cool to see the progress as it happens. You should probably also do additional error checking at the end (if written is -1, an error occurred, and you should check errno.)

Note that due to caching, the program might appear to hang at the end for a little while after it prints "Done". It's not really hanging, it's only that the caching of the write operations is blocking it until they're all finished.

like image 126
Nikos C. Avatar answered Sep 28 '22 22:09

Nikos C.