Resizing an SD partition

Often, when loading a Linux operating system onto an SD Card, the partition is set to the size of the ISO file which may only be 2GB.
Of course if the SD card is 16GB you may want to resize the root partition to use the full SD card space.
This can be done fairly simply. (The following was copied from a post on a forum).

The Short Version:

Backup your system using "dd if=/dev/sdx of=/path/to/image bs=1M"
Remove the main and swap partitions using fdisk (leaving the boot partition alone)
Recreate the main partition to utilize the remaining disk space (excluding the boot partiton). Make sure to reuse the same start sector as the original root partition.
reboot the system
resize the new boot root partition to utilize the full partition size using resize2fs.

Step by Step Instructions:

First make a backup of your SD Card in case something goes wrong.
From the command line or a terminal window enter the following
sudo fdisk /dev/mmcblk0
then type p to list the partition table
you should see three partitions. if you look in the last column labeled System you should have
W95 FAT32
Linux Swap

make a note of the start number for partiton 2, you will need this later. though it will likely still be on the screen (just in case).

next type d to delete a partition.

You will then be prompted for the number of the partition you want to delete. In the case above you want to delete both the Linux and Linux swap partitions.

So type 2

then type d again and then type 3 to delete the swap partition.

Now you can resize the main partition.

type n to create a new partition.

This new partition needs to be a primary partition so type p.

Next enter 2 when prompted for a partition number.

You will now be prompted for the first sector for the new partition.
Enter the start number from the earlier step (the Linux partition)

Next you will be prompted for the last sector you can just hit enter to accept the default which will utilize the remaining disk space.

Type w to save the changes you have made.

Next reboot the system with the following command:

sudo reboot

once the system has reboot and you are back at the commandline enter the following command:

sudo resize2fs /dev/mmcblk0p2

Note: this can take a long time (depending on the card size and speed) be patient and let it finish so you do not mess up the file system and have to start from scratch.

Once it is done reboot the system with the following command:

sudo reboot

You can now verify that the system is using the full capacity of the SD Card by entering the following command:

df -h

Why This Works:

The real magic here is that you delete the root and swap partitions, then recreate only the root partition (using the original start sector) before writing the data to the disk. As a result you don't erase the existing data from the root partition.

By removing the swap partition you allow the root partition room to grow beyond its current size and fill the unused portion of the disk (because of the placement of the partitions -the root partition is sandwiched between the boot and swap partitions - it can't simply be resized leaving the swap partition alone).

You then resize (which is safe to run on a mounted disk) the file system to use all the space in the new root partition.

07.07.2018. 04:46