Resize the Filesystem of Raspdebian (sd card) to make the root partion bigger

First you have to login to the raspberry thorugh ssh as root.

The next step is to execute fdisk with the parameter to the sd-card.

root@raspberry-pi:/# fdisk /dev/mmcblk0

So what you will want to do first is print the partion table.

Command (m for help): p

Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes
4 heads, 16 sectors/track, 242560 cylinders, total 15523840 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: 0x00027aa3

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 2048 104447 51200 b W95 FAT32
/dev/mmcblk0p2 104448 1742847 819200 83 Linux
/dev/mmcblk0p4 15319040 15521791 101376 82 Linux swap / Solaris

 

So from what we see, the swap partion was already deleted before and moved to the end, therefore there is no need for us to delete it.
But what we want is to delete the root partion – don’t worry we will recreate it with the same starting block and extend it to the max size possible.

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes
4 heads, 16 sectors/track, 242560 cylinders, total 15523840 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: 0x00027aa3

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 2048 104447 51200 b W95 FAT32
/dev/mmcblk0p4 15319040 15521791 101376 82 Linux swap / Solaris

The next step is to recreate the partion:

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (104448-15523839, default 104448): 104448
Last sector, +sectors or +size{K,M,G} (104448-15319039, default 15319039): 15319 039

Command (m for help): p

Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes
4 heads, 16 sectors/track, 242560 cylinders, total 15523840 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: 0x00027aa3

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 2048 104447 51200 b W95 FAT32
/dev/mmcblk0p2 104448 15319039 7607296 83 Linux
/dev/mmcblk0p4 15319040 15521791 101376 82 Linux swap / Solari s

So okay, now we’ve recreated the partion. Now we just need to write this to the partion table.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Das Gerät oder die Ressource ist belegt.
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.

So the only thing we need to do is reboot the whole raspberry pi.

root@raspberry-pi:/# reboot

The system is going down for reboot NOW! (pts/0) (Sun Feb  9 12:27:08 2014)

 

 

So after booting the wholle thing up again, i check the partion tables with fdisk -l

root@raspberry-pi:~#

root@raspberry-pi:~# e2fsck -f /dev/mmcblk0p2
e2fsck 1.42.5 (29-Jul-2012)
/dev/mmcblk0p2 is mounted.
e2fsck: Fortsetzung nicht möglich, breche ab.
root@raspberry-pi:~# resize2fs /dev/mmcblk0p2
resize2fs 1.42.5 (29-Jul-2012)
Das Dateisystem ist schon 204800 Blöcke groß. Nichts zu tun!

Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes
4 heads, 16 sectors/track, 242560 cylinders, total 15523840 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: 0x00027aa3

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 2048 104447 51200 b W95 FAT32
/dev/mmcblk0p2 104448 1742847 819200 83 Linux
/dev/mmcblk0p4 15319040 15521791 101376 82 Linux swap / Solaris

So as e2fsck won’t work, while the partion is online we need to use resize2fs.

root@raspberry-pi:~# e2fsck -f /dev/mmcblk0p2
e2fsck 1.42.5 (29-Jul-2012)
/dev/mmcblk0p2 is mounted.
e2fsck: Fortsetzung nicht möglich, breche ab.
root@raspberry-pi:~# resize2fs /dev/mmcblk0p2
resize2fs 1.42.5 (29-Jul-2012)
Das Dateisystem ist schon 204800 Blöcke groß. Nichts zu tun!

The next step i just wanted to make sure the swap is used correctly, as after recreating the swap partion it is now here as /dev/mmcblk0p4

root@raspberry-pi:~# df -k

Dateisystem 1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
rootfs 7498664 843636 6654212 12% /
/dev/root 7498664 843636 6654212 12% /
devtmpfs 216108 0 216108 0% /dev
tmpfs 44876 196 44680 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 110020 0 110020 0% /run/shm
/dev/mmcblk0p1 50396 18484 31912 37% /boot
root@raspberry-pi:~# free -lm
total used free shared buffers cached
Mem: 438 122 315 0 22 63
Low: 438 122 315
High: 0 0 0
-/+ buffers/cache: 36 401
Swap: 0 0 0

so we will need to edit the /etc/fstab

root@raspberry-pi:~# vim /etc/fstab
root@raspberry-pi:~# free -lm
total used free shared buffers cached
Mem: 438 123 314 0 22 63
Low: 438 123 314
High: 0 0 0
-/+ buffers/cache: 36 401
Swap: 0 0 0
root@raspberry-pi:~# mkswap /dev/mmcblk0p4
Setting up swapspace version 1, size = 101372 KiB
no label, UUID=b0a02950-9ee8-41fc-a1fe-4d55e6fe34d5
root@raspberry-pi:~# free -lm
total used free shared buffers cached
Mem: 438 123 314 0 22 64
Low: 438 123 314
High: 0 0 0
-/+ buffers/cache: 37 401
Swap: 0 0 0

Last step is to reboot, Cheers you earned your beer.
root@raspberry-pi:~# reboot