Introduction
Running out of disk space on your Ubuntu system can lead to performance issues and prevent you from installing new software or saving files. In this guide, we’ll show you how to extend disk space (extend parition) on Ubuntu by resizing partitions, expanding filesystems. These step-by-step instructions will help you increase your available storage space without losing data.
Prerequisites for Extending Disk Partition on Ubuntu
- Backup Important Data
- Increase Virtual Disk Size (if applicable)
- If using a virtual machine, use your virtualization platform (e.g., VirtualBox, VMware) to increase the virtual disk size. This step should be done while the VM is powered off.
- Free Up Disk Space
- Before resizing, try to free up some space by removing unnecessary files using commands like
sudo apt autoremove
andsudo apt clean
.
- Before resizing, try to free up some space by removing unnecessary files using commands like
- Sufficient Permissions
- Ensure you have root or
sudo
access to perform disk management and resizing operations.
- Ensure you have root or
Steps to be done
Step 1: Identify hard drive use on your Ubuntu server
root@ubuntucore:/# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 96M 11M 86M 12% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 9.8G 9.8G 0 100% /
tmpfs 479M 0 479M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.8G 129M 1.5G 8% /boot
tmpfs 96M 4.0K 96M 1% /run/user/1000
root@ubuntucore:/# lshw -C disk
*-disk
description: ATA Disk
product: VBOX HARDDISK
vendor: VirtualBox
physical id: 0.0.0
bus info: scsi@2:0.0.0
logical name: /dev/sda
version: 1.0
serial: VB3f1a0754-d18ee5b0
size: 20GiB (21GB)
capabilities: gpt-1.00 partitioned partitioned:gpt
configuration: ansiversion=5 guid=defac7ee-e74c-4159-a912-41f07f74bb1d logicalsectorsize=512 sectorsize=512
Step 2: Increase the Virtual Disk Size
- In your virtualization software (such as VirtualBox, VMware, or Hyper-V), increase the size of the virtual disk to the desired capacity. For example, increase it from 10 GB to 20 GB or your required size.
Step 3: Extend the Partition
After increasing the virtual disk size, you need to extend the partition within Ubuntu.
Use cfdisk
or fdisk
to Resize the Partition.
List and modify partitions:
sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VBOX HARDDISK
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: gpt
Disk identifier: DEFAC7EE-E74C-4159-A912-41F07F74BB1D
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 3719167 3715072 1.8G Linux filesystem
/dev/sda3 3719168 41940991 38221824 18.2G Linux filesystem
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 has been deleted.
Command (m for help): n
Partition number (3-128, default 3):
First sector (3719168-41943006, default 3719168):3719168
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3719168-41943006, default 41943006):41943006
Created a new partition 3 of type 'Linux filesystem' and of size 18.2 GiB.
Partition #3 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: N
Command (m for help): w
The partition table has been altered.
Syncing disks.
Modify Partitions:
- Within
fdisk
, perform the following:- Type
p
to list the partition table. - Note the partition number of the root partition (
/dev/sdaX
). - Type
d
and the partition number to delete it (don’t worry, your data will be preserved as long as you recreate the partition with the same start sector). - Type
n
to create a new partition. - Select the same start sector as the deleted partition (fdisk will suggest the correct value).
- Use the new size for the partition.
- Type
w
to write the changes and exit.
- Type
Step 4: Reboot the System
Reboot your VM to ensure the changes take effect:
sudo reboot
Step 5: Extend Partition
root@ubuntucore:# sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 10.00 GiB (2560 extents) to 18.22 GiB (4665 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
You can check also the article related to the adding new hard drive to Ubuntu server at the link.
Extend Partition Troubleshooting
- Check partitions with
lsblk
- Resize the Physical Volume (PV): Run this command to extend the physical volume to the new partition size:
sudo pvresize /dev/sda3
- Extend the Logical Volume (LV): Now, use
lvextend
to add the remaining space to the logical volume:
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
- Resize the Filesystem: Finally, resize the filesystem to use the new space. For
ext4
:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv