Menu
InformatiWeb Pro
  • Index
  • System admin
  • Virtualization

Login

Registration Password lost ?
FR
  • Windows Server
    • WMS 2012
    • WS2012 R2
    • WS2016
  • Citrix
    • Citrix NetScaler Gateway
    • Citrix XenApp / XenDesktop
    • Citrix XenServer
  • VMware
    • VMware ESXi
    • VMware vSphere
    • VMware Workstation
  • Microsoft
    • Hyper-V
  • RAID
    • Adaptec SmartRAID
  • UPS
    • APC Back-UPS Pro
  • InformatiWeb Pro
  • System admin
  • Linux
  • Install and use the LVM on Linux
  • Linux
  • 02 October 2016 at 15:18 UTC
  • InformatiWeb

Install and use the LVM on Linux

LVM (Logical Volume Manager) is a system that allows you to create logical volumes and to manage multiple file systems on one or more disks.
Indeed, with LVM, you'll be able to dynamically manage a file system residing on one or more physical hard drives. A bit like software RAID.

By using LVM, you could for example store user data on a hard drive and store the rest on another hard drive.

  1. LVM presentation
    1. Physical volumes
    2. Volume groups
    3. Logical volumes
  2. List hard drives and partitions
  3. Use the LVM
    1. Installation
    2. Create a physical volume
    3. Create a volume group
    4. Create a logical volume
    5. Format the logical volume
    6. Mount the logical volume
    7. Resize a logical volume
    8. Remove a logical volume
    9. Remove a volume group
    10. Remove a physical volume

1. LVM presentation

LVM has 3 layers :

  1. Physical volumes
  2. Volume groups
  3. Logical volumes

1.1. Physical volumes

Physical volumes correspond to the physical hard drives or to partitions present on this hard disk (you can choose).

The physical volumes are the link between the hardware (your hard drives) and the LVM.

The commands for managing these physical volumes in LVM begin with : pv

1.2. Volume groups

Volume groups let you group multiple physical hard disks or multiple partitions together.

It is in these volumes groups that we create our logical volumes.

1.3. Logical volumes

These are the volumes in which we create filesystems (ext4, NTFS, ...) and where we can store data.

Note that logical volumes must be mounted in the main file system to store data into them.

2. List hard drives and partitions

To do this, simply type the command :

Bash

fdisk -l

In our case, we have :
- /dev/sda : the hard disk where Debian is installed.
- /dev/sdb : 2nd hard disk that is blank for now. (No partition on the hard drive)

Plain Text

Disque /dev/sda : 64.4 Go, 64424509440 octets
255 têtes, 63 secteurs/piste, 7832 cylindres, total 125829120 secteurs
Unités = secteurs de 1 * 512 = 512 octets
Taille de secteur (logique / physique) : 512 octets / 512 octets
taille d'E/S (minimale / optimale) : 512 octets / 512 octets
Identifiant de disque : 0x000d5066

Périphérique Amorce  Début        Fin      Blocs     Id  Système
/dev/sda1   *        2048   120635391    60316672   83  Linux
/dev/sda2       120637438   125827071     2594817    5  Étendue
/dev/sda5       120637440   125827071     2594816   82  partition d'échange Linux / Solaris

Disque /dev/sdb : 85.9 Go, 85899345920 octets
255 têtes, 63 secteurs/piste, 10443 cylindres, total 167772160 secteurs
Unités = secteurs de 1 * 512 = 512 octets
Taille de secteur (logique / physique) : 512 octets / 512 octets
taille d'E/S (minimale / optimale) : 512 octets / 512 octets
Identifiant de disque : 0x00000000

Le disque /dev/sdb ne contient pas une table de partitions valable

3. Use the LVM

3.1. Installation

To install LVM, just install the lvm2 package.

Bash

apt-get install lvm2

3.2. Create a physical volume

Bash

pvcreate /dev/sdb

Plain Text

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

To verify that the volume has been created, use the "pvdisplay" command that will list existing physical volumes.

Bash

pvdisplay

Plain Text

 "/dev/sdb" is a new physical volume of "80,00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb
VG Name 
PV Size               80,00 GiB
Allocatable           NO
PE Size               0 
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               Tqludv-SipP-Kjcp-22eU-JSVf-NxDJ-IlFFcO

3.3. Create a volume group

To create a volume group, use the command "vgcreate" and specify this :
- the group name
- and the path of the physical volume (created earlier)

Bash

vgcreate iwvg /dev/sdb

Plain Text

Volume group "iwvg" successfully created

To verify that the volume group has been created, also use the "pvdisplay" command.
As you can see, the name of the volume group (VG) is displayed. Which was not the case previously.

Bash

pvdisplay

Plain Text

 --- Physical volume ---
PV Name               /dev/sdb
VG Name               iwvg
PV Size               80,00 GiB / not usable 4,00 MiB
Allocatable           yes 
PE Size               4,00 MiB
Total PE              20479
Free PE               20479
Allocated PE          0
PV UUID               Tqludv-SipP-Kjcp-22eU-JSVf-NxDJ-IlFFcO

To list the volumes groups, use the command :

Bash

vgscan

Plain Text

Reading all physical volumes.  This may take a while...
Found volume group "iwvg" using metadata type lvm2

To get more information about your volume group, use the command :

Bash

vgdisplay iwvg

Plain Text

--- Volume group ---
VG Name               iwvg
System ID 
Format                lvm2
Metadata Areas        1
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                1
Act PV                1
VG Size               80,00 GiB
PE Size               4,00 MiB
Total PE              20479
Alloc PE / Size       0 / 0 
Free  PE / Size       20479 / 80,00 GiB
VG UUID               zxxLNN-lX08-rhB2-MM9T-kUP6-GSaC-TEObDY

3.4. Create a logical volume

Now, you'll be able to create the desired logical volumes.
For this, use the command : lvcreate.

Parameters :
-L : Logical volume size in Mo. Note that it's also possible to specify a size in Go : -L50GO
-n : name of the logical volume
(3rd parameter) : name of the volumes group

Note that it's best to specify the size in GO if you want to create a volume of several gigabytes.
Indeed, 1 GB = 1024 MB, not 1000 MB.
As you'll see a little lower, our volume of 5000 MB will have a size of 48.83 GB, not 50 GB.
But, the volume of 30 GB will have a size of 30 GB.

Bash

lvcreate -L 50000 -n iw_vol1 iwvg

Plain Text

Logical volume "iw_vol1" created

Bash

lvcreate -L 30GO -n iw_vol2 iwvg

Plain Text

Logical volume "iw_vol2" created

To verify that the logical volume has been created, use the "lvscan" command.

Bash

lvscan

Plain Text

ACTIVE            '/dev/iwvg/iw_vol1' [48,83 GiB] inherit
ACTIVE            '/dev/iwvg/iw_vol2' [30,00 GiB] inherit

To get more information about your logical volume, use the command :

Bash

lvdisplay

Plain Text

--- Logical volume ---
LV Path                /dev/iwvg/iw_vol1
LV Name                iw_vol1
VG Name                iwvg
LV UUID                DMOkJu-Y0gu-75KX-da1g-qLgD-yZah-jvFLAZ
LV Write Access        read/write
LV Creation host, time debian, 2016-09-10 14:19:25 +0200
LV Status              available
# open                 0
LV Size                48,83 GiB
Current LE             12500
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:0

--- Logical volume ---
LV Path                /dev/iwvg/iw_vol2
LV Name                iw_vol2
VG Name                iwvg
LV UUID                ie2Oot-Dki9-wmuR-9gqE-FjX5-hwie-kqzLqe
LV Write Access        read/write
LV Creation host, time debian, 2016-09-10 14:22:27 +0200
LV Status              available
# open                 0
LV Size                30,00 GiB
Current LE             7680
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:1

3.5. Format the logical volume

To use your logical volume, you must first format it in order to create the desired file system into it. (As with a normal partition)

To format the volume with ext4 filesystem, use the command below.
Note : /dev/iwvg/iw_vol1 is the path of the logical volume to format. This is the "LV Path" value displayed by the "lvdisplay" command (see above).

Bash

mkfs -t ext4 /dev/iwvg/iw_vol1

3.6. Mount the logical volume

To use your logical volume, it is necessary to mount it.

For that, create a folder where you want and mount your logical volume to this new folder.

Bash

mkdir /my_lvm_volume
mount /dev/iwvg/iw_vol1 /my_lvm_volume

Then, use the command "df -h" to display the file systems list, and the size occupied by them.
You will also see your logical volume "iwvg-iw_vol1" mounted on "/my_lvm_volume".

Bash

df -h

Plain Text

Sys. fich.                                             Taille Util. Dispo Uti% Monté sur
rootfs                                                    57G  4,2G   50G   8% /
udev                                                      10M     0   10M   0% /dev
tmpfs                                                    396M  648K  395M   1% /run
/dev/disk/by-uuid/3f7e5d0d-62e7-4ec9-a36b-a9f0983644c8    57G  4,2G   50G   8% /
tmpfs                                                    5,0M     0  5,0M   0% /run/lock
tmpfs                                                    1,3G  224K  1,3G   1% /run/shm
/dev/mapper/iwvg-iw_vol1                                  49G  180M   46G   1% /my_lvm_volume

Now, the data that you will store in the "/my_lvm_volume" folder will in reality stored in your logical volume : /dev/iwvg/iw_vol1

3.7. Resize a logical volume

To resize a logical volume, you must use the "lvextend" command.

This command can be used in 2 ways.

To extend a logical volume to the desired size, use the command like this :

Bash

lvextend -L50G /dev/iwvg/iw_vol1

To add several MB or GB to a logical volume, use the command like this :

Bash

lvextend -L+10G /dev/iwvg/iw_vol1

Finally, don't forget to update the file system present on the logical volume that you just resized.

To do this, unmount the concerned logical volume :

Bash

umount /dev/iwvg/iw_vol1

Then, check and resize the logical volume file system.

Bash

e2fsck -f /dev/iwvg/iw_vol1
resize2fs /dev/iwvg/iw_vol1

Finally, mount again your logical volume as before :

Bash

mount /dev/iwvg/iw_vol1 /my_lvm_volume

3.8. Remove a logical volume

Finally, if you want to remove a logical volume :
- remove the concerned logical volume
- then, use the "lvremove" command

Bash

umount /dev/iwvg/iw_vol1
lvremove /dev/iwvg/iw_vol1

3.9. Remove a volume group

If a physical volume of a volume group no longer exists, you should use the command :

Bash

vgreduce --removemissing

If you want to remove a volume group, use the "vgremove" command and specify the name of the volume group.

Bash

vgremove iwvg

Note that if logical volumes still exist in the volume group, this command will ask if you want to remove them.
Answer "y" to remove them.

Plain Text

Do you really want to remove volume group "iwvg" containing 2 logical volumes? [y/n]: 

To overcome this question and automatically remove logical volumes present in the volume group to delete, add the "-f" parameter.

Bash

vgremove -f iwvg

3.10. Remove a physical volume

To remove a physical volume :
- unmount the logical volumes affected by this physical volume.
- then, remove the volume group that uses this physical volume.
- and finally, use the "pvremove" command and specify the name of the physical volume to remove.

Bash

pvremove /dev/sdb

Share this tutorial

Partager
Tweet

To see also

  • Debian / Ubuntu - Configure an iSCSI server and an iSCSI client

    Linux 10/11/2016

    Debian / Ubuntu - Configure an iSCSI server and an iSCSI client

  • Debian / Ubuntu - Create a root certification authority

    Linux 2/14/2015

    Debian / Ubuntu - Create a root certification authority

  • Debian / Ubuntu - Install a FTP server

    Linux 2/4/2014

    Debian / Ubuntu - Install a FTP server

  • Debian / Ubuntu - Network monitoring with HTML statistics

    Linux 9/12/2015

    Debian / Ubuntu - Network monitoring with HTML statistics

Comments

You must be logged in to post a comment

Share your opinion

Pinned content

  • Software (System admin)
  • Linux softwares
  • Our programs
  • Terms and conditions
  • Share your opinion

Contact

  • Guest book
  • Technical support
  • Contact

® InformatiWeb-Pro.net - InformatiWeb.net 2008-2022 - © Lionel Eppe - All rights reserved.

Total or partial reproduction of this site is prohibited and constitutes an infringement punishable by articles L.335-2 and following of the intellectual property Code.