Thursday, September 7, 2017

Expanding extended partitions (or how to increase the space when I clone an SD to a larger one?)

I cloned an SD running raspbian from a 16GB to a 32GB one. The clone worked perfectly, however, the new card had a 13GB partition (missing out on the new space!)
The answer: fdisk.
Procedure should be to delete both partitions and then to recreate them with exact same starting sectors.

Key things to remember:
- use "-c=dos" to run legacy mode in disk
- create all logical exactly like it was before. Use expert mode to guarantee it starts in same sector.

$ sudo fdisk -l /dev/mmcblk0 -c=dos

Welcome to fdisk (util-linux 2.27.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/sda: 28.8 GiB, 30908350464 bytes, 60367872 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: 0x000ec4fb

Device     Boot   Start      End  Sectors  Size Id Type
(( make sure to do a print screen of your partitions, and the start/end sectors))

Command (m for help): d
Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

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):
First sector (999424-20479999, default 999424): make sure to use what it was before
Last sector, +sectors or +size{K,M,G,T,P} (1001470-20479999, default 20479999):

Created a new partition 2 of type 'Extended' and of size 27.3 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1003518-20479999, default 1003520):
Last sector, +sectors or +size{K,M,G,T,P} (1003520-20479999, default 20479999):

Created a new partition 5 of type 'Linux' and of size 9.3 GiB.

Command (m for help): x


Expert command (m for help): b
Partition number (1,2,5, default 5):
New beginning of data (1001471-20479999, default 1003520): 1001472

Expert command (m for help): r

Command (m for help): w
Now that you have room for a bigger file system, you must clean and then grow the existing file system that has been displaced by the changing boundaries of its partition. Remember to use the correct device designation.

In this example, I use mmcblk0p2 to represent Partition 2 on an internal SD card slot, but yours may differ.

 $ sudo e2fsck -f /dev/mmcblk0p2

Then resize:

 $ sudo resize2fs /dev/mmcblk0p2