13

I have 3 images. Each image was done by cloning (by dd) a partition (these partitions formatted using ext3 file system). One partition is boot-able with GRUB1 (CentOS)

How can I combine them to one disk image?

(I can copy the partitions images using dd command, but I don't know how to create partitions table and make boot working).

2
  • 1
    What are you really trying to do? Clone a working system onto new hardware, etc.? Were the three partitions from different mount points on a single system? Jun 17, 2011 at 19:26
  • What's the goal? you can use loopback devices to work with filesystem images, but modifying disk images rather than partition/filesystem images is a whole lot more tricky. Jun 17, 2011 at 19:30

1 Answer 1

14

Create a disk image. The following command will create a 10G sparse image:

# dd if=/dev/zero of=mydisk.img bs=1 count=0 seek=10G
0+0 records in
0+0 records out
0 bytes (0 B) copied, 1.6554e-05 s, 0.0 kB/s
# ls -lh mydisk.img
-rw-r--r--. 1 root root 10G Jun 17 15:27 mydisk.img

Partition the image with fdisk:

# fdisk mydisk.img

Make sure you create partitions that are at least as large as the ones you've imaged! For this example I created the following layout:

# fdisk -l mydisk.img
Disk mydisk.img: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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: 0x5519250f

     Device Boot      Start         End      Blocks   Id  System
mydisk.img1            2048     2099199     1048576   83  Linux
mydisk.img2         2099200     4196351     1048576   83  Linux

Use kpartx to create devices corresponding to each one of the partitions:

# kpartx -av mydisk.img
add map loop0p1 (253:3): 0 2097152 linear /dev/loop0 2048
add map loop0p2 (253:4): 0 2097152 linear /dev/loop0 2099200

This will create entries under /dev/mapper:

# ls -l /dev/mapper
lrwxrwxrwx. 1 root root       7 Jun 17 15:33 loop0p1 -> ../dm-3
lrwxrwxrwx. 1 root root       7 Jun 17 15:33 loop0p2 -> ../dm-4

Now you can copy your partition images onto your partitions:

# dd if=image_of_partition_1.img of=/dev/mapper/loop0p1 bs=1M
# dd if=image_of_partition_2.img of=/dev/mapper/loop0p2 bs=1M

Now remove the device mappings:

# kpartx -dv mydisk.img
del devmap : loop0p2
del devmap : loop0p1
loop deleted : /dev/loop0

And you're all set!

Notes

  • You could also accomplish the same thing by using dd and the seek parameter (to start writing at the appropriate offset in your disk image file) instead of using kpartx, but I think that using kpartx is less error-prone.

  • This will not result in a bootable image. If you want that, you'll also have to install a boot loader onto it.

You must log in to answer this question.

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