LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Writing raw bits to a drive. (https://www.linuxquestions.org/questions/programming-9/writing-raw-bits-to-a-drive-767528/)

scarypajamas 11-07-2009 09:31 AM

Writing raw bits to a drive.
 
I'm wondering if someone could help me figure out how to write raw bits to a drive.

What I need is some kind of direct memory access that will allow me to wipe an internal or external device.

Being a newbie to linux, I'm wondering what linux has that can help me accomplish this task.

I'm using ubuntu if that helps any.

pixellany 11-07-2009 09:39 AM

dd

For an article on how to do **everything** with this ultimate power tool, search here for member awesomemachine, and look in his sig.

"man dd" for the basics.

My normal "wiping" is to first write random data to the drive, and then all zeros. Secure wipe does something like, but several times. If you don't care about security, just writing all zeroes should be plenty.

scarypajamas 11-07-2009 10:17 AM

Thanks for the info on dd.

What I'm more interested in though is how to do this myself programmatically in C. Is there anything in the linux header files that helps with DMA access? Also, would
Code:

FILE* fp=fopen("/dev/sda","w");
allow me to write to the drive?

I've also read something about a KIO buffer although I haven't been able to find much info on it through google.

pixellany 11-07-2009 10:26 AM

Don't know, but why not try it? (Perhaps try READING first......;))

BTW, I think "DMA" normally refers to direct access to system memory---ie RAM, and not to block devices.

H_TeXMeX_H 11-07-2009 10:44 AM

If you want to wipe a drive run:

NOTE: This command is dangerous it will wipe all data, irreversibly from /dev/sdx !!!

Code:

dd bs=4M if=/dev/zero of=/dev/sdx
where /dev/sdx is the drive you wanna wipe.

scarypajamas 11-11-2009 04:03 PM

OK, just curious, how would one wipe a USB?

Also, where can I get the source to dd.

scarypajamas 11-11-2009 07:27 PM

* BUMP *

Ok, I understand that dev/hda, dev/sda, etc have to do with the hard drives in the computer. But where in the dev/ folder does it contain information about USB's plugged into the system.

I'm trying to figure out how to use dd to wipe a usb.

How could I find out the current, plugged in USB's 3 character name in the dev/ folder? I want to call something like:

Code:

dd bs=4M if=/dev/zero of=/dev/usb
Where usb is the 3 character name in the dev folder. How do I find out this name?

H_TeXMeX_H 11-12-2009 04:16 AM

There are 2 ways to find it, for example I just plugged in a USB stick, then you run:

Code:

bash-3.1$ dmesg | tail
sd 6:0:0:0: [sdb] Write Protect is off
sd 6:0:0:0: [sdb] Mode Sense: 43 00 00 00
sd 6:0:0:0: [sdb] Assuming drive cache: write through
sd 6:0:0:0: [sdb] 7864320 512-byte hardware sectors: (4.02 GB/3.75 GiB)
sd 6:0:0:0: [sdb] Write Protect is off
sd 6:0:0:0: [sdb] Mode Sense: 43 00 00 00
sd 6:0:0:0: [sdb] Assuming drive cache: write through
 sdb: sdb1
sd 6:0:0:0: [sdb] Attached SCSI removable disk
sd 6:0:0:0: Attached scsi generic sg2 type 0

In this case it is sdb1.

You can also check /dev/disk/by-id and it will be listed in there:

Code:

bash-3.1$ ls -l /dev/disk/by-id/
total 0
lrwxrwxrwx 1 root root  9 2009-11-12 13:45 ata-ST3160815AS_6RAACVGP -> ../../sda
lrwxrwxrwx 1 root root 10 2009-11-12 13:45 ata-ST3160815AS_6RAACVGP-part1 -> ../../sda1
lrwxrwxrwx 1 root root  9 2009-11-12 13:45 scsi-SATA_ST3160815AS_6RAACVGP -> ../../sda
lrwxrwxrwx 1 root root 10 2009-11-12 13:45 scsi-SATA_ST3160815AS_6RAACVGP-part1 -> ../../sda1
lrwxrwxrwx 1 root root  9 2009-11-12 12:13 usb-Corsair_Flash_Voyager_AA04012700008063-0:0 -> ../../sdb
lrwxrwxrwx 1 root root 10 2009-11-12 12:13 usb-Corsair_Flash_Voyager_AA04012700008063-0:0-part1 -> ../../sdb1


scarypajamas 11-12-2009 05:03 PM

Interesting...

Just curious, I know now that dd bs=4M if=/dev/zero of=/dev/sdx will write all zeros and dd bs=4M if=/dev/random of=/dev/sdx will write random bytes. But how does one write all 1's?

smeezekitty 11-12-2009 05:20 PM

Quote:

Originally Posted by scarypajamas (Post 3754826)
Interesting...

Just curious, I know now that dd bs=4M if=/dev/zero of=/dev/sdx will write all zeros and dd bs=4M if=/dev/random of=/dev/sdx will write random bytes. But how does one write all 1's?

make a big file of ones and use that as your if

H_TeXMeX_H 11-13-2009 06:56 AM

Quote:

Originally Posted by scarypajamas (Post 3754826)
Interesting...

Just curious, I know now that dd bs=4M if=/dev/zero of=/dev/sdx will write all zeros and dd bs=4M if=/dev/random of=/dev/sdx will write random bytes. But how does one write all 1's?

You can do:

Code:

dd if=/dev/zero | tr '\000' '\377' > /dev/sdx
or

Code:

dd if=/dev/zero | tr '\000' '\377' | dd of=/dev/sdx
There is a slight overhead because you're piping through tr, but from what I can see, it's a small overhead.

pixellany 11-13-2009 07:06 AM

Quote:

Originally Posted by smeezekitty (Post 3754847)
make a big file of ones and use that as your if

Brilliant!! Assuming that we are talking binary "1" and not ascii, how about a reply that is actually useful----eg how to make that file?

AND---to wipe a large disk, we're going to need a ***really big*** file of "1"s----where do you suggest we store it?????

H_TeXMeX_H 11-13-2009 07:44 AM

The thing is it would need to be something like a device node so you can read unlimited data from it, like /dev/one, and I think there's a kernel patch somewhere that implements it. You can also write a small C program to do it. Or you can use my solution above.

gnashley 11-13-2009 09:19 AM

Maybe create a named pipe with mkfifo and use a loop to echo $char(or whatever) to the pipe and have dd use the other end of the pipe as input.


All times are GMT -5. The time now is 02:20 AM.