There are no simple as in one-command-fits-all ways of restoring an image to a smaller SD card.
The way I'd do it (with access to a linux PC), is:
Use dd to copy the first 100MB or so, just to get a partition table and the /boot partition onto the card.
Use fdisk to delete the (incorrectly sized) second partition and create a new one that fits the card.
Use mkfs.ext4 to format that partition.
Mount the part of the image file that contains the Linux partition on a temporary mount point.
Mount the second partition of the SD card on another temporary mount point.
Use tar or cp or something to copy the contents over.
Unmount both partitions.
Something like: (this is not a script, just hints for you)
Code: Select all
# Assuming /tmp/myimage.img is the image file
# and /dev/sdb is the SD card
dd if=/tmp/myimage.img of=/dev/sdb bs=1M count=100
# remove the card reader and plug it back in to make sure the system reads the new partition table!
fdisk -u /dev/sdb
# p to print partition table. Note start of partition 2.
# d delete partition 2
# n new partition 2, use start noted above, defaults for the rest
# w to write and exit.
mkfs.ext4 /dev/sdb2
# mount the part of the image file that we want. Value for OFFSET is start noted above * 512 (bytes)
mkdir /media/image
mount -t ext4 -o loop,offset=OFFSET /tmp/myimage.img /media/image
mkdir /media/sdb2
mount -t ext4 /dev/sdb2 /media/sdb2
cd /media/image
tar cf - * | tar -xvCF /media/sdb2 -
umount /media/sdb2
umount /media/image
Disclaimer: I typed all that off the top of my head. Please check each command as you use it. I believe I got it right, and it should work.
I'm working on another method of backing up one SD card to another on the Pi, which will work for differing sized cards, but needs a bit of minor fiddling to work.