User avatar
bstanard
Posts: 16
Joined: Tue Oct 11, 2016 8:25 pm

HOWTO: create an SD image to use as a master?

Mon Mar 20, 2017 8:45 pm

In setting up for a summer of Pi with 30 students, I have added one program (GIMP) to the most recent Raspbian image and want to use it as a master from which to create 30 micro SD cards. When I added GIMP to my Pi and then used Win32DiskImager to copy the GIMP-loaded Raspbian image to my PC, the image jumped from 4.2 GB to 15.2 GB, making copying it onto my 30 SD cards impossible. (a) What happened? and (b), How can I create a usable image for making multiple SD cards.
Last edited by bstanard on Tue Mar 21, 2017 1:39 pm, edited 2 times in total.

chalky_flint
Posts: 10
Joined: Fri Mar 10, 2017 1:04 pm

Re: HOWTO: create an SD image to use as a master

Mon Mar 20, 2017 8:53 pm

I assume you meant GB rather than MB?

My guess is, that on first boot, the second partition is resized to the size of the card.

User avatar
bstanard
Posts: 16
Joined: Tue Oct 11, 2016 8:25 pm

Re: HOWTO: create an SD image to use as a master

Mon Mar 20, 2017 9:07 pm

Thanks for catching the MB/GB error, chalky_flint.

User avatar
topguy
Posts: 5781
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: HOWTO: create an SD image to use as a master

Mon Mar 20, 2017 11:00 pm

When you add "HOWTO" to a subject (and dont add a "?"), its usually because you have created a guide for someone else to follow.

Win32DiskImager does what its name implies, it makes an image of the whole disk/card/stick. It doesn't matter what sizes the partitions are.
Use a 4GB card if possible, remove "Wolfram" if you need the space for GIMP, then your image will be 4GB.
If you burn the 4GB image on 32GB cards then you probably need to run the "expand-filesystem" again to expand the partitions to fill the card.

ghans
Posts: 7873
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: HOWTO: create an SD image to use as a master

Tue Mar 21, 2017 7:18 am

You propably want to use gparted to shrink your images second partition. gparted is available as Live CD ,
but does not work on Windows at all.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: HOWTO: create an SD image to use as a master?

Wed Mar 29, 2017 7:56 am

When a new SD card is loaded with a new version of the OS, /boot/cmdline.txt contains a command to execute a script on the very first boot of this SD card. It expands the filesystem to use all space on the SD card, then removes itself from the cmdline file. This automatic method was added to help prevent SD card corruption, I think.

When you run the RPi, it also creates a large swap file space on the SD card that gets saved into the backup image.

For your particular situation, you can change that behaviour by eliminating the expansion statement before you first boot with the card, and turn off swapping to save space on the SD card partition.

Here is a simple, although a little cumbersome method. Depending on your skills, it can be shortened, but this should work. When you use different RPi models, this may not work. Stick to one model for all of this.

Create a new SD card image, like you normally would.
Leave the SD card in the PC after the image has been written. Make a copy of the original cmdline.txt file in the DOS partition of the SD card to make a backup. Edit the original file and remove the expansion command (I can't tell you what it is right now, but it's pretty much self explanatory). Take note of the command(!), and remove it from that single line.

Save the file and then boot the card in the RPi. The best thing is to switch off swapping right away. Execute

Code: Select all

sudo systemctl stop dphys-swapfile.service
Now do your setup (do not expand the filesystem) and load whatever software you need. Make sure it all works. Remove the swap file:

Code: Select all

sudo rm /var/swap
Shutdown the RPi and save that image on your PC. That image should not be much larger than the original one. Keep that image secure!

After you have saved the image on the PC, create new SD card with the saved image of your working system. On that image, you can put the statement back into the cmdline.txt file, or use the backup version, so the partition will be expanded automatically at first boot. Do NOT put that card in the RPi!

Note: When you copy the cmdline.txt file, make sure that Windows does not mess with the file extensions, if in doubt, use the DOS command box.

Save that image on the SD card back on the PC, it will be your master, so keep it safe.

Create a child copy from your master SD image and try that in an RPi before you really start duplicating. The first time you boot, the RPi will boot again after it has expanded the partition on the card. Check everything before you start duplicating.

The added benefit is that you can now also use SD cards with different sizes.

Success!

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: HOWTO: create an SD image to use as a master?

Wed Mar 29, 2017 11:40 am

On the latest Raspbian (and we can assume the TO want's to use the GUI-version, because he plans to install 'gimp') is not much space for any installations, if no expansion is done first!
Tested few minutes ago:

Code: Select all

[email protected]:~# mount -o loop,offset=70254592 /home/user/Work/2017-03-02-raspbian-jessie.img /mnt/tmp/
[email protected]:~# df -k /mnt/tmp/
Dateisystem    1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
/dev/loop0       4155624 3797936    146592   97% /mnt/tmp
Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: HOWTO: create an SD image to use as a master?

Wed Mar 29, 2017 11:55 am

Good point FM81,

In that case, I can see two options that can be used.
1. Delete some bloatware before installing Gimp. Things that come to mind are wolfram-engine, games like scratch minecraft-pi, penguinspuzzle, sonic-pi, dillo, gpicview, oracle-java8-jdk, openjdk-7-jre, oracle-java7-jdk, openjdk-8-jre and maybe libreoffice?
After removing, run: sudo apt-get clean and sudo apt-get autoremove

2. Do the first installation on a small SD card, say 4GB. Allowing to automatically expand that partition size will not cause too many problems with the size.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: HOWTO: create an SD image to use as a master?

Wed Mar 29, 2017 1:14 pm

Make a copy:

Code: Select all

[email protected]:~# cp /home/user/Work/2017-03-02-raspbian-jessie.img /home/remaster/RPi/
Create a dummy of 1GB:

Code: Select all

[email protected]:~# dd if=/dev/zero of=/tmp/null bs=1M count=1024
Append the dummy:

Code: Select all

[email protected]:~# cat /tmp/null >> /home/remaster/RPi/2017-03-02-raspbian-jessie.img
Verify new size:

Code: Select all

[email protected]:~# ls -l /home/remaster/RPi/2017-03-02-raspbian-jessie.img
-rw-r--r-- 1 root root 5467275264 Mär 29 14:52 /home/remaster/RPi/2017-03-02-raspbian-jessie.img
Setup as loop-device:

Code: Select all

[email protected]:~# losetup /dev/loop1 /home/remaster/RPi/2017-03-02-raspbian-jessie.img
Use FDISK to make changes:

Code: Select all

[email protected]:~# fdisk -lu /dev/loop1
Delete second partition and create it new again with same start (137216 in the actual raspbian), but with new end which should be given per default by the FDISK-program.
IMPORTANT: You should exactly knowing, what you're doing and why at this step!

Release old loop-device:

Code: Select all

[email protected]:~# losetup -d /dev/loop1
Reassign again, but now only it's second partition:

Code: Select all

[email protected]:~# losetup -o 70254592 /dev/loop1 /home/remaster/RPi/2017-03-02-raspbian-jessie.img
Check the changed filesystem:

Code: Select all

[email protected]:~# e2fsck -f /dev/loop1
Resize it:

Code: Select all

[email protected]:~# resize2fs /dev/loop1
Release loop once more:

Code: Select all

[email protected]:~# losetup -d /dev/loop1
Repeat check from my previous post:

Code: Select all

[email protected]:~# mount -o loop,offset=70254592 /home/remaster/RPi/2017-03-02-raspbian-jessie.img /mnt/tmp/
[email protected]:~# df -k /mnt/tmp/
Dateisystem    1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
/dev/loop1       5188136 3797936   1126676   78% /mnt/tmp
If you like, to work "clean" don't forget to umount '/mnt/tmp' at last!
Then continue as described by 'paulv' ... (Remove resizing part from 'cmdline.txt' etc.)

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5087
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: HOWTO: create an SD image to use as a master?

Thu Mar 30, 2017 1:41 pm

@bstanard, As a possible alternative to the above suggestions it may be worth trying Raspbian's own "cloning tool" (SD card copier under the Accessories menu, otherwise known as piclone) to either write cloned copies to a USB card reader/writer plugged into the Pi or (probably to a USB stick) write an image file to be saved for later use with other image file writing software on another computer.
Trev.
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my 2xP3A+, P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
bstanard
Posts: 16
Joined: Tue Oct 11, 2016 8:25 pm

Re: HOWTO: create an SD image to use as a master?

Thu Mar 30, 2017 2:00 pm

Trev,
Thank you! That is exactly what I tried a day ago, and it seemed to work without a hitch.
Bill

Return to “Advanced users”