Table of contents of the article:
The Linux world is full of tools and technologies that allow system administrators to manage hardware resources efficiently and flexibly. One such tool is LVM, or Logical Volume Manager. In this article, we'll explore what LVM is, how it works, and how it can be used to manage storage on Linux systems.
1. Introduction to LVM
LVM, short for Logical Volume Manager, represents a revolution in the way system administrators and end users interact and manage storage space on a Linux system. At the heart of its philosophy is the idea of treating physical disks not as rigid and static entities, but rather as flexible resources within a unified pool, known as a "Volume Group". This pool concept allows users to dynamically allocate, resize, and free up disk space, creating what are called "logical volumes."
Unlike traditional partition management, where each partition has a size and physical location on the disk, LVM abstracts this concept. In practice, this means that, with LVM, you are not bound by the physical limitations of the disk. For example, if you need more space for a particular application or service, you can resize a logical volume on the fly, without needing to unmount the file system or stop the service. This flexibility also extends to the ability to move data between different disks transparently, without interruption or downtime.
Additionally, LVM offers a more granular view of storage, allowing for more efficient management of disk space. This results in greater operational efficiency, reducing the risk of wasted space while ensuring resources are available when and where they are needed.
2. Main components of LVM
LVM is based on three main components:
- Physical Volumes (PV): These represent the physical disks or partitions that are used as storage for LVM.
- Volume Groups (VG): A VG is a set of PVs. You can think of a VG as a large virtual disk made up of one or more physical disks.
- Logical Volumes (LV): These are the virtual "discs" that are created inside a VG. LVs can be used as if they were traditional partitions.
3. Why use LVM?
There are many reasons a system administrator might want to use LVM:
- Flexibility: With LVM, you can resize logical volumes on the fly, without the need to unmount the filesystem or stop the service.
- Snapshot: LVM allows you to create snapshots of volumes, which are useful for backups or for testing changes without impacting the original volume.
- Data migration: With LVM, you can move data from one physical disk to another without interruption.
- Simplified management: LVM offers centralized storage management, making it easier to manage large amounts of disk space.
4. How LVM works
Now that we understand the basic concepts of LVM, let's see how it works in practice.
to. Initialization of Physical Volumes
Before you can use a disk or partition with LVM, it must be initialized as a PV. This can be done with the command pvcreate
.
pvcreate /dev/sdb1
b. Creating a Volume Group
Once you have one or more PVs, you can create a VG on them. For example, to create a VG called "myvg" with a PV, you could use:
vgcreate myvg /dev/sdb1
c. Creation of Logical Volumes
With a VG ready, you can start making LVs. For example, to create a 10GB LV called "mylv" inside the "myvg" VG, you could use:
lvcreate -L 10G -n mylv myvg
d. Formatting and editing
Once the LV is created, it can be formatted with a filesystem such as ext4 and mounted like any other partition:
mkfs.ext4 /dev/myvg/mylv mount /dev/myvg/mylv /mnt/mydata
5. Advanced operations with LVM
In addition to basic operations, LVM offers a number of advanced features.
to. Resizing of Logical Volumes
LVM allows you to resize LVs on the fly. For example, to extend an LV by 5GB, you could use:
lvextend -L +5G /dev/myvg/mylv resize2fs /dev/myvg/mylv
b. Creating snapshots
Snapshots are useful for creating a "snapshot" copy of an LV:
lvcreate -L 5G -s -n mylv_snapshot /dev/myvg/mylv
c. Data migration between disks
If you want to move data from one PV to another, you can use pvmove
:
pvmove /dev/sdb1 /dev/sdc1
6. LVM Commands Cheatsheet for Linux
Here is a cheatsheet of LVM (Logical Volume Manager) commands. This guide has been designed to give you a quick and clear overview of the main LVM commands, providing not only a brief description of each command's function, but also syntax examples to make them easier to use. Whether you're new to LVM or just looking for a quick reference, this cheatsheet can help you better understand the desired command and its related syntax. Use it as a starting point or as a quick reference tool during your LVM work sessions.
Commands for Physical Volume (PV)
Command | Function | Example syntax |
---|---|---|
pvscan | Scan all physical devices used as PV | pvscan |
pvccreate | Initialize partitions for PV creation | pvcreate /dev/sdx1 |
pvdisplay | Show attributes provided by PV | pvdisplay /dev/sdx1 |
pvchange | Modify the attribute gained from HP | pvchange /dev/sdx1 |
pv | About PV | pvdisplay /dev/sdx1 |
pvck | Check PV metadata | pvck /dev/sdx1 |
pvremove | Remove VP | pvremove /dev/sdx1 |
Commands for Volume Group (VG)
Command | Function | Example syntax |
---|---|---|
vgscan | Scan each PV for VG | vgscan |
vgcreate | Create a Volume Group | vgcreate myvg /dev/sdx1 |
vgdisplay | Show the attributes available in VG | vgdisplay myvg |
vgchange | Modify the attributes of VG | vgchange myvg |
vgs | About VG | vgs |
vgck | Check the VG metadata | vgck myvg |
vgrename | Rename VG | vgrename oldvgname newvgname |
vgreduce | Remove PV from VG to reduce its size | vgreduce myvg /dev/sdx1 |
vgextend | Add PV to Volume Group | vgextend myvg /dev/sdx2 |
vgmerge | Merge two Volume Groups | vgmerge myvg1 myvg2 |
vgsplit | Separate two Groups of Volumes | vgsplit myvg1 myvg2 |
vgremove | Remove a Volume Group | vgremove myvg |
Commands for Logical Volume (LV)
Command | Function | Example syntax |
---|---|---|
lvscan | Scan all logical volumes for LV | lvscan |
lvcreate | Create a LV in the Volume Group | lvcreate -L 10G -n mylv myvg |
lvdisplay | Show the attributes of LV | lvdisplay /dev/myvg/mylv |
lvchange | Modify the attributes of LV | lvchange /dev/myvg/mylv |
lv | Show the attributes of LV | lvdisplay /dev/myvg/mylv |
lvrename | Rename LV | lvrename /dev/myvg/oldlvname /dev/myvg/newlvname |
lvreduce | Reduce the size of LV | lvreduce -L -5G /dev/myvg/mylv |
liveextend | Increase LV size | lvextend -L +5G /dev/myvg/mylv |
lvresize | Change the size of LV | lvresize -L 15G /dev/myvg/mylv |
lv remove | Remove LV | lvremove /dev/myvg/mylv |
7. Conclusion
LVM is a powerful and flexible tool that every Linux system administrator should know. It offers simplified storage management, with the ability to resize, move, and snapshot volumes on the fly. If you're looking for a way to manage storage more efficiently and flexibly, LVM might be the solution you're looking for.
This article has provided a high-level overview of LVM and its capabilities. There are many more features and details that can be explored; therefore, I encourage you to experiment and delve further into this topic.
Here are some reference websites for LVM, offering detailed documentation, guides and tutorials:
- Official LVM Documentation
- URL: https://www.redhat.com/sysadmin/beginners-guide-lvm This is the official LVM documentation provided by Red Hat. It is a comprehensive resource covering all aspects of LVM, from basic operations to advanced features.
- The Linux Documentation Project (LDP)
- URL: https://tldp.org/HOWTO/LVM-HOWTO/ LDP is one of the oldest and most respected resources for Linux documentation. Their LVM guide is detailed and covers many of the features of LVM.
- Arch Linux Wiki
- URL: https://wiki.archlinux.org/title/LVM While specific to Arch Linux, this wiki is known for its detailed and technical guides. The LVM page is no exception and offers a comprehensive overview of how to use LVM on Arch Linux.
- LinuxConfig.org
- URL: https://linuxconfig.org/lvm-tutorial This site offers a variety of guides and tutorials on various Linux topics, including LVM. The LVM guide is detailed and covers both basic and advanced operations.
These sites are great starting points for anyone who wants to learn more about LVM. I recommend that you explore them and use the information provided to experiment and learn further.