187

Even though my SD card is 16GB, the image I flashed onto it was only 2GB and now I can only see 2GB of storage space on the disk.

How can I resize the image so that I have more space on my root partition?

6
  • Thanks..worked great for me and didn't tank my system..still have IRC and apache webserver and my full 8gb capacity
    – user334
    Commented Jun 23, 2012 at 0:47
  • 1
    Why resize a root partition when you can create new partition at the remaining free SD-card space and then mount it inside the root partition at /mnt/storage for example. This effectively isolates system and data and makes it easier to maintain and backup.
    – mbaitoff
    Commented Sep 11, 2014 at 5:48
  • 3
    @mbaitoff - because the default system image only has 68MiB free space, which doesn't leave much room for adding additional software.
    – Jules
    Commented Mar 8, 2016 at 8:09
  • @Jules - I wasn't asking why one would want to resize a partition. A was asking why would one resize the system partition instead of mounting additional storage at system partition mount point thus effectively isolating system and software/data? If you want storage, mount remaining SD spcae at /mnt/storage, if you want programs - mount at /opt/software/.
    – mbaitoff
    Commented Mar 8, 2016 at 8:55
  • 1
    @mbaitoff most programs install in /usr, not /opt/software. The prebuilt images have the contents of /usr in the root partition. It therefore seems essential to increase the size of the root partition if you want to install any non-default software.
    – Jules
    Commented Mar 8, 2016 at 15:04

5 Answers 5

170

Assuming you are using Debian.

The Short Version:

  1. Backup your system
  2. Remove the main and swap partitions (leaving the boot partition alone)
  3. Recreate the main partition to utilize the remaining disk space (excluding the boot partiton). Make sure to reuse the same start sector as the original root partition.
  4. reboot the system
  5. resize the new boot root partition to utilize the full partition size.

Step by Step Instructions

First make a backup of your SD Card using the instructions found here in case something goes wrong.

From the command line or a terminal window enter the following

sudo fdisk /dev/mmcblk0

then type p to list the partition table

you should see three partitions. if you look in the last column labeled System you should have

  1. W95 FAT32
  2. Linux
  3. Linux Swap

make a note of the start number for partiton 2, you will need this later. though it will likely still be on the screen (just in case).

next type d to delete a partition.

You will then be prompted for the number of the partition you want to delete. In the case above you want to delete both the Linux and Linux swap partitions.

So type 2

then type d again and then type 3 to delete the swap partition.

Now you can resize the main partition.

type n to create a new partition.

This new partition needs to be a primary partition so type p.

Next enter 2 when prompted for a partition number.

You will now be prompted for the first sector for the new partition. Enter the start number from the earlier step (the Linux partition)

Next you will be prompted for the last sector you can just hit enter to accept the default which will utilize the remaining disk space.

Type w to save the changes you have made.

Next reboot the system with the following command:

sudo reboot

once the system has reboot and you are back at the commandline enter the following command:

sudo resize2fs /dev/mmcblk0p2

Note: this can take a long time (depending on the card size and speed) be patient and let it finish so you do not mess up the file system and have to start from scratch.

Once it is done reboot the system with the following command:

sudo reboot

You can now verify that the system is using the full capacity of the SD Card by entering the following command:

df -h

Why This Works:

The real magic here is that you delete the root and swap partitions, then recreate only the root partition (using the original start sector) before writing the data to the disk. As a result you don't erase the existing data from the root partition.

By removing the swap partition you allow the root partition room to grow beyond its current size and fill the unused portion of the disk (because of the placement of the partitions -the root partition is sandwiched between the boot and swap partitions - it can't simply be resized leaving the swap partition alone).

You then resize (which is safe to run on a mounted disk) the file system to use all the space in the new root partition.

ref:

43
  • 1
    @Jivings if done correctly you don't lose a thing, but it is the reason I made step one backup. Commented Jun 19, 2012 at 10:43
  • 2
    From the comments it seems like you should also document why this works, not just how.
    – Jivings
    Commented Jun 19, 2012 at 10:51
  • 1
    @Jivings hopefully this addresses your concerns. Commented Jun 19, 2012 at 11:24
  • 9
    It seems very wrong that you have to delete a partition in order to resize it. Commented Jun 19, 2012 at 14:08
  • 5
    @AwesomeUser You don't do it on the mac, you do it on the pi itself. Also, if you are running raspbian you can boot the pi and from the command line issue the following command sudo raspi-config, and choose the resize root partition option. Commented Feb 28, 2014 at 3:47
109

Using the Debian-Wheezy Beta image, there is a configuration utility built in that makes this easy:

There is a utility called raspi-config. This runs on first boot if you're connected directly to the RPi. If you're over SSH you can run it manually using $ sudo raspi-config. (I think you can re-run the tool manually at any time).

The second option on the blue dialog that follows is titled expand_rootfs, with the description "Expand root partition to fill SD card".

Selecting this will cause your root partition to be resized to fill the card the next time you boot your RPi.

Update for 2015!

I'm coming back to this answer with a note of caution: I have been using a bunch of 16GB SD cards, and I got caught out by maxxing my partition to fill the card I was using. Doing so caused a bunch of problems when switching to a new card which (even though it was stated as 16GB) was about 200MB smaller than the older card.

This came to a head when I switched a couple of RPis from v1 to the new v2 model, which uses Micro SD cards.

I now leave about 1GB free to make absolutely sure that my images can be switched between SD cards without issue.

7
  • This process took about 10 minutes with a 16 GB card on my Pi. Commented Jan 28, 2013 at 12:57
  • 5
    This is so much simpler than the top answer. This is the solution to use. Commented May 10, 2013 at 21:14
  • @JamesSkimming: This is the solution to use for Debian-Wheezy Beta image. Not for others.
    – SF.
    Commented Aug 2, 2013 at 22:53
  • 1
    This doesn't work for Raspbian when installed from NOOBS on the SDCard's they're shipping with RPi kits. when you try to run the expand option it says that it's not supported by this config, but since your running noobs it's probably using all the space anyway... problem is I'm trying to reflash the 8GB image onto a 32GB Card Commented Dec 28, 2013 at 19:31
  • 3
    How do you leave the 1GB free space? or how/where do you specify how much to expand the partition size? Commented Nov 12, 2018 at 2:32
29

If you are not very comfortable working on the command line, like in Steve Robillards excellent answer, there are some GUI applications available. In particular gparted works very well.

I think it's installed by default on a Ubuntu LiveCD but not on an installed system. There is of course an easy fix for this: apt-get install gparted. You can't do this on the actual Pi, because to resize partitions they need to be unmounted.

The picture below is in Dutch, but don't let that spoil the fun. It's very easy to use. This is the layout of the default debian image on a 8gb sdcard.

GParted

Right-click the partions to unmount them and then right-click to resize or move the partition. You can also create new partitions and format existing ones. In the picture below I've removed the swap partition and now I'm able to resize the root partition to the entire space on the right of it. Don't fill it entirely, because you still have to add a swap partition.

Resize

You can play with the partition layout, changes are only written if you apply the changes (the grayed out 'return' key in the first picture).

It's very easy to move partitions but like in Steve's answer I'd recommend you remove the swap partition entirely, then resize the root and then recreate a swap. Moving a partition can take a very long time and the swap does not contain data you need to keep.

4
  • 1
    I have used this method; works like a charm. Commented Jul 4, 2012 at 11:11
  • I agree. I've used gparted to do exactly this and its very easy
    – JayDev
    Commented Jan 16, 2015 at 11:43
  • If you are connecting through VNC, run sudo -E gparted
    – forzagreen
    Commented Jul 3, 2017 at 16:37
  • May also need to run the following command before gparted if you're running under Wayland (not sure these issues have been resolved): xhost +SI:localuser:root
    – John Manko
    Commented Mar 15, 2019 at 0:14
11

If you use the newer Debian Wheezy image: http://www.raspberrypi.org/archives/1435 A boot-time script allows you to resize to fill the sdcard automatically.

3
  • 1
    I'm using Arch Linux.
    – Jivings
    Commented Jun 20, 2012 at 17:50
  • 2
    How would do such a thing though?
    – user46
    Commented Jun 20, 2012 at 17:57
  • 2
    This is the easiest way to resize the partitions for any up-to-date wheezy image.
    – Yuval Adam
    Commented Nov 27, 2012 at 10:47
-3

I followed this video and it worked perfectly for me (I have a 16GB card):

http://www.youtube.com/watch?v=R4VovMDnsIE&feature=plcp

1
  • 10
    -1 Answers should be able to stand on their own in case of link rot.
    – Jivings
    Commented Jun 21, 2012 at 18:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.