RPi Resize Flash Partitions

This page is a guide to modifying partitions on the Raspberry Pi (and Pi clones, such as the Banana Pi) for Linux-based operating systems, such as Raspian Linux, CentOS, Arch, and Linux distributions. Incorrectly using these instructions is likely to corrupt your system.

The prepared images for the Raspberry Pi are created for SD cards usually 2 GB in size. If you install it on a larger SD card than what it was designed for, you can resize or restructure the system, once it is installed, to use the full size of the SD card.

Raspi-config
If using the official Raspian images released by the Raspberry Pi Foundation, you can use the raspi-config utility 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 use it.

The raspi-config tool is fully automatic. All you have to do is launch it, select the option expand_rootfs in the raspi-config menu, and reboot the Raspberry Pi. It takes some time for the changes to be made. Once it's finished, the Pi returns to a command-line or graphical login prompt.

Alternatively, you can just run raspi-config --expand-rootfs to make it a completely non-interactive process.

Explanation
Storage devices need some structure that allows the operating system to locate existing files and create new ones. This is done using partitions and file systems. 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 file system, in 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 the file system remains a single structure. Generally, users do not need to know about, or see, how partitions are used. This is different from Windows, where 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 onto a device.

If you skipped it, the Partition (basics) page gives more details.

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

The first step is to discover where your system accesses your SD card. How the SD card shows up on your system depends largely on whether you have attached it directly into an SD card slot in your computer or into an external card reader plugged into a USB port of your computer.

The best way to discover the storage devices connected to your computer is the lsblk command. Here is the sample output:

$ lsblk NAME       MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT sda          8:0    0 149.1G  0 disk ├─sda1       8:1    0  48.5G  0 part └─sda2       8:2    0  96.3G  0 part mmcblk0    179:0    0   7.5G  0 disk ├─mmcblk0p1 179:1   0   50MB  0 part └─mmcblk0p2 179:2   0     2G  0 part

In this sample outpet, the sda device is the computer's internal hard drive, and the mmtblk0</tt> device is an SD card that has been inserted into the computer's SD card reader.

Here is an example of an SD card in an external card reader that is plugged into a computer's USB port:

sda          8:0    0 149.1G  0 disk ├─sda1       8:1    0  48.5G  0 part └─sda2       8:2    0  96.3G  0 part sdb          8:16   0 465.9G  0 disk ├─sdb1       8:17   0   50MB  0 part /boot └─sdb2       8:18   0     2G  0 part /

If you are not sure which device represents your SD card, just remove your SD card and run lsblk</tt> again. The device that disappears was your SD card, so insert your card again and run lsblk</tt> and take note of the new device in the list.

Backup
If you have any data other than a fresh image on the SD card, backup your SD card before resizing partitions. Windows users may use the HDD Raw Copy Tool.

Linux users can just use dd</tt> (the same command you probably used to get the image onto the SD card in the first place) or the dcfldd command (which shows the progress of the operation unlike dd). For example, to copy all contents off the device that represents your SD card into an .img</tt> file in your home directory:

You should find out the block size of your sd card first (using an improper block size can lead to issues later on... TRUST) $ sudo fdisk -l /dev/mmcblk0

Now, look at this line from the output of the previous command. It will determine what we should use as the bs (block size, not bullsh!t): Units: sectors of 1 * 512 = 512 bytes So, bs=512 would be the proper syntax in this instance. Other examples could be: bs=2MB (for block size = 2 Megabytes), or bs=4MB (for block size = 4 Megabytes), without specification, dd/dcfldd default to the bytes value.

And finally, to copy all contents off the device that represents your SD card into an .img</tt> file in your home directory: $ sudo dd if=/dev/mmcblk0 of=$HOME/sdbackup.img bs=512

Resizing
With the SD card in your computer or card reader, make sure it's unmounted. Use the correct device designation. In this example, I use mmcblk0</tt> but yours may differ:

$ sudo umount /dev/mmcblk0

Use the  (partition editor) tool to resize the partitions.

Use parted</tt> to examine the card:

$ sudo parted /dev/mmcblk0 (parted) unit chs (parted) print Disk /dev/mmcblk0: 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)

Nothing uses the card from end of 'cylinder' 29743 to the card's maximum at 121535. Partition 1 is the boot partition. Leave that one alone. Partition 2 is the root partition, which can afford to grow to fill most of the card. If your distribution has a Partition 3 for swap space, then you need to move that to the end of the card. If your card does not have a swap partition, or if its swap partition is Partition 2, then you can skip the swap step.

Swap
To move the swap partition (if it exists between your root partition and the end of your SD card), first calculate how many cylinders your swap partition needs to move. To calculate the number to use:.

In this example:

Assuming you are using parted</tt> version 2.4 or greater:

(parted) move 3 118479,0,0

Expand the partition
To grow the root partition, you must first remove the partition boundaries, then recreate the partition as a larger container, and then resize the file system. As scary as that sounds, it doesn't destroy data, it just redefines the area "around" that data. Even so, you should make sure that you backup any important data before attempting this!

To remove and then recreate your root partition, assuming that your root partition is numbered 2 (it may not be, if you have a swap partition in the 2 slot, so use print</tt> in parted to double check):

(parted) rm 2 (parted) mkpart primary 1232,0,0 118478,3,31 (parted) quit

In this example, the starting address of the new partition is identical to its original value, and the ending address is the end of the SD card (or the start of the swap partition, if you had a swap partition that you had to move).

Clean and grow the file system
Now that you have room for a bigger file system, you must clean and then grow the existing file system that has been displaced by the changing boundaries of its partition. Remember to use the correct device designation. In this example, I use mmcblk0p2</tt> to represent Partition 2 on an internal SD card slot, but yours may differ.

$ sudo e2fsck -f /dev/mmcblk0p2

If e2fsck</tt> asks to create a Lost+Found directory, it's OK to allow it.

Then resize:

$ sudo resize2fs /dev/mmcblk0p2

Boot
Your system now occupies all available space on your SD card. Put the card in your Pi and boot.

Once booted, verify your hard work with df</tt>:

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

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</tt> 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