Using the full space on your SD card in the Raspberry PI

If you got your Raspberry PI from RS like I did I also ordered both a power supply & an SD card. Now you’ll probably find that you got a 4GB card. The problem is that the Debian image that you can download for the pi only uses 2GB of it, so you will find that you’ll run out of space pretty quickly. This article shows how you can allocate the remainder of the card so it’s available for use.

You do this at your own risk. Make sure you have backed up everything you need before doing this.

Now there is a good video tutorial on YouTube by RaspberryPiTutorials which shows one method of resizing the image. Now this is fine although it does go through hoops using a VM etc but what about from the pi itself?

Well here I’m not going to resize the root partition. What I’m going to do is to create a new partition using up the remainder of the space and move /home to that new partition.

The benefit of this is that you are freeing up the root partition for just the OS and the remainder of the card for user files.

Finally sorry for the lack of images here, I ran though this prior to writing it & forgot to capture them as I went 😦

Creating the new partition

First log into the pi:

peter@somehost:~$ ssh -Y pi@raspberrypi

Remember from a previous article, the -Y allows the pi to use your local X11 display.

Next we need to run gparted:

pi@raspberrypi:~$ gksudo gparted

You should now see a display with the partitions displayed and a large unallocated section to the right in grey. Select that unallocated space right click and select new.

In that dialog you’ll see aot of boxes. The main two you want are the first one (top left) which says space before the partition, it will show 0 but you need to change it to 1. This follows the layout of the other partitions which have a 1Mb buffer between each partition.

The other one is the file system dropdown. I chose ext4 as thats the same as the root.

Apply these and you should then see the partition is now marked as allocated.

Click on apply. This now applies your changes.

Next right click the new partition, select format to and choose ext4.

Now make a cup of coffee – as it will run for a few minutes as it formats the new drive.

Once it’s done you can close gparted.

Moving home to the new partition

Now at the command line you need to mount the new partition so you can copy your home directories.

pi@raspberrypi:~$ sudo vi /etc/fstab

And add the following to the end of the file:

/dev/mmcblk0p4  /mnt  ext4  defaults  0 0

Save that and exit vi then mount the drive. You should then see the new drive present.

pi@raspberrypi:~$ sudo mount -a
pi@raspberrypi:~$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                    95416         0     95416   0% /lib/init/rw
udev                     10240       148     10092   2% /dev
tmpfs                    95416         0     95416   0% /dev/shm
rootfs                 1602528   1311868    209252  87% /
/dev/mmcblk0p1           76186     28549     47637  38% /boot
/dev/mmcblk0p4         1994640     35744   1857572   2% /mnt

Now we need to copy the home directories across:

pi@raspberrypi:~$ sudo su -
X11 connection rejected because of wrong authentication.
root@raspberrypi:~# mv /home/pi /mnt
root@raspberrypi:~# ls -l /mnt
total 4
drwxr-xr-x 12 pi pi 4096 Jun 19 11:57 pi

Next whilst we are still in the root shell we’ll unmount /mnt and move it back to /home:

root@raspberrypi:~# umount /mnt
root@raspberrypi:~# vi /etc/fstab

Now change the entry for /mnt to /home and save it. Next we’ll mount it again this time as /home

root@raspberrypi:~# mount -a
root@raspberrypi:~# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                    95416         0     95416   0% /lib/init/rw
udev                     10240       148     10092   2% /dev
tmpfs                    95416         0     95416   0% /dev/shm
rootfs                 1602528   1305236    215884  86% /
/dev/mmcblk0p1           76186     28549     47637  38% /boot
/dev/mmcblk0p4         1994640     42380   1850936   3% /home

There it’s done, now /home is a 1.8Gb partition whilst root holds the base OS.

Now reboot the pi & log in again. You should now see all your files present but it’s now in the new larger partition.

Advertisements

8 Comments

  1. Just put the SD card in ur android phone or camera and formate it.. It will recover all space which was unallocated.. Worked for me with 4 gb card and android phone..

    1. True that would release the free space, except that it ends up with a blank drive.

      Here we have a drive installed with a 2Gb image & we don’t want to wipe the drive but keep it’s contents. To do this you have to resize the existing drive – not format it.

      Saying that, since I wrote this article the recent raspian images now give you an option to resize on the first boot, but these instructions still hold for other drives.

    1. That error means you are running the command without an X session, gparted isn’t a command line app.

      When you run that are you already root via sudo or logged in via SSH?

      If root then you need to come back out to your normal user first.

      If SSH then you need to add -Y to the SSH command so that it links back to your local X session.

  2. when running this i get,

    (gksudo:2400): GLib-CRITICAL **: g_str_has_prefix:assertion ‘str != NULL’ failed

    any ideas?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s