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:
notruncis only important to prevent truncation when writing into a file. This has no effect on a block device such assdaorsdb.Educational version
I looked into the coreutils source code which contains dd.c to see how
notruncis processed.Here's the segment of code that I'm looking at:
We can see here that if
notruncis not specified, then the output file will be opened with O_TRUNC. Looking below at howO_TRUNCis treated, we can see that a normal file will get truncated if written into.O_TRUNC
Effects of
notrunc/O_TRUNCIIn the following example, we start out by creating
junk.txtof 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 thenotruncoption and we can see that the new file size is 512. This is because it was opened withO_TRUNC.Effects of
notrunc/O_TRUNCIII still haven't answered your original question of why when doing a disk-to-disk clone, why
conv=notruncis important. According to the above definition,O_TRUNCseems 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_TRUNCflag.Normal File Test
We can see below that the simple act of opening and closing a file with
O_TRUNCwill 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_TRUNCoption. From what I can tell, the filesystem is still mountable and the files are accessible and intact.Effects of
notrunc/O_TRUNCIIIOkay, for my final test I will use
ddon 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
notruncis 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.