0

I create a 100M disk img and partition it with sgdisk to create a BIOS boot and a ESP partition.

dd if=/dev/zero of=file.img bs=1M count=100
sgdisk --clear -n 1:2048:4095 -t 1:ef02 -c 1:'BIOS Boot' -n 2:4096:17983 -t 2:ef00 -c 2:'EFI System Partition'

I then create a partition and format it for FAT:

losetup -f file.img -P
mkfs.vfat /dev/loop0p2
  1. If I mount the /dev/loop0p2, no problem.
  2. If I copy the partition out of the disk img and mount it, no problem:

    dd if=file.img of=part2.img bs=512 count=13888 skip=4095

Now, part2.img is mountable directly. Of course, if I dd it back into the image, it remains mountable

dd if=part2.img of=file.img bs=512 count=13888 seek=4095 conv=notrunc

Mount of /dev/loop0p2 works fine.

However, if I create an identically sized partition img:

mkfs.vfat -C directpart.img 13888
dd if=directpart.img of=file.img bs=512 count=13888 seek=4095 conv=notrunc

The file directpart.img can be mounted, just like part2.img (which was extracted from the disk image) can. However, mounting /dev/loop0p2 no longer works.

What does mkfs.vfat do differently when applied to a partition in a disk img file (or real block device) from a standalone file?

Interestingly, if I use losetup to get the full-disk image, the dd if=<new_file> of=/dev/loop0p2 works with both the direct mkfs.vfat -C <new_file> created file and the file created by dd off the disk.

  • What exactly do you mean / did you do by "dd it into the exact partition in the original disk image"? – Tom Yan Aug 11 '17 at 10:12
  • And how is that file.img mentioned in the first paragraph involved? Your post is quite confusing to be honest... – Tom Yan Aug 11 '17 at 10:14
  • OK, I can update it. – deitch Aug 11 '17 at 10:16
  • Is that clearer? – deitch Aug 11 '17 at 10:24
  • You should seek=4096, coz the partition start from LBA 4096, which means there are 4096 blocks before it (LBA 0 - LBA 4095). – Tom Yan Aug 11 '17 at 10:35
1

You should use 4096 for seek= and skip=, because the second partition starts from LBA 4096, which means there are 4096 blocks before it, namely LBA 0 - 4095.

  • Also known as, PEBKAC a.k.a. "user made a dumb mistake in copying and didn't realize what he did." Thanks to TomYan for finding it. – deitch Aug 11 '17 at 11:10

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.