LVM - How to Create a Partition in Linux

April 21, 2005
This is a short tutorial that explains how to create an LVM in Linux. Let's get started.

I have got 2 hard disks of 2GB size each -

# fdisk -l 2>/dev/null | grep '/dev/sd[a-b]'
Disk /dev/sda: 2147 MB, 2147483648 bytes
Disk /dev/sdb: 2147 MB, 2147483648 bytes

The following steps will show you how to create logical volumes on these hard disks.

Step 1: Prepare the disks


If you are using entire disks for creating logical volumes, then make sure they are raw disks. ie they should not contain any partitions.

If you are using individual partitions on a disk for LVM, then you should open fdisk (or another partitioning tool) and set each partition as type "Linux LVM" - 0x8e, so that they can be recognized by the LVM system.


Step 2: Initialize the disks to be used as a physical volume


To initialize the disks to be used as a physical volume, you use pvcreate.

# pvcreate /dev/sda
Writing physical volume data to disk "/dev/sda"
Physical volume "/dev/sda" successfully created

# pvcreate /dev/sdb
Writing physical volume data to disk "/dev/sdb"
Physical volume "/dev/sdb" successfully created

Now use lvmdiskscan to verify that the hard disks have been properly initialized.

# lvmdiskscan
/dev/sda  [ 2.00 GiB] LVM physical volume
...
/dev/sdb  [ 2.00 GiB] LVM physical volume
...
2 disks
2 LVM physical volume whole disks
0 LVM physical volumes

Step 3: Create the volume group


To create a volume group, you run the vgcreate as follows.

# vgcreate vg_sda /dev/sda
Volume group "vg_sda" successfully created

# vgcreate vg_sdb /dev/sdb
Volume group "vg_sdb" successfully created

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

Step 4: Create the logical volumes


Logical volumes can be classified into 3 types -

  1. Linear volumes
  2. Stripped volumes, and
  3. Mirrored volumes

Logical volumes are similar to partitions in hard disks - only better.


Use lvcreate command to create a logical volume.

Lets create a single linear logical volume within each of the volume groups that was created.

# lvcreate --extents 100%FREE --name lv_sda vg_sda
Logical volume "lv_sda" created

# lvcreate --extents 100%FREE --name lv_sdb vg_sdb
Logical volume "lv_sdb" created

In the above commands, --extents gives the number of logical extents to allocate for the new logical volume. Here we have specified the number as the percentage of the free space in the volume group.

lvcreate takes a lot of options. Do check its man page to know more.


Step 5: Verify your work


Lastly, you should scan all disks for logical volumes and volume groups. For this you use the tools - vgscan and lvscan respectively.

# vgscan
Reading all physical volumes. This may take a while...
Found volume group "vg_sdb" using metadata type lvm2
Found volume group "vg_sda" using metadata type lvm2

and scan the logical volumes on all the disks ...

# lvscan
ACTIVE  '/dev/vg_sdb/lv_sdb'  [2.00 GiB] inherit
ACTIVE  '/dev/vg_sda/lv_sda'  [2.00 GiB] inherit

The logical volumes are included in the /dev directory in the format /dev/vg/lv where vg is volume group name, and lv is logical volume name. In our case it will be -

/dev/vg_sda/lv_sda and /dev/vg_sdb/lv_sdb.

Run lvdisplay to see the details of the logical volumes that were created.

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg_sdb/lv_sdb
  VG Name                vg_sdb
  LV UUID                ytRveJ-ZCsD-gIXd-Uzfl-UOeO-8CYF-xdXb72
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             511
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
   
  --- Logical volume ---
  LV Name                /dev/vg_sda/lv_sda
  VG Name                vg_sda
  LV UUID                gTG0Sy-TiF9-3bmF-C7U5-FlqJ-TbZG-eYULtf
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             511
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

From here on, you can access the logical volumes the same way you access physical disks. All the commands such as for mounting disks, creating file systems, formatting and so on will work on the logical volumes as well.

What to do next


Before you start using the logical volumes to store your files, you have to do 2 things, namely -

  1. Format the logical volume, and
  2. Mount the logical volume in a suitable location.

First lets format the logical volumes

Formating the logical volume involves creating a file system on it. You use the mke2fs command to create a file system.

# mkfs.ext4 /dev/vg_sda/lv_sda

and for the second logical volume ...

# mkfs.ext4 /dev/vg_sdb/lv_sdb

Now lets mount the logical volumes in a suitable location.

Mounting the first logical volume at /mnt/volume-a

# mkdir /mnt/volume-a
# mount -t ext4 /dev/vg_sda/lv_sda /mnt/volume-a

and mounting the second logical volume at /mnt/volume-b

# mkdir /mnt/volume-b
# mount -t ext4 /dev/vg_sdb/lv_sdb /mnt/volume-b

If you want to automatically mount the logical volume(s) each time you boot into Linux, you will have to enter the appropriate line in the /etc/fstab file as well.


List of LVM Commands


To recap, following are the LVM commands we have used to create logical volumes in Linux.

pvcreate - Initializes physical volume for later use by logical volume manager (LVM).

lvmdiskscan - Scan for all devices visible to LVM.

vgcreate - Create a volume group.

lvcreate - Create a logical volume in an existing volume group.

vgscan - Scan all disks for volume groups and rebuild caches.

lvscan - Scan all disks for logical volumes.

lvdisplay - Display attributes (information) of all logical volumes.

There are many more LVM commands that accomplish other tasks. However, these set of LVM commands should get you started on creating an LVM in Linux.

0 comments: