22

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?

  • 2
    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
  • I need this to keep the drive awake instead of waiting to spin up. – neverMind9 Jun 3 '18 at 20:54
11

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.

(Note: the question was about busybox dd which to date still does not support iflag=direct.)

  • 1
    Just a note: that only works with Linux. Also, the /proc filesystem is disabled in some embedded configurations of Linux. – user26112 Jul 12 '13 at 20:28
  • @EvanTeitelman Right, It seems to work here though, so I'm going to upvote and accept this answer. – ardent Jul 12 '13 at 20:40
  • 2
    I wondered if this has a once-off effect, or is ongoing. According to this, the effect is a once-off. – Craig McQueen Feb 7 '17 at 4:10
  • This will drop the caches, but does nothing at all to prevent dd from trashing them in the process if transferring large amount of data. – Jan Hudec Feb 19 at 9:10
9

Based on @sendmoreinfo's answer:

dd if=/dev/device iflag=direct bs=1M

It does not affect read-cache.

  • That works, but does it also not cache what's being read?. – neverMind9 Jun 3 '18 at 20:57
  • 1
    Correct. That is the goal. – Ole Tange Jun 3 '18 at 21:06
8

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

  • 4
    This is the right answer: dd if=/dev/device iflag=direct bs=1M; it does not affect read-cache. – Ole Tange Feb 4 '17 at 20:42
  • 1
    @OleTange: Why don't you post this as your own answer then? This is the #1 hit on google for "dd no cache", and the actual correct answer is in a comment to some non-accepted answer... – mic_e Oct 11 '17 at 10:41
4

A small flourish to the the iflag=direct answer; provide a progress bar too:

dd iflag=direct if=~/source.iso | pv | dd oflag=direct of=/dev/sdb bs=8M

Without the iflag/oflag the pv reports it's finished and then looks like it's hung; but the dd to the device is still working on the cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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