Friday, April 8, 2016

Move free space out of LVM


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?



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.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...