User Tools

Site Tools


linux:lvm_setup

LVM in CentOS / RHEL 5

Principle

With LVM you are able to combine different physical harddrives to VolumeGroups and create Logical Volumes inside this group. This gives us the benefit that
we can create, resize and move partitions (LV) inside this VolumeGroup independently from your physical disks.

 This shows the principle of LVM

This Page assumes that you want a /data partition made out of 2 physical harddrives

Preparation

Check if LVM is installed on your System.

modprobe dm_mod
lvm version

Let's assume that we added 2 physical harddrives to our server.

(~) root@ldap:$ fdisk -l

Disk /dev/hdb: 1073 MB, 1073741824 bytes
16 heads, 63 sectors/track, 2080 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/hdd: 1073 MB, 1073741824 bytes
16 heads, 63 sectors/track, 2080 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

Disk /dev/hdd doesn't contain a valid partition table

Preparing the Drives

Partitions need to be marked with a flag so that you can use them for LVM.
First we create a Partition on the harddrive.

fdisk /dev/hdb

Press n to create a new partition. We will use the default values because we want to use the whole drive anyway.
Press t to change the filesystem, use Hex Code 8e to mark it as “Linux LVM” and don't forget to write your changes.

Initial scan (vgscan)

We need to scan our system for VolumeGroups. We dont have any, but thanks to the 8e flag LVM will recognise our new harddrives.

vgscan

1. Creating a Physical Volume (pvcreate)

pvcreate /dev/hdb1
pvcreate /dev/hdd1

2. Creating a Volume Group (vgcreate)

vgcreate myvolumegroup1 /dev/hdb1

We will add the second partition later, thanks to LVM thats no problem at all ;)
myvolumegroup1 is our new created Volume Group. It acts as a pool for our Logical Volumes
Just keep the picture of the Principle in mind!

3. Creating a Logical Volume (lvcreate)

lvcreate -L 1G -n myvolume1 myvolumegroup1
Switch Description
-L Gives the size to allocate for the new logical volume.
-n The name for the new logical volume.

For more info man lvcreate

We can now use our myvolume1 as a normal partition on our system!

mkfs.ext3 /dev/myvolumegroup1/myvolume1
mkdir /data
mount -t ext3 /dev/myvolumegroup1/myvolume1 /data

Finally we have successfully mounted our new Logical Volume.

Resizing Partitions

Extend Partitions (lvextend)

Before:

(/) root@ldap:$ df -h -T /data
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/myvolumegroup1-myvolume1
              ext3    788M   17M  731M   3% /data

With lvextend you can extend your Logical Volume as long as you have enough space in your Volume Group. You might want to add space first.

(/) root@ldap:$ lvextend -L 1500M /dev/myvolumegroup1/myvolume1
  Extending logical volume myvolume1 to 1.46 GB
  Logical volume myvolume1 successfully resized

Hint:
Your Logical Volume is now resized but the actual file system is still at the old size.

  1. Unmount your Volume (umount)
  2. Do a file system check (fsck.ext3)
  3. Resize your “Partition” (resize2fs)
  4. Remount your “Partition”
umount /data
fsck.ext3 -f /dev/myvolumegroup1/myvolume1
resize2fs /dev/myvolumegroup1/myvolume1
mount -t ext3 /dev/myvolumegroup1/myvolume1 /data

After:

(/) root@ldap:$ df -h -T /data
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/myvolumegroup1-myvolume1
              ext3    1.5G   18M  1.4G   2% /data

Shrink Partitions (lvreduce)

First we need to shrink the actual filesystem before we can shrink the Logical Volume.

umount /data
fsck.ext3 -f /dev/myvolumegroup1/myvolume1
resize2fs /dev/myvolumegroup1/myvolume1 1100M

Now we can shrink the Logical Volume:

lvreduce --size 1100M /dev/myvolumegroup1/myvolume1

After that we can remount our partition:

mount -t ext3 /dev/myvolumegroup1/myvolume1 /data

Adding Harddisks / Partitions

First we prepare the new harddrive (in this case its /dev/hdd)

fdisk /dev/hdd
>n
>t
>8e
>w
pvcreate /dev/hdd1

We add the new Physical Volume to our VolumeGroup

(/) root@ldap:$ vgextend myvolumegroup1 /dev/hdd1
Volume group "myvolumegroup1" successfully extended
(/) root@ldap:$ vgdisplay
  --- Volume group ---
  VG Name               myvolumegroup1
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1.99 GB
  PE Size               4.00 MB
  Total PE              510
  Alloc PE / Size       200 / 800.00 MB
  Free  PE / Size       310 / 1.21 GB
  VG UUID               tHfGhe-Gxqm-kxMx-Iuc6-8TIF-mvEz-IYTAOI  

LVM Commands

Command Example Description
vgscan vgscan Scans the computer for Volume Groups.
pvcreate pvcreate /dev/sdc1 Creates a Physical Volume
vgcreate vgcreate myvg1 /dev/sdc1 Creates a Volume Group
lvcreate lvcreate -L 2G -n myvol1 myvg1 Creates a Logical Volume out of a Volume Group
lvextend lvextend -L 3G /dev/myvg1/myvol1 Extends a Logical Volume
lvreduce lvreduce –size 2560M /dev/myvg1/myvol1 Reduces the size of a Logical Volume
vgextend vgextend myvg1 /dev/sdc2 Extends a Volume Group
vgdisplay vgdisplay myvg1 Displays information on a particular Volume Group
lvscan lvscan scan (all disks) for logical volumes

Permissions / fstab entry

To create a global /data mount which should be acessible to all the users:

mkdir /data
mount -t ext3 /dev/myvolumegroup1/myvolume1 /data
chown -R root.users /data
chmod -R 770 /data
# /etc/fstab
/dev/myvolumegroup1/myvolume1 /data ext3 defaults 0 1

UUIDs

A word on UUIDs. It is usefull to use the UUID of the according partition instead of the /dev Parameter.
Because UUIDs dont change if you add or remove harddisks.
To find out what UUID your partition has use /lib/udev/vol_id:

(/data) root@ldap:$ /lib/udev/vol_id /dev/myvolumegroup1/myvolume1
ID_FS_USAGE=filesystem
ID_FS_TYPE=ext3
ID_FS_VERSION=1.0
ID_FS_UUID=75b1fb0e-edb3-45e8-962a-c521d2edc9a5
ID_FS_LABEL=
ID_FS_LABEL_SAFE=

or on CentOS 6

(~) root@dc:$ ls -l /dev/disk/by-uuid/
total 0
5981 0 drwxr-xr-x 2 root root 140 Aug 30 00:10 .
5917 0 drwxr-xr-x 5 root root 100 Aug 30 00:10 ..
5982 0 lrwxrwxrwx 1 root root  10 Aug 30 00:10 19ace9c4-0ba7-4b65-ad59-baba01f4d                                                                             8c2 -> ../../sda3
6004 0 lrwxrwxrwx 1 root root  10 Aug 30 00:10 5d1ecbae-5169-4566-9bd8-b9499f481                                                                             79f -> ../../sda2
6023 0 lrwxrwxrwx 1 root root  10 Aug 30 00:10 9fc7d91f-2f30-45f6-b0d3-afdb0d87b                                                                             cc3 -> ../../sda1
8016 0 lrwxrwxrwx 1 root root  10 Aug 30 00:10 a319b1d8-7ceb-4095-95ed-2c7091c07                                                                             c09 -> ../../sdd1
8289 0 lrwxrwxrwx 1 root root  10 Aug 30 00:10 bb9c9fd2-1f5a-4e29-94b7-76514a6f9                                                                             187 -> ../../dm-0

LVM Snapshots

LVM provides the possibility to create snapshots of your Logical Volume.
To do so you first have to create a snapshot volume.

lvcreate -L 1.46G -s -n lv1_snapshot /dev/vg1/lv1

After that you can mount the device and you have a perfekt copy of your original Logical Volume.

linux/lvm_setup.txt · Last modified: 2011/09/02 22:22 by skull@darktemple.ch