You can do it all on the Pi if you have SSH or terminal access, below are some notes I made when I first did it, you shouldn't lose any data if you do it correctly but as always it pays to have a backup just in case :
At a terminal prompt (you may need to amend the partition names depending on your layout/requirements) N.B. the results in this example were based on resizing the partition on an attached 500Gb USB hard drive
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
/dev/mmcblk0p1. Here you are working on some other partition, presumably on an attached harddrive, which can easily be unmounted and modified. It is possible to resize the primary partition with the system running, but if you screw it up you will have problems. – goldilocks Jan 27 '18 at 20:29N– pmiguelpinto90 Jul 17 '20 at 08:31