Sunday, June 16, 2019

How to Expand a Linux Partition - Command Line Style

I have several small images of Rasbian and OSMC that are heavily modified that I have wanted to install on larger SD cards.  This is the process that I've followed that appears to work without issue from the command line.


  • Image SD Card with your favorite tool (I've been using ApplePie Baker)
  • See Option 1 and Option 2 below.  Option 2 represents what my SD card experience has been with OSMC.
    • Basically - fdisk -l to identify partition(s).  Delete the partition (or second one if it shows two) and note the start block!  Create a new partition and start it with the exact block of the previous one.  If you want to use the rest of the SD card (like I did) you can use the default end block.  Write and exit.   Reboot - space has not been allocated for use yet...run the command "resize2fs /dev/mmcblk0p..." as root and it will allocate the space.
_________________________________________________________

Option 1



Expand partition and filesystem
List existing devices and partitions with sudo fdisk -l
    $ sudo fdisk -l
    Disk /dev/mmcblk0: 7861 MB, 7861174272 bytes
    4 heads, 16 sectors/track, 239904 cylinders, total 15353856 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

    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0x0002c262

    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            8192      122879       57344    c  W95 FAT32 (LBA)
    /dev/sda2          122880     5785599     2831360   83  Linux
Partition to resize is /dev/sda2 make note of Start (122880)
Now run this command:
sudo fdisk /dev/sda
At the prompt (Command (m for help):) type the next letters and then press enter after each:
p
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0x0002c262

    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            8192      122879       57344    c  W95 FAT32 (LBA)
    /dev/sda2          122880     5785599     2831360   83  Linux
d delete partition
2 partition to delete i.e /dev/sda2
p to confirm the deletion
n new partition (default)
p primary (default)
2 partition number (default)
122880 first sector as noted above (N.B. not default)
+50G last sector make size 50Gb (N.B. not default) or take default to use the remainder of the disk
p to confirm new partition is created
w writes the partition information, reboot required if the partition was already mounted (as it was in this instance)
After exit, run:
sudo resize2fs /dev/sda2 to expand the file system to fill the new partition

____________________________________________________________________
Option 2
run sudo fdisk /dev/mmcblk0

Then press the keys in order: 
d 2 to delete,
n p 2 Enter Enter to re-create.

Verify the prompts to make sure what you are entering is sensible.

Reboot and enter:-

sudo resize2fs /dev/mmcblk0p2
This is similar to the other answer, but using Pi partition names.
There is one thing to be wary of. MAKE SURE the new partition STARTS on the same block! If it tries to create a new partition in the spare 3M you will need to manually enter start block. Running sudo fdisk -l /dev/mmcblk0 BEFORE will list existing partitions.
Disk /dev/mmcblk0: 14.5 GiB, 15523119104 bytes, 30318592 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: 0x6d9a0006

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk0p1       8192    93814    85623 41.8M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      94208 30318591 30224384 14.4G 83 Linux

No comments: