This HowTo explains how to setup LVM (Logical Volume Management) on an existing system. The typical way of setting up LVM on Ubuntu is to use the alternative installation CD to wipe your whole hard drive and setup LVM. Instead of starting from scratch with a clean system, we're going to add LVM to your already running Ubuntu.
This has been tested on Ubuntu 7.04 (Feisty Fawn) through 8.04 (Hardy Heron), but should work on previous releases.
An alternative option is to use blocks to-lvm, which does an automated in-place conversion.
Backup your data!!
- Map out your goals for LVM. Do you just want some expandable space for media files? Do you want to move your /home into LVM? Do you want to move everything under LVM? If you want to put everything under LVM, you should create a separate /boot partition (100MB should be fine) - otherwise you may not be able to boot your system.
- You're going to need some free space to setup your volume groups. You can use:
- Free space on your primary hard drive.
- A partition that you are not using any more. (such as the Windows partion you haven't booted in 6 months, that is what Qemu/VMWare are for isn't it??)
- A new blank hard drive you've added to your system.
- Format your free space/partition/new drive using your favorite disk management tool to "Linux LVM". Also use some of this space to create your separate /boot partition (100MB, ext3, bootable flag) if you are planning on putting everything under LVM.
- Now that you have an LVM partition you can create your volume groups on. Note that you don't need a single large partition formated to Linux LVM, you can format as many smaller LVM partitions as you like, and combine them into a single filesystem using the power of LVM later - this is the preferred method as it allows more flexibility.
- Install "lvm2" from the Ubuntu repositories. ("sudo aptitude install lvm2")
Optionally install "system-config-lvm". Here: http://ubuntuforums.org/showthread.php?t=216117 is a forum thread on how to install it from Fedora RPM, (its not available in Ubuntu repositories.)
Load the LVM module - modprobe dm-mod. You can skip this step in Jaunty (9.04) because the dm-mod module is compiled into the kernel.
- If that works without any errors, you can go ahead and add dm-mod to the end of your /etc/modules file (sudo nano /etc/modules). This will autoload LVM when Ubuntu starts - very important if you plan on moving your system directories under LVM.
Code Listing 2.2: Activating LVM
(Avoid scanning all devices but our disks)
#sudo nano -w /etc/lvm/lvm.conf
(Look for the following line)
filter = [ "a/.*/" ]
(Replace it with the following one to scan /dev/hda and /dev/hdb and reject anything else)
filter = [ "a|/dev/hd[ab]|", "r/.*/" ]
(Save the file and quit nano) NOTE: The default setup of Ubuntu 7.04 LVM2 already has this setup correctly.
#sudo vgscan Reading all physical volumes. This may take a while... No volume groups found
(Make any previously set up volume groups available)
#sudo vgchange -a y
Prepare the partitions.
Preparing the partitions
# sudo pvcreate /dev/hda4 /dev/hdb1 No physical volume label read from /dev/hda4 Physical volume "/dev/hda4" successfully created No physical volume label read from /dev/hdb1 Physical volume "/dev/hdb1" successfully created
Setup a volume group. A volume group is the result of combining several physical units into a single logical device.
In our example, /dev/hda1, /dev/hda2 and /dev/hda3 are the /boot, swap and root partitions so we need to combine /dev/hda4 and /dev/hdb1. It can be done with a single command, but, as an example, we will create our volume group and extend it.
Code Listing 2.4: Creating and extending a volume group
(Create a volume group named vg)
# sudo vgcreate vg /dev/hda4 /etc/lvm/backup: fsync failed: Invalid argument (Ignore this warning) Volume group "vg" successfully created (Extending an existing volume group) # sudo vgextend vg /dev/hdb1 /etc/lvm/backup: fsync failed: Invalid argument (Ignore this warning, again and later as well) Volume group "vg" successfully extended
Create the logical volumes. Logical volumes are the equivalent of partitions you would create using fdisk in a non LVM2 environment. In our example, we create the following partitions:
Directory Size /usr 10 GB /home 5 GB /opt 5 GB /var 10 GB /tmp 2 GB
Since we are going to use LVM2, we should not worry too much about partition sizes because they can always be expanded as needed. However, it is easier to enlarge partitions than it is to shrink them, so it is best to start smaller and add space as needed.
Creating and extending logical volumes
# sudo lvcreate -L10G -nusr vg Logical volume "usr" created (Further similar messages not displayed) # sudo lvcreate -L5G -nhome vg # sudo lvcreate -L5G -nopt vg # sudo lvcreate -L10G -nvar vg # sudo lvcreate -L2G -ntmp vg
(As an example, let's extend a logical volume with 5 extra Gbytes)
# sudo lvextend -L+5G /dev/vg/home
Create filesystems on the logical volumes the same way you would on a regular partition. We use ext3 on the logical volumes but any filesystem of your choice will work:
Creating the filesystems:
# sudo mke2fs -j /dev/vg/usr # sudo mke2fs -j /dev/vg/home # sudo mke2fs -j /dev/vg/opt # sudo mke2fs -j /dev/vg/var # sudo mke2fs -j /dev/vg/tmp
Mount your partitions as described in the handbook and mount your LVM2 logical volumes as if they were partitions. Replace the usual /dev/hdxx with /dev/vg/logical_volumename.
Code Listing 2.7: Mounting your logical volumes
(Make sure you have mounted your root partition as described in the handbook first)
# sudo mkdir /mnt/ubuntu/usr # sudo mount /dev/vg/usr /mnt/ubuntu/usr # sudo mkdir /mnt/ubuntu/home # sudo mount /dev/vg/home /mnt/ubuntu/home # sudo mkdir /mnt/ubuntu/opt # sudo mount /dev/vg/opt /mnt/ubuntu/opt # sudo mkdir /mnt/ubuntu/var # sudo mount /dev/vg/var /mnt/ubuntu/var # sudo mkdir /mnt/ubuntu/tmp # sudo mount /dev/vg/tmp /mnt/ubuntu/tmp
NOTE: These mount points are for testing purposes. After you have copied /usr to /mnt/ubuntu/usr, (and also for home, opt, var, tmp, etc), You can change your /etc/fstab mount points to permantent paths.
For a nice GUI, you can run system-config-lvm from the shell. This will let you manage your LVM volume groups more easily. However, be careful, as changes are made immediately and you don't want to accidentally wipe your data.