I recently made a dd from a disk partition. I want to use it as a disk in a virtual machine. I can't because the disk has no partition table.

Can I had a partition table to a loop device? how?

apparently a similar question exists here. If someone knows how to do this without duplicating all data, that would be great.

I did dd if=/dev/sda1 of=myfile

I can't copy the full sda disk because it's just too big.

  • Just use fdisk (or other utility) as you normally would. – Patrick Apr 8 '13 at 13:36
  • 1
    @patrick does not work, because there is no space on the device for it. – Guerrio Apr 8 '13 at 13:57
  • I just tried it, works perfectly for me. You should add the exact commands you've used (all the way from creating the image to the fdisk command). – Patrick Apr 8 '13 at 14:03
  • @Patrick I'm guessing the OP did something like dd if=/dev/sda1 of=myfile and now wants to create a MBR partition structure around the current contents of myfile so it is useful as a virtual hard disk rather than simply a single partition dump. – a CVn Apr 8 '13 at 14:11
  • @MichaelKjörling that's exactly it. I can't copy the complete sda because I don't have enough space/interest on that. – Guerrio Apr 8 '13 at 14:27

If you still have the original drive, then delete the image and create it again with:

dd if=/dev/sda1 of=myfile bs=512 seek=2048

That will leave the usual 1 MiB of space at the start for the partition table to be added later.

You also may want to look into using qcow2 for the image instead of a full, raw image file that contains all of the junk in the unused blocks.

  • Thanks. Why 1 Mbyte? The partition table plus the boot sector usually takes 512bytes – Guerrio Apr 9 '13 at 11:22
  • 1
    @Guerrio, because partition tools these days align the partition to a MiB boundary for performance reasons. Also you need to leave some room usually for the boot loader. – psusi Apr 9 '13 at 15:37

Something like this?

sda1_size=$((`blkdev --getsize64 /dev/sda1`/1024/1024+1))
dd if=/dev/sda of=myfile bs=1M count=$(($sda1_size+1))

Start dd for entire sda, but copy only until the first partition is covered. Achieved by adding +1 MiB to the count. This is assuming that sda1 is at the very beginning of the disk. If there's an empty gap larger than 1MiB, add more than +1.

Rather than the math stuff, you could also just look how large in MiB the partition is in any partitioning program, and then just be generous and add a bit to it.

  • Thanks. Why 1 Mbyte? The partition table plus the boot sector usually take 512bytes. – Guerrio Apr 9 '13 at 7:38
  • 1
    Many reasons. dd is slow when copying in 512 byte sectors; partitions are often MiB-aligned these days; and in general it's just better to copy a MiB or two too many than too few. – frostschutz Apr 9 '13 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.