11

I have Ubuntu installed on a VirtualBox VM. How can I resize the Linux partition (not the virtual drive)?

I already performed the VM resize using VBoxManage modifyhd to resize the virtual drive, but I need to resize the Linux partition. I do not have a GUI to gparted, so I need to resize the partition from the command line.

Jake
  • 491

3 Answers3

8

If you use LVM (Logical Volume Manager):

  1. Expand physical volume (sda3 replace by your partition name):

    sudo pvresize /dev/sda3
    
  2. Check free space:

    sudo vgdisplay
    
  3. Add space to logical volume (VG00/share replace by your LV path; you can get it from sudo lvdisplay):

    sudo lvresize –L -r 100G VG00/share
    
  4. Check changes:

    sudo lvdisplay
    

If you don't use LVM:

All below commands work for unmounted partition, so you should boot from another source, for example, from iso (liveCD image).

  1. Change partition size: sudo fdisk /dev/sda:

    • Remove the old partition (d).
    • Create a new one, starting at the same sector but bigger (n).
    • You should get a message saying that partition contains a signature. When asked, whether you want to remove it, answer n. If you didn't get the message, then something is wrong, don't continue (don't write the changes) but quit (q).
    • If all is well, write changes (w).
  2. Expand file system:

    sudo e2fsck -f /dev/sda3
    sudo resize2fs /dev/sda3
    
  3. Reboot system, boot as usual.

  4. Check partitions:

    df
    
Nadia
  • 91
4

If it is a physical disk (not LVM, you can check it with lvdisplay), then you need two steps:

  1. Grow your partition (e. g. /dev/sdb1):

    sudo growpart /dev/sdb 1
    

    Yes, separate the partition number from the device ;-)

  2. Grow your filesystem:

    sudo resize2fs /dev/sdb1
    

If it is a LVM, you need three steps:

  1. Grow your physical volume:

    sudo pvresize /dev/sdb1
    

    And wait until sudo pvdisplay returns your new size. While pvresize already reports that the resize happened and thus terminates, the resize may actually still be ongoing.

  2. Grow your logical volume[1]:

    sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
    
  3. Grow your file system[1]:

    sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
    

[1] Where /dev/ubuntu-vg/ubuntu-lv is the path to your logical volume. You can query it with sudo lvdisplay. Alternatively, you can use the mapped name of your logical volume, e. g. /dev/mapper/ubuntu--vg-ubuntu--lv, that you see in the output of df -h.

0

After resizing the virtual machine disk, I could see in fdisk that /dev/sda was 128 GiB but /dev/sda3 was only using 99G. I used a combination of these three answers to grow the partition and expand my LV:

$ sudo growpart /dev/sda 3
CHANGED: partition=3 start=2101248 old: size=207611904 end=209713152 new: size=266334175 end=268435423

$ sudo pvresize /dev/sda3 Physical volume "/dev/sda3" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized

$ sudo lvextend -l +100%FREE -r /dev/mapper/ubuntu--vg-ubuntu--lv Size of logical volume ubuntu-vg/ubuntu-lv changed from <99.00 GiB (25343 extents) to <127.00 GiB (32511 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized. ...

Now I can see the disk space I expected:

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              3.2G  3.0M  3.2G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  125G   40G   80G  33% /
...
rymo
  • 161