How to Create, Extend, and Remove LVM on Debian 12

To Create, Extend, Remove LVM On Debian 12

Introduction

Logical Volume Management (LVM) is a disk management solution used in Linux systems, offering advanced features for efficient disk storage management. It acts as a buffer between physical storage devices (e.g., hard drives, solid-state drives, or partitions) and the filesystems they contain. LVM enables system administrators to easily allocate, resize, and relocate storage capacity without interrupting data access or services, making it a valuable tool for both desktop and server environments.

Procedure Steps

Step 1: Check the OS version by using the below command.

root@linuxhelp:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Check disks by using the below command

root@linuxhelp:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   10G  0 disk 
sdb      8:16   0    5G  0 disk 
sdc      8:32   0    5G  0 disk 
sdd      8:48   0   60G  0 disk 
├─sdd1   8:49   0  1.9G  0 part /boot
├─sdd2   8:50   0  3.7G  0 part [SWAP]
└─sdd3   8:51   0 54.4G  0 part /
sr0     11:0    1 1024M  0 rom  

Step 3: Create physical volumes by using the below command

root@linuxhelp:~# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

Step 4: Check the physical volumes by using the below command

root@linuxhelp:~# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  5.00g 5.00g
  /dev/sdc      lvm2 ---  5.00g 5.00g

Step 5: Create volume group by using the below command

root@linuxhelp:~# vgcreate volumegroup /dev/sdb /dev/sdc
  Volume group "volumegroup" successfully created

Step 6: Check the volume group by using following command.

root@linuxhelp:~# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  volumegroup   2   0   0 wz--n- 9.99g 9.99g

Step 7: Check disks by using following command for extend volume group size.

root@linuxhelp:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   10G  0 disk 
sdb      8:16   0    5G  0 disk 
sdc      8:32   0    5G  0 disk 
sdd      8:48   0   60G  0 disk 
├─sdd1   8:49   0  1.9G  0 part /boot
├─sdd2   8:50   0  3.7G  0 part [SWAP]
└─sdd3   8:51   0 54.4G  0 part /
sr0     11:0    1 1024M  0 rom  

Step 8: Create a raw disk to physical volume to extend volume group size by using the below command.

root@linuxhelp:~# pvcreate /dev/sda
  Physical volume "/dev/sda" successfully created.

Step 9: Check the physical volumes by using following command.

root@linuxhelp:~# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/sda               lvm2 ---  10.00g 10.00g
  /dev/sdb   volumegroup lvm2 a--  <5.00g <5.00g
  /dev/sdc   volumegroup lvm2 a--  <5.00g <5.00g

Step 10: Extend the volume group size by using the below command.

root@linuxhelp:~# vgextend volumegroup /dev/sda
  Volume group "volumegroup" successfully extended

Step 11: Check volume group size by using following command.

root@linuxhelp:~# vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  volumegroup   3   0   0 wz--n- <19.99g <19.99g

Step 12: Create linear logical volume by using the below command.

root@linuxhelp:~# lvcreate -L +1G -n logicalvolume volumegroup
  Logical volume "logicalvolume" created.

Step 13: Check the logical volume by using the below command.

root@linuxhelp:~# lvs
  LV            VG          Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  logicalvolume volumegroup -wi-a----- 1.00g   

Step 14: Make the file system for logical volume by using the below command

root@linuxhelp:~# mkfs -t ext4 /dev/volumegroup/logicalvolume
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 35382189-25bd-4c42-b61b-4655168fe29f
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Step 15: Create a directory to mount file system by using the below command.

root@linuxhelp:~# mkdir lvolume

Step 16: Mount the file system temporarily by using the below command.

root@linuxhelp:~# mount /dev/volumegroup/logicalvolume /root/lvolume

Step 17: Check the file system size and mount point by using the below command.

root@linuxhelp:~# df -h
Filesystem                             Size  Used Avail Use% Mounted on
udev                                   1.4G     0  1.4G   0% /dev
tmpfs                                  291M  1.5M  289M   1% /run
/dev/sdd3                               54G   13G   38G  25% /
tmpfs                                  1.5G     0  1.5G   0% /dev/shm
tmpfs                                  5.0M  8.0K  5.0M   1% /run/lock
/dev/sdd1                              1.8G  140M  1.6G   9% /boot
tmpfs                                  291M   88K  291M   1% /run/user/1000
/dev/mapper/volumegroup-logicalvolume  974M   24K  907M   1% /root/lvolume

Step 18: Mount the file system permanently by using the below command.

root@linuxhelp:~# vim /etc/fstab 
/dev/mapper/volumegroup-logicalvolume /root/lvolume ext4 defaults 0 1

Step 19: Unmount the file system for checking permanently mount by using the below command.

root@linuxhelp:~# umount /root/lvolume

Step 20: Apply all permanent mount by using the below command.

root@linuxhelp:~# mount -a

Step 21: Check the file system size and mount point by using the below command

root@linuxhelp:~# df -h
Filesystem                             Size  Used Avail Use% Mounted on
udev                                   1.4G     0  1.4G   0% /dev
tmpfs                                  291M  1.5M  289M   1% /run
/dev/sdd3                               54G   13G   38G  25% /
tmpfs                                  1.5G     0  1.5G   0% /dev/shm
tmpfs                                  5.0M  8.0K  5.0M   1% /run/lock
/dev/sdd1                              1.8G  140M  1.6G   9% /boot
tmpfs                                  291M   88K  291M   1% /run/user/1000
/dev/mapper/volumegroup-logicalvolume  974M   24K  907M   1% /root/lvolume

Step 22: Check the logical volume by using the below command.

root@linuxhelp:~# lvs
  LV            VG          Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  logicalvolume volumegroup -wi-ao---- 1.00g         

Step 23: Extend the logical volume by using the below command.

root@linuxhelp:~# lvextend -L +2G /dev/volumegroup/logicalvolume
  Size of logical volume volumegroup/logicalvolume changed from 1.00 GiB (256 extents) to 3.00 GiB (768 extents).
  Logical volume volumegroup/logicalvolume successfully resized.

Step 24: Check the logical volume by using the below command.

root@linuxhelp:~# lvs
  LV            VG          Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  logicalvolume volumegroup -wi-ao---- 3.00g       

Step 25: Check the file system size by using the below command.

root@linuxhelp:~# df -h
Filesystem                             Size  Used Avail Use% Mounted on
udev                                   1.4G     0  1.4G   0% /dev
tmpfs                                  291M  1.5M  289M   1% /run
/dev/sdd3                               54G   13G   38G  25% /
tmpfs                                  1.5G     0  1.5G   0% /dev/shm
tmpfs                                  5.0M  8.0K  5.0M   1% /run/lock
/dev/sdd1                              1.8G  140M  1.6G   9% /boot
tmpfs                                  291M   88K  291M   1% /run/user/1000
/dev/mapper/volumegroup-logicalvolume  974M   24K  907M   1% /root/lvolume

Step 26: Extend the file system size by using the below command.

root@linuxhelp:~# resize2fs /dev/volumegroup/logicalvolume
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/volumegroup/logicalvolume is mounted on /root/lvolume; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/volumegroup/logicalvolume is now 786432 (4k) blocks long.

Step 27: Check the file system size by using the below command.

root@linuxhelp:~# df -h
Filesystem                             Size  Used Avail Use% Mounted on
udev                                   1.4G     0  1.4G   0% /dev
tmpfs                                  291M  1.5M  289M   1% /run
/dev/sdd3                               54G   13G   38G  25% /
tmpfs                                  1.5G     0  1.5G   0% /dev/shm
tmpfs                                  5.0M  8.0K  5.0M   1% /run/lock
/dev/sdd1                              1.8G  140M  1.6G   9% /boot
tmpfs                                  291M   88K  291M   1% /run/user/1000
/dev/mapper/volumegroup-logicalvolume  3.0G   24K  2.8G   1% /root/lvolume

Step 28: Create striped logical volume by using the below command.

root@linuxhelp:~# lvcreate -i 3 -I128k -L +2G -n stripedlv volumegroup
  Rounding size 2.00 GiB (512 extents) up to stripe boundary size 2.00 GiB (513 extents).
  Logical volume "stripedlv" created.

Step 29: Check the logical volume by using the below command.

root@linuxhelp:~# lvs
  LV            VG          Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  logicalvolume volumegroup -wi-ao---- 3.00g                                                    
  stripedlv     volumegroup -wi-a----- 2.00g      

Step 30: Create mirror logical volume by using the below command.

root@linuxhelp:~# lvcreate -m1 -L +2G -n mirrorlv volumegroup
  Logical volume "mirrorlv" created.

Step 31: Check the logical volume by using the below command.

root@linuxhelp:~# lvs
  LV            VG          Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  logicalvolume volumegroup -wi-ao---- 3.00g                                                    
  mirrorlv      volumegroup rwi-a-r--- 2.00g                                    100.00          
  stripedlv     volumegroup -wi-a----- 2.00g     

Step 32: unmount the logical volume file system by using the below command.

root@linuxhelp:~# umount /root/lvolume

Step 33: Remove the logical volumes by using the below command.

root@linuxhelp:~# lvremove /dev/volumegroup/logicalvolume
Do you really want to remove active logical volume volumegroup/logicalvolume? [y/n]: y
  Logical volume "logicalvolume" successfully removed.
root@linuxhelp:~# lvremove /dev/volumegroup/stripedlv
Do you really want to remove active logical volume volumegroup/stripedlv? [y/n]: y
  Logical volume "stripedlv" successfully removed.
root@linuxhelp:~# lvremove /dev/volumegroup/mirrorlv
Do you really want to remove active logical volume volumegroup/mirrorlv? [y/n]: y
  Logical volume "mirrorlv" successfully removed.

Step 34: Check the logical volume by using the below command.

root@linuxhelp:~# lvs

Step 35: Remove the physical volumes from volume group by using the below command.

root@linuxhelp:~# vgreduce volumegroup /dev/sda
  Removed "/dev/sda" from volume group "volumegroup"
root@linuxhelp:~# vgreduce volumegroup /dev/sdb
  Removed "/dev/sdb" from volume group "volumegroup"

Step 36: Check the volume group by using the below command.

root@linuxhelp:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  volumegroup   1   0   0 wz--n- <5.00g <5.00g

Step 37: Remove the volume group by using the below command.

root@linuxhelp:~# vgremove /dev/volumegroup
  Volume group "volumegroup" successfully removed

Step 38: Check the physical volumes by using the below command.

root@linuxhelp:~# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sda      lvm2 ---  10.00g 10.00g
  /dev/sdb      lvm2 ---   5.00g  5.00g
  /dev/sdc      lvm2 ---   5.00g  5.00g

Step 39: Remove the physical volumes by using the below command.

root@linuxhelp:~# pvremove /dev/sda
  Labels on physical volume "/dev/sda" successfully wiped.
root@linuxhelp:~# pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to Create, Extend, Remove LVM on Debian 12. Your feedback is much welcome

FAQ
Q
Is it possible to remove a physical volume from an LVM setup?
A
Yes, you can remove a physical volume from an LVM setup, but it requires careful planning and execution to avoid data loss. Generally, you would follow these steps:
1. Migrate data off the physical volume you want to remove to other volumes.
2. Remove the physical volume from the volume group using vgreduce.
3. Optionally, use pvmove to relocate data from the physical volume to other volumes in the volume group before removing it.
4. After ensuring no data remains on the physical volume, use pvremove to wipe LVM metadata from the physical volume.
Q
How do I add more storage to an existing LVM setup?
A
To add more storage to an existing LVM setup, you can follow these general steps:
1. Add one or more new physical volumes (disks or partitions) to the system.
2. Initialize the new physical volumes using pvcreate.
3. Extend the existing volume group to include the new physical volumes using vgextend.
4. Allocate space from the extended volume group to existing or new logical volumes using lvextend.
5. Resize the filesystem within the logical volume to utilize the additional space.
Q
Can I resize LVM volumes?
A
Yes, one of the main benefits of LVM is its ability to resize logical volumes dynamically. You can resize a logical volume by using lvresize to increase or decrease its size, and then resize the filesystem within the logical volume to match the new size using filesystem-specific tools (resize2fs for ext2/3/4, xfs_growfs for XFS, etc.).
Q
How do I create an LVM volume?
A
To create an LVM volume, you typically follow these steps:
1. Initialize physical volumes (PVs) on the desired storage devices using pvcreate.
2. Create a volume group (VG) with one or more physical volumes using vgcreate.
3. Create logical volumes (LVs) within the volume group using lvcreate.
4. Format the logical volumes with a filesystem using mkfs.
5. Mount the logical volumes to a directory in the filesystem.
Q
What is Logical Volume Manager?
A
Logical Volume Management (LVM) allocates disk space flexibly, allowing administrators to dynamically resize volumes and filesystems as needed without disrupting services. LVM abstracts the physical storage devices (such as hard drives or SSDs) into logical volumes, which can then be managed independently of the underlying hardware.