183

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?

Steve Robillard
  • 34,158
  • 17
  • 102
  • 108
Jivings
  • 22,208
  • 11
  • 88
  • 138
  • Thanks..worked great for me and didn't tank my system..still have IRC and apache webserver and my full 8gb capacity –  Jun 23 '12 at 00: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 Sep 11 '14 at 05:48
  • 3
    @mbaitoff - because the default system image only has 68MiB free space, which doesn't leave much room for adding additional software. – Jules Mar 08 '16 at 08: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 Mar 08 '16 at 08: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 Mar 08 '16 at 15:04
  • @Jules mount the space at `/usr/local` – mbaitoff Mar 10 '16 at 02:40

5 Answers5

166

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:

Steve Robillard
  • 34,158
  • 17
  • 102
  • 108
  • A good answer. But this will still only give a maximum of 2GB I believe, the size of the image. – Jivings Jun 19 '12 at 10:30
  • But `resize2fs` cannot normally be used on a mounted partition can it? – finnw Jun 19 '12 at 10:31
  • @Jivings check the linked reference for proof it works - also I did this before putting xbmc on the pi. – Steve Robillard Jun 19 '12 at 10:35
  • @finnw It looks to me like this method deletes the root partition so that it can be recreated larger, which is not really optimal. – Jivings Jun 19 '12 at 10:42
  • @finnw You can see more evidence for this method here http://www.raspberrypi.org/phpBB3/viewtopic.php?f=5&t=5584 as well as an asnwer to your resize2fs question. – Steve Robillard Jun 19 '12 at 10:42
  • 1
    @Jivings if done correctly you don't lose a thing, but it is the reason I made step one backup. – Steve Robillard Jun 19 '12 at 10:43
  • 2
    From the comments it seems like you should also document **why** this works, not just how. – Jivings Jun 19 '12 at 10:51
  • 1
    @Jivings hopefully this addresses your concerns. – Steve Robillard Jun 19 '12 at 11:24
  • 9
    It seems very wrong that you have to delete a partition in order to resize it. – Alex Chamberlain Jun 19 '12 at 14:08
  • 1
    You are deleting it but not formatting so no files are lost – Steve Robillard Jun 19 '12 at 14:10
  • @AlexChamberlain I think if the command was resize (it is actually called resize in Windows) partition rather than delete partition this would go down a little easier. – Steve Robillard Jun 19 '12 at 14:30
  • This really felt like complete spine replacement surgery (with leaving old spinal cord in place) on a live patient without using anesthetics or even interrupting their daily routine, but surprisingly, it worked just fine. If you're scared, do it on a newly-flashed card, which you can flash anew if anything goes wrong; also you might want to create a swapfile and mount it, since the swap partition got removed. – SF. Aug 02 '13 at 22:51
  • 1
    I've been using this process like forever without any problem, however the latest images from arch linux arm changed the partition structure, so you can't follow the above instructions in a 'copy-and-paste' fashion, I found the updated instructions in this website: http://jan.alphadev.net/post/53594241659/growing-the-rpi-root-partition, this works for me. – Couto Dec 07 '13 at 18:11
  • @Couto if that is the case please post an answer with the steps for arch? – Steve Robillard Dec 07 '13 at 18:25
  • Wait, how do you do that on a Mac? I am trying to install Raspbian again on my sd card but I have formatted it several times with the erasing kind of formatting and it still does not seem to be gaining any memory. – AwesomeUser Feb 28 '14 at 03:00
  • 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. – Steve Robillard Feb 28 '14 at 03:47
  • What if you actually like your swap partition, and would like to recreate it after following these steps? – Zaz Nov 30 '15 at 14:52
  • @zaz, that is really a separate question and should not be asked in the comments of an answer. – Steve Robillard Nov 30 '15 at 15:07
  • 1
    @SteveRobillard the question was "how can i make my root partition bigger" - not "how can i make my root partition bigger and as a bonus remove my other partitions" – Zaz Nov 30 '15 at 15:32
  • @zaz and that is why it should be a separate question. Click the ask button above and ask about your swap partition. New questions do not belong in the comments especially to old questions. By asking it here the only person who is likely to see it is me. if you ask a new question the entire community will have a chance to see and answer it. Also, note that depending on the OS/distro you are running Raspbian for instance includes a script to resize the root partition, and does not need to be done manually. – Steve Robillard Nov 30 '15 at 15:48
  • 1
    I know how to create a swap partition, what I'm saying is you should add "leave space for swap partition" and "fdisk another swap partition to replace the one deleted in step 2" to the answer, because the way it is presented currently makes it look like the swap partition isnt neccesary. – Zaz Nov 30 '15 at 17:54
  • Actually it would probably just be enough to post a link to http://raspberrypi.stackexchange.com/questions/70/how-to-set-up-swap-space or something, and make a remark along the lines of "We removed the swap partition, but those are a bad idea on a raspberry anyway because sd card." – Zaz Nov 30 '15 at 17:58
  • 1
    There no longer is a swap partition in Raspbian. – RhinoDevel Dec 19 '15 at 20:02
  • I tried this on raspberry pi3 but I am getting this error after reboot: `PANIC: VFS: Unable to mount root fs on unknown-block(179,2) Entering kdb (current=0xcb82ac80, pid 1) due to Keyboard Entry`. – haccks Aug 02 '16 at 10:59
  • @haccks First you should not ask new questions in the comments to another question. Second, assuming you are running Raspbian did you not use raspi-config to do this. As you have learned this way can bork your system if not done correctly. I would suggest re-imaging your card and starting fresh. – Steve Robillard Aug 02 '16 at 14:20
  • @SteveRobillard; I am not asking a new question. I came out to this answer and gave it a try. And I am using Kali image instead of Raspbian. I wanted to know how to do this using CLI ( earlier I used `gparted`). – haccks Aug 02 '16 at 14:34
  • Can this be done on a PI to shrink instead of grow a partition? (While doing that on the same PI). – clankill3r Mar 02 '17 at 09:41
  • @clankill3r Maybe, but I would not recommend it, as it can potentially destroy your system. A search here or google will turn up complete instructions for shrinking the size of the disc image. – Steve Robillard Mar 02 '17 at 09:44
  • For [NOOBS partitions](https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained), the partitions would be e.g. primary "1", and extended "2" that contains the **logical** partitions "5" (`SETTINGS`), "6" (`/boot`) and "7" (`/` root). Make sure you select "7" and **logical** as partition type when creating the new partition. For more check [this tutorial](https://dracoy.com/2016/06/26/expanding-partition-on-sd-card-for-raspberry-pi-with-noobs-pre-installed/). – forzagreen Aug 09 '17 at 20:53
  • @Steve Robillard Thank you Steve, this worked like a charm! After failing to resize from another OS and messed up the installation, I tried this method. It is the right method to resize. We don't need to remove and move the card to another system just to resize a partition. Great work! – GTodorov Nov 01 '17 at 14:34
  • Worked like a charm for my OSMC on RPi2! – huseyint Nov 06 '17 at 13:46
  • even this is an old post, it still works with a current Kali 2018 image – normic Mar 17 '18 at 02:31
  • 1
    I did: `sudo resize2fs -p /dev/mmcblk0p3` and got `resize2fs 1.43.3 (04-Sep-2016) resize2fs: Device or resource busy while trying to open /dev/mmcblk0p3 Couldn't find valid filesystem superblock.` – Elia Weiss Apr 10 '18 at 13:47
  • @EliaWeiss are you running noobs? What distro are you using? Is the filesystem already expanded? – Steve Robillard Apr 10 '18 at 19:47
  • @SteveRobillard Please see more info in https://raspberrypi.stackexchange.com/questions/82585/resize2fs-device-or-resource-busy-while-trying-to-open-dev-mmcblk0p3 – Elia Weiss Apr 11 '18 at 04:46
  • @Steve Robillard Thank you Steve. Just one complementary information, in my case I use a RPi2, I boot on the SDCard and use an external HD as /, so disk path is /dev/sda2. Plus I have no swap partition ... – tdaget Oct 14 '18 at 18:49
  • 1
    Great answer! Would you mind adding something about deleting the signature? – Dan Green-Leipciger Mar 28 '19 at 17:35
  • @DanGreen-Leipciger What signature? – Steve Robillard Mar 28 '19 at 19:55
  • 1
    The last prompt was if I wanted to delete the partition signature. – Dan Green-Leipciger Mar 29 '19 at 06:36
  • 1
    Same here! After creating the partition (i.e. enter the number of last sector) it asks me "Do you want to remove the signature? [Y]es/[N]o: " – Muffo Apr 11 '19 at 03:46
  • An easier CLI option is to open `fdisk mmcblk1`, choose `O` to dump the disk layout to a local script, and back it up. Use `p` to get the number of sectors on the disk, and in each partition. Sum up the `Sectors` values for partitions you want to keep, and subtract them from the total. Now edit the script, and use that number for the `size` of the target partition. Save it, use `I` to load it into `fdisk`, and write your changes. Use`partprobe /dev/mmcblk1` instead of rebooting and, finally, `resize2fs /dev/mmcblk1p2`. – Stephen C Sep 22 '19 at 05:49
  • Following all the steps the partition was changed, but the /dev/root filesystem still had only 16G (instead of 128G). If someone has the same issue just scroll one answer deeper. raspi-config does the magic :) – Tobias Apr 23 '21 at 09:57
  • May have been said but you do not need to delete the swap. – addohm Dec 15 '21 at 01:32
106

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.

Jon Egerton
  • 2,983
  • 2
  • 21
  • 31
  • This process took about 10 minutes with a 16 GB card on my Pi. – Oliver Salzburg Jan 28 '13 at 12:57
  • 5
    This is so much simpler than the top answer. This is the solution to use. – James Skimming May 10 '13 at 21:14
  • @JamesSkimming: This is the solution to use for Debian-Wheezy Beta image. Not for others. – SF. Aug 02 '13 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 – Eoin Campbell Dec 28 '13 at 19:31
  • Raspi-config doesn't know how to resize root of Raspbian Wheezy. expand_rootfs as you suggested caused issues. The answer above yours works like a charm in a live system and no need to "leave about 1 GB". – GTodorov Nov 01 '17 at 14:39
  • For anyone using Plex and moving your sd card files to a larger sd card, this worked great after copying the new image. – Jason Glisson Dec 18 '17 at 16:54
  • 3
    How do you leave the 1GB free space? or how/where do you specify how much to expand the partition size? – user2813274 Nov 12 '18 at 02: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.

Joost
  • 761
  • 7
  • 13
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.

popcornmix
  • 186
  • 2
-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

Mark Ingram
  • 859
  • 2
  • 11
  • 18