Jump to Navigation

264 - Basic LVM commands

1. Initializing disks or disk partitions

To use LVM, partitions and whole disks must first be converted into physical volumes (PVs) using the pvcreate command.
For example, to convert /dev/hda and /dev/hdb into PVs use the following commands :

# pvcreate /dev/hda

# pvcreate /dev/hdb

If a Linux partition is to be converted make sure that it is given partition type 0x8E using fdisk, then use pvcreate :

# pvcreate /dev/hda1

2. Creating a volume group

Once you have one or more physical volumes created, you can create a volume group from these PVs using
the vgcreate command. The following command:

# vgcreate  VolGroup01 /dev/hda /dev/hdb

creates a new VG called VolGroup01 with two disks, /dev/hda and /dev/hdb, and 4 MB PEs.
If both /dev/hda and /dev/hdb are 128 GB in size, then the VG VolGroup01 will have a total of 2**16 physical extents that can be
allocated to logical volumes.

Additional PVs can be added to this volume group using the vgextend command.
The following commands convert /dev/hdc into a PV and then adds that PV to VolGroup01 :

# pvcreate /dev/hdc

# vgextend VolGroup01 /dev/hdc

This same PV can be removed from VolGorup01 by the vgreduce command:

# vgreduce VolGroup01 /dev/hdc

Note that any logical volumes using physical extents from PV /dev/hdc will be removed as well.
This raises the issue of how we create an LV within a volume group in the first place.

3. Creating a logical volume

We use the lvcreate command to create a new logical volume using the free physical extents in the VG pool.
Continuing our example using VG VolGroup01 (with two PVs /dev/hda and /dev/hdb and a total capacity of 256 GB),
we could allocate nearly all the PEs in the volume group to a single linear LV called LogVol01 with the following LVM command:

# lvcreate -n LogVol01   --size 255G VolGroup01

Instead of specifying the LV size in GB we could also specify it in terms of logical extents.
First we use vgdisplay to determine the number of PEs in the VolGroup01 :

vgdisplay VolGroup01 | grep "Total PE"

which returns

Total PE   65536

Then the following lvcreate command will create a logical volume with 65536 logical extents and fill the volume group completely :

# lvcreate -n LogVol01  -l 65536 VolGroup01

To create a 1500MB linear LV named LogVol01 and its block device special file /dev/VolGroup01/LogVol01 use the following command:

# lvcreate -L1500 -n LogVol01 VolGroup01

The lvcreate command uses linear mappings by default.

Striped mappings can also be created with lvcreate. For example, to create a 255 GB large logical
volume with two stripes and stripe size of 4 KB the following command can be used:

# lvcreate -i2 -I4 --size 255G -n LogVol-Stp01 VolGroup01

It is possible to allocate a logical volume from a specific physical volume in the VG by specifying the PV or PVs at the end of the lvcreate command.
If you want the logical volume to be allocated from a specific physical volume in the volume group, specify the PV or PVs at the end of the lvcreate
command line. For example, this command:

# lvcreate -i2 -I4 -L128G -n LogVol-Stp01 VolGroup01 /dev/hda /dev/hdb

creates a striped LV named logical_volume_one that is striped across two PVs (/dev/hda and /dev/hdb)
with stripe size 4 KB and 128 GB in size.

An LV can be removed from a VG through the lvremove command, but first the LV must be unmounted:

# umount /dev/VolGroup01/LogVol01

# lvremove /dev/VolGroup01/LogVol01

Note that LVM volume groups and underlying logical volumes are included in the device special file directory tree in the /dev directory
with the following layout:

/dev/<volume_group_name>/<logical_volume_name>

so that if we had two volume groups myvg1 and myvg2 and each with three logical volumes named lv01, lv02, lv03,
six device special files would be created :

/dev/myvg1/lv01
/dev/myvg1/lv02
/dev/myvg1/lv03
/dev/myvg2/lv01
/dev/myvg2/lv02
/dev/myvg2/lv03

4. Extending a logical volume

An LV can be extended by using the lvextend command. You can specify either an absolute size for the
extended LV or how much additional storage you want to add to the LVM. For example:

# lvextend -L120G /dev/myvg/homevol

will extend LV /dev/myvg/homevol to 120 GB, while

# lvextend -L+10G /dev/myvg/homevol

will extend LV /dev/myvg/homevol by an additional 10 GB. Once a logical volume has been extended, the underlying file system can be expanded
to exploit the additional storage now available on the LV. With Red Hat Enterprise Linux 4, it is possible to expand both the ext3fs and GFS file
systems online, without bringing the system down. (The ext3 file system can be shrunk or expanded offline using the
ext2resize command.) To resize ext3fs, the following command :

# ext2online /dev/myvg/homevol

will extend the ext3 file system to completely fill the LV, /dev/myvg/homevol, on which it resides.

The file system specified by device (partition, loop device, or logical volume) or mount point must currently be mounted, and it will be enlarged
to fill the device, by default. If an optional size parameter is specified, then this size will be used instead.

 

 

 

 

Linux:


Main menu 2

Story | by Dr. Radut