New December 2017

Raspberry Pi SD Cards and Backups

 

 


Not all SD Cards are equal size and how to get round this problem


 

When you create a new file system for a Raspberry Pi on a SDCard the menu allows you to expand your new file system to take advantage of the full space available on the SDCard. This is necessary as the original image has very little free space.

 

Sometimes you might want to transfer a copy of the whole image on the SDCard to another card, there are a couple of reasons that I have for this:

  1. I keep a backup of my SDCards on my hard disc so if I manage to corrupt the card I can reload the image with all the changes I had made to get things to work. This is addition to more frequent backups of the home directory.

  2. To clone a working image to another SDCard for use on another Raspberry Pi. In this case you need to make some changes to the identity of the new computer and password but that is a lot quicker and easier than re-typing all the changes you have made to get a working installation.

Recently I needed to clone the SDCard I use with a PiZero to operate my Pi-Hut Christmas trees. No problem or so I thought. I removed the working SDCard from the PiZero and used Win32DiskImager to copy an image of my card to the hard disk on my desktop. As this was an 8GByte image this took a while but was eventually copied and verified.

 

I then tried to copy this to a new 8GB microSDCard but was stopped by Win32DiskImager giving an error message stating that there was insufficient space for the image on the new card. Rather confused I reformatted the new card and tried again with the same result.

 

After a lot of head scratching and trying to sort out the true information from the wrong information on the Internet I discovered that not all SDCards are exactly the same size when manufactured. The memory chips are made with spare blocks in case some fail during testing. In some cases there are few failures and you get a card with more storage space and in others there are more manufacturing defects and the card has a slightly lower capacity.

 

As a result if you started with an image of a larger card it will not fit onto one that is slightly smaller though both are described as the same size in this case 8GBytes.

 

I could not check the exact size in windows so I used GParted (Linux Gnome Visual Partition Editor) on a laptop running Linux Mate to find the exact size of the two SDCards. This gave the following results:

  1. First Card: 15,353,856 sectors of 512KBytes

  2. Second Card: 15,126,528 sectors of 512 KBytes 227,328 sectors smaller ( about 114 MBytes)

Both cards are from the same manufacturer so it is not a case of different manufacturers supply different sizes.

 

To copy the image to the new smaller card I needed to reduce the size of the original image so that it would fit on the new smaller SDCard.

 

There is no option on the Raspberry Pi to shrink the file system and using GParted on a running system is not recommended and may not be possible. As I had a copy of the original (large) image on my hard disk I decided that the best way was to write this to a larger SDCard, in my case I used a 16GByte.

 

Following writing and verifying this I used GParted on my Linux Laptop to shrink the size of the RasPi file system. Luckily it appears that RasPi Linux uses the start of the area first and the higher part was empty so I was able to resize it with no problems. A couple of points to help here:

  • Linux Mate I am running on my laptop and probably other versions of Linux mount the SD Card when you insert it into the laptop. To resize the partition you need to use the use the GParted option to unmount the SD Card.

  • As usual with Linux tools they are made for those who know what they are doing and are not fool proof ! Make sure you unmount and resize the SDCard not the hard drive(s) on your laptop !

  • To decide how much to reduce the size of the partition you need to work out the new size required from the number of blocks in your original (large) image and the space available on the smaller card. It is better to make the image smaller as long as you do not shrink it into the used space on the card. That way the image can easily be expanded to the full size when you put the card into the Raspberry Pi.

SD Card Partions in GParted

 

The image above shows the 16 GByte SD Card following the un-mounting of the ext4 partition /dev/sdb2 in this case prior to using GParted to shrink the partition. The yellow area shows the used part of the partition. When you insert a SD Card into a Linux machine it is automatically mounted - you have to un-mount the partition to be shrunk before the controls are enabled to allow you to shrink it.

 

When you select the ext4 partition the size will be shown (in blocks and MBytes) so you can reduce the size by the appropriate number. There is little harm in reducing it more than is required as long as you do not try to shrink the used area. The partition can be expanded to the full size of the SD Card later using the Raspberry Pi function after booting.

 

Don't try to resize the fat32 boot partition as this is required for the OS to load. Incidentally this is the only part Windows can see - it does not recognize the ext4 partition.

 

When the partition has been shrunk the (16Gbyte in this case) SDCard can be removed from the laptop and Win32DiskImager can be used to copy this image to your hard disk. NB Ensure you tick the 'Read only Allocated Partitions' box before starting if  you fail to do this you will end up with an image the full size of the SDCard you used rather than the new small image you were trying to create.

 

Note: When using Win32DiskImager the interface is not intuitive - 'Read actually reads from 'Device' and writes to the image file. While write does the opposite - the tooltips are useful as a reminder of what is happening.

 

 

 


John Murrell V1 3rd December 2017

  


 

Clicking on the Home logo will return you to the home page