Ubuntu – Can dd overwrite adjacent partitions



dd if=/dev/zero of=somepartition bs=512

also wipe partitions after somepartition or stop at the end of somepartition?

Best Answer

Overwrite a partition with dd

dd is a very powerful but also dangerous tool. It does what you tell it to do without questions. So if you tell it to wipe the family pictures, ... and it is a minor typing error away.

But if you check and double-check, you can use it.

dd if=/dev/zero of=somepartition bs=512

or I would suggest

dd if=/dev/zero of=/dev/sdxn bs=4096

where x is the drive letter and n is the partition number and block size 4096 bytes makes the write process faster.

It is important that you write to a partition in this case. If you write to the whole drive (the drive head end) /dev/sdx the whole drive will be overwritten. But writing to the partition will be interrupted at the end of the partition and partitions behind it will be preserved. (I tested now on a USB pendrive in Lubuntu 16.04 LTS, so I know that it works like that.)

Exception for an extended partition

There is an exception for an extended partition (which is a container for logical partitions, in order to have more than four partitions in an MSDOS partition table). This is described in the following link,

Can I make image of 'extended' partition using dd?

But there is another problem too. I tested your command in a test environment, and dd read only one kibibyte (1024 bytes) when I wanted it to make an image of an extended partition.

I tested also this now on a USB pendrive in Lubuntu 16.04 LTS, and this applies to writing (as well as to reading). Only the first kibibyte is overwritten.

So to summarize, overwriting primary partitions and logical partitions work according to the main description in this answer. But do not use this method to overwrite an extended partition because only the first kibibyte will be overwritten. The extended partition's logical partitions will no longer be found via the partition table, but the data stored in them are still there.

Related Question