1

I have a QEMU image myserver.img that is about 20GiB. The file serves as a "virtual" hard disk for the server. Now the first 512 bytes contain the MBR, and I am trying to overwrite those bytes with zeroes. On a normal disks, this one would work:

dd if=/dev/zero of=/dev/sda bs=512 count=1

But when I try that on the image file, the file gets overwritten and is only 512 bytes in size (instead of 20GiB). Is there a way to change only the first 512 bytes, preferably with dd?

Daniel
  • 3,047
  • 5
  • 22
  • 27

2 Answers2

7

Try using dd with conv=notrunc

dd if=/dev/zero of=myserver.img bs=512 count=1 conv=notrunc

From the dd man page

   notrunc
          do not truncate the output file

e.g.

ls -l myserver.img
-rw-r--r-- 1 iain users 1536 Mar 27 12:31 myserver.img

dd if=/dev/zero of=myserver.img bs=512 count=1 conv=notrunc

ls -l myserver.img
-rw-r--r-- 1 iain users 1536 Mar 27 12:32 myserver.img
user9517
  • 115,471
  • 20
  • 215
  • 297
6

Add conv=notrunc:

dd if=/dev/zero of=YOUR_IMAGE_FILE bs=512 count=1 conv=notrunc
ash108
  • 276
  • 1
  • 5