I have 2 .img files (embedded Linux images, each with 2 partitions- a boot partition and a rootfs). I need to replace the rootfs partition in one with the other's.How can I do thisIn the .imgs directlyWith the .imgs written to SD cards.I would appreciate if answers included commands.
Replacing a partition within a .img with another one
filesystems;mount;partition;images;fdisk
First make sure that the partition you need to replace is at least the same size as the one you will copy. If your image files are not compressed, they can be mounted as loopback file systems using kpartx.Copying file systems across disk imagesYou'll need LVM2 for this to work. Let disk1.img and disk2.img, assuming both images are the exact same size in the following example:# Let device mapper detect partitions on disk imageskpartx -a disk1.imgkpartx -a disk2.img# Unmount source and destination file systemsumount /dev/mapper/loop?p?# Copy source file system to destinationdd if=/dev/loop1p2 of=/dev/loop0p2 bs=512# or:# cat dev/loop1p2 > /dev/loop0p2# Remove device nodeskpartx -d disk1.imgkpartx -d disk2.imgIf the source file system is smaller, you will have to extend the one on /dev/mapper/loop0p2 using resize2fs. If the destination file system is smaller, then you'll have to shrink the source file system to the exact same size as the destination's using your partitioning tool of choice. I'd recommend fdisk.EDIT: With the SD card detected as /dev/mmcblk0 (assuming the destination is the second partition of the SD card, for instance) all you need to do is copy the file system from /dev/mapper/loop0p2, for instance, onto the second partition of the SD card, say /dev/mmcblk0p2.EDIT: If the destination file system is smaller, the source file system shall be reduced to fit the target partition.Shrinking the source file systemAssuming loop devices have been added with kpartx -a and none is mounted:# Check file systems block count and sizetune2fs -l /dev/mapper/loop0p2 | grep -E 'Block (count|size)'tune2fs -l /dev/mapper/loop1p2 | grep -E 'Block (count|size)'# Shrink source file system to fit the destination# (FS check is mandatory before shrinking)fsck -f /dev/mapper/loop1p2resize2fs /dev/mapper/loop1p2 <loop0p2 block count># Proceed with file system copy...resize2fs will fail if there are not enough blocks to fit the requested size (although I never was in such a situation). Should block sizes differ, you may multiply <block count> by <block size> and append B to the result when passing the new file system size:# Example: 123456 blocks, 4096 bytes per blockresize2fs /dev/mapper/loop1p2 $((123456*4096))BOptionally you can shrink the source file system to its minimum size and then expand the destination file system to fit its partition, no calculation involved:resize2fs -M /dev/mapper/loop1p2# Do the copy ...resize2fs /dev/mapper/loop0p2Either will fail only if the operation is not possible.You will most probably get an error at the end of the copy operation because the source partition has not been resized. It should normally do no harm as the kernel won't allow writing past the end of the allocated target partition.You may want to be extremely careful and use dd to limit the amount of data to copy to the destination partition size (mind block size bs and `count'). I'll leave the command and arguments up to you as you have all you need for an educated guess ;-).