Linux LVM
Finding the volumes wanted
Use the command lsblk to find what devices you want to use (if it isn't partitioned yet).
sdb 8:16 0 32G 0 disk sdc 8:32 0 32G 0 disk sdd 8:48 0 32G 0 disk sde 8:64 0 32G 0 disk sdf 8:80 0 32G 0 disk
In my case I'll use sdb, sdc, sdd, sde and sdf.
Formating the partition
To format the partitions I'll use the fdisk command, to use it you can do the following:
fdisk /dev/YOUR-DISK
(e.g.)
fdisk /dev/sdb
And I'll do the following for each disk.
Create a primary partition
To create a primary partition we can use the letter 'n' select 'p' to primary and everything else can be just enter, enter, enter.
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-67108863, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-67108863, default 67108863): Created a new partition 1 of type 'Linux' and of size 32 GiB.
Change the partition type to Linux LVM
Now to change the partition type to Linux LVM we click 't' and then list all partitions type with the command L, usually Linux LVM can be configured using 'Linux LVM', '8e' or '44'. In my case I wrote Linux LVM
Command (m for help): t Selected partition 1 Hex code or alias (type L to list all): Linux LVM Changed type of partition 'Linux' to 'Linux LVM'.
Save and exit
To save our work and exit the fdisk we click 'w' and it's done.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Now we have the /dev/sdb formated let's just reply the same to all other drives wanted.
This is what looks like formating a disk to be an LVM:
# fdisk /dev/sdd Welcome to fdisk (util-linux 2.39.3). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS (MBR) disklabel with disk identifier 0x51929328. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-67108863, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-67108863, default 67108863): Created a new partition 1 of type 'Linux' and of size 32 GiB. Command (m for help): t Selected partition 1 Hex code or alias (type L to list all): Linux LVM Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Creating the PV (Physical volume)
Now we have all partitions created we need to create all PVs, this is just simple, use the command pvcreate and the partition created like that:
pvcreate /dev/sdb1
Then the output should be like
Physical volume "/dev/sdb1" successfully created.
We can check using the command pvs, like:
# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- <32.00g <32.00g /dev/sdc1 myvg lvm2 a-- <32.00g <32.00g /dev/sdd1 myvg lvm2 a-- <32.00g <32.00g /dev/sde1 myvg lvm2 a-- <32.00g <32.00g /dev/sdf1 myvg lvm2 a-- <32.00g <32.00g
Creating the VG (Volume group)
Now that all our PVs is created we can create a volume group with the command vgcreate and pass all our PVs to the VG
vgcreate VG_NAME pv1 pv2 ...
(e.g.)
vgcreate myvg /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
And we should see a message like that
Volume group "myvg" successfully created
We can check using the command vgs, like:
# vgs VG #PV #LV #SN Attr VSize VFree myvg 5 0 0 wz--n- 159.98g 159.98g
Adding PVs to the VG
To add a new PV on the VG we can use the command vgextend passing the vg name and the pv wanted, like:
vgextend VG_NAME pv3
(e.g.)
vgextend myvg /dev/sdf1
And we should have a response like that:
Volume group "myvg" successfully extended
Removing PVs from the VG
To remove a pv from a vg we can use the command vgreduce passing the vg name and the pv wanted, like:
vgreduce myvg pv3
(e.g.)
vgreduce myvg /dev/sdf1
And we should have a response like that:
Removed "/dev/sdf1" from volume group "myvg"
Updating changes
After any change run the command partprobe just to update all configurations. Shouldn't get any response from the shell.
Verifying the VG created
To check all informations about our VG we can use the command vgdisplay, like:
# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 5 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 5 Act PV 5 VG Size 159.98 GiB PE Size 4.00 MiB Total PE 40955 Alloc PE / Size 0 / 0 Free PE / Size 40955 / 159.98 GiB VG UUID 2yqVka-ysy3-9Adj-lc6v-3P0f-8a3w-dDb0IH
Creating the LV
To create the LV we can choose use the size or the PE units.
To create using the PE we can use the command:
lvcreate -L SIZE -n LV_NAME VG_NAME
(e.g.)
lvcreate -L 150G -n mylv myvg
And we should have a response like that:
Logical volume "mylv" created.
We can check using the command lvs, like:
# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 150.00g
Adding volume to the lv
First add check if you have volume avail on the vg, if yes you can do in 2 ways percentage or size:
Percentage:
lvextend -l X%FREE /dev/myvg/mylv
(e.g.)
lvextend -l 100%FREE /dev/myvg/mylv
That way all the avail volume on the vg will be added to the LV.
Fixed size:
lvextend -L +NG /dev/myvg/mylv
(e.g.)
lvextend -L +1G /dev/myvg/mylv
That way the lv will extend in 1G.
Resize
After these changes always run
resize2fs /dev/myvg/mylv
Than the values will be formatted and the lv will assume the actual total volume.
Formating the LV and mounting on the OS
To create a file system on the LV we can use the command mkfs.ext4 or mkfs.xfs or other types of FS, like
mkfs.ext4 /dev/VG_NAME/LV_NAME
(e.g.)
# mkfs.ext4 /dev/myvg/mylv
We should get something like that from the shell:
mke2fs 1.47.0 (5-Feb-2023) Discarding device blocks: done Creating filesystem with 39321600 4k blocks and 9830400 inodes Filesystem UUID: 0d1418be-f4f7-4ea9-a309-245c618c9d5a Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
To mount this FS on the OS we can use the command mount
mount /dev/VG_NAME/LV_NAME OS_MOUNTING_POINT
(e.g.)
mount /dev/myvg/mylv /mnt
With the command df -h we can check the mounting points, like that:
Filesystem Size Used Avail Use% Mounted on /dev/mapper/nfs-nfs 147G 28K 140G 1% /mnt
Auto mounting the LV on the OS on boot
We can do it in two ways, just write the mount command on the /etc/rc.local and the mount command will be executed on boot (link) or we can use the /etc/fstab to mount on boot (link).