Ubuntu expand disk space – Command Line

Warning: Be extremely careful when making changes to partitions and disk as it can lead to broken systems and lost data. Make sure you have a backup.

This scenario is done on a basic Ubuntu install. No fancy LVM stuff going on. If you need that, refer to here

Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 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: 0x2062ec28
Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 65011711 65009664   31G 83 Linux
/dev/sda2       65013758 67106815  2093058 1022M  5 Extended
/dev/sda5       65013760 67106815  2093056 1022M 82 Linux swap / Solaris

From the above output of fdisk -l, we see that the disk has 64GiB available, but the primary partition is only 31G. To make the primary partition larger we need to

  • Run fdisk “fdisk /dev/sda”
  • Delete partitions 2 and 5,
  • Delete Partition 1
  • Create Partition 1 again on the same starting boundary
  • Put the end boundary close to the end so we end up with ~62GiB for that partition
  • Recreate sda2, the 1GiB extended partition
  • Write changes to disk
  • Run resize2fs to resize the filesystem

You may need to boot up in recovery to get this command working. Also if you boot up in recovery, you’ll need to remount the root / partition read/write. More info here.

resize2fs /dev/sda1

Helpful Links
https://access.redhat.com/articles/1190213
https://access.redhat.com/articles/1196353

Finding a Drives Name in Linux From the Command Line

There are a few different ways to find out a drives(sdcard, usb drive, external hard drive) name.

dmesg command

One way to do it is to look at dmesg. Insert your drive and then run the command. It displays a lot of info, what we are interested in is the end which should say something about your drive.

dmesg
[ 4443.109976] mmc0: new high speed SDHC card at address aaaa
[ 4443.111857] mmcblk0: mmc0:aaaa SU04G 3.69 GiB 
[ 4443.120836]  mmcblk0: p1 p2
[ 4453.045338] EXT4-fs (mmcblk0p2): recovery complete
[ 4453.086165] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[ 4453.086184] SELinux: initialized (dev mmcblk0p2, type ext4), uses xattr

This tells us that the device is mmcblk0. The “p2” at the end is the partition number.

df Command

Another way to do it is to run the df command.
Run the below command without your drive plugged in.

df -h

it’ll return something like this

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/fedora-root   50G   12G   36G  24% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G  600K  1.9G   1% /dev/shm
tmpfs                    1.9G  1.0M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs                    1.9G   28K  1.9G   1% /tmp
/dev/sdb1                477M  115M  333M  26% /boot
/dev/mapper/fedora-home   76G   45G   23G  87% /home

The above command returns all the partitions that are mounted on your computer.
Now mount your drive and run the command again, it should show your drive at the bottom.

[me@fedora ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/fedora-root   50G   12G   36G  24% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G  600K  1.9G   1% /dev/shm
tmpfs                    1.9G  1.1M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs                    1.9G   28K  1.9G   1% /tmp
/dev/sdb1                477M  115M  333M  26% /boot
/dev/mapper/fedora-home   76G   45G   23G  87% /home
/dev/mmcblk0p2           3.6G  2.3G  1.1G  69% /run/media/me/fc522c75-9sws

You can see that the bottom one “/dev/mmcblk0p2 ” is the partition of the drive you just plugged in.

Using fdisk

You can also use fdisk.

sudo fdisk -l

It will return something similar to the following.

Disk /dev/mapper/fedora-home: 78.8 GiB, 191931351040 bytes, 374865920 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/mmcblk0: 3.7 GiB, 3965190144 bytes, 7744512 sectors

The bottom section is the drive “mmcblk0”.

Using lsblk

lsblk is another cool tool to list drives and partions. When run with the -p option it shows the path to the drive and partition.

Example output of what you may get with “lsblk -p”

admin@localhost:~$ lsblk -p
NAME                              MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
/dev/nvme1n1                      259:0    0   450G  0 disk  
├─/dev/nvme1n1p1                  259:1    0   499M  0 part  
├─/dev/nvme1n1p2                  259:2    0   100M  0 part  
├─/dev/nvme1n1p3                  259:3    0    16M  0 part  
└─/dev/nvme1n1p4                  259:4    0 449.3G  0 part  
/dev/nvme0n1                      259:5    0   477G  0 disk  
 ├─/dev/nvme0n1p1                  259:6    0   512M  0 part  /boot/efi
 ├─/dev/nvme0n1p2                  259:7    0   732M  0 part  /boot
 └─/dev/nvme0n1p3                  259:8    0   400G  0 part  
   └─/dev/mapper/vg-root           253:1    0   391G  0 lvm   /
   └─/dev/mapper/vg-swap_1         253:2    0   7.9G  0 lvm   [SWAP]
admin@localhost:~$