Creating and Resizing LVM

From Phospher
Jump to: navigation, search

How to Create Logical Volume Groups

Assuming you've already installed or attached the new disk and the OS is aware.

Be sure you understand what you're doing when modifying partition tables. You "could" destroy your data.

I'm using EMC LUN designators for this walk through; everything here will work with standard disks (hda, sda, etc..)


First: choose a name for your Volume Group. RedHat auto names their groups "VolGroup00, 01 02 03 etc.."
RedHat distributions (Fedora, CentOS, RedHat) keep their volume groups under /dev/VolGroup00, 01 02 03 etc..


Second: determine which disks you want in your volume group. You can list available drives by issuing the command:

   fdisk -l


Third: create the volume group. I want /dev/emcpowerb and /dev/emcpowerc in my volume group.

   vgcreate VolGroup01 /dev/emcpowerb /dev/emcpowerc

Fourth: confirm the group exists and is working properly. You should see all volume groups on the system with the command below.

[root@cerebellum ~]# vgdisplay
 --- Volume group ---
 VG Name               VolGroup01
 System ID
 Format                lvm2
 Metadata Areas        3
 Metadata Sequence No  2
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                0
 Open LV               0
 Max PV                0
 Cur PV                3
 Act PV                3
 VG Size               149.99 GB
 PE Size               4.00 MB
 Total PE              38397
 Alloc PE / Size       0 / 0
 Free  PE / Size       38397 / 149.99 GB
 VG UUID               HC2P3o-Otoz-oUgU-Af8Y-eDc5-OKlJ-2MiJbz

 --- 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               72.88 GB
 PE Size               32.00 MB
 Total PE              2332
 Alloc PE / Size       2332 / 72.88 GB
 Free  PE / Size       0 / 0
 VG UUID               cefsWo-B7tC-r3p3-F5XJ-NZ1p-G0gl-RzqTVs

[root@cerebellum ~]#


How to Create Logical Volumes

First: determine the size you wish your new logical volume to be. You can see the available space of your volume group by running:

 [root@cerebellum ~]# vgdisplay
 --- Volume group ---
 VG Name               VolGroup01
 System ID
 Format                lvm2
 Metadata Areas        3
 Metadata Sequence No  2
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                0
 Open LV               0
 Max PV                0
 Cur PV                3
 Act PV                3
 VG Size               149.99 GB
 PE Size               4.00 MB
 Total PE              38397
 Alloc PE / Size       0 / 0
 Free  PE / Size       38397 / 149.99 GB
 VG UUID               HC2P3o-Otoz-oUgU-Af8Y-eDc5-OKlJ-2MiJbz

[root@cerebellum ~]#


Second: I want to create a 100GB logical volume in VolGroup01 and name it LVData01:


[root@cerebellum mnt]# lvcreate --size 100G VolGroup01 --name LVData01
Logical volume "LVData01" created


Third: Confirm:

[root@cerebellum mnt]# fdisk -l /dev/VolGroup01/LVData01

Disk /dev/VolGroup01/LVData01: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/VolGroup01/LVData01 doesn't contain a valid partition table
[root@cerebellum mnt]#


Fourth: lay down a filesystem. I chose EXT3:

[root@cerebellum mnt]# mkfs.ext3 /dev/VolGroup01/LVData01
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
13107200 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
800 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
       32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
       4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@cerebellum mnt]#


Fifth: edit your fstab to have Linux automatically mount your new volume. I'm mounting my new volume to /data. Make sure you create the destination directory first.

  vi /etc/fstab

add the line:

 /dev/VolGroup01/LVData01 /data    ext3   defaults   1 1 


You can test your fstab (you can also do this if you're not able to reboot the system) by remounting the filesystems:

  mount -a

Confirm everything with df:

[root@cerebellum mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       69G  5.0G   61G   8% /
/dev/sda1              99M   13M   81M  14% /boot
tmpfs                 1.7G     0  1.7G   0% /dev/shm
/dev/mapper/VolGroup01-LVData01
                       99G  188M   94G   1% /data
[root@cerebellum mnt]#


How to Resize Logical Volume Groups

First: be sure you've added LVM storage to the system and it is not being used. I've added "/dev/emcpowere".

[root@cerebellum ~]# pvscan
  PV /dev/emcpowerb   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerc   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerd   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/sda2        VG VolGroup00   lvm2 [72.88 GB / 0    free]
  PV /dev/emcpowere                   lvm2 [50.00 GB]
  Total: 5 [272.86 GB] / in use: 4 [222.86 GB] / in no VG: 1 [50.00 GB]
[root@cerebellum ~]#


Second: Extend the volume group by adding your new physical volume:

 vgextend VolGroup01 /dev/emcpowere


Third: That's it! Confirm the size of the volume with vgdisplay:

   [root@cerebellum ~]# vgdisplay
 --- Volume group ---
 VG Name               VolGroup01
 System ID
 Format                lvm2
 Metadata Areas        3
 Metadata Sequence No  2
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                0
 Open LV               0
 Max PV                0
 Cur PV                3
 Act PV                3
 VG Size               199.98 GB
 PE Size               4.00 MB
 Total PE              51196
 Alloc PE / Size       0 / 0
 Free  PE / Size       51196 / 199.98 GB
 VG UUID               HC2P3o-Otoz-oUgU-Af8Y-eDc5-OKlJ-2MiJbz


How to Resize Logical Volumes

First: be sure you've added LVM storage to the system.

[root@cerebellum mnt]# vgdisplay
 --- Volume group ---
 VG Name               VolGroup01
 System ID
 Format                lvm2
 Metadata Areas        3
 Metadata Sequence No  12
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                1
 Open LV               1
 Max PV                0
 Cur PV                3
 Act PV                3
 VG Size               144.99 GB
 PE Size               4.00 MB
 Total PE              38097
 Alloc PE / Size       26580 / 100.00 GB
 Free  PE / Size       11517 / 39.99 GB
 VG UUID               HC2P3o-Otoz-oUgU-Af8Y-eDc5-OKlJ-2MiJbz

[root@cerebellum mnt]# 


Second: extend the volume! I've added 5GB.

[root@cerebellum mnt]# lvextend --size 105G /dev/VolGroup01/LVData01
 Extending logical volume LVData01 to 105.00 GB
 Logical volume LVData01 successfully resized
[root@cerebellum mnt]#


Third: confirm:

[root@cerebellum mnt]# lvdisplay
 --- Logical volume ---
 LV Name                /dev/VolGroup01/LVData01
 VG Name                VolGroup01
 LV UUID                jRRknT-7wWA-GCOD-E28P-4yYc-iQF5-cI0ofj
 LV Write Access        read/write
 LV Status              available
 # open                 1
 LV Size                105.00 GB
 Current LE             26880
 Segments               3
 Allocation             inherit
 Read ahead sectors     0
 Block device           253:2

[root@cerebellum mnt]#

Don't forget! if you're extending a volume group in order to extend a logical volume you must also resize the filesystem being used on the volume.


Resize your filesystem:

[root@cerebellum mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       69G  5.0G   61G   8% /
/dev/sda1              99M   13M   81M  14% /boot
tmpfs                 1.7G     0  1.7G   0% /dev/shm
/dev/mapper/VolGroup01-LVData01
                      99G  188M   94G   1% /data
[root@cerebellum mnt]#
[root@cerebellum mnt]# resize2fs /dev/VolGroup01/LVData01
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup01/LVData01 is mounted on /data; on-line resizing required
Performing an on-line resize of /dev/VolGroup01/LVData01 to 27525120 (4k) blocks.
The filesystem on /dev/VolGroup01/LVData01 is now 27525120 blocks long.
  
[root@cerebellum mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       69G  5.0G   61G   8% /
/dev/sda1              99M   13M   81M  14% /boot
tmpfs                 1.7G     0  1.7G   0% /dev/shm
/dev/mapper/VolGroup01-LVData01
                      104G  188M   98G   1% /data
[root@cerebellum mnt]#


Troubleshooting

Make sure you've created a Physical Volume for each disk. Run "pvscan":


[root@cerebellum ~]# pvscan
  PV /dev/emcpowerb   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerc   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerd   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/sda2        VG VolGroup00   lvm2 [72.88 GB / 0    free]
  Total: 4 [222.86 GB] / in use: 4 [222.86 GB] / in no VG: 0 [0   ]
[root@cerebellum ~]# pvcreate /dev/emcpowere
  Physical volume "/dev/emcpowere" successfully created
[root@cerebellum ~]# pvscan
  PV /dev/emcpowerb   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerc   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/emcpowerd   VG VolGroup01   lvm2 [50.00 GB / 50.00 GB free]
  PV /dev/sda2        VG VolGroup00   lvm2 [72.88 GB / 0    free]
  PV /dev/emcpowere                   lvm2 [50.00 GB]
  Total: 5 [272.86 GB] / in use: 4 [222.86 GB] / in no VG: 1 [50.00 GB]
[root@cerebellum ~]#


Feel free to contact me: brian at phospher dot com