12

I have had one pi set up and running good for a while (RPi - 1). I wanted to create an image of RPi - 1 so I can load an exact copy onto my 2nd pi (RPi - 2). The SD card in RPi - 1 is 4GB (only about half is actually used) and the SD card in RPi - 2 is also 4GB. I use Win32 Disk Imager to create a copy of RPi - 1, successfully. I then formatted the SD card for RPi - 2 with SD Formatter 4.0 (on Windows 7). However, when I use Win32 Disk Imager to write the RPi - 1 image onto RPi - 2's SD card I get: "Not enough space on disk: Size 7744512 Available: 7626752 sectors Sector size: 512"

I know its not a bad image because I can format the SD card for RPi - 1 and write the image back to it no problem.

What am I missing here? Are not all 4GB SD cards the same size or do they vary slightly? Is there something you need to do to a brand new SD card that I have missed?

(RPi - 1 SD Card = Sandisk 4GB // RPi - 2 SD Card = Kingston 4GB)

  • 2
    there are NO two SD cards with the same size, unless they come from the same manufacturer, same brand and from the same production batch. you should NOT use SD card image for backup purpose, but copy files instead. – lenik May 9 '14 at 3:12
  • How much space does your computer say is on the empty card? – Beta Decay May 9 '14 at 22:24
6

Indeed not all SD cards are the same exact size. You'd have to shrink the partition to make it fit on the smaller SD. I had a similar problem myself. What I did was have a separate SD with (in my case) Raspbian. And I plugged in my SD I wanted to resize, using a USB card reader.

Before you can safely shrink you partition, you need to make sure there is no data present at the end you are removing. You can do this with resize2fs -M -p /dev/sda2 (you have to substitute /dev/sda2 with the correct path to the USB-card reader and the linux partition you want to resize). This will resize the filesystem to the smallest size possible. You could actually specify the exact size you want, but it involves some math and converting sizes to blocksizes and sectors and what not. Safer and easier to not do this.

After that you can shrink the partion. I think I used fdisk for this. Get the exact start position of the partion, delete it an recreate it using the same start position, but with a smaller size.

Lastly you need to enlarge the filesystem again to occupy the entire partition using resize2fs -p /dev/sda2

  • I just want to clarify this answer a bit as I found it a tad difficult to follow. I first unmounted the partitions on the SD card. Then I had to run e2fsck -f /dev/mmcblk0p2 (I'm shrinking the second partition of mmcblk0) to check for consistency. Then I was allowed to run resize2fs -M -p /dev/mmcblk0p2 as suggested. – audiFanatic Jan 6 '15 at 23:46
1

You'd be better off making a tarball of the filesystem, because when you make a full disk image, you also copy deleted data. When you delete a file, the inode is unlinked but the actual data is still on disk. A full disk image using dd would copy that old data (except if you first overwrite it with /dev/zero, which takes some time) but a backup using tar would only copy the current data.

Or... you could do like many distributions do: reduce your filesystem size to something safe like 1.9 GiB before you make your image, and resize to fill the disk after you restore the backup.

Caveat: I am not familiar with Windows imaging tools, I am a Linux user.

1

if you use linux, here a little script :

#!/bin/bash
# Automatic Image file resizer
# Written by SirLagz
strImgFile=$1

export PATH=$PATH:/sbin

if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This should be run as root ***"
echo "**********************************"
echo ""
exit
fi

if [[ -z $1 ]]; then
echo "Usage: ./autosizer.sh <Image File>"
exit
fi

if [[ ! -e $1 || ! $(file $1) =~ "x86" ]]; then
echo "Error : Not an image file, or file doesn't exist"
exit
fi

partinfo=`parted -ms $1 unit B p | awk '{if (NR!=2) {print}}'`  
fileSystemType=`echo "$partinfo" | grep -o 'ext4\|ext3'`
numberOfMatchPart=`echo "$fileSystemType" | wc -l`

if [[ $fileSystemType == "" || $numberOfMatchPart -eq 0 || $numberOfMatchPart -gt 1 ]] ; then
echo "Error : Your partition layout is not currently supported by this tool."
exit
fi

partnumber=`echo "$partinfo" | grep $fileSystemType | awk -F: ' { print $1 } '`
partstart=`echo "$partinfo" | grep $fileSystemType | awk -F: ' { print substr($2,0,length($2)) } '`
loopback=`losetup -f --show -o $partstart $1`

e2fsck -f $loopback

minsize=`resize2fs -P $loopback | awk -F': ' ' { print $2 } '`
minsize=`echo $minsize+200000 | bc`

resize2fs -p $loopback $minsize
sleep 1
losetup -d $loopback

partnewsize=`echo "$minsize * 4096" | bc`
newpartend=`echo "$partstart + $partnewsize" | bc`

parted $1 rm $partnumber
parted $1 unit B mkpart primary $partstart $newpartend
endresult=`parted -m $1 unit B print free | tail -1 | awk -F: ' { print substr($2,0,length($2)) } '`
truncate -s $endresult $1

credit : Sirlagz (soooo sorry !)

  • 4
    would you be so kind to explain what this script is supposed to do? how can I specify the required image size, by telepathy? – lenik May 9 '14 at 12:15
  • the script downsize an img, take the used size, add 200000 to end (for log etc), after a boot with this img you need to enlarge to fit size the new SD card ... – Gilles Grandguillaume May 10 '14 at 3:47
  • Hi, I wrote this script so feel free to shoot me any questions on my blog : sirlagz.net - This script needs to be run on Linux. The script will downsize the image to the minimum size possible, plus a little bit of slack space. This allows you to put a 8 gb image (of which only 1gb is used) onto a 1gb card. – Lawrence Jun 11 '14 at 11:57
  • it should use "partstart=echo "$partinfo" | grep $fileSystemType | awk -F: ' { print substr($2,0,length($2) - 1) } ' " to get rid of B in partstart. – netawater Oct 25 '15 at 13:25
  • @netawater i have a different result for this command with kubuntu/debian : sirlagz.net/2013/03/10/script-automatic-rpi-image-downsizer/… ... i use this script without "-1" with success ! i don't understand. – Gilles Grandguillaume Oct 26 '15 at 14:04
1

I know this is an old article, however, I just had the same issue and resolved it using the method described here http://www.aoakley.com/articles/2015-10-09-resizing-sd-images.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.