Monday, 4 June 2012

Raspberry Pi: Expanding the SD Card Partition

32GB card with an expanded root partition and
an expanded swap partition
The Debian image for raspberry pi only totals 1.8GB; so, if your SD card is larger than 2GB, a lot of space will be going to waste.

You can resize the partition directly via the Pi (the hard way), or you can take out the SD card and do it on another PC (the easy way). Let's do that.

Insert the SD card into your other computer. Don't mount the SD card; or if it automounts, unmount it first. Install and open gparted (the friendly GUI for parted):


# apt-get install gparted
# gparted

find your SD card device using the dropdown menu in the top-right. It might be obvious which it is from the partition sizes, but if in doubt, check. You can refer to the system logs:

# tail -n 1 /var/log/messages
Jun  4 17:46:02 ace2 kernel: [321194.297736] sd 9:0:0:0: [sde] Attached SCSI removable disk

Once you've selected the device, you should see a fat32, and ext4, and a swap partition. 
  • the fat32 partition is the boot (/boot) partition
  • the ext4 partition is your root (/) partition
  • the swap partition is for swap space, but is by default unused by the Pi
You have various options here. To expand the root partition leaving other partitions untouched:
  • select the swap partition, right-click and select Resize/Move. In the window that appears, drag the partition to the end of the available space, until "Free space following (MiB)" shows 0.
  • select the ext4 partition, right-click and select Resize/Move. In the window that appears, drag the arrow on the right of the partition to the end, until "Free space following (MiB)" shows 0.
  • ensure you have backed up any important data
  • click the tick to "Apply all operations". This may take a few minutes. 
Now when you boot your Pi, your root partition should be considerably larger.

Swap space
4GB SD card with an expanded root partition and no swap

You are free to delete the swap partition instead of just moving it. By default, your Pi has swap space disabled, hence it is just wasted space anyway. 

Alternatively, you can enable use of the swap space via the swapon command or by editing /etc/fstab. I think the reason this isn't done by default is twofold:
  • it will make heavy use of your SD card in terms of IOPs, thereby reducing its lifespan
  • random read/write performance is pretty poor for SD cards, hence this might slow you down considerably
I think it's best to leave swap disabled, and delete the partition if you see fit.