Friday, December 9, 2016

raspberry pi resize sd card NOOBS image

For some reason we ordered a 16G SD NOOBS card with our Pi, but since I have to CM our SD card image it would be convenient to have a smaller SD card image. Resizing partitions is often done with Linux and the Pi in particular, but it turns out that we shot ourselves in the foot by getting NOOBS because apparently its nontrivial to resize in that case. I could just download a new copy of Raspbian, and in fact I've done that but it came with a bunch of extra stuff and the UI is wildly different from the Debian look, whereas whatever version was installed with NOOBS is basically perfect and looks only slightly different from Debian. So I'm looking into how to remove NOOBS from the installation that I am presently using, and then doing the resizing.

Here is a beautiful tutorial on how to use gparted to resize the partitions, but (*sniff*) it can't be used with a NOOBS image. The author of this page writes so clearly that I have complete confidence in everything written here and reading it will help me understand the general process better: http://www.aoakley.com/articles/2015-10-09-resizing-sd-images.php

Note: The primary tools required for resizing partitions are gparted and dcfldd.

Here is another instructable which seems nice and leads with an awesome "Honey I Shrunk The Pi Image" graphic but is again inappropriate for NOOBS. However, in the comments there are a lot of great links for doing this process different ways: http://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/

This instructable claims to explain how to do it on a PC, but all it's doing is explaining how to use WinDiskImager and 7Zip. At the end there are some limp handwavings about using gparted to shrink partitions.http://www.instructables.com/id/Backup-Your-Pi/

So, a google search for how to just scrub NOOBS from an installation produced a lot of good hints:

In response to this question, a bunch of unhelpful people say just to give up, until somebody adds on a comment explaining how they do it: http://raspberrypi.stackexchange.com/questions/13324/deleting-noobs-from-sd-card-and-only-keep-raspbian

This forum thread was referenced by the helpful answer in the link above. Interestingly, it is identically named to the one above. It has the same unhelpful answers as the first but the one helpful answer that inspired the helpful answer in the link above is somewhat terse: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=68072

The helpful response in the second link up from this one references this wiki article which is amazingly informative and explains why there are a pile of partitions on a NOOBS installation and why they can be safely removed: https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained

Finally, I stumbled across a link where step-by step instructions on how to remove NOOBS was provided! Walking through the below was super instructive, and for even more instruction I had to figure out one small thing which I had to do differently.

Here is the link on how to remove NOOBS: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=106529

So, the thing that I had to do differently was that since my SD card came pre-formatted with one partition, I had to do an extra step first to remove the partition. Wow! Before doing anything else in fdisk in the first step, I had to use the 'd' command to remove partition one, then write the result, then everything else worked exactly as written.

Now I had a new 16G SD card with Raspbian only, with just two partitions, boot and root.

Unfortunately, this sad version of Raspbian didn't have gparted or dcfldd. While I was googling around for options of doing the partition resize with only parted and dd, I accidentally stumbled across news which solved my problem without gaving to do any partition resizing at all! It turns out that in the current version of Raspbian, 'Jessie,' there is a new utility called "SD Copier" which not only makes a copy of a pi image, but it also can resize the image up or down automatically to match the size of the SD card being copied to!!! Here's one of the links where this clue was found:

http://tech.scargill.net/raspberry-pi-backups/

I'm using Jessie, but OF COURSE my distribution of it doesn't have this utility installed. From the link below I learned that it is a simple matter to install it using apt-get without disturbing anything else, it's actually a stand-alone program called 'piclone' which is a good name on its own. The link below is a forum post from someone who figured this out accidentally between bouts of rage and people unhelpfully telling him to just do a new download, but in rereading the previous link I can see that the person above's first step was to download piclone before trying it out.

https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=155927&p=1018505

Anyhow, despite an unrelated hardware issue with my home Pi, I got piclone installed on my NOOBSless 16G SD card, and then ran piclone to copy the installation onto a 4G SD card with no problems, swapped SD cards and verified that the 4G card boots. Now I can extract the image from the 4G card and it compresses down to 1G before eveb trying any other tricks to improve compression efficiency.