Ubuntu – How to create LV(Logical Volume) after ubuntu is installed


everyone. I need to create LV for xen guest. There is only one disk of 70G. While installing, I use the default process and not the LVM. Do I need to reinstall Ubuntu to create the LV? Is there some better method? Thanks a lot.

root@vswitch-BlackfordESB2:/home/vswitch# vgdisplay
  No volume groups found
root@vswitch-BlackfordESB2:/home/vswitch# vgcreate vm /dev/sda1
  No physical volume label read from /dev/sda1
  Can't open /dev/sda1 exclusively.  Mounted filesystem?
  Unable to add physical volume '/dev/sda1' to volume group 'vm'.
root@vswitch-BlackfordESB2:/home/vswitch# pvdisplay

root@vswitch-BlackfordESB2:/home/vswitch# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              60G  4.2G   53G   8% /
udev                  3.6G  4.0K  3.6G   1% /dev
tmpfs                 1.5G  1.1M  1.5G   1% /run
none                  5.0M     0  5.0M   0% /run/lock
none                  3.6G     0  3.6G   0% /run/shm

Best Answer

LVM can be set up after the system is installed. But putting the root partition on the LVM after the fact is quite difficult and probably would be better done with a reinstallation. Furthermore, it is not possible to just create a LVM on a already mounted partition.

But it seems that the main goal is to provide some space on a LVM for Xen domains. This can be done by shrinking /dev/sda1 and creating a new partition /dev/sda2 for a LVM physical volume.

Warning: The procedure described below will change your partitions and filesystems. If there is any data on this system you cannot do without, a backup is strongly recommended.

While it is possible to resize a mounted filesystem and partition, it is not recommended and should only be done if you really know what you are doing. I'd recommend using the GParted Live System. It provides graphical tool, to easily resize partitions and create new ones.

Create a GParted Live CD or USB drive and boot from it. Once it is running, there is a desktop with some icons. Open the one saying GParted.

In the GParted program select your hard drive (it should say "/dev/sda (60 GiB)"). Right-click on the partition ("/dev/sda1") and select "Resize/Move". You now can set the size in "New Size (MiB)". Depending on what you want to do with the system itself, I'd recomend to leave at least 12 GiB for your system (you already use 4.2 GiB and you may want to be able to download ISOs for installation of your Xen guests). Ensure that it still says "0" in "Free space preceeding (MiB)", as you want to create the new partition after your root partition. Save the changes.

Now right-click on "unallocated" and select "New". Change "File system" to "lvm2 pv". The rest should already be set to fill the entire free space. Add the partition.

Finalize these changes by clicking on "Apply all Operations" (either the check mark in the toolbar or in the "Edit" menu)

Restart your machine to your own system.

Now you can create a volume group on /dev/sda2

# vgcreate vm /dev/sda2

Then locical volumes can be created in this volume group with

# lvcreate -L 10G -n lv0 vm

The new logical volume will have a size of 10 GB and be named "lv0". It's device will be named /dev/mapper/vm-lv0.

The logical volume may then be formatted with a filesystem, for example ext4

# mkfs.ext4 /dev/mapper/vm-lv0

Note that creating the logical volumen may already be done by the tools used to set up the Xen domain and formatting will usually be done by the system setup inside the virtual machine.

Related Question