If you look up how to clone an entire disk to another one on the web, you will find something like that:
dd if=/dev/sda of=/dev/sdb conv=notrunc,noerror
While I understand the noerror
, I am getting a hard time understanding why people think that notrunc
is required for "data integrity" (as ArchLinux's Wiki states, for instance).
Indeed, I do agree on that if you are copying a partition to another partition on another disk, and you do not want to overwrite the entire disk, just one partition. In thise case notrunc
, according to dd's manual page, is what you want.
But if you're cloning an entire disk, what does notrunc
change for you? Just time optimization?
TL;DR version:
notrunc
is only important to prevent truncation when writing into a file. This has no effect on a block device such assda
orsdb
.Educational version
I looked into the coreutils source code which contains dd.c to see how
notrunc
is processed.Here's the segment of code that I'm looking at:
We can see here that if
notrunc
is not specified, then the output file will be opened with O_TRUNC. Looking below at howO_TRUNC
is treated, we can see that a normal file will get truncated if written into.O_TRUNC
Effects of
notrunc
/O_TRUNC
IIn the following example, we start out by creating
junk.txt
of size 1024 bytes. Next, we write 512 bytes to the beginning of it withconv=notrunc
. We can see that the size stays the same at 1024 bytes. Finally, we try it without thenotrunc
option and we can see that the new file size is 512. This is because it was opened withO_TRUNC
.Effects of
notrunc
/O_TRUNC
III still haven't answered your original question of why when doing a disk-to-disk clone, why
conv=notrunc
is important. According to the above definition,O_TRUNC
seems to be ignored when opening certain special files, and I would expect this to be true for block device nodes too. However, I don't want to assume anything and will attempt to prove it here.openclose.c
I've written a simple C program here which opens and closes a file given as an argument with the
O_TRUNC
flag.Normal File Test
We can see below that the simple act of opening and closing a file with
O_TRUNC
will cause it to lose anything that was already there.Block Device File Test
Let's try a similar test on a USB flash drive. We can see that we start out with a single partition on the USB flash drive. If it get's 'truncated', perhaps the partition will go away (considering it's defined in the first 512 bytes of the disk)?
It looks like it has no effect whatsoever to open either the disk or the disk's partition 1 with the
O_TRUNC
option. From what I can tell, the filesystem is still mountable and the files are accessible and intact.Effects of
notrunc
/O_TRUNC
IIIOkay, for my final test I will use
dd
on my flash drive directly. I will start by writing 512 bytes of random data, then writing 256 bytes of zeros at the beginning. For the final test, we will verify that the last 256 bytes remained unchanged.Summary
Throughout the experiment it seems that
notrunc
is only important when you want to write to a regular file, but don't want to truncate it. It seems to have no effect on a block device such as sda or sdb.