硬盘分好区或者部署为 RAID 磁盘阵列之后,再想修改硬盘分区大小就不 容易了

逻辑卷管理器是 Linux 系统用于对硬盘分区进行管理的一种机制,理论性较强,其创建 初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。尽管对传统的硬盘分区进 行强制扩容或缩容从理论上来讲是可行的,但是却可能造成数据的丢失。而 LVM 技术是在硬 盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行 卷组合并。这样一来,用户不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分 区的动态调整。

物理卷处于 LVM 中的最底层,可以将其理解为物理硬盘、硬盘分区或者 RAID 磁盘阵 列,这都可以。卷组建立在物理卷之上,一个卷组可以包含多个物理卷,而且在卷组创建之后 也可以继续向其中添加新的物理卷。逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建 立后可以动态地扩展或缩小空间。这就是 LVM 的核心理念。

 

让新添加的两块硬盘设备支持 LVM 技术

[root@Node1 ~]# ls /sys/class/scsi_host/          //确定主机总线SCSI总线号    
host0  host1  host2
[root@Node1 ~]#  echo "- - -" > /sys/class/scsi_host/host0/scan   //重新扫描 SCSI总线添加磁盘
[root@Node1 ~]#  echo "- - -" > /sys/class/scsi_host/host1/scan
[root@Node1 ~]#  echo "- - -" > /sys/class/scsi_host/host2/scan
[root@Node1 ~]# fdisk -l | grep "^Disk /dev"     //检查硬盘是否在线
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 sectors

 

让新添加的两块硬盘设备支持 LVM 技术

[root@ccc ~]# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
[root@ccc ~]# vgcreate storage /dev/sdb /dev/sdc         #添加storage卷组
Volume group "storage" successfully created

 

[root@ccc ~]# vgdisplay            #查看新添加的卷组情况
  --- Volume group ---
  VG Name               storage
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               39.99 GiB
  PE Size               4.00 MiB
  Total PE              10238
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10238 / 39.99 GiB
  VG UUID               5fmYm8-vgOw-BqWB-7KJA-VozO-CwSy-CV3aYe
   
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               CVLGL0-AlCR-uXLk-EYy3-rmPB-GnQ0-KPWdwz

切割出一个约为 150MB 的逻辑卷设备

在对逻辑卷进行切割时有两种计量单位。第一种是以容 量为单位,所使用的参数为-L。例如,使用-L 150M 生成一个大小为 150MB 的逻辑卷。另外 一种是以基本单元的个数为单位,所使用的参数为-l。每个基本单元的大小默认为 4MB。例 如,使用-l 37 可以生成一个大小为 37×4MB=148MB 的逻辑卷

lvcreate -n vo -l 37 storage
lvdisplay

把生成好的逻辑卷进行格式化,然后挂载使用

mkfs.ext4 /dev/storage/vo

创建挂载目录,进行挂载

mkdir /linuxprobe
mount /dev/storage/vo /linuxprobe

查看挂载状态,并写入到配置文件,使其永久生效

df -h

echo "/dev/storage/vo /linuxprobe ext4 defaults 0 0" >> /
etc/fstab

 

 

扩容逻辑卷

用户在使用存储设备时感 知不到设备底层的架构和布局,更不用关心底层是由多少块硬盘组成的,只要卷组中 有足够的资源,就可以一直为逻辑卷扩容。扩展前请一定要记得卸载设备和挂载点的 关联。

[root@ccc ~]# umount /linuxprobe                          #卸载设备
[root@ccc ~]# lvextend -L 290M /dev/storage/vo             #扩容到290M
  Rounding size to boundary between physical extents: 292.00 MiB.
  Size of logical volume storage/vo changed from 148.00 MiB (37 extents) to 292.00 MiB (73 extents).
  Logical volume storage/vo successfully resized.
[root@ccc ~]# e2fsck -f /dev/storage/vo                     #检查硬盘完整性
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/vo: 11/38000 files (0.0% non-contiguous), 10453/151552 blocks
[root@ccc ~]# resize2fs /dev/storage/vo                        #重置硬盘容量
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/vo to 299008 (1k) blocks.
The filesystem on /dev/storage/vo is now 299008 blocks long.

[root@ccc ~]# mount -a                                       #重新挂载
[root@ccc ~]# df -h                                           #查看验证
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  3.8G   14G  23% /
/dev/sda1               1014M  138M  877M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/storage-vo   279M  2.1M  259M   1% /linuxprobe
[root@ccc ~]# 

 

缩小逻辑卷

相较于扩容逻辑卷,在对逻辑卷进行缩容操作时,其丢失数据的风险更大。所以在生产 环境中执行相应操作时,一定要提前备份好数据。另外 Linux 系统规定,在对 LVM 逻辑卷进 行缩容操作之前,要先检查文件系统的完整性(当然这也是为了保证我们的数据安全)。在执 行缩容操作前记得先把文件系统卸载掉。

[root@ccc ~]# umount /linuxprobe                       #卸载挂载点
[root@ccc ~]# e2fsck -f /dev/storage/vo                #检查数据完整性
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/vo: 11/74000 files (0.0% non-contiguous), 15507/299008 blocks
[root@ccc ~]# resize2fs /dev/storage/vo 120M                       #缩容到12M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/vo to 122880 (1k) blocks.
The filesystem on /dev/storage/vo is now 122880 blocks long.

[root@ccc ~]# lvreduce -L 120M /dev/storage/vo                    #重置逻辑卷容量
  WARNING: Reducing active logical volume to 120.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce storage/vo? [y/n]: y
  Size of logical volume storage/vo changed from 292.00 MiB (73 extents) to 120.00 MiB (30 extents).
  Logical volume storage/vo successfully resized.
[root@ccc ~]# mount -a                                        #重新挂载
[root@ccc ~]# df -h                                           #验证
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  3.8G   14G  23% /
/dev/sda1               1014M  138M  877M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/storage-vo   113M  1.6M  103M   2% /linuxprobe
[root@ccc ~]# 

 

 

逻辑卷快照

LVM 还具备有“快照卷”功能,该功能类似于虚拟机软件的还原时间点功能。例如,可 以对某一个逻辑卷设备做一次快照,如果日后发现数据被改错了,就可以利用之前做好的快 照卷进行覆盖还原。LVM 的快照卷功能有两个特点: ➢ 快照卷的容量必须等同于逻辑卷的容量; ➢ 快照卷仅一次有效,一旦执行还原操作后则会被立即自动删除。

[root@ccc ~]# vgdisplay          #查看逻辑卷信息
  --- Volume group ---
  VG Name               storage
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  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               39.99 GiB
  PE Size               4.00 MiB
  Total PE              10238 
  Alloc PE / Size       30 / 120.00 MiB             #使用了120M逻辑卷
  Free  PE / Size       10208 / <39.88 GiB          #剩余磁盘空间
  VG UUID               5fmYm8-vgOw-BqWB-7KJA-VozO-CwSy-CV3aYe
   
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               CVLGL0-AlCR-uXLk-EYy3-rmPB-GnQ0-KPWdwz
   
[root@ccc ~]# clear
[root@ccc ~]# echo "Welcome to Linuxprobe.com" > /linuxprobe/readme.txt       #在逻辑卷挂载目录下创建一个文件
[root@ccc ~]# ls -l /linuxprobe/readme.txt 
-rw-r--r--. 1 root root 26 Sep 27 15:00 /linuxprobe/readme.txt
[root@ccc ~]# ls -l /linuxprobe
total 14
drwx------. 2 root root 12288 Sep 27 14:19 lost+found
-rw-r--r--. 1 root root    26 Sep 27 15:00 readme.txt
[root@ccc ~]# lvcreate -L 120M -s -n SNAP /dev/storage/vo                      #创建一个快照SNAP,大小120M,还需要在命 令后面写上是针对哪个逻辑卷执行的快照操作
  Logical volume "SNAP" created.
[root@ccc ~]# lvdisplay                                                        #查看
  --- Logical volume ---
  LV Path                /dev/storage/vo
  LV Name                vo
  VG Name                storage
  LV UUID                X7HzFw-x1B6-st30-lcJt-Xuag-mSi1-R62cnm
  LV Write Access        read/write
  LV Creation host, time ccc.liutao.com, 2021-09-27 14:08:15 +0800
  LV snapshot status     source of
                         SNAP [active]        #快照状态
  LV Status              available
  # open                 1
  LV Size                120.00 MiB         #快照卷大小
  Current LE             30
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/storage/SNAP
  LV Name                SNAP
  VG Name                storage
  LV UUID                Sn1FWG-FCLn-J97O-rU1w-1XHC-SGyG-HX51vf
  LV Write Access        read/write
  LV Creation host, time ccc.liutao.com, 2021-09-27 15:01:44 +0800
  LV snapshot status     active destination for vo          #vo逻辑卷的快照
  LV Status              available
  # open                 0
  LV Size                120.00 MiB
  Current LE             30
  COW-table size         120.00 MiB
  COW-table LE           30
  Allocated to snapshot  0.01%                              #占用情况
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:5
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Wpvm1Z-1ysn-T0B7-EeE8-qyTL-J33e-lCCYy7
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                1seXT8-3rrk-eFGz-iTcR-oXmq-1pE2-5pbewa
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@ccc ~]# dd if=/dev/zero of=/linuxprobe/files count=1 bs=100M              #在逻辑卷所挂载的目录中创建一个 100MB 的垃圾文件,然后再查看快照卷的状 态。可以发现存储空间占的用量上升了
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 2.77815 s, 37.7 MB/s
[root@ccc ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/storage/vo
  LV Name                vo
  VG Name                storage
  LV UUID                X7HzFw-x1B6-st30-lcJt-Xuag-mSi1-R62cnm
  LV Write Access        read/write
  LV Creation host, time ccc.liutao.com, 2021-09-27 14:08:15 +0800
  LV snapshot status     source of
                         SNAP [active]
  LV Status              available
  # open                 1
  LV Size                120.00 MiB
  Current LE             30
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/storage/SNAP
  LV Name                SNAP
  VG Name                storage
  LV UUID                Sn1FWG-FCLn-J97O-rU1w-1XHC-SGyG-HX51vf
  LV Write Access        read/write
  LV Creation host, time ccc.liutao.com, 2021-09-27 15:01:44 +0800
  LV snapshot status     active destination for vo
  LV Status              available
  # open                 0
  LV Size                120.00 MiB
  Current LE             30
  COW-table size         120.00 MiB
  COW-table LE           30
  Allocated to snapshot  76.91%                         #占用情况
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:5
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Wpvm1Z-1ysn-T0B7-EeE8-qyTL-J33e-lCCYy7
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                1seXT8-3rrk-eFGz-iTcR-oXmq-1pE2-5pbewa
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@ccc ~]# 

 

为了校验 SNAP 快照卷的效果,需要对逻辑卷进行快照还原操作。在此之前记 得先卸载掉逻辑卷设备与目录的挂载。

[root@ccc ~]# umount /linuxprobe/
[root@ccc ~]# lvconvert --merge /dev/storage/SNAP
  Merging of volume storage/SNAP started.
  storage/vo: Merged: 100.00%
[root@ccc ~]# mount -a
[root@ccc ~]# ls /linuxprobe/
lost+found  readme.txt
[root@ccc ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/storage/vo
  LV Name                vo
  VG Name                storage
  LV UUID                X7HzFw-x1B6-st30-lcJt-Xuag-mSi1-R62cnm
  LV Write Access        read/write
  LV Creation host, time ccc.liutao.com, 2021-09-27 14:08:15 +0800
  LV Status              available
  # open                 1
  LV Size                120.00 MiB
  Current LE             30
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Wpvm1Z-1ysn-T0B7-EeE8-qyTL-J33e-lCCYy7
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                1seXT8-3rrk-eFGz-iTcR-oXmq-1pE2-5pbewa
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-09-10 11:55:27 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   

删除快照后,创建的100M垃圾也没有了

 

 

删除逻辑卷

当生产环境中想要重新部署 LVM 或者不再需要使用 LVM 时,则需要执行 LVM 的删除 操作。为此,需要提前备份好重要的数据信息,然后依次删除逻辑卷、卷组、物理卷设备,这 个顺序不可颠倒

取消逻辑卷与目录的挂载关联,删除配置文件中永久生效的设备参数

[root@ccc ~]# umount /linuxprobe/
[root@ccc ~]# vim /etc/fstab 
[root@ccc ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Sep 10 11:55:28 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=feb40a38-e230-4cce-9038-2c53eaa707dc /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
#/dev/storage/vo /linuxprobe ext4 defaults 0 0                                          #删除永久生效设备参数
[root@ccc ~]# lvremove /dev/storage/vo                                                     #删除逻辑卷
Do you really want to remove active logical volume storage/vo? [y/n]: y
  Logical volume "vo" successfully removed
[root@ccc ~]# vgremove storage                                                          #删除卷组
  Volume group "storage" successfully removed
[root@ccc ~]# pvremove /dev/sdb /dev/sdc                                               #删除物理卷
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
[root@ccc ~]#