LVM Management

From Initech Technical Wiki
Jump to: navigation, search

LVM Basics

General Description

LVM allows a server filesystem to be created, extended and even moved while running, without the need for a reboot and without needing to unmount the filesystem.

Order of things

Firstly there are physical volumes, these are entire physical disks basically (not sure whether you can use partitions as physical volumes). Physical volumes are part of volume groups.

Volume groups group together physical volumes and you make logical volumes out of them, volume groups basically become 'extent' pools which are large (~4MB normally) blocks.

Logical volumes are the actual file-systems which are formatted as ext2/3/whatever. When you create or extend one you pick which volume group to use and how many 'extents' to use. The number of extents times the size of the extent becomes your file-system size.

So you can see from the drawing below the layers and how they relate to one another.

ext3
swap
LV
LV
VG
PV


If we were to want to extend the LVM though it would look like this (green parts are the new ones and yellow parts are extended bits)

ext3
swap
LV
LV
VG
PV
PV

Rescanning scsi bus

When you hotplug a hard drive in a virtual machine, the drive will not show in fdisk -l output until you reboot your VM or rescan the SCSI bus.

The rescan will be triggered through sysfs pseudo filesystem.

# for BUS in `ls -1 /sys/class/scsi_host`; do echo "- - -" > /sys/class/scsi_host/$BUS/scan; done

Now, you should be able to see the drive when running:

# fdisk -l

Extending a logical volume

Commands

All the commands are along the same lines and use an abbreviated prefix and a full suffix. the prefixes are as follows:

  • pv [Physical Volume]
  • vg [Volume Group]
  • lv [Logical Volume]

And the suffixes (not exhaustive):

  • create
  • display
  • extend
  • reduce
  • remove

The commands that you'll need for an extend operation are (in this order): fdisk, pvdisplay, pvcreate, vgdisplay, vgextend, lvdisplay, lvextend, resize2fs. Lets get started.

Example

Here is an example of a extension that i needed to do on the backup-mirror server. I needed to add 60GB to the logical volume that was created when the system was installed. Unfortunately due to the licensing on the ESX server i couldn't hot-plug a new physical drive into the virtual machine so needed to reboot to do that part, but all the rest should be able to be done while it's running which is the general idea anyway.

Displaying what we have

Firstly we should should get an inventory of what we have in the system as far as physical disks are concerned.


[root@backup-mirror ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

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

So we can see from that output that there is two phyiscal disks, one ~30GB and one ~60GB. This is what i expected to see. We should check what physical volumes we have allocated now:

[root@backup-mirror ~]# pvdisplay
  /dev/hdc: open failed: No medium found
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               29.90 GB / not usable 24.06 MB
  Allocatable           yes (but full)
  PE Size (KByte)       32768
  Total PE              956
  Free PE               0
  Allocated PE          956
  PV UUID               4pBA3O-TeaQ-hP8J-Vpgy-MXYJ-zY8W-bp9sj2

So we can see that there is one physical volume and it isn't /dev/sdb so we should go ahead and create that now:

Creating the physical volume

[root@backup-mirror ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created

Now double check with pvdisplay again:


[root@backup-mirror ~]# pvdisplay
  /dev/hdc: open failed: No medium found
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               29.90 GB / not usable 24.06 MB
  Allocatable           yes (but full)
  PE Size (KByte)       32768
  Total PE              956
  Free PE               0
  Allocated PE          956
  PV UUID               4pBA3O-TeaQ-hP8J-Vpgy-MXYJ-zY8W-bp9sj2

  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               VolGroup00
  PV Size               60.00 GB / not usable 32.00 MB
  Allocatable           yes
  PE Size (KByte)       32768
  Total PE              1919
  Free PE               1919
  Allocated PE          0
  PV UUID               OZDal1-EKnd-kqQZ-YiVr-CQ6f-XRwr-Tm0iFe

You can see from the output the 'Total PE', 'Free PE' and 'Allocated PE' sections show how many physical extents are where.

Now we should see what logical volumes we have:

Checking volume groups


[root@backup-mirror ~]# vgdisplay
  /dev/hdc: open failed: No medium found
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               29.88 GB
  PE Size               32.00 MB
  Total PE              956
  Alloc PE / Size       956 / 29.88 GB
  Free  PE / Size       0 / 0
  VG UUID               MpnZFf-2Gq1-nUg6-wC42-zlDL-0GzS-k1QSqC

We can now extend our volume group with the following command:

Extending Volume Groups

[root@backup-mirror ~]# vgextend VolGroup00 /dev/sdb
  /dev/hdc: open failed: No medium found
  Volume group "VolGroup00" successfully extended

Don't worry about the error message about /dev/hdc. Its just complaining about the cdrom drive not being available. We should now check that the volume group has been extended properly:

[root@backup-mirror ~]# vgdisplay
  /dev/hdc: open failed: No medium found
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               89.84 GB
  PE Size               32.00 MB
  Total PE              2875
  Alloc PE / Size       956 / 29.88 GB
  Free  PE / Size       1919 / 59.97 GB
  VG UUID               MpnZFf-2Gq1-nUg6-wC42-zlDL-0GzS-k1QSqC

Looks good, now lets check the logical volumes:

Checking logical volumes


[root@backup-mirror ~]# lvdisplay
  /dev/hdc: open failed: No medium found
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                k1jp0v-yom4-N37J-SKWT-6SgH-tEjG-KFxr5t
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                28.88 GB
  Current LE             924
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                KdnN1i-JyRt-LXdl-8j03-msZ7-uDfI-yvVESA
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.00 GB
  Current LE             32
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

When we're extending the logical volume we need to make sure that we extend the right one, check the 'LV Size' of each to make sure and also check the 'mount' output or cat /etc/fstab.


[root@backup-mirror ~]# cat /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot              /boot                   ext3    defaults        1 2
tmpfs                    /dev/shm                tmpfs   defaults        0 0
devpts                   /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                    /sys                    sysfs   defaults        0 0
proc                     /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0

We can see from this output that the filesystem mounted on '/' (which is the one we want) is /dev/VolGroup00/LogVol00 which is the logical volume that we want to extend.

Extending logical volumes

You'll need to decide by how many 'PE's you want to extend the logical volume by, normally you'd just use all the free 'PE's in the volume group:

[root@backup-mirror ~]# lvextend -l +1919 /dev/VolGroup00/LogVol00
  /dev/hdc: open failed: No medium found
  Extending logical volume LogVol00 to 88.84 GB
  Logical volume LogVol00 successfully resized

The command above extends the logical volume /dev/VolGroup00/LogVol00 by 1919 physical extents.

Extending the filesystem

The last thing we need to do is extend the actual filesystem on the logical volume with the following command (this will take some time):


[root@backup-mirror ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing require                          d
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 23289856 (4k) blocks                          .

The filesystem on /dev/VolGroup00/LogVol00 is now 23289856 blocks long.