KZL
Posts: 66
Joined: Fri May 17, 2013 11:58 pm

Merging of .img

Sat May 17, 2014 8:34 pm

Hi everyone,

I saved 2 images from my raspberry. One to boot and an another with the main partition of the raspberry.
I saved the both separately, because I modified the main partition.
Now I would like to merge the both. Someone know how to do that ?

Thanks :-)

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Merging of .img

Sat May 17, 2014 10:30 pm

It's not directly possible.

You could mount both images as an -o loop filesystem and use cp or mv as appropriate. Best of luck with that.

The, probably, easier way is boot one img on your RPi and copy out configuration files and user data (from /root, /home, /etc & /var/) to a USB stick, boot the other image and copy things back from the USB stick. It depends how much you've changed from a stock Raspbian installation how much effort that will be.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KZL
Posts: 66
Joined: Fri May 17, 2013 11:58 pm

Re: Merging of .img

Sun May 18, 2014 2:41 pm

Hello,

Thank you, to have taken a few time for me :-)
I will explain my main goal, it's maybe better. I want to save my sd card, so I use a nfs sharing thanks my nas, to have a lot of space available with my raspberry.

I need to shrink the final image to the minimum size :
- So I exported with dd command the whole image of /dev/mmcblk (Size : 8GO)
- Then I used "losetup" to create a loop fs of the both partition of my image of 8GO. (boot : /dev/loop1; main partition : /dev/loop2)
- Futhermore, I used zerofree to put all bit 1 unused to 0 in loop2.
- Then I used resize2fs to resize loop2 to approximately 2,5Go (info given by resize2fs)
- Now, I need to export de both loop and create a new image of my distro.

How can I do that ? Because at first, I had /dev/mmcblk, now only /dev/loop1 /dev/loop2 separately.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Merging of .img

Sun May 18, 2014 2:46 pm

If you write a new partition table (you'll have to make sure you get the sizes and formats correct) you can copy back the individual partitions with dd.

On Raspbian, mmcblk0p1 partition (primary type 0x0E, FAT, /boot) normally runs from 8192 to 122880. The mmcblk0p2 (primary type 0x83, Linux, rootfs) partition normally runs from 122881 to END.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: Merging of .img

Sun May 18, 2014 4:06 pm

DougieLawson wrote:On Raspbian, mmcblk0p1 partition (primary type 0x0E, FAT, /boot) normally runs from 8192 to 122880. The mmcblk0p2 (primary type 0x83, Linux, rootfs) partition normally runs from 122881 to END.
You mean 8192 to 122879, and 122880 to END.

It is important that all start sectors are at least a multiple of eight (ie. 4KiB), preferably a multiple of 2048 (ie. 1MiB) or more. Otherwise, blocks within the filesystem are misaligned with respect to physical blocks in the card. That makes extra work for the SD controller, which will harm performance and may reduce card longevity or even trigger firmware bugs.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Merging of .img

Sun May 18, 2014 4:56 pm

jojopi wrote:
DougieLawson wrote:On Raspbian, mmcblk0p1 partition (primary type 0x0E, FAT, /boot) normally runs from 8192 to 122880. The mmcblk0p2 (primary type 0x83, Linux, rootfs) partition normally runs from 122881 to END.
You mean 8192 to 122879, and 122880 to END.
You are not wrong.

Of course, what I do is fdisk -l /dev/sda > /somewhere/safe/sda.parts.txt so that I'll know what it looked like when I come back to it in three months time.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KZL
Posts: 66
Joined: Fri May 17, 2013 11:58 pm

Re: Merging of .img

Mon May 19, 2014 4:35 pm

I will do that ! Thank DougieLawson.

But I don't understand why images start from 8192 to 122879 ?
There is nothing from 0 to 8192 ?
Futhermore, why I have to write from 8192 to 122879 and not 122880 ?
1022880-8192 = 114688 bytes = 112*1024 bytes, there is the space number 0 ?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Merging of .img

Mon May 19, 2014 5:07 pm

The MBR and partition table are at 0.

The first partition starts at 8192 to optimise the SD card access. If the filesystem blocks don't align with with the SD card blocks then the card will have to update two blocks instead of one for every write and wear out faster. The amount of wasted space is trivial.

Return to “Advanced users”