Next Previous Contents

5. Commands

5.1 Initializing disk or partition (pvcreate)

Initializing the whole disk

To initialize the whole disk

pvcreate /dev/DISK

This creates a volume group descriptor at the start of the disk.

Note 1: Using the whole disk no any partition table is created. This means you could have problem by trying to backup this disk (e.g.: partimage).

Note 2: If you can't initialize the disk because there is a partition table on it execute before the pvcreate command:

dd if=/dev/zero of=/dev/DISK bs=1k count=1
blockdev --rereadpt /dev/DISK

The first one will destroy the partition table. The second one will update the /proc/partitions files, so that no any reboot are necessary.

Initializing a single partition

If you have already a partition, before you can initialize it, the partition type must be changed. You can do it with fdisk, parted, cfdisk, etc. Cfdisk is really simple and is included in the most distributions.

The partition type must be set to

8E

This creates a LVM partition type.

You can now initialize the single partition executing

pvcreate /dev/PARTITION

This creates a volume group descriptor at the start of the partition.

5.2 VG: creating a Volume Group (vgcreate)

To create a VG

vgcreate GROUP /dev/DISK /dev/PARTITION

This creates a volume group and adds the /dev/DISK and /dev/PARTITION to this group.

You can specify only one or more HD and/or only one or more partitions, but at least one must be specified here.

5.3 VG: scanning for Volume Groups (vgscan)

To scan all the HD for searching all the existing VG

vgscan

This lists all the found VG and updates /etc/lvmtab and /etc/lvmtab.d.

5.4 VG: activating a Volume Group (vgchange)

To activate a VG

vgchange -a y GROUP

This activates the VG GROUP and make it usable. Without this step is not possible to access the LV or make any changes to GROUP.

You can activate all the VG by calling

vgchange -a y

Note: This is normally automatically done at the boot time.

5.5 VG: deactivating a Volume Group (vgchange)

To deactivate a VG

vgchange -a n GROUP

This deactivates the VG GROUP and makes it temporary unusable.

5.6 VG: Removing a Volume Group (vgremove)

To remove a VG it's necessary to deactivate it before. Please, see related chapter.

Then, for removing it execute

vgchange -a n GROUP vgremove GROUP

This removes permanently the VG.

Note: You must remove all its LV before that you can remove a VG.

5.7 PV: adding a Physical Volume to a Volume Group (vgextend)

To add a PV to a VG

vgextend GROUP /dev/DISK /dev/PARTITION

This adds all the specified DISK and PARTITION to the group GROUP.

You can specify only one or more HD and/or one or more partitions, but at least one must be specified here.

5.8 PV: removing a Physical Volume from a Volume Group (vgreduce)

To remove a PV from a VG you must first move it's content to an other PV. To verify if the PV is allocated execute

vgdisplay /dev/DISK /dev/PARTITION

This shows how many PE are allocated.

You can specify only one or more HD and/or one or more partitions, but at least one must be specified here.

You can now remove the PV

vgreduce GROUP /dev/DISK /dev/PARTITION

This removes the specified DISK and/or PARTITION from GROUP.

5.9 LV: creating a Logical Volume in a Volume Group (lvcreate)

To create a LV

lvcreate -L BYTES -n NAME GROUP

This creates a LV of size BYTES, calls it NAME and adds it to GROUP.

You can specify the amount of BYTES in megabyte adding a "M" or in gigabyte adding a "G" after the value.

To create a LV that uses the whole free space of the VG

vgdisplay GROUP | grep Free

This displays the amount of free PE. Create now a LV specifying the numbers of PE

lvcreate -l PE -n NAME GROUP

Don't forget to format the new LV (ext2, ext3 or reiser)

mke2fs /dev/GROUP/NAME
mke2fs -j /dev/GROUP/NAME
mkfs.reiserfs /dev/GROUP/NAME

5.10 LV: removing a Logical Volume from a Volume Group (lvremove)

To remove a LV from a VG you must unmount it before

umount /dev/GROUP/NAME
lvremove /dev/GROUP/NAME

This removes the LV from the specified VG and permanently destroys its content.

5.11 LV: resizing a unmounted Logical Volume (e2fsadm)

To resize a LV there are more possibilities. Here I just explain the easiest and fastest way. The only condition for this possibility is that the LV must be unmounted. If you can't umount your LV, please refere you to the next chapter or to the official LVM-HOWTO.

umount /dev/GROUP/NAME
e2fsadm -L BYTES NAME

This resizes the LV to BYTES.

You can specify the amount of BYTES in megabyte adding a "M" or in gigabyte adding a "G" after the value.

You can also specify how much must be resized the LV adding a "+" or "-" before the value.

5.12 LV: resizing a mounted Logical Volume (lvextend/lvreduce)

If you can't unmount your LV and your LV has a reiser filesystem, you can easily resize it anyway.

Extending

First step: change the physically size of the LV

lvextend -L BYTES /dev/GROUP/NAME

This resizes the LV to BYTES.

You can specify the amount of BYTES in megabyte adding a "M" or in gigabyte adding a "G" after the value.

You can also specify how much must be resized the LV adding a "+" or "-" before the value.

Second step: change the size of the filesystem

resize_reiserfs /dev/GROUP/NAME

This resizes the filesystem to the maximum size of the LV.

Reducing

First step: change the size of the filesystem

resize_reiserfs -s BYTES /dev/GROUP/NAME

This resizes the filesystem to BYTES.

You can specify the amount of BYTES in megabyte adding a "M" or in gigabyte adding a "G" after the value.

You can also specify how much must be resized the LV adding a "+" or "-" before the value.

Second step: change the physically size of the LV

Very very important note: First reduce the filesystem and then the Logical Volume. Doing otherwise, there is the risk of losing the datas!

lvreduce -L BYTES /dev/GROUP/NAME (NOT before resize_reiserfs)

This resizes the LV to BYTES.

5.13 PE: moving Physical Extents content from a Physical Volume to an other (pvmove)

To move PE content

pvmove /dev/DISK pvmove /dev/PARTITION

This moves all the PE content present in the PV DISK or PV PARTITION to others PV present in the same group.

To move PE contents to a specific PV

pvmove /dev/DISK_SOURCE /dev/DISK_DESTINATION
pvmove /dev/PARTITION_SOURCE /dev/PARTITION_DESTINATION

This moves all the PE content present in DISK_SOURCE or PARTITION_SOURCE to DISK_DESTINATION or PARTITION_DESTINATION.


Next Previous Contents