If you have installed EmonBase on an SD-card for the Raspberry PI you may have discovered that the root partition only has a size of about 2GB.
Maybe you have also tried to expand it to use the full size of the SD-card by running the
raspi-config command and selecting the first option, Expand Filesystem. If so, you know that this does not work because the root partition is not the last partition on the SD-card.
EmonHub places the root partition as the second of three partitions on the SD-card, so that the partition table looks like this:
That is why expanding the root partition using
raspi-config does not work.
So, what do you do? There are a few options that enables you to expand a partition that is not the last in the partition table, e.g. using
fdisk. However powerful
fdisk may be, it is not for novices and expanding a partition using
fdisk requires fiddling with sector and block counts, which can quickly go wrong and make a mess of your partition table and thus the data in the partitions. The clear advantage of the
fdisk-option is that you can do it all in-place on the Raspberry PI and does not require another computer (other than the one you use to ssh to the RPI, if that is how you access it).
In this article I will describe another option, which might seem somewhat pragmatic and cumbersome, but is safer to use. For this method, you need a separate computer running Linux(it might be possible to follow the method using the same RPI, if it features a desktop environment installed).
The first step is to power off the Raspberry PI and replace the SD-card in the separate computer. Then open the partition editor, GParted, and make sure that the SD-card is selected. You should see three partitions at the start of the partition table diagram and an amount of unallocated space filling the remaining space of the diagram.
To expand the root partition, the pi user’s data partition must first be moved to the end of the SD-card. In GParted, you can do this by selecting the last allocated partition and selecting Resize/Move from the Partition menu (or use right-click on the partition in the diagram). This opens up a new Window in which you can drag the partition to move it – drag it all the way to the right (you can also resize the data partition in this step, if you so desire.
Clicking Resize/Move will save the changes as a step in the overall process of modifying the partition table and the partition table diagram should now look like this:
Now the partition table is ready for what we are actually about: resizing the root partition. As done with the data partition, select the root partition and select Resize/Move from the Partition menu. In the new window, drag the end of the partition to the end of the diagram to resize the partition to the maximum available space.
Again, clicking Resize/Move will save the changes as a step in the overall process of modifying the partition table and the partition table diagram should now look like this:
Note that there might be a small amount of unallocated space following the data partition. This is because the partitions are laid out according to either sector boundaries or whole mega bytes – whichever has been chosen in GParted – and, if the boundaries of the selected partition scheme does not fit within the chosen alignment scheme, some unallocated space is inevitable. Each of these unallocated blobs of space should always be less than what fits in a sector or one mega byte, though.
Now, the partition table is laid out as we want it, with the root partition expanded to utilise all the unallocated space on the SD-card. To apply the changes and have GParted update the partition table on he SD-card, select Apply All Operations from the Edit menu, or click the green check mark on the toolbar.
GParted will now move the data partition to the end of the drive, which takes approximately the same amount of time as copying the entire partition worth of data from one place to another place on the drive. Then, the root partition is resized to the chosen size and finally the partition table is saved and your SD-card is ready to use.