Resizing SD Card Raspberry Pi to fully use free space

From Iwan
Jump to: navigation, search

When I was capturing data with tshark I discovered that I my 16GB SD-card was not fully used to store my Wireshark captures on. I investigated why this was and I stumbled up on this article that explained how the disk space could be "released" again.

Releasing the disk space

Verifying the current space

root ##b##ubuntu@RPi2-02:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
root ##b##/dev/mmcblk0p2  1.7G  674M  892M  44% /
devtmpfs        458M  4.0K  458M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             93M  240K   93M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            462M     0  462M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/mmcblk0p1   64M   17M   48M  27% /boot/firmware

Using fdisk to delete and recreate a partition

root ##b##ubuntu@RPi2-02:/$ sudo fdisk /dev/mmcblk0
sudo: unable to resolve host RPi2-02

root ##b##Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

root ##b##Command (m for help): p

Disk /dev/mmcblk0: 16.1 GB, 16088301568 bytes
4 heads, 16 sectors/track, 490976 cylinders, total 31422464 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: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)
root ##b##/dev/mmcblk0p2          133120     3670015     1768448   83  Linux

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

root ##b##Command (m for help): p

Disk /dev/mmcblk0: 16.1 GB, 16088301568 bytes
4 heads, 16 sectors/track, 490976 cylinders, total 31422464 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: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)

root ##b##Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
root ##b##Select (default p): p
root ##b##Partition number (1-4, default 2): 2
root ##b##First sector (133120-31422463, default 133120): 133120
root ##b##Last sector, +sectors or +size{K,M,G} (133120-31422463, default 31422463): 
Using default value 31422463

root ##b##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: 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.

Rebooting

root ##b##ubuntu@RPi2-02:/$ sudo reboot
sudo: unable to resolve host RPi2-02

Broadcast message from ubuntu@RPi2-02
        (/dev/pts/0) at 7:05 ...

root ##b##The system is going down for reboot NOW!

Do another verification of the space

root ##b##ubuntu@RPi2-02:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
root ##b##/dev/mmcblk0p2  1.7G  675M  892M  44% /
devtmpfs        458M  4.0K  458M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             93M  240K   93M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            462M     0  462M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/mmcblk0p1   64M   17M   48M  27% /boot/firmware

Resize the file system

root ##b##ubuntu@RPi2-02:~$ sudo resize2fs /dev/mmcblk0p2
sudo: unable to resolve host RPi2-02
[sudo] password for ubuntu: 
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
root ##b##The filesystem on /dev/mmcblk0p2 is now 3911168 blocks long.

Reboot again

root ##b##ubuntu@RPi2-02:~$ sudo reboot
sudo: unable to resolve host RPi2-02

Broadcast message from ubuntu@RPi2-02
        (/dev/pts/1) at 7:17 ...

root ##b##The system is going down for reboot NOW!

Verify again

root ##b##ubuntu@RPi2-02:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
root ##b##/dev/mmcblk0p2   15G  676M   14G   5% /
devtmpfs        458M  4.0K  458M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             93M  244K   93M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            462M     0  462M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/mmcblk0p1   64M   17M   48M  27% /boot/firmware

And we have release the space for usage.