LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2009, 09:31 AM   #1
scarypajamas
LQ Newbie
 
Registered: May 2009
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
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.
 
Old 11-07-2009, 09:39 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 11-07-2009, 10:17 AM   #3
scarypajamas
LQ Newbie
 
Registered: May 2009
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 11-07-2009, 10:26 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 11-07-2009, 10:44 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 11-11-2009, 04:03 PM   #6
scarypajamas
LQ Newbie
 
Registered: May 2009
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
OK, just curious, how would one wipe a USB?

Also, where can I get the source to dd.
 
Old 11-11-2009, 07:27 PM   #7
scarypajamas
LQ Newbie
 
Registered: May 2009
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
* 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?
 
Old 11-12-2009, 04:16 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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
 
Old 11-12-2009, 05:03 PM   #9
scarypajamas
LQ Newbie
 
Registered: May 2009
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
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?
 
Old 11-12-2009, 05:20 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by scarypajamas View Post
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
 
Old 11-13-2009, 06:56 AM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by scarypajamas View Post
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.

Last edited by H_TeXMeX_H; 11-13-2009 at 07:00 AM.
 
Old 11-13-2009, 07:06 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by smeezekitty View Post
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?????

Last edited by pixellany; 11-13-2009 at 07:07 AM.
 
Old 11-13-2009, 07:44 AM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 11-13-2009, 09:19 AM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing raw data to a block device nacio Linux - Software 4 05-17-2007 10:37 PM
Formatting hard drive leaves out bits?? Pedroski Linux - Hardware 10 05-16-2007 02:28 PM
Fedora Core 5 on Pavilion 8000 64 bits problem writing Santuscore Linux - Laptop and Netbook 0 06-19-2006 11:29 PM
writing raw data to a tiff file James_dean Programming 4 10-25-2005 05:03 AM
raw drive not detected next to installed drive markraem Solaris / OpenSolaris 3 08-11-2005 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration