3 3
peter12

tutorial Clone/backup bootable microsd card - make as small as possible image

Recommended Posts

Hi there, this very short tutorial is a solution when you need to backup/clone/save as small *.img file as possible of your whole fully bootable system (e.g. you have 8GB card but you want to make smaller system image for 2GB card). And another reason why I created this tutorial - I need to burn the same image to many microsd cards.

 

I am using Windows (yup, hate me know) :) and Debian in these steps:

  1. Put your microsd card to Windows machine and make image of card with Win32diskimager. If your card is e.g. 8GB, you get *.img file with the same size (my name backup.img).
  2. Now in linux maxine (in my case Debian virtual machine) we are going to work with backup.img file.
  3. Run these commands in terminal (if you are not root use sudo at the start of each line)
    modprobe loop
    losetup -f
    losetup /dev/loop0 backup.img
    partprobe /dev/loop0

     

  4. Run gparted with this command and move slider of main partition to the left to make partition smaller (leave some space, e.g. I left 400MB free space)

    gparted /dev/loop0

    Click on Apply once you are happy with it.

  5. We can unload loopback device now

    losetup -d /dev/loop0

     

  6. With fdisk find out end of last block

    fdisk -l backup.img

      

  7.  In my case last block is 3571711 so I run command 

    truncate --size=$[(3571711+1)*512] backup.img

     

  8. Done! Size of your backup.img file should be now about 1.5GB.

 

 

You can burn this new backup.img file to another (or the same) card/s now. In my case I dramatically reduced time needed for burning data to many many cards (it saves hours/days when you need to burn a lot of microsd cards). Enjoy!

 

P.S. this can be done also with just Debian, so no need for Windows, in this case you just need to make first step with backup.img with linux command (e.g. dd). And of course you can set up a script on your newly burned card to expand system partition to the whole microsd size during first boot, if needed. 

Share this post


Link to post
Share on other sites
On 25.10.2017 at 7:43 PM, peter12 said:

this can be done also with just Debian, so no need for Windows, in this case you just need to make first step with backup.img with linux command (e.g. dd)

 

Better use ddrescue for this (apt install gddrescue). Then to get an idea how much to shrink the partition one could mount /dev/loop0p1 (in case it's a single partition image -- better check /proc/partitions) and look at the output from 'df -k' then und umount the partition. And in case it's not about to create an image that gets burned directly after but to archive an image (compressed) then it's a good idea to mount the shrinked partition and zero out all empty space:

dd if=/dev/zero of=/mnt/loop0p1/zeroes bs=1M || (sync ; rm /mnt/loop0p1/zeroes ; umount /mnt/loop0p1)

 

On 25.10.2017 at 7:43 PM, peter12 said:

And of course you can set up a script on your newly burned card to expand system partition to the whole microsd size during first boot, if needed.

 

Why re-inventing the wheel? The way better alternative is /etc/init.d/resize2fs start. And cloning already existing installations is dangerous for a variety of reasons since you end up with same SSH keys on all devices and on some boards MAC addresses of Ethernet or Wi-Fi are stored in files below /etc (so you end up with duplicate MAC addresses in your network causing all sorts of 'funny' troubles).

 

If anyone starts to think about cloning Armbian installations that were already booted (and ran /etc/init.d/firstrun therefore), it's strongly recommended to read and understand this script (except the useless do_firstrun_automated_user_configuration function). Since the best idea is to prepare the installation to clone in a way that on new boards both /etc/init.d/firstrun and /etc/init.d/resize2fs get started again (even if a full expand is not wanted, better shrink the partition before to an absolute minimum and use the documented tweaks to control resizing later)

Share this post


Link to post
Share on other sites
Quote
On 10/26/2017 at 12:38 PM, tkaiser said:

Since the best idea is to prepare the installation to clone in a way that on new boards both /etc/init.d/firstrun and /etc/init.d/resize2fs get started again (even if a full expand is not wanted, better shrink the partition before to an absolute minimum and use the documented tweaks to control resizing later)

Could you please show the right way, how to run the scripts in the clone?

 

Share this post


Link to post
Share on other sites
1 hour ago, moebius said:

Could you please show the right way, how to run the scripts in the clone?

 

No, since it's necessary to...

On 26.10.2017 at 12:38 PM, tkaiser said:

read and understand this script (except the useless do_firstrun_automated_user_configuration function)

 

Depending on the board you use and what you want to achieve different adjustments are needed before you execute the following two lines and power off the board prior to cloning:

systemctl enable firstrun
systemctl enable resize2fs

 

Share this post


Link to post
Share on other sites

thanks! 

My situation is, that i have "unprepared" images (pi0) from backups and now i wanted to setup a clone.

After writing the clone to the sd card, i could do the preparation steps., but how to enable a service in an offline system?

 

I dont really understand the script, so i dont know which parts of it i need, to bring up the clone (i only need a wifi connection and ssh access).

 

Now, after some try and error, i've manually edited the xradio_wlan.conf file to change the mac address...

Share this post


Link to post
Share on other sites

pishrink does all that in a script....just copy the sdcard to a *.img file...then run pishrink...and viola

 

for a "backup"

as tkaiser said run

systemctl enable resize2fs

before you

shutdown -h now

and image your sdcard

 

For a clone to other SBC's...do both of what he said :)

Share this post


Link to post
Share on other sites
2 hours ago, WarHawk_AVG said:

https://github.com/Drewsif/PiShrink does all that in a script

 

By default Pishrink will even resize the image to the maximum (which is IMO not the best strategy) at next boot: https://github.com/Drewsif/PiShrink/blob/8aae06a4b20b1638237e4a5442a26ebb3fe6a3b1/pishrink.sh#L11 (you need to call the script with -s to skip auto resize at next boot). Disclaimer: I've neither used the script before nor know how good it copes with all partitioning schemes Armbian supports (when it's targeting the Raspberry Pi then the script's assumption will be '1 FAT partition followed by 1 ext4 partition' while with Armbian it's completely different)

 

In Armbian we do the resize never to full capacity but 'waste' 5% on SD cards of 4GB in size (or smaller), 2% with up to 8 GB and 1% above. The reason is to allow people to clone cards to another one 'of same size' (but two cards 'of same size' from different manufacturers never have the same capacity but differ slightly) and to help old/crappy cards with wear leveling (that's the 'leave a 5% spare area on old/small SD cards' rule explained above).

 

BTW: The above commands also work after shutting down the board. Simply execute them followed by a reboot and you're done.

 

BTW 2: All occurrences of resize2fs here in the forum are soon worthless since the service will be called armbian-resize-filesystem in the future. Same with firstrun which will be called armbian-firstrun. Of course all of this is stuff for our documentation but for whatever reasons contributions to Armbian are only declining...

Share this post


Link to post
Share on other sites

sure enough..if one was to edit the pishrink.sh script and change this in the script 

should_skip_autoexpand=false

to true...then no need to add the extra -s

I need to make sure I edit all my howto's with either the edit or your -s option

Share this post


Link to post
Share on other sites
3 3