Friday, July 13, 2012

Sample commands for working with Linux LVM

October 15, 2009 by  
Filed under General

Learn how to use LVM to partition. This article will show you how to partition using LVM. It is a very quick quite. You have to read the man pages for more information.

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,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

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”

Let’s create volume group, named lv02

[root@t10 ~]# lvm vgcreate vg02 /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde
Volume group “vg02″ successfully created

Next create one striped on 3 PV 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

And create on this volume filesystem and mount it 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

Next, let’s create a mirrored logical volume lv02 with a size of 250 MB
blockquote>[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

Next extend logical volume lv01 to 500MB and then extend the filesystem.

[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

[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.

Next shrink the filesystem and shrink logical volume lv01 to 200MB. When you try to shrink the file system, for ext2 and ext3 you will need to unmount it first…

[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

[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

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

[root@t10 ~]# df -k /a/lv01
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg02-lv01
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

To create RAID10 I use a slightly more complicated method, but it works…

create 2 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 volumes as PV

[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

The above is just 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

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

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

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

GD Star Rating
loading...
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Related posts:

  1. How do I find out Linux Resource utilization to detect system bottlenecks?

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!