I have Ubuntu installed on a drive. The problem is it is running low on disk space. This is installed on vmware. I expanded the virtual drive and booted into ubuntu. But when I opened gparted(sudo gparted), the move/resize option is unavailable. This is the partition Ubuntu is installed on, but I need to resize it. Any ideas? I am comfortable using command line
As a matter of fact, you CAN enlarge the root filesystem while Ubuntu is running (I learned this recently myself here) - this sounds incredible but it's true :)
Here's the list of steps for a simple scenario where you have two partitions,
/dev/sda1
is an ext4 partition the OS is booted from and/dev/sdb2
is swap. For this exercise we want to remove the swap partition an extend/dev/sda1
to the whole disk.As always, make sure you have a backup of your data - since we're going to modify the partition table there's a chance to lose all your data if you make a typo, for example.
Run
sudo fdisk /dev/sda
p
to list the partitions. Make note of the start cylinder of/dev/sda1
d
to delete first the swap partition (2
) and then the/dev/sda1
partition. This is very scary but is actually harmless as the data is not written to the disk until you write the changes to the disk.n
to create a new primary partition. Make sure its start cylinder is exactly the same as the old/dev/sda1
used to have. For the end cylinder agree with the default choice, which is to make the partition to span the whole disk.a
to toggle the bootable flag on the new/dev/sda1
w
to write the new partition table to disk. You'll get a message telling that the kernel couldn't re-read the partition table because the device is busy, but that's ok.Reboot with
sudo reboot
. When the system boots, you'll have a smaller filesystem living inside a larger partition.The next magic command is
resize2fs
. Runsudo resize2fs /dev/sda1
- this form will default to making the filesystem to take all available space on the partition.That's it, we've just resized a partition on which Ubuntu is installed, without booting from an external drive.
Two Methods
1. Tested in Ubuntu Server 16.04 to 18.10
After extending the volume in VMware ESXi (shutdown vm, backup/export vm, remove all snapshots, then increased number in disk size, take snapshot of vm again before following the next steps so you can revert mistake/error).
The following example is extending volume on root partition on a system that is already using LVM.
ssh
into the system, switch to root and runcfdisk
# cfdisk
After exiting cfdisk
Reboot system before next step (or else you might get
Device /dev/sda3 not found (or ignored by filtering)
)Initialize the new volume
Get the name of volume to extend
Extend volume group
Extend root
Get filesystem name for next step
Extend filesystem
See the increased size
2. Faster Method Tested in Ubuntu Server 18.04.3 LTS
This method is a little faster than the first. This is due to a new option in the
cfdisk
menu called "Resize" that saves some steps.After extending the volume in VMware ESXi (shutdown vm, backup/export vm, remove all snapshots, then increased number in disk size, take snapshot of vm again before following the next steps so you can revert mistake/error).
The following example is extending volume on root partition on a system that is already using LVM.
ssh
into the system, switch to root and runcfdisk
.Choose the partition to extend and select "Resize".
Set the "New size".
After pressing enter, you'll see screen with the following note "Partition [someNumber] resized":
Next you'll need to "Write" (save) your changes:
Quit
cfdisk
. When you exit you may see message "syncing disks".I rebooted my system at this point but it may not be necessary. Now you can pickup from step 19 from the first method (repeated below).
(19) Get filesystem name for next step
(20) Extend filesystem
(21) See the increased size
Articles:
https://wiki.ubuntu.com/Lvm
http://www.geoffstratton.com/expand-hard-disk-ubuntu-lvm
EDIT: As mentioned below by @kent this is an old answer and no longer true.
You are almost there.
You can not resize the partition using GParted while Ubuntu is running.
You will need to attach the GParted ISO's as a CD to the VM machine and reboot the machine so that the GParted will be loaded instead of Ubuntu (I think you can boot from the virtual CD by pressing F12 immediately after machine is started).
Once you booted into GParted the option to move/resize will be enabled as Ubuntu is not currently running.
jtlindsey's "faster" method works on LVMs but is missing some steps.
df -h
wouldn't show the additional space.sudo lsblk
to determine your partition name (mine was sda3).sudo pvresize /dev/sda3
sudo lvextend -L +76G /dev/mapper/ubuntu--vg-ubuntu--lv
(replacing the size and the name with your own)Source: https://askubuntu.com/a/1047512/393368
I noticed that my ubuntu VM had a LVM that simply was not using all the allocated space. So to add space to my LVM it was quite simply this: https://wiki.ubuntu.com/Lvm chapter: "resizing partitions".
For example: A quick way to know if you have an LVM is to do
df -h
and look for this line:And... see that is not actually the classic "/dev/sda1" type of mount point.
Then run the extension commands on that partition.
Another quick clue that reminded me to check for an LVM was that I noticed when I looked at the fdisk partitions and saw that I had a large /dev/sda3 partition that was not really mounted anywhere. (ie: when I checked with the mount command).
The answer from Sergey was excellent. If you have the empty space not directly in the next device, however, you will need to delete other devices. For example:
In the above case, if you want to merge
/dev/sda3
with/dev/sda1
, you would need to remove/dev/sda1
,/dev/sda2
, and/dev/sda3
(withd
command), and re-add/dev/sda2
and update its type (withn
andt
commands).I succeeded in enlarging my primary ext4 partition using the methodology described here.
Some notes from my personal experience:
With
cfdisk
andresize2fs
I was able to extend my/
partition on proxmox VM. Thanks to jtlindsey for the example.I had the next picture before extending:
With
cfdisk
I've removed swap partition, then resized my/
partition with Resize option to 80Gb and recreated the swap partition. Then I've written changes with Write option:After this I've rebooted and used the following command to extend my file system to the current partition's size:
Make sure you've created backup of important data before any disk changing action. So, jtlindsey's method could be used even for none-LVM partitions.
Also do not forget to get swap back by:
And change UUID of swap partition in
/etc/fstab
to whatsudo blkid
shows for it.