The first thing you will be interested in doing after getting a new RaspberryPi will be to boot it up with a suitable OS. When I installed rasbian for the first time on a 32GB micro SD Card, this is how the partition layout was.

pi@raspberrypi:~ $ df -h  
Filesystem      Size  Used Avail Use% Mounted on  
/dev/root       1.3G  1.2G     0 100% /
devtmpfs        459M     0  459M   0% /dev  
tmpfs           463M     0  463M   0% /dev/shm  
tmpfs           463M  6.2M  457M   2% /run  
tmpfs           5.0M  4.0K  5.0M   1% /run/lock  
tmpfs           463M     0  463M   0% /sys/fs/cgroup  
/dev/mmcblk0p1   60M   20M   41M  34% /boot
tmpfs            93M     0   93M   0% /run/user/1000  

In my 32GB memory card only 1.3 GB was used for / filesystem. This makes it useless, as you wont be able to install new softwares to it. So the trick lies in expanding the / partition.

This should be the first thing you should be doing after installing OS to sdcard so that there is only less chance of data loss.

First of all if you do a lsblk you can see the total size of the disk and the space used by the partitions

pi@raspberrypi:~ $ sudo lsblk  
mmcblk0     179:0    0 29.7G  0 disk  
├─mmcblk0p1 179:1    0   60M  0 part /boot
└─mmcblk0p2 179:2    0  1.3G  0 part /

Now to resize the / partition we are doing an interesting step using fdisk.

  • Note the starting sector number of / partition (mmcblk0p2)
  • Then delete the / partition (mmcblk0p2)
  • Then create a new partition with the same start sector number
  • Specify the the end sector as the highest sector number
  • Finally write the partition and resize the filesystem

Step 1

pi@raspberrypi:~ $ sudo fdisk -l /dev/mmcblk0

Disk /dev/mmcblk0: 29.7 GiB, 31914983424 bytes, 62333952 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  
Disklabel type: dos  
Disk identifier: 0xb3c5e39a

Device         Boot  Start     End Sectors  Size Id Type  
/dev/mmcblk0p1        8192  131071  122880   60M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      131072 2848767 2717696  1.3G 83 Linux

In this step you can see that the Start sector number of /dev/mmcblk0p2 is 131072 and the highest sector number is 62333952

Step 2

First open fdisk and delete the partition.

pi@raspberrypi:~ $ sudo fdisk /dev/mmcblk0 

Welcome to fdisk (util-linux 2.25.2).  
Changes will remain in memory only, until you decide to write them.  
Be careful before using the write command.

Command (m for help):  
In the prompt press d and hit enter. Select partition number 2 to delete

Command (m for help): d  
Partition number (1,2, default 2): 2

Partition 2 has been deleted.  

Now create a new partition with type Primary, Partition number as 2 and the first sector as the one you noted before.

Command (m for help): n  
Partition type  
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p  
Partition number (2-4, default 2):  
First sector (2048-62333951, default 2048): 131072  
Last sector, +sectors or +size{K,M,G,T,P} (131072-62333951, default 62333951): 

Created a new partition 2 of type 'Linux' and of size 29.7 GiB.  

Step 4

Finally write and quit fdisk utility

Command (m for help): w  
The partition table has been altered.  
Calling ioctl() to re-read partition table.  
Re-reading the partition table failed.: 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).

Now to make the kernel to use the new partition table run partprobe command and check whether the new space is shown in lsblk

pi@raspberrypi:~ $ sudo partprobe

pi@raspberrypi:~ $ lsblk  
mmcblk0     179:0    0 29.7G  0 disk  
├─mmcblk0p1 179:1    0   60M  0 part /boot
└─mmcblk0p2 179:2    0 29.7G  0 part /

Now as the last step to resize the filesystem of / run the reisze2fs command.

pi@raspberrypi:~ $ sudo resize2fs /dev/mmcblk0p2  
resize2fs 1.42.12 (29-Aug-2014)  
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required  
old_desc_blocks = 1, new_desc_blocks = 2  
The filesystem on /dev/mmcblk0p2 is now 7775360 (4k) blocks long.  

Now you have a bigger / partition. Enjoy!