How to Merge Multiple ext3 Partition Images into a Single Bootable Disk Image with GRUB1


2 views

When dealing with disk imaging, we often encounter situations where we need to combine multiple partition images (created using dd) into a single bootable disk image. In this case, we have:

  • 3 partition images (ext3 filesystem)
  • One partition contains GRUB1 bootloader (CentOS)
  • Need to preserve partition structure and boot capability
sudo apt-get install kpartx parted grub

1. Create a Raw Disk Image Container

# Calculate total size needed (sum of all partitions + overhead)
dd if=/dev/zero of=combined.img bs=1M count=4096  # Adjust size as needed

2. Set Up Partition Table

sudo parted combined.img
(parted) mklabel msdos
(parted) mkpart primary ext3 1MiB 2GiB    # Adjust sizes
(parted) mkpart primary ext3 2GiB 4GiB
(parted) mkpart primary ext3 4GiB 6GiB
(parted) set 1 boot on
(parted) quit

3. Map Partitions and Copy Data

sudo kpartx -av combined.img
# Note the loop devices created (e.g., /dev/mapper/loop0p1)

# Copy partition images to their respective slots
sudo dd if=partition1.img of=/dev/mapper/loop0p1
sudo dd if=partition2.img of=/dev/mapper/loop0p2
sudo dd if=partition3.img of=/dev/mapper/loop0p3

sudo kpartx -dv combined.img  # Unmap when done

4. Install GRUB Bootloader

sudo losetup -Pf combined.img
sudo mount /dev/loop0p1 /mnt  # Mount boot partition

# Install GRUB1
sudo grub-install --root-directory=/mnt --no-floppy /dev/loop0

# Verify bootloader configuration
cat /mnt/boot/grub/menu.lst  # Should contain correct root partition

5. Verify the Combined Image

qemu-system-x86_64 -hda combined.img  # Test booting
  • If GRUB fails, check /boot/grub/device.map matches your setup
  • Ensure partition UUIDs in /etc/fstab match the new partitions
  • Use gdisk -l combined.img to verify partition table integrity

For more precise control over partition layout:

# Create partition table script
cat << EOF | sfdisk combined.img
label: dos
unit: sectors

/dev/sda1 : start=2048, size=3906250, type=83, bootable
/dev/sda2 : start=3906251, size=3906250, type=83
/dev/sda3 : start=7812501, size=3906250, type=83
EOF

html

When working with disk images created via dd, we often need to merge multiple partition images into a single disk image while preserving boot capability. Here's a complete solution for combining three ext3 partition images (including a GRUB1 boot partition) into one functional disk image.

  • Original partition images (partition1.img, partition2.img, partition3.img)
  • Linux environment with root access
  • Basic knowledge of fdisk, dd, and grub-install
  • Loop device support in kernel

First, create a blank disk image large enough to contain all partitions:

# Calculate total size needed
SIZE=$(( $(stat -c%s partition1.img) + $(stat -c%s partition2.img) + $(stat -c%s partition3.img) + 1048576 )) # Add 1MB overhead
dd if=/dev/zero of=combined_disk.img bs=1 count=0 seek=$SIZE

Create a partition table using fdisk:

fdisk combined_disk.img << EOF
n
p
1
2048
+256M
a
n
p
2
526336
+10G
n
p
3
21626880

t
1
83
t
2
83
t
3
83
w
EOF

Associate the disk image with loop devices:

LOOPDEV=$(losetup --find --show --partscan combined_disk.img)

Copy partition data to their respective locations:

dd if=partition1.img of=${LOOPDEV}p1 bs=4M status=progress
dd if=partition2.img of=${LOOPDEV}p2 bs=4M status=progress
dd if=partition3.img of=${LOOPDEV}p3 bs=4M status=progress

For CentOS with GRUB1, we need to install the bootloader properly:

mkdir /mnt/combined
mount ${LOOPDEV}p1 /mnt/combined
mount --bind /dev /mnt/combined/dev
mount --bind /proc /mnt/combined/proc
mount --bind /sys /mnt/combined/sys

chroot /mnt/combined /bin/bash << "CHROOT_EOF"
grub-install --recheck ${LOOPDEV}
update-grub
exit
CHROOT_EOF

umount /mnt/combined/{dev,proc,sys,}
umount /mnt/combined

Before using the final image, verify its integrity:

fsck.ext3 -f ${LOOPDEV}p1
fsck.ext3 -f ${LOOPDEV}p2
fsck.ext3 -f ${LOOPDEV}p3

# Clean up
losetup -d $LOOPDEV

The resulting combined_disk.img can now be used with virtualization software or written to physical media.

  • Boot failure: Ensure the boot partition is marked active (flag 'a' in fdisk)
  • GRUB errors: Verify /boot/grub/menu.lst contains correct root partition references
  • Size mismatches: Always check partition sizes match before copying
  • Filesystem UUIDs: Update /etc/fstab with new partition UUIDs if needed