The following sections outline some common administrative tasks for an LVM system. This is no substitute for reading the man pages.
Before you can use a disk or disk partition as a physical volume you will have to initialize it:
For entire disks:
# pvcreate /dev/hdb
This creates a volume group descripter at the start of disk.For partitions:
# pvcreate /dev/hdb1
This creates a volume group descriptor at the start of the /dev/hdb1
partition.
Use the 'vgcreate' program:
# vgcreate my_volume_group /dev/hda1 /dev/hdb1
NOTE: If you are using devfs it is essential to use the full devfs name of the device rather than the symlinked name in /dev. so: the above would be:
# vgcreate my_volume_group /dev/ide/host0/bus0/target0/lun0/part1 \
/dev/ide/host0/bus0/target1/lun0/part1
You can also specify the extent size with this command if the default of 4MB is not suitable for you with the '-s' switch. In addition you can put some limits on the number of physical or logical volumes the volume can have.
After rebooting the system or running vgchange -an, you will
not be able to access your VGs and LVs. To reactivate the volume
group, run:
# vgchange -a y my_volume_group
Make sure that no logical volumes are present in the volume group, see later section for how to do this.
Deactivate the volume group:
# vgchange -a n my_volume_group
Now you actually remove the volume group:
# vgremove my_volume_group
Use 'vgextend' to add an initialized physical volume to an existing volume group.
# vgextend my_volume_group /dev/hdc1
^^^^^^^^^ new physical volume
Make sure that the physical volume isn't used by any logical volumes by using then 'pvdisplay' command:
# pvdisplay /dev/hda1--- Physical volume ---
PV Name /dev/hda1
VG Name myvg
PV Size 1.95 GB / NOT usable 4 MB [LVM: 122 KB]
PV# 1
PV Status available
Allocatable yes (but full)
Cur LV 1
PE Size (KByte) 4096
Total PE 499
Free PE 0
Allocated PE 499
PV UUID Sd44tK-9IRw-SrMC-MOkn-76iP-iftz-OVSen7
If the physical volume is still used you will have to migrate the data to another physical volume.
Then use 'vgreduce' to remove the physical volume:
# vgreduce my_volume_group /dev/hda1
Decide which physical volumes you want the logical volume to be allocated on, use 'vgdisplay' and 'pvdisplay' to help you decide.
# lvcreate -L1500 -ntestlv testvg
Will create a 1500MB linear LV named 'testlv' and its block device
special '/dev/testvg/testlv'.
# lvcreate -i2 -I4 -l100 -nanothertestlv testvg
Will create a 100 LE large logical volume with 2 stripes and stripesize 4 KB.
If you want to create an LV that uses the entire VG, use vgdisplay to find the "Total PE" size, then use that when running lvcreate.
# vgdisplay testvg | grep "Total PE"
Total PE 10230
# lvcreate -l 10230 testvg -n mylv
This will create an LV called mylv filling the testvg VG.
A logical volume must be closed before it can be removed:
# umount /dev/myvg/homevol
# lvremove /dev/myvg/homevol
lvremove -- do you really want to remove "/dev/myvg/homevol"? [y/n]: y
lvremove -- doing automatic backup of volume group "myvg"
lvremove -- logical volume "/dev/myvg/homevol" successfully removed
To extend a logical volume you simply tell the lvextend command how much you want to increase the size. You can specify how much to grow the volume, or how large you want it to grow to:
# lvextend -L12G /dev/myvg/homevol
lvextend -- extending logical volume "/dev/myvg/homevol" to 12 GB
lvextend -- doing automatic backup of volume group "myvg"
lvextend -- logical volume "/dev/myvg/homevol" successfully extended
will extend /dev/myvg/homevol to 12 Gigabytes.
# lvextend -L+1G /dev/myvg/homevol
lvextend -- extending logical volume "/dev/myvg/homevol" to 13 GB
lvextend -- doing automatic backup of volume group "myvg"
lvextend -- logical volume "/dev/myvg/homevol" successfully extended
will add another gigabyte to /dev/myvg/homevol.
After you have extended the logical volume it is necessary to increase the file system size to match. how you do this depends on the file system you are using.
By default, most file system resizing tools will increase the size of the file system to be the size of the underlying logical volume so you don't need to worry about specifying the same size for each of the two commands.
# umount /dev/myvg/homevol/dev/myvg/homevol
# resize2fs /dev/myvg/homevol
# mount /dev/myvg/homevol /home
If you don't have e2fsprogs 1.19 or later, you can download the ext2resize
command from
ext2resize.sourceforge.net
and use that:
# umount /dev/myvg/homevol/dev/myvg/homevol
# resize2fs /dev/myvg/homevol
# mount /dev/myvg/homevol /home
For ext2 there is an easier way. LVM ships with a utility called e2fsadm
which does the lvextend and resize2fs for you (it can also do file system
shrinking, see the next section) so the single command
# e2fsadm -L+1G /dev/myvg/homevol
is equivalent to the two commands:
# lvextend -L+1G /dev/myvg/homevol
# resize2fs /dev/myvg/homevol
Note that you still need to unmount the file system first though.
# resize_reiserfs -f /dev/myvg/homevol
Offline:
# umount /dev/myvg/homevol
# resize_reiserfs /dev/myvg/homevol
# mount -treiserfs /dev/myvg/homevol /home
# xfs_growfs /home
Logical volumes can be reduced in size as well as increased. However, it is very important to remember to reduce the size of the file system or whatever is residing in the volume before shrinking the volume itself, otherwise you risk losing data.
# umount /home
# e2fsadm -L-1G /dev/myvg/homevol
# mount /home
If you prefer to do this manually you must know the new size of the volume
in blocks and use the following commands:
# umount /home
# resize2fs /dev/myvg/homevol 524288
# lvreduce -L-1G /dev/myvg/homevol
# mount /home
# umount /home
# resize_reiserfs -s-1G /dev/myvg/homevol
# lvreduce -L-1G /dev/myvg/homevol
# mount -treiserfs /dev/myvg/homevol /home
If you want to take a disk out of service it must first have all of its active physical extents moved to another disk. This disk must be an LVM physical volume in the same volume group as the disk to be removed and have enough free space to hold the extents to be copied from the old disk. For further detail see Removing an Old Disk.
The following command moves all the data from the IDE disk partition /dev/hdb1 onto a SCSI disk partition /dev/sdg1. Be aware that this command can take a considerable amount of time to complete.
Also, if the extents contain a striped logical volume then the process cannot be interrupted so it is strongly recommended that you take a backup of your data before starting pvmove.
# pvmove /dev/hdb1 /dev/sdg1