23

In my lab environment I have an Ubuntu server 12.04 which I am trying to add space to. LVM was set up at boot up and after adding 100GB through the edit disk wizard I am unable to add that space to the partitions. I have tried using Part Magic but it is not allowing be to expand the size even though I do see the added space.

fdisk -l gives me;

Disk /dev/sda: 108.4 GB, 108447924224 bytes
255 heads, 63 sectors/track, 13184 cylinders, total 211812352 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
Disk identifier: 0x00097307

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    33552383    16525313    5  Extended
/dev/sda5          501760    33552383    16525312   8e  Linux LVM

Disk /dev/mapper/ubuntu--vg-root: 14.8 GB, 14751367168 bytes
255 heads, 63 sectors/track, 1793 cylinders, total 28811264 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
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu--vg-root doesn't contain a valid partition table

Disk /dev/mapper/ubuntu--vg-swap_1: 2143 MB, 2143289344 bytes
255 heads, 63 sectors/track, 260 cylinders, total 4186112 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
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu--vg-swap_1 doesn't contain a valid partition table

I haven't really used LVM before and am not a a Linux guru at all so I am not sure what my next steps would be as I am searching for howtos on this process. In the end I would like to add some space to the boot partition as well.

Thanks for your time in reading this question and let me know if there are additional details I could give you.

3 Answers 3

32

If you want to do this without GUI tools, you can use parted.

On my system, I was expanding the root filesystem (which was on dev/sda3) from 128GB to 160GB. Adjust the examples below depending on your setup. All the commands must be run as root (sudo -i to login as root).

Beforehand, I was at 95% usage:

# df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  124G  113G  6.3G  95% /

With Gen2 virtual machines, everything can be done online, while the guest OS is running, and without rebooting. Each step is almost instantaneous.

First, expand the virtual disk using the Hyper-V GUI or CLI tools (easiest way is to open the VM's settings, select the hard drive under the SCSI controller, then choose "Edit" then "Expand").

Next, you need to tell Ubuntu to rescan the block device, in this case sda:

# echo 1 > /sys/block/sda/device/rescan

Next, run parted and choose p for print. It should recognize that the partition table isn't using the full space and ask whether you want to fix it, so choose Fix at this point.

# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an
extra 69206016 blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 172GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  538MB   537MB   fat32              boot, esp
 2      538MB   1612MB  1074MB  ext4
 3      1612MB  136GB   135GB

Now you need to actually expand the partition. While still in parted, enter the resizepart command to accomplish this. In my case, since I'm expanding sda3 I tell it to resize partition 3 to 100% of available space. Again, adjust the command for your setup.

(parted) resizepart 3 100%
(parted) p
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 172GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  538MB   537MB   fat32              boot, esp
 2      538MB   1612MB  1074MB  ext4
 3      1612MB  172GB   170GB

(parted) quit

Type quit to exit parted.

If you're using LVM, you now need to tell it to expand the physical volume. pvs shows the info on your physical volumes; use pvresize to expand it. With no parameters given, it will use all available space.

# pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <125.50g    0
# pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
# pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <158.50g 33.00g

Now, you need to expand the logical volume, as well as the filesystem itself. lvextend with the -r switch will do both at once, assuming you're using a filesystem it knows about such as ext3/4.

# lvs
  LV        VG        Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  ubuntu-lv ubuntu-vg -wi-ao---- <125.50g

# lvextend -l +100%FREE -r /dev/mapper/ubuntu--vg-ubuntu--lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <125.50 GiB (32127 extents) to <158.50 GiB (40575 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 16, new_desc_blocks = 20
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 41548800 (4k) blocks long.

That's it! Now verify that the filesystem sees the extra space:

# df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  156G  113G   38G  76% /

If you're not using LVM, you can replace the pvresize/lvextend steps with resize2fs instead. A simple resize2fs /dev/sda3 will tell the filesystem to use all available space on the newly-increased partition.

This article on Teknophiles has some more details on this process along with screenshots of the Hyper-V tools. (Update Dec 2021: apparently teknophiles.com is now defunct; updated link goes to an archive.org snapshot of the site.)

2
  • 5
    This worked perfectly on a Generation 2 Ubuntu 20.04 LTS VM with a resized dynamically expanding disk on Hyper-V 2019. Thanks! May 23, 2020 at 14:04
  • 1
    Amazing post, works perfectly, on Hyper-V 2019 + lvm
    – garyj
    Nov 30, 2021 at 1:28
20

As of 2019, I was having some issues trying to resize a Ubuntu 18.04 V.M. running on Hyper-V. Installing gparted: sudo apt install gparted, running it: sudo gparted and going through the resizing process on the application's GUI solved my problem.

gparted GUI aid: https://youtu.be/cDgUwWkvuIY?t=330

EDIT: Apparently, this still works as far as Jan/2021 as this answer got upvotes recently.

4
  • Worked like a charm! Jun 14, 2019 at 11:53
  • I've successfully used gparted to adjust disk space allocation within a Quick Create Ubuntu 20.04 VM run by Hyper-V Manager (Windows 10 Enterprise). Thanks Marcelo! Oct 1, 2021 at 17:30
  • On ubuntu 20 created with hyper-v quick create, sudo gparted didn't work for me. I had to simply run gparted and it prompted me for credentials, after which I was able to make the changes with the GUI.
    – MikeyT
    Oct 4, 2022 at 19:11
  • Latest I tried this (27 December 2022) on Ubuntu 22.04 VM, I couldn't resize partitions using gparted; so I used Disks utility instead, and that worked smoothly Dec 27, 2022 at 13:26
5

You use gparted to resize partitions. In 12.04 you will need to boot from the livecd and run it from there as it can not resize partitions that are mounted. In later releases, it can grow many partitions while they are mounted. This will only give LVM more space to allocate to logical volumes. If you want to grow a logical volume, you can run for instance lvresize -L 30g ubuntu-vg/root to increase the size of your root lv to 30g. Then you need to tell the filesystem to use that new space, which you can do with resize2fs /dev/ubuntu-vg/root, assuming you are using the default ext4 filesystem. These two latter steps can be performed online rather than booting from other media. You can also specify 100% instead of 30g to lvresize to use all available space, but it kind of defeats the purpose of using LVM in the first place to allocate all space to one logical volume, since you don't have any free space for creating snapshots, new logical volumes, or growing some volumes later.

2
  • Thank you for your response. I ended up using gparted iso to boot from and increased the size. When I logged into the system it was still showing the same space but I then used the commands you posted and this worked. Thanks again sir. Jul 10, 2015 at 3:06
  • @JosephBarker, you are welcome. Please check the tick box next to the answer to accept it.
    – psusi
    Jul 10, 2015 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .