Linux LVM logical volume management

Posted by cdinca on Tue, 08 Mar 2022 04:13:31 +0100

Introduction to LVM

LVM: Logical Volume Manager, Chinese name for logical volume management, is a convenient disk partition management mechanism. The difference from general disk partition management is that LVM can easily resize each partition without downtime. At the same time, you can also integrate multiple small physical disks into a large logical partition, so as to break through the size limit of physical disks and store larger data blocks.
There are three concepts in logical volume management: physical volume, volume group, logical volume and physical expansion block.
Physical volume (PV): a disk or disk partition that contains LVM related management parameters.
Volume group (VG): a collection of physical volumes.
Logical volume (LV): a logical disk composed of space extracted from a volume group.
Physical expansion block (PE): it is the smallest storage unit in a logical volume. The default size is 4MB.

Principle introduction

LVM logical volume creation and implementation process

flow chart

code implementation

Partition sdc

Specific partition steps can be queried
Hard disk partition

It should be noted that after the partition is completed, the partition type needs to be changed to 8e (Linux LVM)

Click to view the code
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
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.

Create physical volume (pvcreate)

Divide sdb and sdc1 into physical volumes. Command format:

pvcreate [disk or partition directory] [disk or partition directory]

Click to view the code
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc1
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc1" successfully created.

Create volume group ts01(vgcreate)

Divide sdb and sdc1 into volume group ts01. Command format:

vgcreate volume group name [physical volume] [physical volume]

Click to view the code
[root@localhost ~]# vgcreate ts01 /dev/sdb /dev/sdc1
 Volume group "ts01" successfully created

Create logical volume ls01

Divide 7G space from volume group ts01 and create it as logical volume ls01. Command format:

lvcreate -L [partition size] - n [volume group name] [logical volume name]

Click to view the code
[root@localhost ~]# lvcreate -L 7G -n lv01 ts01
  Logical volume "lv01" created.

Mount format uses logical volumes

Create the data directory, format the logical volume lv01 as an xfs file system, and mount it in the data directory.

Click to view the code
[root@localhost ~]# mkdir /data
[root@localhost ~]# mkfs.xfs /dev/ts01/lv01
meta-data=/dev/ts01/lv01         isize=512    agcount=4, agsize=458752 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1835008, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/ts01/lv01 /data

View the mount effect.

Click to view the code
[root@localhost ~]# df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   42G  3.7G   39G   9% /
devtmpfs                 470M     0  470M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  8.6M  478M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  166M  849M  17% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   32K   98M   1% /run/user/0
/dev/sr0                 4.3G  4.3G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/ts01-lv01    7.0G   33M  7.0G   1% /data

It should be noted that this is a temporary mount. The mount fails after restart. The mount information needs to be written to / etc/fstab for automatic mount.

Auto Mount

Extended logical volume

To expand a logical volume, first ensure that the remaining space in the volume group is sufficient. If it is insufficient, first add a new physical volume to the volume group to expand the logical volume. Expand logical volume command format:

lvextend -L + [extended size] [absolute path of logical volume]

After capacity expansion, you need to refresh the mount point before the new size appears. Command format:

xfs_growfs [mount point]

Click to view the code
[root@localhost ~]# lvextend -L +1G /dev/ts01/lv01
  Size of logical volume ts01/lv01 changed from 7.00 GiB (1792 extents) to 8.00 GiB (2048 extents).
  Logical volume ts01/lv01 successfully resized.
[root@localhost ~]# xfs_growfs /data
meta-data=/dev/mapper/ts01-lv01  isize=512    agcount=4, agsize=458752 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1835008, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1835008 to 2097152
[root@localhost ~]# df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   42G  3.7G   39G   9% /
devtmpfs                 470M     0  470M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  8.6M  478M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  166M  849M  17% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   32K   98M   1% /run/user/0
/dev/sr0                 4.3G  4.3G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/ts01-lv01    8.0G   33M  8.0G   1% /data

Topics: cloud computing