Sample commands for working with Linux LVM

Contents

1 Introduction
2 Initialize Volumes
3 Creating Volume Groups
4 Creating the Volume File System
5 Creating a Mirror Logical Volume
6 Extending Logical Volumes and Filesystems
7 Shrinking the Filesystem and Logical Volume
8 How to Create RAID10 Logical Volumes
9 Display Volume Group Information
10 Destroying a Logical Volume
11 Destroying a Volume Group
12 Related Keywords

Introduction

The purpose of this document is to give you a quick view of some of the commands available in the Linux Logical Volume Manager (LVM.) (For deeper knowledge it is recommended to read the “man” pages and accompanying documents.)

Initialize Volumes

Initialize physical volumes

[root@t10 ~]# lvm pvcreate /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde

Physical volume “/dev/sda” successfully created

Physical volume “/dev/sdb” successfully created

Physical volume “/dev/sdc” successfully created

Physical volume “/dev/sdd” successfully created

Physical volume “/dev/sde” successfully created

Right now, the hard disk drives do not seem to have any partitions

[root@t10 ~]# fdisk -l /dev/sda

Disk /dev/sda: 2147 MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

(Disk /dev/sda doesn’t contain a valid partition table.)

But when you use lvm command you can see the PV label

[root@t10 ~]# lvm pvdisplay /dev/sda

— NEW Physical volume —

PV Name /dev/sda

VG Name

PV Size 2.00 GB

Allocatable NO

PE Size (KByte) 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID Oge2Af-c1s1-cgoK-5Qwz-XzHG-Oexi-dykFBh

You can also get this data in a summarized format using pvs

[root@t10 ~]# pvs

PV VG Fmt Attr PSize PFree

/dev/sda lvm2 a– 2.00G 1.53G

Compared to non initialized disk

[root@t10 ~]# lvm pvdisplay /dev/sda

No physical volume label read from /dev/sda

Failed to read physical volume “/dev/sda”

Creating Volume Groups

Creating a volume group, named lv02

[root@t10 ~]# lvm vgcreate vg02 /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde

Volume group “vg02” successfully created

Creating a striped volume
Note: The volume is created on Logical Volume lv01 with size of 250 MB with stripe size 128 KB

[root@t10 ~]# lvm lvcreate vg02 –stripes 3 –stripesize 128k –name lv01 –size 250M

Rounding up size to full physical extent 252.00 MB

Logical volume “lv01” created

Creating the Volume File System

Creating and mounting a volume (on /a/lv1)

[root@t10 ~]# mkfs -t ext3 /dev/vg02/lv01

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

64512 inodes, 258048 blocks

12902 blocks (5.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=67371008

32 block groups

8192 blocks per group, 8192 fragments per group

2016 inodes per group

Superblock backups stored on blocks:

8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or

180 days, whichever comes first. Use tune2fs -c or -i to override.

[root@t10 ~]# mount /dev/vg02/lv01 /a/lv01

Creating a Mirror Logical Volume

Creating a mirrored logical volume lv02 with a size of 250 MB

[root@t10 ~]# lvm lvcreate vg02 –mirrors 2 –name lv02 –size 250M

Rounding up size to full physical extent 252.00 MB

Logical volume “lv02” created

Extending Logical Volumes and Filesystems

Extending logical volume lv01 to 500MB

[root@t10 ~]# lvm lvextend /dev/vg02/lv01 –size 500M

Using stripesize of last segment 128.00 KB

Rounding size (125 extents) down to stripe boundary size for segment (123 extents)

Extending logical volume lv01 to 492.00 MB

Logical volume lv01 successfully resized

Follow this up by extending the Filesystem on that Logical Volume lv01

[root@t10 ~]# resize2fs /dev/vg02/lv01

resize2fs 1.39 (29-May-2006)

Filesystem at /dev/vg02/lv01 is mounted on /a/lv01; on-line resizing required

Performing an on-line resize of /dev/vg02/lv01 to 503808 (1k) blocks.

The filesystem on /dev/vg02/lv01 is now 503808 blocks long.

Shrinking the Filesystem and Logical Volume

Shrink the filesystem first
Warning: Unmount ext2 and ext3 filesystems before trying to shrink them

[root@t10 ~]# e2fsck -f /dev/vg02/lv01

e2fsck 1.39 (29-May-2006)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/vg02/lv01: 11/124992 files (9.1% non-contiguous), 22199/503808 blocks

Shrinking the Logical Volume lv01 to 200MB

[root@t10 ~]# resize2fs /dev/vg02/lv01 200M

resize2fs 1.39 (29-May-2006)

Resizing the filesystem on /dev/vg02/lv01 to 204800 (1k) blocks.

The filesystem on /dev/vg02/lv01 is now 204800 blocks long.

[root@t10 ~]# lvm lvreduce /dev/vg02/lv01 –size 202M

Rounding up size to full physical extent 204.00 MB

WARNING: Reducing active logical volume to 204.00 MB

THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce lv01? [y/n]: y

Reducing logical volume lv01 to 204.00 MB

Logical volume lv01 successfully resized

Remount the filesystem

[root@t10 ~]# mount /dev/vg02/lv01 /a/lv01

Verify the new size

[root@t10 ~]# df -k /a/lv01

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg02-lv01

198437 5664 184581 3% /a/lv01

As you see now, the size of the logical volume is 204 MB, because the size of extends do not permit me to make the size exactly 200 MB for other reason than volume allocation, boundary size for segments

How to Create RAID10 Logical Volumes

This is an example and is not recommended for a production environment. For creation of RAID10, it is better to use the mdadm tool and work with physical disk a
Create Two Mirrored Logical Volumes

[root@t10 ~]# lvm lvcreate vg02 –mirrors 2 –name lv03 –size 512M

Rounding up size to full physical extent 512.00 MB

Logical volume “lv03” created

[root@t10 ~]# lvm lvcreate vg02 –mirrors 2 –name lv04 –size 512M

Rounding up size to full physical extent 512.00 MB

Logical volume “lv04” created

Initialize the volumes as Physical Volumes

[root@t10 ~]# lvm pvcreate /dev/vg02/lv03

Physical volume “/dev/vg02/lv03” successfully created

[root@t10 ~]# lvm pvcreate /dev/vg02/lv04

Physical volume “/dev/vg02/lv04” successfully created

Create new Volume Group vg03

[root@t10 ~]# lvm vgcreate vg03 /dev/vg02/lv03 /dev/vg02/lv04

Volume group “vg03” successfully created

Create Striped Logical Volume lv05

[root@t10 ~]# lvm lvcreate vg03 –stripes 2 –name lv05 –size 512M

Logical volume “lv05” created

Display Volume Group Information

Display information about the volume group

[root@t10 ~]# lvm vgdisplay vg02

— Volume group —

VG Name vg02

System ID

Format lvm2

Metadata Areas 5

Metadata Sequence No 9

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 16

Open LV 3

Max PV 0

Cur PV 5

Act PV 5

VG Size 9.98 GB

PE Size 4.00 MB

Total PE 2555

Alloc PE / Size 1083 / 4.23 GB

Free PE / Size 1472 / 5.75 GB

VG UUID LqyQWT-LLC6-v544-WEIB-yDgu-V41p-SaHawA

Display information about the volume group

[root@t10 ~]# lvm lvdisplay /dev/vg02/lv02

— Logical volume —

LV Name /dev/vg02/lv02

VG Name vg02

LV UUID RFzxGa-S7od-7M3d-pI4Y-nGjZ-d3Yb-KmWlDb

LV Write Access read/write

LV Status available

# open 0

LV Size 252.00 MB

Current LE 63

Segments 1

Allocation inherit

Read ahead sectors 0

Block device 253:7

Destroying a Logical Volume

Destroy logical volume. Warning: this will erase all the information, resident on this volume

[root@t10 ~]# lvm lvremove /dev/vg03/lv05

Do you really want to remove active logical volume “lv05”? [y/n]: y

Logical volume “lv05” successfully removed

Destroying a Volume Group

Destroy volume group. Warning: volume group should be empty i.e. no logical volumes defined

[root@t10 ~]# lvm vgremove vg03

Volume group “vg03” successfully removed