I want to resize a partition from a virtual machine without erasing any data.

I only have access to this VM via SSH, so I am not able to run a live CD.

The OS is Ubuntu 14.04.2 LTS.

My current partitions are:

Number  Start   End     Size    Type      File system  Flags
1      1049kB  256MB   255MB   primary    ext2         boot
2      257MB   10,7GB  10,5GB  extended
5      257MB   10,7GB  10,5GB  logical                 lvm

I have 10,7GB unallocated. df -h output:

Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/server--vg-root    7,6G  5,8G  1,4G  81% /
none                           4,0K     0  4,0K   0% /sys/fs/cgroup
udev                           2,0G  4,0K  2,0G   1% /dev
tmpfs                          396M  516K  395M   1% /run
none                           5,0M     0  5,0M   0% /run/lock
none                           2,0G     0  2,0G   0% /run/shm
none                           100M     0  100M   0% /run/user
/dev/sda1                      236M   43M  181M  20% /boot

My /dev/sda has 21.5GB and I am only using about 10GB

All the tutorials that I've found requires a live CD.

Any idea how can I manage to do that?



It doesn't look like you actually have the empty space available - the "extended" partition usually covers all remaining disk space. It's possible that you do ; particularly if you created the disk image at 10GB and then it was resized to 20GB.

So, presuming you do have the empty space, you need to :

  • Make sure you have backups!
  • Resize the extended partition to fill the new upper sector limit
    • Use fdisk for this
    • Be careful! fdisk can wreck your partition table
    • A method for doing that is here : resize/grow partition without losing data
    • The method can be summarized as
      • Remove the existing partitions (2 and 5)
      • Recreate them with exactly the same starting sector numbers
      • For partition 2, allow it to consume the whole disk
      • For partition 5, use the exact starting sector and size it has now
  • Enrol a new LVM partition in the root volume group
    • Create a new Linux LVM partition in the extended space, allow it to consume remaining disk space
    • Make this a "physical volume" with pvcreate /dev/sdaX where X is the new partition
    • List the volume groups
      • vgdisplay
    • Extend the server volume group (replace name with name from previous step as required)
      • vgextend server-vg /dev/sdaX
    • Show the logical volumes
      • lvdisplay
    • Extend the root fs volume
      • lvextend /dev/server-vg/root /dev/sdaX
    • Resize the root FS to fit the new space
      • Depends on the FS you're using... e.g. for ext based FS
      • resize2fs /dev/server-vg/root

References :



  • Thanks @Adrian. In order to achieve the first step (Resize the extended partition to fill the new upper sector limit) I need to reboot in RESCUE mode. Since I don't have access to the VM (only SSH), I think I can't do this in rescue mode. Is that true? Dec 15 '15 at 12:26
  • I think I've messed up the partitions. Now I am getting an "Environmental block too small" :S Any idea on this? Dec 15 '15 at 18:24

I did it in a VM with Ubuntu Server 18.04 and without LVM.

Initial state after increase the HDD in VirtualBox.

ivanx@ivanx:~$ lsblk
sda      8:0    0   40G  0 disk
├─sda1   8:1    0    1M  0 part
└─sda2   8:2    0   20G  0 part /

Next step is to delete the partition and resize with fdisk.

ivanx@ivanx:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 41940991 41936896  20G Linux filesystem

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): p
Device     Start   End Sectors Size Type
/dev/sda1   2048  4095    2048   1M BIOS boot

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (4096-83886046, default 4096): 4096
Last sector, +sectors or +size{K,M,G,T,P} (4096-83886046, default **83886046**): 

Created a new partition 2 of type 'Linux filesystem' and of size 40 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p
Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 83886046 83881951  40G Linux filesystem

Command (m for help): w
The partition table has been altered.
Syncing disks.

Check results:

ivanx@ivanx:~$ df -h
/dev/sda2        20G   19G  466M  98% /

ivanx@ivanx:~$ sudo resize2fs /dev/sda2
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/sda2 is now 10485243 (4k) blocks long.

ivanx@ivanx:~$ df -h
/dev/sda2        40G   19G   20G  49% /

Why not try GParted? It has a GUI and allows [partition resizing and data moving.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.