Every time I've written a file to an empty raw block device, e.g.
# dd if=image.iso of=/dev/sdb status=progress
I've never used any type of sync
(i.e. sync
; conv=fsync
; conv=fdatasync
; oflag=sync
; oflag=dsync
).
I've noticed that dd
doesn't ever exit until all writing has finished.
I always verify this using Conky's I/O facility and grep Dirty /proc/meminfo
. Also, the checksum of the device always matches that of the file that was written to it. So I am always 100% sure that the whole file was written to the device.
I've written files to an ext4 volume to compare. For example using:
$ dd if=/dev/urandom of=~/file bs=1M count=50 iflag=fullblock
When writing to an ext4 volume, after dd
exits there is always a delay of about 20 seconds before the data is actually written to the disk.
Many people advocate using the sync
command after the dd
command, or including one of the several sync
options in the dd
command when writing to a block device. E.g. here and here. However, I've not known anyone actually prove that it is necessary.
One of the comments on this page is:
sync
is pointless here [i.e. writing directly to/dev/sdX
]. It only affects file system operations.
Five people have upvoted this comment and it is consistent with my experience.
So when writing to a block device, are the any circumstances in which dd
will exit before all writing has completely finished? Has this actually ever happened to anyone?
What about other writing options, such as cp
and cat
? Can they exit before writing to a block device has finished?
Best Answer
It's about time to answer this.
For such a long time, I've been thinking that only writes to filesystems were cached, not block devices, only to discover that I was wrong : writes to block devices are cached.
Strictly speaking I don't have an official source on this, and will update my answer as I get one
, but you can find some info here : linux - Block device cache v.s. a filesystem - Unix & Linux Stack Exchange. Sorry, I just noticed you saw that question too !In fact, I had the problem of dd'ing an image to a USB stick, eject it after completion and then discover it was corrupted. It shouldn't have behaved like that since it seems a complete sync is done on the last
close()
on the device, hence I suppose either I got data corruption on my device, or I missed a process that still had the block device opened, but anyway I'm not taking chances any more.So yes, adding sync options to
dd
when writing to a block device doesn't seem to be an urban legend. I thinkconv=fdatasync
should be enough and best performing (no file metadata is added after all, and only syncing at the end of the process is really needed), but the extremists among us may preferoflag=sync
(complete data + metadata sync after each write). Checkman 2 open
andman 2 fdatasync
for more insight.According to what I've read (but nothing official as of now, unfortunately),
cp
andcat
to a block device can indeed exit before sync if some other process still has the block device open, unless explicitly requested with some SYNC flags when the device isopen()
ed or with a sync system call such asfdatasync()
,fsync()
or more exaggeratinglysync()
, which none of these commands does...