Simon The Pi Man bookstore
Mmmm Raspberry Pi
Simon the Pi Man
To Infinity (probably) and beyond
(A beginners Resource for the Raspberry Pi computer using the Debian distro)
since June 2012
Keep up to date with our RSS feed

If you are looking for info on the Raspberry Pi computer stay on this site
however
If you are looking for a good cafe in Rye in East Sussex try "Simon the Pie man" near the church.

How2 give my Pi more disk space


One of a set of simple easy to use guides for beginners to set up a Raspberry Pi computer.


Ok this instruction sheet will allow you to increase the primary partition of a Raspberry Pi computer from the default download image of 2Gb. The default image will only allow the addition of a small number of programs or utilities to the Pi. However if your image is loaded onto a 4Gb/8Gb or 16Gb card you will be wasting a lot of space.

WARNING: This can permanently damage your booting Raspberry Pi so please be careful, it wont physically harm the SD Card or your Pi but you may need to reload the Operating System and rebuild your Pi.
But if you do mess it up dont PANIC see (How2Setup 1. The Pi SD Card).

1. Lets just have a look at the current disk partition.
pi@raspberrypi~$ df -k

-----------------------------------------------------
Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                    95416         0     95416   0% /lib/init/rw
udev                     10240       136     10104   2% /dev
tmpfs                    95416         0     95416   0% /dev/shm
rootfs                 1602528   1206320    314800  80% /
/dev/mmcblk0p1           76186     28089     48097  37% /boot
-----------------------------------------------------

The rootfs filesystem is 80% full to start with so leaves only a small amount of space spare for programs and utilities.

2. OK lets get going - we first have to run the fdisk utility - please note there is no real equivalent in the windows world, so be a bit careful to follow the instructions, the fdisk command allows you to change partition details.
pi@raspberrypi~$ sudo fdisk -cu /dev/mmcblk0

3. Inside this program the main prompt is Command (m for help) and the first thing we need to find out what are we working with so we type p(rint) to see what partitions the disk has.
p

-----------------------------------------------------
Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes
4 heads, 32 sectors/track, 60504 cylinders, total  sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          157696     3414015     1628160   83  Linux
/dev/mmcblk0p3         3416064          195584   82  Linux swap / Solaris
-----------------------------------------------------

From the above details we can see that the disk contains 3965Mb which is sectors (the first blue field), but the maximum sectors that are allocated can be seen on the third partition with the second highlighted field . So we actually have 3937281 sectors spare and as each sector is 512bytes we have 3937281 * 512 bytes unallocated to any partitions so in effect spare space this works out about 1.97Mb.
NOTE: Please make a note of the field highlighted in red as this is important and will be used further on in our guide.

Some notes on the partitions shown above /dev/mmcblk0p1 is the first partition which contains the debian boot code.
The second partition /dev/mmcblk0p2 is where the main programs and root of the system are stored - we need to make this bigger.
The last partition /dev/mmcblk0p3 is the swap partition, and it is where unix puts programs whan they are not in use, or when the physical memory is fully used - this always used to be set to aprox 2.5 times physical memory so we will allocate about 2.5 * 256Mb for swap(as the Pi Model B has 256Mb memory).
As you can see from the highlighted entries above partitions are usually numbered p1 -> p(x) where x is the last partition.

4. Right we need to delete the swap partition as it stops us expanding the main partition, and also the second parition with the operating system on, as we will need to re-create it in a larger size. So lets delete partition 3.
d
3

5. We have deleted the third partition and now to delete the second partition
d
2

6. Lets just check that the partitions are gone so type p(rint) again so we can see the current status
p

-----------------------------------------------------
Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes
4 heads, 32 sectors/track, 60504 cylinders, total  sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
-----------------------------------------------------

We can see from the above that there is just one partition left, the boot partition.

7. Now we need to re-create the second partition to a larger size so we use n(ew) p(artition)
n

-----------------------------------------------------
Command action
   e   extended
   p   primary partition (1-4)
-----------------------------------------------------


8. Now we need to choose the partition type which should be a p(rimary) partition.

-----------------------------------------------------
Partition number (1-4):
-----------------------------------------------------


9. And now we need to choose the partition number, so lets re-create partition 2 in a larger size - to start we need to choose the partition number.
2

-----------------------------------------------------
First sector (155648-7744511, default 155648):
-----------------------------------------------------


10. Next we need to choose the start of the partition.
157696
This MUST BE the figure in red from step 2

-----------------------------------------------------
Last sector, +sectors or +size{K,M,G} (157696-7744511, default 7744511):
-----------------------------------------------------


11. Next we need to choose the end sector of the partition.
The default is always 1 less than the blue highlighted number from step 5. However if we use the default we will not allow enough space for a 3rd partition - swap.
So we need to allow for about 750Mb of swap space which is about 1500000 sectors. Therefore lets set the end of the second partition to 7744511 - 1500000 = 6244511 so lets use that.
6244511

-----------------------------------------------------
Command (m for help):
-----------------------------------------------------


12. Lets just check all is OK at the moment.
p

-----------------------------------------------------
Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes
4 heads, 32 sectors/track, 60504 cylinders, total  sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          157696     6244511     3043408   83  Linux
-----------------------------------------------------

As you can see we now have more blocks than is step 2 for the /dev/mmcblk0p2 partition.

13. Now we need to re-create the swap partition
n

-----------------------------------------------------
Command action
   e   extended
   p   primary partition (1-4)
-----------------------------------------------------


14. Now we need to choose the partition type which should be a p(rimary) partition.

-----------------------------------------------------
Partition number (1-4):
-----------------------------------------------------


15. And now we need to choose the partition number, so lets re-create the swap partition - to start we need to choose the partition number.
3

-----------------------------------------------------
First sector (155648-7744511, default 155648):
-----------------------------------------------------


16. Next we need to choose the start sector of the partition.
This MUST BE one more than the Last Sector number input in step 9
6244512

-----------------------------------------------------
Last sector, +sectors or +size{K,M,G} (157696-7744511, default 7744511):
-----------------------------------------------------


17. Now we need to choose the end sector of the partition.
The end of the swap partition should be at the end of the disk so press [Enter] to select the default.

-----------------------------------------------------
Command (m for help):
-----------------------------------------------------


18. Lets just check all is OK at the moment.
p

-----------------------------------------------------
Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes
4 heads, 32 sectors/track, 60504 cylinders, total 7744512 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          157696     6244511     3043408   83  Linux
/dev/mmcblk0p3         6244512     7744511      750000   83  Linux
-----------------------------------------------------


19. If you are not happy with the above details press [Ctrl]-[C] to stop the program, nothing will have been changed.

20. All seems ok so now lets write this to the disk.
w

-----------------------------------------------------
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
-----------------------------------------------------


21. We need to reboot for the partition to become active.
pi@raspberrypi~$ sudo reboot

22. Wait till the Pi has rebooted then log back in and run the following command which will show the current size of the disk partitions.
pi@raspberrypi~$ df -k

-----------------------------------------------------
Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                    95416         0     95416   0% /lib/init/rw
udev                     10240       136     10104   2% /dev
tmpfs                    95416         0     95416   0% /dev/shm
rootfs                 1602528   1206320    314800  80% /
/dev/mmcblk0p1           76186     28089     48097  37% /boot
-----------------------------------------------------


23. Now to runthe following command which will resize your partition to its new size.
pi@raspberrypi~$ sudo resize2fs /dev/mmcblk0p2

-----------------------------------------------------
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mmcblk0p2 to 760852 (4k) blocks.
The filesystem on /dev/mmcblk0p2 is now 760852 blocks long.
-----------------------------------------------------


24. Lets re-run the following command which will show the new size of the disk partitions.
pi@raspberrypi~$ df -k

-----------------------------------------------------
Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                    95416         0     95416   0% /lib/init/rw
udev                     10240       136     10104   2% /dev
tmpfs                    95416         0     95416   0% /dev/shm
rootfs                 2996128   1206312   1641452  43% /
/dev/mmcblk0p1           76186     28089     48097  37% /boot
-----------------------------------------------------

If you look at the rootfs filesystem you will see that the Use% has dropped from 80% to 43% giving you more space for programs or files on the SD Card.

25. Right lets see if the swap partition is in use.
pi@raspberrypi~$ free -lm

-----------------------------------------------------
             total       used       free     shared    buffers     cached
Mem:           186         38        147          0          5         20
Low:           186         38        147
High:            0          0          0
-/+ buffers/cache:         12        174
Swap:            0          0          0
-----------------------------------------------------


26. Ok the system is not using the swap space so we had better enable it.
pi@raspberrypi~$ sudo vi /etc/fstab

-----------------------------------------------------
proc            /proc           proc    defaults        0       0
/dev/mmcblk0p1  /boot           vfat    defaults        0       0
#/dev/mmcblk0p3  none            swap    sw              0       0
-----------------------------------------------------

We just need to uncomment the #/dev/mmcblk0p3 by deleting the # so that the file looks like

-----------------------------------------------------
proc            /proc           proc    defaults        0       0
/dev/mmcblk0p1  /boot           vfat    defaults        0       0
/dev/mmcblk0p3  none            swap    sw              0       0
-----------------------------------------------------

Then save the file :wq

27. A couple more steps and we will be done - so we need to turn our swap partition into a proper swap partition, so to convert the partition.
pi@raspberrypi~$ sudo mkswap /dev/mmcblk0p3

28. OK we need to reboot now.
pi@raspberrypi~$ sudo reboot

29. Log back in when you reboot then check the swap is now working.
pi@raspberrypi~$ sudo free -lm

-----------------------------------------------------
             total       used       free     shared    buffers     cached
Mem:           186         37        148          0          5         20
Low:           186         37        148
High:            0          0          0
-/+ buffers/cache:         12        173
Swap:          732          0        732
-----------------------------------------------------

Looks like all is working now - Bye Bye

simonthepiman.com 2012->2017
email:
simon@simonthepiman.com

hosted by VillageNetThe Raspberry Pi Foundation is a UK registered charity which exists to promote the study of computer science and related topics, especially at school level, and to put the fun back into learning computing.

Please Note:- any mention of the Raspberry Pi computer on these pages refers to the Raspberry Pi Foundation's product, who also have trademark rights to the term 'Raspberry Pi'.