Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm working on an embedded system with the busybox version of dd. I'm trying to test an erase to the drive from some outside utility, however dd does not read from the disc again after the erase, but shows me the cached data.

I've narrowed it down to dd as when I do an initial dd, see the data, restart my system to flush the cache, did the erase, and then ran dd again it came up with all zeros.

However, if I do dd on factory settings, erase the drive, and do dd again without restarting it won't show me all zeros until a restart.

I've read in the GNU manpage that dd supports the iflag opt, with a nocache flag, but busybox does not support that option so that's out of the question.

My question is how can I force dd to read from the disk again rather than from cache?

share|improve this question
1  
Potential nitpicking, but... dd does not cache it. The kernel/disk system does. This is why Frostschutz's command (which does not get send to dd) works. –  Hennes Jul 12 '13 at 22:26

3 Answers 3

up vote 5 down vote accepted

You could try

sync
echo 3 > /proc/sys/vm/drop_caches

which drops all sorts of caches.

For details see /usr/src/linux/Documentation/sysctl/vm.txt on drop_caches.

share|improve this answer
1  
Just a note: that only works with Linux. Also, the /proc filesystem is disabled in some embedded configurations of Linux. –  paraxor Jul 12 '13 at 20:28
    
@EvanTeitelman Right, It seems to work here though, so I'm going to upvote and accept this answer. –  ardentsonata Jul 12 '13 at 20:40

You can accomplish what you want by turning on synchronous IO for dd by adding conv=fsync. This basically tells dd to perform a fsync call after every write (fsync is an OS call to flush data to disk. See man 2 fsync). When doing this, you will want to use large block sizes as the default is 512b, which is horribly inefficient for hard drives.

For example:

dd if=/dev/zero of=/dev/sda bs=1M conv=fsync

(the conv=fsync option is supported by busybox's dd).


Addendum:
Frostschutz's solution does work, but calling sync flushes all IO, not just the dd stuff. Trivial non-issue most likely, but I think conv=fsync is the "proper" solution.

share|improve this answer
    
Will try this when I get back to work! Thanks for the suggestion. –  ardentsonata Jul 14 '13 at 10:02
    
So while fsync is supported by busybox, it isn't supported on the (much older) version of busybox that's on the embedded system, and as a result @frostschutz's is more correct for me. But your solution is definitely the more general correct way to do it, so have an upvote. –  ardentsonata Jul 16 '13 at 21:35

Direct I/O (open mode O_DIRECT) should work, but your kernel and/or dd may not support it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.