La création de volumes logiques se fait en plusieurs étapes. Ci-dessous un flux de travail de base pour utiliser LVM sous Linux.
Création d’un stockage LVM
Préparation des disques
Créez une nouvelle partition LVM avec fdisk :
# fdisk /dev/vde
Créez une nouvelle partition LVM :
n : créer une nouvelle partition
type de partition : par défaut primaire, appuyez sur Enter
numéro de partition : par défaut le prochain disponible, appuyez sur Enter
premier secteur : appuyez sur Enter pour utiliser la valeur par défaut
taille de la partition : syntaxe +size{K,M,G,T} (+2G pour une partition de 2 Go)
t: changer le type de partition, type 8e pour Linux LVM
w : sauvegarder les changements
# fdisk /dev/sde
[...] Command (m for help): g Created a new GPT disklabel (GUID: 0E2E2C20-9860-C94D-AC0B-870E7F5BC2E4).
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): Using default response p.
Partition number (1-4, default 1): First sector (2048-10485759, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485759, default 10485759): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): t Selected partition 1 Hex code or alias (type L to list all): Linux LVM Changed type of partition 'Empty' to 'Linux LVM'.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS [...] vde 8:64 0 5G 0 disk └─vde1 8:65 0 2G 0 part
Redémarrez la table de partitions
Après avoir créé la nouvelle partition, il faut redémarrer la table de partitions :
# partprobe /dev/vde
Création de PV, VG et LV
Création de Physical Volumes (PV)
Créez un nouveau Physical Volume (PV) avec pvcreate sur la nouvelle partition :
# pvcreate /dev/vde1
Création d’un Volume Group (VG)
Créez un Volume Group (VG) avec vgcreate sur le Physical Volume :
# vgcreate vg01 /dev/vde1
Création de Logical Volumes (LV)
Créez un Logical Volume (LV) avec lvcreate dans le Volume Group :
# lvcreate -n lv01 -L 20G vg01
Formatage du Logical Volume
Formattez le lv avec mkfs :
# mkfs.ext4 /dev/vg01/lv01
ou
# mkfs -t ext4 /dev/vg01/lv01
Montage du Logical Volume :
Créer un répertoire de montage (s’il n’existe pas) et monter le Logical Volume dedans, ensuite utiliser le répertoire de montage comme point d’accès pour stocker des fichiers et données :
# mkdir /mnt/root/DATAS # mount /dev/vg01/lv01 /mnt/root/DATAS
Configuration persistante
Pour que le système de fichiers soit disponible de manière persistante il faut ajouter une entrée dans /etc/fstab :
# /dev/vg01/lv01 /mnt/root/DATAS xfs defaults 0 0
Affichage des informations LVM
La commande pvdisplay affiche les informations des PV (Physical Volumes) :
# pvdisplay /dev/vde1
La commande vgdisplay affiche les informations des VG (Volume Groups) :
# vgdisplay vg01
La commande lvdisplay affiche les informations des LV (Logical Volumes) :
# lvdisplay /dev/vg01/lv01
Extension du swap en LVM
Désactiver le swap sur le LV :
# swapoff -v /dev/vg01/swap
Étendre le LV :
# lvextend -L +300M /dev/vg01/swap
Formater le LV en tant qur swap :
# mkswap /dev/vg01/swap
Activer le swap sur le LV :
# swapon /dev/vg01/swap
Extension et réduction du stockage LVM
Extension LVM
Étendre le Physical Volume (PV)
Si besoin, créez un nouveau Physical Volume (PV) avec pvcreate sur la nouvelle partition :
# pvcreate /dev/vde1
Ajouter de l’espace (disque, partition, ou RAID) avec pvresize au Physical Volume (PV) si besoin :
# pvresize /dev/vde3
Étendre le Volume Group (VG)
Étendre le Volume Group (VG) avec vgextend :
# vgextend vg01 /dev/vde3
Étendre le Logical Volume (LV)
Étendre le Volume Group (VG) avec lvextend :
# lvextend -L +500M /dev/vg01/lv01
Extension du système de fichiers
Redimensionner le système de fichiers avec resize2fs pour du ext4 :
Permet d’étendre le système de fichiers afin qu’il occupe le LV étendu.
Réduction LVM
Réduire le système de fichiers
Réduire le système de fichiers avec resize2fs pour du ext4 :
# resize2fs /dev/vg01/lv01 1G
Réduire le Logical Volume (LV)
Réduire le Logical Volume (LV) avec lvreduce :
# lvreduce -L 1G /dev/vg01/lv01
Réduire le Volume Group (VG)
Réduire (si nécessaire) le VG pour libérer de l’espace non alloué :
# pvmove /dev/vde3
Réduire le Volume Group (VG) avec vgreduce (si vous libérez complètement un disque, par exemple) :
# vgreduce vg01 /dev/vde3
Réduire le Physical Volume (PV)
Si un disque a été retiré du VG, on peut réduire le Physical Volume avec pvresize :
# pvresize /dev/vde3
Suppression d’un stockage LVM
Supprimer le système de fichiers
Supprimer le système de fichiers avec unmount :
# umount /mnt/root/DATAS
Supprimer le Logical Volume (LV)
Supprimer le Logical Volume (LV) avec lvremove :
# lvremove /dev/vg01/lv01
Supprimer le Volume Group (VG)
Supprimer le Volume Group (VG) avec vgremove:
# vgremove vg01
Supprimer le Physical Volume (PV)
Supprimer le Physical Volume (PV) avec pvremove :
# pvremove /dev/vde1
/!\ Résumé des commandes
Commandes :
# fdisk /dev/vde # partprobe /dev/vde // Redémarrer la table de partitions
// -- CREATION --------------------------------------------------------------------------------------------------- # pvcreate /dev/vde1 // Créez un nouveau Physical Volume sur la nouvelle partition # vgcreate vg01 /dev/vde1 // Créez un Volume Group (VG) sur le Physical Volume # lvcreate -n lv01 -L 20G vg01 // Créez un Logical Volume (LV) dans le Volume Group # mkfs.ext4 /dev/vg01/lv01 // Formattez le **lv** avec mkfs # mkfs -t ext4 /dev/vg01/lv01 // # mkdir [-p] /mnt/root/DATAS // Créer un répertoire de montage # mount /dev/vg01/lv01 /mnt/root/DATAS // Formattez le **lv** avec mkfs # /dev/vg01/lv01 /mnt/root/DATAS xfs defaults 0 0 // ajouter une entrée dans /etc/fstab pour la persistance
// -- SWAP -------------------------------------------------------------------------------------------------------- # swapoff -v /dev/vg01/swap // Désactiver le swap sur le LV # lvextend -L +300M /dev/vg01/swap // Étendre le swap # lvextend -r /dev/vg01/lv0 -L 85G // Étendre le LV # mkswap /dev/vg01/swap // Formater le LV en tant qur swap # swapon /dev/vg01/swap // Activer le swap sur le LV
// -- ETENDRE LE STOCKAGE ----------------------------------------------------------------------------------------- # pvresize /dev/vde3 // Ajouter de l'espace (disque, partition, ou RAID) au Physical Volume (PV) # vgextend vg01 /dev/vde3 // Étendre le Volume Group (VG) # lvextend -L +500M /dev/vg01/lv01 // Étendre le Volume Logique (LV)
// -- REDUIRE LE STOCKAGE ----------------------------------------------------------------------------------------- # lvreduce -L <NewSize> /dev/vg01/lv01 // Réduire le Logical Volume (LV) avec ``lvreduce`` # pvmove /dev/vde3 // Réduire (si nécessaire) le VG pour libérerde l'espace non alloué # vgreduce vg01 /dev/vde3 // Réduire le Volume Group (VG) si vous libérez complètement un disque
// -- REDIMENSIONNER LE STOCKAGE ---------------------------------------------------------------------------------- # xfs_growfs /mnt/root/DATAS/ // XFS Redimensionner le système de fichiers # resize2fs /dev/mapper/vg01-lv01 // EXT4 Redimensionner le système de fichiers
// -- SUPPRIMER LE STOCKAGE --------------------------------------------------------------------------------------- # umount /mnt/root/DATAS // Démonter le système de fichiers # lvremove /dev/vg01/lv01 // Supprimer le Logical Volume (LV) # vgremove vg01 // Supprimer le Volume Group (VG) # pvremove /dev/vde1 // Supprimer le Physical Volume (PV)
Cas pratiques
Création et extension de volumes logiques
Avec fdisk, création de 2 partitions LVM de 2Go en GPT :
[root@SERVER01 ~]# fdisk /dev/sde [...]
Command (m for help): g Created a new GPT disklabel (GUID: AC472E3E-D3D9-8B4E-B028-6D58B9417627).
Command (m for help): n Partition number (1-128, default 1): 1 First sector (2048-10485726, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485726, default 10485726): +2G
Created a new partition 1 of type 'Linux filesystem' and of size 2 GiB.
Command (m for help): t Selected partition 1 Partition type or alias (type L to list all): Linux LVM Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): n Partition number (2-128, default 2): 2 First sector (4196352-10485726, default 4196352): Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-10485726, default 10485726): +2G
Created a new partition 2 of type 'Linux filesystem' and of size 2 GiB.
Command (m for help): t Partition number (1,2, default 2): 2 Partition type or alias (type L to list all): Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Changement du nom des partitions avec parted :
[root@SERVER01 ~]# parted /dev/sde GNU Parted 3.5 Using /dev/sde Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) name 1 part1 (parted) name 2 part2 (parted) quit
Vérification :
[root@SERVER01 ~]# parted /dev/sde print Model: VMware Virtual disk (scsi) Disk /dev/sde: 5369MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags:
Number Start End Size File system Name Flags 1 1049kB 2149MB 2147MB part1 lvm 2 2149MB 4296MB 2147MB part2 lvm
[root@SERVER01 ~]# mount /root/DATAS/ mount: (hint) your fstab has been modified, but systemd still uses the old version; use 'systemctl daemon-reload' to reload.
[root@SERVER01 ~]# systemctl daemon-reload
Vérification si /root/DATAS est bien monté et si on peut copier des fichier :
[root@SERVER01 ~]# cp -a /etc/*.conf /root/DATAS/
[root@SERVER01 ~]# ls /root/DATAS/ | wc -l 27
Informations sur le PV :
[root@SERVER01 ~]# pvdisplay /dev/sde1 --- Physical volume --- PV Name /dev/sde1 VG Name SERVER01_Group PV Size 2.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 511 Free PE 411 Allocated PE 100 PV UUID cgLKRq-doQc-5j1B-RF8G-Hhoq-rgND-G5VhzF
[root@SERVER01 ~]# pvdisplay /dev/sde2 --- Physical volume --- PV Name /dev/sde2 VG Name SERVER01_Group PV Size 2.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 511 Free PE 511 Allocated PE 0 PV UUID Ug28OZ-Wdkp-zjsa-f2PA-Fbmx-RQ8p-fQzfO7
Informations sur le VG :
[root@SERVER01 ~]# vgdisplay SERVER01_Group --- Volume group --- VG Name SERVER01_Group System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 100 / 400.00 MiB Free PE / Size 922 / 3.60 GiB VG UUID tv0veY-L3xE-c0hq-XD0p-MNnI-29QA-V0l88B
Informations sur le LV : .
[root@SERVER01 ~]# lvdisplay /dev/SERVER01_Group/SERVER01_Volume --- Logical volume --- LV Path /dev/SERVER01_Group/SERVER01_Volume LV Name SERVER01_Volume VG Name SERVER01_Group LV UUID rkQ1Oo-YjHK-zyDZ-2cjj-tzqa-LZTl-fvrMfe LV Write Access read/write LV Creation host, time Redhat1-Test3, 2024-10-05 18:53:33 +0200 LV Status available # open 1 LV Size 400.00 MiB Current LE 100 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
Taille totale du volume :
[root@SERVER01 ~]# df -h /root/DATAS/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/SERVER01_Group-SERVER01_Volume 336M 23M 314M 7% /root/DATAS
Création d’une 3ème partition de type Linux LVM appelée part3 (Pas besoin de recréer une “partition GPT” car ça reformate tout) :
[root@SERVER01 ~]# fdisk /dev/sde [...] Command (m for help): n Partition number (3-128, default 3): 3 Last sector, +/-sectors or +/-size{K,M,G,T,P} (8390656-10485726, default 10485726):
Created a new partition 3 of type 'Linux filesystem' and of size 1023 MiB.
Command (m for help): t Partition number (1-3, default 3): 3 Partition type or alias (type L to list all): Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): p Disk /dev/sde: 5 GiB, 5368709120 bytes, 10485760 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: AC472E3E-D3D9-8B4E-B028-6D58B9417627
Device Start End Sectors Size Type /dev/sde1 2048 4196351 4194304 2G Linux LVM /dev/sde2 4196352 8390655 4194304 2G Linux LVM /dev/sde3 8390656 10485726 2095071 1023M Linux LVM
Command (m for help): w The partition table has been altered. Syncing disks.
changement du nom de la 3ème partition :
[root@SERVER01 ~]# parted /dev/sde [...] (parted) name 3 part3
Enregistrement de la nouvelle partition dans noyau :
Étendre le VGSERVER01_Group avec le nouveau PV/dev/vde3 :
[root@SERVER01 ~]# vgextend SERVER01_Group /dev/sde3 Volume group "SERVER01_Group" successfully extended
Étendre le LVSERVER01_Volume existant à 200 Mio :
[root@SERVER01 ~]# lvextend -L 50M /dev/SERVER01_Group/SERVER01_Volume Rounding size to boundary between physical extents: 52.00 MiB. New size given (13 extents) not larger than existing size (100 extents)
Vérifier la nouvelle taille du système de fichiers :
[root@SERVER01 ~]# df -h /root/DATAS/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/SERVER01_Group-SERVER01_Volume 3.4G 46M 3.3G 2% /root/DATAS
[root@SERVER01 ~]# ls /root/DATAS | wc -l 27
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS [...] sde 8:64 0 5G 0 disk ├─sde1 8:65 0 2G 0 part │ └─SERVER01_Group-SERVER01_Volume 253:2 0 3.4G 0 lvm /root/DATAS ├─sde2 8:66 0 2G 0 part │ └─SERVER01_Group-SERVER01_Volume 253:2 0 3.4G 0 lvm /root/DATAS └─sde3 8:67 0 1023M 0 part └─SERVER01_Group-SERVER01_Volume 253:2 0 3.4G 0 lvm /root/DATAS
Gestion de la pile de stockage
NOTE : WARNING : Bordel sans nom et illogisme ci-dessous - Je vais refaire dans pas longtenps…
Avec fdisk, création de 3 partitions LVM de 2Go en GPT :
[root@SERVER02 ~]# fdisk /dev/sdf [...]
Command (m for help): g Created a new GPT disklabel (GUID: DE9C90FB-059B-BA44-A241-5D76884D1EC3).
Command (m for help): n Partition number (1-128, default 1): First sector (2048-10485726, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485726, default 10485726): +2G
Created a new partition 1 of type 'Linux filesystem' and of size 2 GiB.
Command (m for help): n Partition number (2-128, default 2): First sector (4196352-10485726, default 4196352): Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-10485726, default 10485726): +2G
Created a new partition 2 of type 'Linux filesystem' and of size 2 GiB.
Command (m for help): n Partition number (3-128, default 3): First sector (8390656-10485726, default 8390656): Last sector, +/-sectors or +/-size{K,M,G,T,P} (8390656-10485726, default 10485726): +500M
Created a new partition 3 of type 'Linux filesystem' and of size 500 MiB.
Command (m for help): t Partition number (1-3, default 3): 1 Partition type or alias (type L to list all): Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): t Partition number (1-3, default 3): 2 Partition type or alias (type L to list all): Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): t Partition number (1-3, default 3): 3 Partition type or alias (type L to list all): Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Changement du nom des partitions avec parted :
[root@SERVER02 ~]# parted /dev/sdf GNU Parted 3.5 Using /dev/sdf Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) name 1 part1 (parted) name 2 part2 (parted) name 3 part3 (parted) quit
Enregistrement des nouvelles partitions auprès de udev et du noyau :
Vérification que le LV nouvellement créé est monté avec la taille prévue :
[root@SERVER02 ~]# df -h /root/DATAS2 Filesystem Size Used Avail Use% Mounted on /dev/mapper/SERVER02_Group-SERVER02_Volume03 763M 19M 744M 3% /root/DATAS1
[root@SERVER02 ~]# df -h /root/DATAS2 Filesystem Size Used Avail Use% Mounted on /dev/mapper/SERVER02_Group-SERVER02_Volume02 123M 7.6M 116M 7% /root/DATAS2
Détails du LVSERVER02_Volume03 :
[root@SERVER02 ~]# lvdisplay /dev/SERVER02_Group/SERVER02_Volume03 --- Logical volume --- LV Path /dev/SERVER02_Group/SERVER02_Volume03 LV Name SERVER02_Volume03 VG Name SERVER02_Group LV UUID 1pY3DZ-fs1F-mptC-fL32-e8tG-PFBT-bs7LSJ LV Write Access read/write LV Creation host, time SERVER02.lab.example.com, 2022-05-05 14:40:51 -0400 LV Status available # open 1 LV Size 768.00 MiB Current LE 192 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
Détails du LVSERVER02_Volume02 :
[root@SERVER02 ~]# lvdisplay /dev/SERVER02_Group/SERVER02_Volume02 --- Logical volume --- LV Path /dev/SERVER02_Group/SERVER02_Volume02 LV Name SERVER02_Volume02 VG Name SERVER02_Group LV UUID 0aJIb6-Ti2b-jLCk-imB6-rkLx-mUoX-acjkz9 LV Write Access read/write LV Creation host, time SERVER02.lab.example.com, 2022-05-05 14:45:46 -0400 LV Status available # open 1 LV Size 128.00 MiB Current LE 32 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
Documentation
MAN fdisk(8) MAN gdisk(8) MAN lvm(8) MAN pvcreate(8) MAN vgcreate(8) MAN lvcreate(8) MAN mkfs(8) MAN pvdisplay(8) MAN vgdisplay(8) MAN lvdisplay(8) MAN vgextend(8) MAN lvextend(8) MAN xfs_growfs(8) MAN resize2fs(8) MAN swapoff(8) MAN mkswap(8) MAN swapon(8) MAN pvmove(8) MAN vgcfgbackup(8) MAN vgreduce(8) MAN lvremove(8) MAN vgremove(8) MAN pvremove(8)