(Originally posted May 6th 2016)
At my day job, we use Raspberry Pi’s. We use them, a lot. So naturally, we see a problems or processes that are difficult, and streamline them as best as possible. Being the primary Raspberry Pi developer here, I had a need to shrink down the images for easy transport to our corporate office, across state lines. The problem though, is some of our development images are 4 or 8 GB. In case you have never tried transferring files that big, Its a pain. So I set out to find a way to shrink the size of the images as much as possible for the sake of bandwidth and disk space.
My first stop on this list was at the suggestion of my peers in the office. Ultimate Boot CD has a Live CD (run without installing) that has plenty of Linux goodies relating to hard drives, and their partitions already installed, with very little diddling required. I highly recommend this.
This is going to be a Windows users’ guide using VirtualBox. It makes things so much easier to do it this way. Otherwise you may need external drives, and a ton more. Make sure when setting up VirtualBox to give yourself at least 1GB of RAM, and enough disk space for your image. For this guide, I will be using a 4GB SD Card, so the gains will not appear to be a lot.. 8GB and others will see better gains.
This tutorial is a bit rough mainly because it took me half a day to find a way to explain it, so hopefully its easier for you than my first go around was. It also takes a lot more time using the VirtualBox, so its also highly suggested to do this on an actual computer instead, but this is strictly for demonstration.
Load up VirtualBox with the Ultimate Boot CD image. Choose the Parted Magic option from the list, followed by choosing Default Settings.
Next, to make things easier, I am going to make a network share on the host machine, and mount it on the VirtualBox. Otherwise, there is a whole slew of instructions on how to use Guest Additions, and its a mess. If you are not familiar with Windows sharing, right clicking the folder will show Properties, then click Sharing tab, and click Share. If you have issues with passwords, Click the Network and Sharing Center link at the bottom, and disable the Password Protection on sharing either for good, or for now, depending on what your network environment is like. (Coffee shop = for now, Home = for good). Once that’s done, make a folder in the root of your VirtualBox called host. Then use your host machines IP Address and share name to add it to /etc/fstab in the VirtualBox. Once done, do a mount -a, and viola. When you are done with that, go ahead and cd /host, for simplicity sake.
Here’s my line in /etc/fstab and some pictures:
//192.168.1.6/VBoxShare /host cifs
It may ask you for a password, but if you took my advice about the password stuff when setting up the windows share, you should just be able to press enter, and your in. Lets keep going.
Now that we are talking to the host machines file system, it will save us the hassle of dealing with excessive disk space. We need to now prep the SD Card. In your VirtualBox window, select the SD Card reader from the Devices list. If all goes well, the software will find it and show it in Partition Magic Mount.
We need to modify a partition on the SD Card. Refer to the path of the SD Card from Partition Magic Mount, and open GParted. From the drop down at the top right, make sure you have the correct disk. In my case, it is /dev/sdc.
Here you can see that the 2nd partition has something like 820 MB of unused space, which is quite a bit. Go ahead and click on the 2nd partition, and click Resize/Move. Drag the right most arrow as far left as possible. Should look something like this:
Click the Resize/Move button, and Apply changes.
If you get errors during this part, drag the arrow as far left as possible, then arrow up on the New Size field about 10MB and it seems to prevent that. Wait as the operation finishes.
A few minutes later, the operation is complete. We have shrunk the required partitions to just under 3 GB. Again, I am only on a 4 GB SD Card, so depending on your image, and software installed, you may see a lot more drastic results. If you want to decrease this even more, consider removing non-essential packages and data from Raspbian or whatever OS you have installed.
Now that we have shrunk the partition, lets get copying. This might take a while, so let it finish.
dd if=/dev/sdc of=MyImage.img
Now if you are like me and are impatient when things don’t give you a status, you can open another terminal window, cd /host, and repeatedly spam ls -lh, which will show you the size of the file.
Once its done, you will have to do one last thing to make it a lightweight version of its former self..
Do an fdisk on the image..
fdisk -l MyImage.img
You will see the partitions we previously saw in GParted, however, we are looking for the number for the End of the Second Partition. In this picture, we see that it is 6225919.
Lastly, we truncate it. Replace my number with yours in the code below, and run it.
Finished! You now have a finished, working, minimized image you can rewrite and expand later on the Pi. You can use this with Win32Disk Imager, Apple Pi Baker, or dd to write it back to the card.
If this helped you, or even if it didn’t, I would love to hear back.