Reclaiming some space

Just noticed that my raspberry pi running arch linux is not using all the available space in the 4 GB SD card.

 # df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.7G  1.5G   89M  95% /
devtmpfs        213M     0  213M   0% /dev
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M  336K  217M   1% /run
tmpfs           217M     0  217M   0% /sys/fs/cgroup
tmpfs           217M     0  217M   0% /tmp
/dev/mmcblk0p1   90M   26M   65M  29% /boot
tmpfs            44M     0   44M   0% /run/user/0

Let’s take a look at the partitions with fdisk. The device name is /dev/mmcblk0.

# fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.26.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): p
Disk /dev/mmcblk0: 3.7 GiB, 3973054464 bytes, 7759872 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: 0x417ee54bDevice         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        2048  186367  184320   90M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      186368 3667967 3481600  1.7G  5 Extended
/dev/mmcblk0p5      188416 3667967 3479552  1.7G 83 Linux

So it looks like the total space in the SD card is  3.7 GiB = 3.7*230  = 3.97 GB as expected. There is one primary partition (the boot partition) and one extended partition. The extended partition contains one logical volume.

It is quite easy to adjust the partitions with fdisk. In this case we should recreate the extented partition and the logical volume so that the start point remains the same but the end is moved to the end of the available SD card space. This is illustrated in the figure below.

reclaim-02

So let’s record the starting points for the two parts:

 Partition Start point
 Extended partition  186368
 Logical volume  188416

Then let’s delete the extended partition (this will also delete the logical volume).

Command (m for help): d
Partition number (1,2,5, default 5): 2
Partition 2 has been deleted.
Command (m for help):

And then create a new extended partion and a new logical volume using the recorded start points.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 2
First sector (186368-7759871, default 186368):
Last sector, +sectors or +size{K,M,G,T,P} (186368-7759871, default 7759871):
Created a new partition 2 of type 'Extended' and of size 3.6 GiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (188416-7759871, default 188416):
Last sector, +sectors or +size{K,M,G,T,P} (188416-7759871, default 7759871):
Created a new partition 5 of type 'Linux' and of size 3.6 GiB.Command (m for help):

Finally let’s check the result.

Command (m for help): p
Disk /dev/mmcblk0: 3.7 GiB, 3973054464 bytes, 7759872 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: 0x417ee54b
Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        2048  186367  184320   90M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      186368 7759871 7573504  3.6G  5 Extended
/dev/mmcblk0p5      188416 7759871 7571456  3.6G 83 Linux

Looks ok. Let’s save the changes and reboot.

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).
# reboot

As the final step we need to resize the file system to occupy all the available partition space.

# resize2fs /dev/mmcblk0p5
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/mmcblk0p5 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p5 is now 946432 (4k) blocks long.

Let’s check the result.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.6G  1.5G  2.0G  42% /
devtmpfs        213M     0  213M   0% /dev
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M  304K  217M   1% /run
tmpfs           217M     0  217M   0% /sys/fs/cgroup
tmpfs           217M     0  217M   0% /tmp
/dev/mmcblk0p1   90M   26M   65M  29% /boot
tmpfs            44M     0   44M   0% /run/user/0

The root partition size is now 3.6 GiB.

Leave a Reply

Your email address will not be published.