8

I have a 500 GB LVM2 container on a 500 GB HDD which holds a 96 GB root partition (ext4) and a 4 GB swap partition. I made this setup using the alternate cd and set the amount of disk space to be used to 100GB, presuming that the remainder would become available as regular, unencrypted disk space.

It turns out that the 400 GB of free disk space is actually encapsulated in the LVM2 container, as Disk Utility shows me. How do I shrink the LVM2 container so I can utilize this 400 GB?

2 Answers 2

5

Simple answer - you can't. (But you may want to do something else to get what you want - see below)

LVM2 has 3 'levels', the Physical Volumes (which is what I assume you are referring to as an LVM2 container), Volume Groups and Logical Volumes.

Generally you define one or more partitions to be Physical Volumes for LVM2. Once this is done, the partition cannot be used for anything else (in the same way a partition can only be used for one filesystem).

E.g.:

# pvcreate /dev/sdb3

You then create a new Volume Group or add the physical volume to an existing group

# vgcreate MyVolGroup /dev/sdb3

or

# vgextend MyVolGroup /dev/sdb3

You then create the Logical Volume, which actually gets used for a filesystem.

# lvcreate -L100G --name MyVolume MyVolGroup

The Logical Volumes can be resized without losing data, although it's non-trival. But the Physical Volumes' sizes are fixed as the partition sizes.

If you actually want to use your 400Gb of space which is already allocated to a Physical Volume (LVM2 container) then the answer is to create another Logical Volume and use that.

To find then name of the Volume Group (if you don't already know it) use the vgs command.

# lvcreate -L400G --name MySpace VolGroup
# mkfs -text4 /dev/VolGroup/MySpace
# mount /dev/VolGroup/MySpace /mnt

The more advanced alternative, if you have enough spare space to create a new partition, is to force the existing Logical Volume to be moved (using pvmove) so that you can free up the 500Gb Physical Volume (vgreduce and pvremove commands) and then create a smaller container and move the data back again, but there's probably little advantage over simply using Logical Volumes for everything.

1
  • Your extensive answer is much appreciated, I will be looking into it and giving feedback.
    – user77111
    Jul 19, 2012 at 22:41
3

You didn't specify why you want to shrink the LVM partition, so it is possible that you may just want to create new logical volumes within the LVM container. If you are sure that you really want to reduce the size of the LVM container, then you can use pvresize to reduce the size of the Physical Volume, and then partitioning tools such as fdisk or parted to reduce the size of the disk partition that contains the physical volume. You need to make sure that you are using units of sectors when partitioning ( in parted, the unit s command will do this ). You will need to delete the partition, then recreate it with the same starting sector, but a smaller size. If you specify the size to pvresize in GB, you will need to convert to sectors by multiplying by 1024 * 1024 * 2, and should round up for safety, i.e. add an extra GB. You will need to reboot for the changes to the partition table to take effect, and then you may want to use pvresize again without a size argument to expand it back up to the maximum size the new partition holds. You will then have unpartitioned space to do with as you wish.

2
  • 2
    The man page currently notes a restriction on pvresize that it won't shrink a Physical Volume if there are extents after where the new end would be; it doesn't automatically move them out of the way. AFAIK, this hasn't been fixed yet, which often means you have to do a pvmove and clear the Physical Volume, after which it's easier to just use pvremove and delete/recreate the partition rather than worrying about size calculations.
    – StarNamer
    Aug 2, 2012 at 23:48
  • 1
    @StarNamer, it depends. If you have never filled up the pv in the past, then odds are good that the existing volumes won't use the space you are trying to remove. If they do then yes, you may need to manually pvmove to compact volumes to the start of the disk.
    – psusi
    Aug 6, 2012 at 23:30

You must log in to answer this question.