skim32
Posts: 2
Joined: Thu Mar 08, 2018 10:12 pm

Creating an Image after setting up a PI

Thu Mar 08, 2018 10:18 pm

I have my golden image for my raspberry pi with various servers and applications on it. I wanted to make an image of it so I can install it on multiple SD cards. Regardless of the size of the SD card. I tried all the major USB Image tools (w32diskimager, usbimage and etc.) However I am wondering how to create and image that will stretch to any size SD card. All the apps that I could find can only do block level imaging so if you start with a 32gb card, you need another 32gb or larger card to use the image. It has to be possible because the Raspian image that I can download from this website does it just fine. For example I can install raspain and 4gb card and it will install and stretch the volume to 4gb. And I can put it on a 32 and it will stretch it as well. However any image I make can only be put on the same size card or larger. Any ideas?

klricks
Posts: 7574
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Creating an Image after setting up a PI

Fri Mar 09, 2018 12:38 am

Raspbian has a SD copy utility which will make an exact copy of the running SD card (same size as original).
[Menu] -> [Accessories]
You will need to attach a USB SD card reader to the RPi.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

droleary
Posts: 174
Joined: Fri Feb 09, 2018 3:45 am
Location: Minneapolis, MN USA
Contact: Website Skype

Re: Creating an Image after setting up a PI

Fri Mar 09, 2018 4:05 pm

I'd argue that imaging is the wrong solution for what you're doing. Odds are pretty good that each machine is going to have some other settings that differ beyond just the SD card size. Consider instead creating a metapackage for Debian or using configuration management tools.

gkaiseril
Posts: 679
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Creating an Image after setting up a PI

Fri Mar 09, 2018 5:06 pm

Making an exact image for another PI will require some editing of the configuration files on the copy to change the Pi's unit name and IP address at the very least. If these features are not modified a conflict in the network will arise and cause no end of problem.

SD copy or piclone can make copies of the running image to a larger or smaller SD card so long as there is enough space for the image data being copied.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

epoch1970
Posts: 6126
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Creating an Image after setting up a PI

Fri Mar 09, 2018 6:45 pm

Resize2fs can extend an ext filesystem, but it can shrink it, too.
  • Take an image of your SD,
  • Loop-mount that image,
  • Shrink your filesystems, note their sizes,
  • Create a new image file, loop-mount it, create the adequately sized partition table in it,
  • With dd stream the right amount of bytes from each partition in the master image to those on the new image.
NOTE. In your master you would need to add some "run once" script that will expand the image to the actual size of the SD at first boot: i. alter the partition table, ii. resize2fs to expand the filesystem.

Quite fiddly overall... Parted, or Gparted on a desktop should be able to take care of the filesystem+partition resizing bit on a loop-mounted image.
I think qemu-img can be used to mount, shrink, etc. image containers better than using truncate or dd. The excellent Nairobi Embedded (try their ALSA docs!) has a nice write-up on using qemu-img.
(losetup mentioned in that page is too old. Use kpartx instead if needed)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
procount
Posts: 2454
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Creating an Image after setting up a PI

Fri Mar 09, 2018 8:59 pm

Instead of a creating an image, you could create an archive of each partition.
Use tar to create the archive then compress it using gzip or xz.
To reinstate on another sd card, create and format your partitions first then expand your archives onto the appropriate partitions. This avoids having to have exactly the same sized sd cards.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

skim32
Posts: 2
Joined: Thu Mar 08, 2018 10:12 pm

Re: Creating an Image after setting up a PI

Sun Mar 11, 2018 3:27 am

Thanks for everyones replies. I am not concerned about IPs and Hostnames. I don't statically assign IPs at the host. Instead I handle that one a DHCP server. Besides IP there isn't much configuration that specific to any one machine. Hostname maybe, but that isn't a big deal for me either. I like some of the ideas suggested. AKA using PiClone. Also creating a backup of each partition by archiving them. However I wish I knew how the Raspbian imgs are created by the Raspbian team. What tool do they use that their images are stretchable, just using etcher or other img tools.

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

Re: Creating an Image after setting up a PI

Sun Mar 11, 2018 6:49 am

skim32 wrote:
Sun Mar 11, 2018 3:27 am
I wish I knew how the Raspbian imgs are created by the Raspbian team. What tool do they use that their images are stretchable, just using etcher or other img tools.
They don't. There is a script that runs on the first boot that expands the file system to fill the card. The script is /usr/lib/raspi-config/init_resize.sh and it is triggered by adding the following to the end of cmdline.txt

Code: Select all

init=/usr/lib/raspi-config/init_resize.sh
When it runs it also removes that entry so that it doesn't run on subsequent boots, and then triggers a reboot.

hoserr9
Posts: 13
Joined: Mon Aug 24, 2020 5:05 pm

Re: Creating an Image after setting up a PI

Fri Sep 18, 2020 8:55 pm

Hi RPI team,
Is there an easier way to create an image off an existing PI installation so you can install on multiple SD cards for Pis? This post is from 2018 maybe 2020 it got better/easier?

Thanks
Jose

bassamanator
Posts: 91
Joined: Wed Jun 10, 2020 7:04 am

Re: Creating an Image after setting up a PI

Sat Sep 19, 2020 2:35 am

image-backup is what you're looking for: viewtopic.php?f=29&t=247568

Pi power tools is a great tool as well and it has a GUI https://github.com/Botspot/Pi-Power-Tools
| Raspberry Pi 4-8GB | 2.0GHz | over_voltage@5 |
| Raspberry Pi OS 32bit |
| https://github.com/bassamanator/raspberrypi-scripts |

rudiratlos
Posts: 216
Joined: Tue May 01, 2012 8:47 am
Location: Germany (old europe)

Re: Creating an Image after setting up a PI

Tue Sep 22, 2020 9:56 am


teamtiem
Posts: 30
Joined: Fri Jun 19, 2020 4:31 pm

Re: Creating an Image after setting up a PI

Tue Sep 22, 2020 8:58 pm

In Raspbian you have under Accessoiries SD Card Copier, You can copy a SD Card to any SD-Card bigger or smaller. The program expand or shrink the root partition as needed. All the room on the copy is used. Only when the data on the original don't fit on the copy it gives a error.

Return to “Raspberry Pi OS”