RPi Resize Flash Partitions

This page describes activities relating to partitions on the Raspberry Pi for Linux based operating systems, such as Raspian Linux. It may also apply to other operating systems too, but you should check. Incorrectly using the following instructions is likely to corrupt your system.

The prepared images for the Raspberry Pi are created for SD cards of the size of 2GB. The SD card can be resized or restructured to use the full size of a SD card that is greater than 2GB.

Raspi-config
If using the Raspian or Debian images the raspi-config utility can be used to resize the main partition to fill the SD card. Instructions are on the RPi raspi-config page. Read on if you want to know if you should.

This will happen automatically. You select the menu option "expand_rootfs" in raspi-config and reboot the Raspberry Pi. After a wait while the changes are made, the Pi returns to a command line or graphical login prompt.

Explanation
Storage devices need some structure that allows the operating system to locate existing files and create new files. This is done using partitions and filesystems. For a simplistic explanation, see Partition (basics), which applies to all systems that use partitioning.

A partition is a section of a storage device, which is formatted with a filesystem, onto which the operating system creates a directory structure. The Linux system has a single directory structure starting at the root directory ("/"). Partitions are "mounted" at points in the directory structure, but it still shows as a single structure. Generally, users do not need to know about, or see, how partitions are used. In Windows, each partition becomes a separate drive, referenced by a letter such as C:, D:, and so on. A storage device can have a single partition, or several partitions; changing a partition structure might be seen as a difficult operation to perform without losing data, so that structure should be considered carefully before putting data on to a device. If you skipped it, the Partition (basics) page gives more details.

Backup
You might want to backup your SD before resizing partitions. Windows-Users may use the HDD Raw Copy Tool

Manually resizing the SD card on Linux
Tutorial video here: http://www.youtube.com/watch?v=R4VovMDnsIE

Following on from the instructions above, keep the newly-written SD card in the card reader, but unmounted. We'll use the  (partition editor) tool to resize the partitions.

$ df -h
 * Show partition information to find your SD card

Look for a partition that matches the roughly 2GB size of your distribution image.

$ umount /dev/sdc2
 * Unmount the partition

$ sudo parted /dev/sdd (parted) unit chs (parted) print Disk /dev/sdd: 121535,3,31 Sector size (logical/physical): 512B/512B BIOS cylinder,head,sector geometry: 121536,4,32. Each cylinder is 65.5kB. Partition Table: msdos Number Start      End         Type     File system     Flags 1     16,0,0     1215,3,31   primary  fat32           lba 2     1232,0,0   26671,3,31  primary  ext4 3     26688,0,0  29743,3,31  primary  linux-swap(v1)
 * Use parted to examine the card
 * This shows how my SD card was formatted after writing the image. Notice that nothing uses the card from end of 'cylinder' 29743 to the card's maximum at 121535.
 * Partition 1 is the boot partition: we'll leave that alone. Partition 2 is the root partition, which we'll grow to fill most of the card. Some OS versions will have a Partition 3 for swap space, which needs to be moved to the end of the card. Note that on some other versions of linux (and some other versions of hardware) use /sde not /sdd.

(parted) move 3 118479,0,0 (parted) rm 2 (parted) mkpart primary 1232,0,0 118478,3,31 (parted) quit
 * Move the swap partition if it exists (you'll have to adjust the numbers so that the end of partition 3 is at the end cylinder/head/sector of the card)
 * to calculate the number to use in the following command do:-   so in this example
 * Will not work with parted versions later than 2.4
 * Now grow the root partition. This involves removing the partition, re-creating it, then using  to grow the filesystem to fill the partition. It won't destroy any data.
 * Note that the starting address of the new partition is identical to its original value, and the ending address is immediately before the start of the swap partition.

$ sudo e2fsck -f /dev/sdd2
 * Now clean and resize the root partition. As before, some users may need to use /sde2 instead.
 * (allow it to add lost-and-found)

$ sudo resize2fs /dev/sdd2 pi@raspberrypi:~$ df -h Filesystem           Size  Used Avail Use% Mounted on tmpfs                  94M  4.0K   94M   1% /lib/init/rw udev                  10M  168K  9.9M   2% /dev tmpfs                 94M     0   94M   0% /dev/shm rootfs               7.1G  1.3G  5.4G  20% / /dev/mmcblk0p1        75M   28M   48M  37% /boot
 * Then put the card in the RPi and boot. You end up with a 7Gb partition to use.

Manually extracting partitions from the image on Linux
Get the information about offsets and sizes from the SD-Card-Image:

$ parted -s SD-Card-Image unit KiB print

Here, the -s option directs parted to go into scripting mode and the commands unit KiB print tells parted to display its results in blocks of 1024 byte (KiB, see also block size in GNU Coreutils docu) and print the partition table.

You will get some information like the following (this is created with the Raspbian Wheezy image dated 15-Jul-2012):

Disk SD-Card-Image: 1894400kiB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start     End         Size        Type     File system  Flags 1     4096kiB   61440kiB    57344kiB    primary  fat16        lba 2     61440kiB  1894400kiB  1832960kiB  primary  ext4

Now you can extract the partitions with

$ dd if=SD-Card-Image of=Part1 bs=1024 skip=4096 count=57344 $ dd if=SD-Card-Image of=Part2 bs=1024 skip=61440 count=1832960

Fill in the skip and count parameters with the numbers for start and size, which you got from the parted command above.

Manually resizing the SD card using a GUI with GParted
If you are using a PC with a linux distribution to resize the partitions, you can run GParted to resize the partitions using a GUI. This method is tested on Ubuntu 10.10 using the Gnome desktop. Versions of GParted differ slightly in their GUIs. For Windows users, it is also possible to follow the same procedure (less the installation) using the live version of GParted (GParted Live CD/USB/PXE/HD).

GParted can be installed using: Note: I had to physically remove and re-insert the SD card from the card reader after writing the image before the partitions were recognised properly and the following could be done. You're done!
 * Start GParted (on my system it is ).
 * Select the drive corresponding to your SD card (was  on my system). You now see the partitions mentioned above (with some tiny unallocated areas in between and a large one after).
 * Select the swap partition by clicking on it.
 * If the Resize/Move toolbar icon or  menu option is disabled, go to Partition / Unmount.
 * Select the menu option  and drag the partition to the right (click/drag in the middle).
 * Select the ext4 partition.
 * If the Resize/Move toolbar icon or  menu option is disabled, go to Partition / Unmount.
 * Resize the partition by dragging the right edge of the partition all the way to the right (click/drag the right edge).
 * When you are satisfied with the changes, click on the green check mark, "Return" arrow, or other "apply" control to execute these changes.

Manually resizing the SD card on Raspberry Pi
You can also resize the partitions of the SD card that your Pi is running on.

First you need to change the partition table with fdisk. You need to remove the existing partition entries and then create a single new partition than takes the whole free space of the disk. This will only change the partition table, not the partitions data on disk. The start of the new partition needs to be aligned with the old partition!

Start fdisk:

sudo fdisk /dev/mmcblk0

Then delete partitions with d and create a new with n. You can view the existing table with p.


 * p to see the current start of the main partition
 * d, 3 to delete the swap partition
 * d, 2 to delete the main partition
 * n p 2 to create a new primary partition, next you need to enter the start of the old main partition and then the size (enter for complete SD card). The main partition on the Debian image from 2012-04-19 starts at 157696, but the start of your partition might be different. Check the p output!
 * w write the new partition table

Now you need to reboot:

sudo shutdown -r now

After the reboot you need to resize the filesystem on the partition. The  command will resize your filesystem to the new size from the changed partition table.

sudo resize2fs /dev/mmcblk0p2

This will take a few minutes, depending on the size and speed of your SD card.

When it is done, you can check the new size with:

df -h