Introduction to lvcreate command
lvcreate is a command-line utility to create a new logical volume in a volume group. It includes the allocation of logical extents from the free physical extents of that volume group. If there is not enough space on the volume group, it can be extended with other physical volumes (vgextend
), or the existing logical volumes can be reduced or removed (lvreduce
, lvremove
). lvcreate can also create snapshots of existing logical volumes which store the content of the original logical volume from the time the snapshot was created. It is useful for backup purposes.
Are you new to LVM and still learning how it works?
We have written detailed articles covering different areas of managing logical volumes, which you can follow using the below links:
Manage Logical Volume in Linux - One STOP Solution
Understand LVM Architecture
Create LVM during installation RHEL/CentOS 7/8
How to use LVM Snapshot for Backup and Restore
Create Mirrored Logical Volume
Create Striped Logical Volume
How to install lvcreate
lvcreate
command is available in the lvm2 package in Linux. You can use the following command to install the lvm2 package according to your Linux distribution.
To install lvm2 on CentOS, Fedora, and RHEL
$ sudo yum install lvm2
To install lvm2 on Ubuntu and Debian
$ sudo apt install lvm2
Syntax to use lvcreate command in Linux
You will need the root privileges for the functionality of lvcreate
command.
The syntax for lvcreate
command is as follows:
$ sudo lvcreate option
We will try to cover the most used OPTIONs with lvcreate
command.
Create physical volumes and volume groups
Before creating a logical volume, you need to have physical volumes and volume groups on the system.
You can create a physical volume on the disk using pvcreate
command.
golinux@ubuntu-PC:~$ sudo pvcreate /dev/sda1
Physical volume "/dev/sda1" successfully created.
To list the physical volumes, you can use pvs
command.
golinux@ubuntu-PC:~$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/sda1 lvm2 --- 512.00m 512.00m
Now, let's create a volume group named vol_grp
using /dev/sda1
with the help of vgcreate
command.
golinux@ubuntu-PC:~$ sudo vgcreate vol_grp /dev/sda1
Volume group "vol_grp" successfully created
You can use vgs
command to view the list of volume groups.
golinux@ubuntu-PC:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
vol_grp 1 0 0 wz--n- 508.00m 508.00m
Different examples to use lvcreate command in Linux
1. Create a linear logical volume
A linear logical volume combines space from one or more physical volumes. If you have two 60GB drives, you can create a 120GB logical volume. The -L
or --size
option creates a new linear logical volume in the volume group.
For example, the following commands create a linear logical volume named golinux_vol
with a usable size of 100MiB on the volume group vol_grp
.
$ sudo lvcreate -L 100M -n golinux_vol vol_grp
OR
$ sudo lvcreate --size 100M --name golinux_vol vol_grp
Sample Output:
The -n
or --name
option allows you to specify a name for the new logical volume.
2. Create striped logical volume
By creating a striped logical volume, you can control how data is written to physical volumes. This can increase data I/O efficiency for large sequential reads and writes. With striping, I/O can be done in parallel.
You can use -i
or --stripes
option to create a new striped logical volume on the volume group.
The following commands create a striped logical volume named strip_lv
with 1 stripe and size of 50MiB in the volume group named vol_grp
.
$ sudo lvcreate -i 1 -n strip_lv -L 50M vol_grp
OR
$ sudo lvcreate --stripes 1 -n strip_lv -L 50M vol_grp
Sample Output:
The number of stripes (-i) must not exceed the number of physical volumes.
The -I
or --stripesize
option can be used to specify the number of kilobytes for the granularity of the stripes.
HINT:
3. Create a mirrored logical volume
​When you create a mirrored logical volume, LVM ensures that data written to an underlying physical volume is mirrored onto a separate physical volume. It provides protection in case of device failures.
The -m
or --mirrors
option creates a new mirrored logical volume with mirrors copies on the volume group. For example, specifying "-m 1" creates one mirror with two copies, i.e., a linear volume and one copy.
The commands below create a mirrored logical volume named mirror_lv
with a single mirror on the volume group vol_grp
. The size of the mirror_lv
is 100MiB.
$ sudo lvcreate -m 1 -n mirror_lv -L 100M vol_grp
OR
$ sudo lvcreate --mirrors 1 -n mirror_lv -L 100M vol_grp
Sample Output:
golinux@ubuntu-PC:~$ sudo lvcreate -m 1 -n mirror_lv -L 100M vol_grp Logical volume "mirror_lv created."
4. Create a snapshot logical volume
The -s
or --snapshot
options create a snapshot logical volume that keeps the contents of the original logical volume.
The following commands create a snapshot LV named snap
of the logical volume golinux_vol
on the volume group vol_grp
.
$ sudo lvcreate -s -n snap -L 100M vol_grp/golinux_lv
OR
$ sudo lvcreate --snapshot -n snap -L 100M vol_grp/golinux_lv
Sample Output:
5. Create a raid logical volume
The --type
option creates a logical volume of specified segment type (e.g. "raid5", "mirror", "snapshot", "thin", "thin-pool"). You can specify a raid level with --type
option to create a raid logical volume.
The command below creates a 100MiB RAID5 logical volume named raid_lv
with 3 stripes on the volume group vol_grp
.
$ sudo lvcreate --type raid5 -i 3 -L 100M -n raid_lv vol_grp
Sample Output:
golinux@ubuntu-PC:~$ sudo lvcreate --type raid5 -i 3 -L 100M -n raid_lv vol_grp Logical volume "raid_lv" created.
6. Create a thin pool logical volume
The -T
option creates a thin pool logical volume when the optional argument -L
or --size
is used.
These commands create a thin pool logical volume named thinpool_lv
with 1 stripe on the volume group vol_grp
.
$ sudo lvcreate -i 1 -L 50M -T vol_grp/thinpool_lv
OR
$ sudo lvcreate -i 1 -L 50M --thinpool vol_grp/thinpool_lv
Sample Output:
7. Create a thin logical volume
The -T
or --thin
option creates a thin logical volume when the optional argument -V
or --virtualsize
is used.
The following command creates a thin provisioned logical volume thin_lv
with thinpool_lv
on the volume group vol_grp
.
$ sudo lvcreate -V 50M -T vol_grp/thinpool_lv -n thin_lv
OR
$ sudo lvcreate -V 50M --thin vol_grp/thinpool_lv -n thin_lv
Sample Output:
8. Create a thin logical volume, first creating a new thin pool for it
The following command creates a thin logical volume mythin
, first creating a new thin pool LV mypool
having size 200MiB with 1 stripe on the volume group vol_grp
.
$ sudo lvcreate --type thin -n mythin --thinpool mypool -V 50M -L 200M -i 1 vol_grp
Sample Output:
9. Create a thin snapshot of a thin logical volume
The following commands create a thin snapshot mysnap
of a thin logical volume mythin
on the volume group vol_grp
. You must not use the size option otherwise, a copy-on-write snapshot will be created.
$ sudo lvcreate -s -n mysnap vol_grp/mythin
OR
$ sudo lvcreate --snapshot --name mysnap vol_grp/mythin
Sample Output:
10. Create a cache logical volume
A cache logical volume can improve the performance of a larger and slower logical volume by storing the frequently used blocks on the smaller and faster logical volume. For this, it uses a small logical volume consisting of fast block devices (such as SSD drives).
The following command creates the cache logical volume named cache_lv
of the size 100MiB on the fast device /dev/sda1
, which is part of the volume group vol_grp
.
$ sudo lvcreate -L 100M -n cache_lv vol_grp /dev/sda1
Sample Output:
11. Create a cache pool from a fast physical device
You can use --type
option to specify a cache pool. This command creates a cache pool named cpool
of size 60MiB on the fast device /dev/sda1
and volume group vol_grp
.
$ sudo lvcreate --type cache-pool -L 60M -n cpool vol_grp /dev/sda1
Sample Output:
The cache pool can be used to cache the logical volume.
12. Use percentage to specify the size of the logical volume
The -l
or --extent
option allows you to specify the percentage of the total space in the volume group to use as the size for the new logical volume. For example, the following command creates a new logical volume mylv
that uses 60% of the total space in the volume group vol_grp
.
$ sudo lvcreate -l 60%VG -n mylv vol_grp
OR
$ sudo lvcreate --extent 60%VG -n mylv vol_grp
Sample Output:
13. Create logical volume using the remaining space in the volume group
You can also use -l
or --extent
option to specify the percentage of the remaining free space as the size of the logical volume in the volume group. Using 100%FREE
will create a new logical volume that uses all of the unallocated space in the volume group.
$ sudo lvcreate -l 100%FREE -n new_lv vol_grp
OR
$ sudo lvcreate --extent 100%FREE -n new_lv vol_grp
Sample Output:
Conclusion
This article teaches you how to use lvcreate
command to create a new logical volume in the Linux system. It is a useful command that helps to create different types of logical volume such as striped, mirrors, raid, thin, thin-pool, and snapshots of the existing logical volume. If you have any queries about this article or suggestions to improve it, please share them with us in the comment section below.
Further Reading