How can I resize an LVM partition? (i.e: physical volume)

421,595

Solution 1

Back up all your important data before attempting this. Always assume that you can lose all your data when resizing partitions.

Shrink LVM without dataloss

This forum thread suggests the following procedure, in this example we shrink a partition from 10G to 9G:

First, we unmount.

sudo umount /dev/vg_blah/lv_blah

Check the file system.

sudo e2fsck -f /dev/vg_blah/lv_blah

Resize the file system and the logical volume. Doing this in one step can avoid mistakes which cause dataloss. (Note lvreduce parameter -L interprets G as 2^30 bytes and g as 10^9 bytes.)

sudo lvreduce --resizefs -L 9G /dev/vg_blah/lv_blah

(optional) Remove a physical drive from the volume group if desired (if you now have an unused drive).

sudo vgreduce vg_blah /dev/sdxy

All credit goes to brianmcgee.

Note: vgreduce will fail with cannot resize to xxxxx extents as later ones are allocated if you have another lv at the end of the disk - I had a swap lv, which I deleted. See How to shrink Ubuntu LVM logical and physical volumes? for help on that situation.

Solution 2

You can shrink/extend a logical volume very easily with a GUI tool: system-config-lvm. Because system-config-lvm does not come pre-installed, once booted from a live-cd, you have to install it:

sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe"
sudo apt-get update
sudo apt-get install system-config-lvm

Once it is installed, run the program, select the logical volume you want to change in the left panel, click 'Edit Properties'.

Main Logical Volume Management program GUI

Now change the parameters as you wish!

Edit Logical Volume window

One good thing about system-config-lvm is that it won't allow you to go ahead if the new partition size is smaller than all your existing data combined (I've just tried this and seen for myself).

References:

  1. How-To Geek tutorial (this is the origin of the images).
  2. Red Hat's complete guide for the program.

Solution 3

Note: You should run almost all of the following commands using a Live CD. Sometimes, it wants you to unmount the partition that already mounted on / (in this case, you cannot unmount it).

These are all the steps required to resize a LVM or LVM2 partition:

sudo lvresize --verbose --resizefs -L -150G /dev/mapper/ubuntu

sudo pvresize --setphysicalvolumesize {some-space} /dev/sda5

If the second command produces something like this:

/dev/sda5: cannot resize to xxxxx extents as later ones are allocated.

Then, you have to rearrange the unallocated space at the end of the LVM. That means after root and swap_1 partition. So, initially, you need to check physical volumes' information using the command below:

sudo pvs -v --segments /dev/sda5

This will show the output like:

/dev/sda5 ubuntu lvm2 a-- 698.04g 150g   0 xxx root 0 linear /dev/sda:0-rrr
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx sss 0 free                          
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g zzz ttt swap 0 linear /dev/sda5:yyy-www

Note the yyy-www. Use the following command to remove external fragmentation:

sudo pvmove --alloc anywhere /dev/sda5:yyy-www

Now, let us see how it goes:

sudo pvs -v --segments /dev/sda5

This will show the output like (note the changes):

/dev/sda5 ubuntu lvm2 a-- 698.04g 150g   0 xxx root 0 linear /dev/sda:0-rrr
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx ttt swap 0 linear /dev/sda5:xxx-sss
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyy www 0 free

After that, use the GParted and resize the LVM to maximum used area and rest will be in unallocated space. Enjoy...

Solution 4

I would recommend another GUI tool - KVPM. It combines functionality of GParted and system-config-lvm. Moreover, it allows some operations on live (mounted) partitions that system-config-lvm doesn't allow However, the volume has to be unmounted for shrinking.

enter image description here enter image description here

It is in universe repository since Ubuntu 12.04, so just do sudo apt-get install kvpm

Note: start it with root rights sudo kvpm

Solution 5

Only the latest version of Gparted (0.14) supports resizing LVM physical volumes. The version that ships with Ubuntu 12.10 or 13.04 does not support it.

Here's how to resize an LVM physical volume:

  1. If the volume group associated with the LVM physical volume does not have enough unallocated space, you'll need to make some by shrinking a logical volume. Look at bigbadonk420's answer for instructions on how to do that. You can also use GNOME Disks (included with Ubuntu) for some of the steps if you prefer a GUI.

  2. Burn the latest .iso image from GParted's website, and burn it on a CD or put it on a USB stick. Boot from the CD or the USB stick. Alternatively, you could compile the latest version of GParted from source.

  3. Use GParted to resize the LVM physical volume. GParted won't let you shrink the LVM physical volume to a size smaller than what the unallocated space allows.

Share:
421,595

Related videos on Youtube

Idan Yadgar
Author by

Idan Yadgar

Updated on September 18, 2022

Comments

  • Idan Yadgar
    Idan Yadgar over 1 year

    I want to shrink my second (LVM) partition, in order to create a new partition in the newly freed space. I am using the Live CD to do so, because I know I can't resize/move this partition while it is in use. When I opened GParted in the Live CD, I realized that I could not resize the partition, because when I right-click it, the option "resize/move" is disabled.

    I tried to unmount it, to "lazy" unmount it (umount -l /dev/sda2) but it didn't work.

    A screenshot from GParted:

    GParted screenshot

  • Idan Yadgar
    Idan Yadgar over 11 years
    e2fsck -f /dev/vg_blah/lv_blah returns: "e2fsck: Device or resource busy while trying to open /dev/sda2 Filesystem mounted or opened exclusively by another program?", the same with resize2fs...
  • pzkpfw
    pzkpfw over 11 years
    Is it unmounted? What did you type in your command specifically (I'm assuming you didn't type vg_blah)
  • Idan Yadgar
    Idan Yadgar over 11 years
    Yes, it is unmounted. I typed e2fsck -f /dev/sda2.
  • Flimm
    Flimm over 11 years
    The answer is missing how to resize the physical volume. Also, I highly recommend combining the two resizing steps into one with lvreduce -r.
  • Flimm
    Flimm over 11 years
    Also, you need to realise that 1G to lvreduce is 1000000000 bytes, whereas to resize2fs it is a gibibyte. If you want to pass 1 gibibyte to lvreduce, use 1g instead.
  • Martijn Heemels
    Martijn Heemels about 11 years
    Thanks Flimm. I was expecting 12.10 to have a recent enough version of gparted. Your answer pointed me in the right direction.
  • Paul
    Paul over 9 years
    I got the latest gparted, but it won't let me change the partition sizes... I have /dev/sda2 of type ext2 and under that, /dev/sda5 of type lvm2. But gparted says for both that min and max sizes are the same, and therefore does not allow me to change it... :-(
  • Mike
    Mike over 9 years
    GUI is definitely the way to go, however you will likely need to run it from a live CD.
  • 203967
    203967 over 9 years
    Or use pvresize of the LVM2 suite, right?
  • 203967
    203967 over 9 years
    I disagree a bit. Shrinking LVM logical volumes with system-config-lvm is definitely riskier than lvresize -r or lvresize after an earlier file system resize.
  • Castiblanco
    Castiblanco over 9 years
    @Paul did you solve it? I have exactly the same problem :S
  • Paul
    Paul over 9 years
    @Castiblanco I didn't solve it, I ended up wiping the whole things and reinstalling without LVM. For me, one lesson learned: Don't use LVM for standard desktop installations.
  • Castiblanco
    Castiblanco over 9 years
    @paul I just did the same haha
  • psusi
    psusi over 9 years
    You don't use vgreduce; that is for removing a pv from a volume group.
  • krystan honour
    krystan honour over 9 years
    don't forget your apt-get update in between add the repo and calling apt-get install or its not going to find it
  • Alexey Ce
    Alexey Ce over 9 years
    why is system-config-lvm riskier than lvresize or lvresize -r?
  • bain
    bain about 9 years
    You could also try blivet-gui (github, Ubuntu PPA), the next-gen partition manager that handles LVM drives.
  • Urhixidur
    Urhixidur almost 9 years
    I strongly suspect lvreduce uses G = gibibyte in differential mode. It certainly does in absolute mode. Its man pages are poorly written and don't explain what their units mean, which I consider as bordering on criminal negligence.
  • Urhixidur
    Urhixidur almost 9 years
    Beware that system-config-lvm uses powers-of-1024 units, despite using the (incorrect) 'gigabyte' wording. It really should do like palimpsest (a.k.a. Disk Utility) and specify its meaning in unambiguous bytes.
  • STW
    STW almost 9 years
    Use lvresize --resizefs -L 9G /dev/vg_blah/lv_blah (or -L -1G) rather than two seperate commands to resize the logical volume and the filesystem
  • Erhannis
    Erhannis almost 8 years
    This, subsequently combined with askubuntu.com/a/604305/106119 worked for me.
  • Jeff Puckett
    Jeff Puckett almost 8 years
    it is not in the universe repository: E: Unable to locate package kvpm
  • Stefan
    Stefan almost 8 years
    This answer seems to talk about resizing a logical volume, whereas the question is talking about resizing a physical volume.
  • Stefan
    Stefan almost 8 years
    How does that help the OP's problem of resizing a physical volume?
  • Stefan
    Stefan almost 8 years
    pvresize seems to only make it possible to move the end of a physical volume. How would one go about moving the start of one?
  • Stefan
    Stefan almost 8 years
    This lets you shrink a logical volume, but the OP's question is about shrinking a physical volume.
  • Stefan
    Stefan almost 8 years
    Again, the OP wants to reduce a physical volume.
  • Stefan
    Stefan almost 8 years
    But that doesn't shrink the physical volume.
  • Kos
    Kos almost 8 years
  • Quang Hà
    Quang Hà over 7 years
    you saved my life
  • Admin
    Admin over 7 years
    This worked for me, where the "system-config-lvm" did not load any connected devices/drives/filesystems. With the added bonus of not having to download and extra repository and being preconfiged on my Ubuntu machine. Also, worked over Gparted since that was spitting errors about the file system being reduced too much. (I'm resizing a RetroOrangepi SD Card down from 32GB to 8GB for backup purposes)
  • Dhinesh.B
    Dhinesh.B over 6 years
    To get the volume paths and to avoid the error Invalid path for Logical Volume, use lvdisplay. The path of my volume was /dev/myHost/root/ when performing parititioning with the GParted live CD.
  • Suncatcher
    Suncatcher about 6 years
    system-config-lvm no longer delivered since 17.10
  • m93a
    m93a almost 6 years
    @Suncatcher on 17.10 and 18.04 you have to add the Xenial repository. sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu xenial universe"
  • user217905
    user217905 almost 6 years
    Also if you're connected through SSH I insistently recommend you to use SCREEN or similar app. It's a good idea to make sure connection problems would lead to data loss.
  • user217905
    user217905 almost 6 years
    Wouldn't lead to data loss of course.
  • Xen2050
    Xen2050 about 5 years
    Looks good, thought it might want an extra ~100M for all the other K dependencies, if you're not already using KDE
  • pzkpfw
    pzkpfw over 4 years
    I have marked this answer as community wiki, feel free to improve it.
  • JC1
    JC1 almost 4 years
    On Ubuntu 20.04 LTS live cd I get an unmet dependencies issue after adding the xenial repository.
  • MrMas
    MrMas almost 3 years
    I had to start with gparted since lvresize reported "Insufficient space" with 0 extents available. I'm using Ubuntu 18.04 live CD run in VirtualBox 6.1 and the LVM was created by Ubuntu 18.04 server install.