0

I need to generate a disk image containing the MBR, and one primary partition. The partition contains an ext3 Filesystem, which was resized to the minimum with resize2fs -M /dev/sdc1. I got the output that the filesystem is now N blocks large, where one block is 4K. Now I guess that I need the first N*4K Bytes of the partition. But how many bytes are before that partition (I guess that the MBR and partition table themselves does have some size) are also needed?

The layout is only that one primary partition starting at the beginning of the disk.

So the question is how can I get from the N block count to the real required count of bytes?

1 Answer 1

1

The MBR is right at the beginning of the disk block device ("on the zeroth sector"). You can use fdisk to find out the offset of the partition (i.e. where the first block of the filesystem is relative to the start of the disk). Note that I'm using u to switch the units to sectors, which as shown in the output are 512 bytes for this disk.

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000bbac4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2         1026048   167772159    83373056   8e  Linux LVM

Command (m for help): q
#

(To avoid confusion, I should clarify that the blocks in the Blocks column of fdisk are 1k blocks, not whatever size blocks your filesystem or block device might use.)

In this example from a VM, /dev/sda1, which is my /boot partition, starts at sector 2048, which is the 256th 4k block (2048 sectors * 512 bytes / 4096 bytes = 256 4k blocks).

If I knew I had just resized the filesystem on /dev/sda1 down to 100000 4k blocks, I could copy the first 256 + 100000 = 100256 4k blocks from /dev/sda to the image to get the MBR and all of the /boot partition.

e.g.

dd if=/dev/sda of=my_backup.img bs=4096 count=100256

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .