一、LVM介绍

  管理磁盘、使用磁盘的一种方式的称呼

  优势:

    1、在不影响数据的情况下, 扩容、缩容

    2、支持快照功能, 方便数据备份

  LVM工作流程:

    磁盘/分区 ---> pv(物理卷) ----> vg(卷组) -----> lv(逻辑卷) ----> 格式化/挂载使用

  注意:

    磁盘/分区,要求是裸设备(无文件系统的设备)

    建议规范做法将分区的ID修改8e

二、创建逻辑卷
  创建一个逻辑卷/dev/data/web大小为 5G 文件系统 ext4文件系统 挂在在/data/web下

  关闭虚拟机添加至少三块硬盘,然后重新启动虚拟机

  1.创建物理卷

    首先查看我们添加的网卡名称,分别是sdb,sdc,sdd,sde四块硬盘。

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
sdc           8:32   0   20G  0 disk 
sdd           8:48   0   20G  0 disk 
sde           8:64   0   20G  0 disk 
sdf           8:80   0   20G  0 disk 
sr0          11:0    1  4.1G  0 rom  

  创建物理卷并查看

[root@localhost ~]# pvscan 
  PV /dev/sda2   VG cl              lvm2 [19.00 GiB / 0    free]
  Total: 1 [19.00 GiB] / in use: 1 [19.00 GiB] / in no VG: 0 [0   ]
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# pvscan 
  PV /dev/sda2   VG cl              lvm2 [19.00 GiB / 0    free]
  PV /dev/sdb                       lvm2 [20.00 GiB]
  PV /dev/sdc                       lvm2 [20.00 GiB]
  Total: 3 [59.00 GiB] / in use: 1 [19.00 GiB] / in no VG: 2 [40.00 GiB]

  2.创建卷组data

[root@localhost ~]# vgcreate data /dev/sdb /dev/sdc
  Volume group "data" successfully created

[root@localhost ~]# vgdisplay data
  --- Volume group ---
  VG Name               data
  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               fnBs7P-WSXx-BuZE-0qic-peDx-i5QI-iX0RJi

  3.创建逻辑卷web

[root@localhost ~]# lvcreate -L 5G -n web data
  Logical volume "web" created.
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [5.00 GiB] inherit

  4.创建文件系统ext4 挂载使用

[root@localhost ~]# mkfs -t ext4 /dev/data/web
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# tail -1  /etc/fstab 
/dev/data/web    /data/web    ext4    defaults    0 0
[root@localhost data]# mkdir  -p /data/web
[root@localhost data]# mount -a
[root@localhost data]# df -hT | grep "data"
/dev/mapper/data-web ext4      4.8G   20M  4.6G    1% /data/web
[root@localhost ~]# touch /data/web/{1..10}.html
[root@localhost ~]# ls /data/web/
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  lost+found
[root@localhost ~]# ls -l /dev/data/web 
lrwxrwxrwx. 1 root root 7 12月 26 09:39 /dev/data/web -> ../dm-2
[root@localhost ~]# ls -l /dev/dm-2 
brw-rw----. 1 root disk 253, 2 12月 26 09:39 /dev/dm-2
[root@localhost ~]# ls -l /dev/mapper/data-web 
lrwxrwxrwx. 1 root root 7 12月 26 09:39 /dev/mapper/data-web -> ../dm-2

三、以指定的PE数量方式创建逻辑卷

  创建逻辑卷/dev/data/nginx, 容量800M, xfs文件系统    /data/nginx

[root@localhost ~]# lvcreate -l 200 -n nginx data
  Logical volume "nginx" created.
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [5.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [800.00 MiB] inherit
[root@localhost ~]# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.
[root@localhost ~]# vgcreate -s 16 vg01 /dev/sdd
  Volume group "vg01" successfully created
[root@localhost ~]# vgdisplay vg01
  --- Volume group ---
  VG Name               vg01
  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               19.98 GiB
  PE Size               16.00 MiB
  Total PE              1279
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1279 / 19.98 GiB
  VG UUID               Uhqfbe-d76p-c3Fp-VNVj-EFWj-e2th-s7vwym

四、逻辑卷扩展(第一种方式)

  流程:
      1、扩展逻辑卷的物理边界大小
      2、扩展逻辑卷的文件系统大小
              resize2fs    扩展ext文件系统
              xfs_growfs    扩展xfs文件系统

  扩展逻辑卷/dev/data/web容量到25GB

    1) 确认卷组有足够的剩余空间

[root@localhost ~]# vgdisplay data | grep -i "free"
  Free  PE / Size       8758 / 34.21 GiB

    2) 扩展物理边界 

[root@localhost ~]# lvextend -L +20G /dev/data/web 
  Size of logical volume data/web changed from 5.00 GiB (1280 extents) to 25
.00 GiB (6400 extents).  Logical volume data/web successfully resized.
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [25.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [800.00 MiB] inherit

    3) 扩展文件系统

[root@localhost ~]# resize
resize2fs   resizecons  resizepart  
[root@localhost ~]# resize2fs /dev/data/web 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/data/web is mounted on /data/web; on-line resizing requir
edold_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/data/web is now 6553600 blocks long.

[root@localhost ~]# df -hT | grep "web"
/dev/mapper/data-web ext4       25G   28M   24G    1% /data/web

  第二种方式

    为逻辑卷/dev/data/nginx扩展30GB空间  【xfs文件系统】

    1) 确认卷组的剩余空间

[root@localhost ~]# vgdisplay data | grep -i "free"
  Free  PE / Size       3638 / 14.21 GiB

    2) 扩展卷组data

[root@localhost ~]# pvcreate /dev/sdf
  Physical volume "/dev/sdf" successfully created.
[root@localhost ~]# vgextend data /dev/sdf
  Volume group "data" successfully extended
[root@localhost ~]# vgdisplay data | grep -i "free"
  Free  PE / Size       8757 / 34.21 GiB

    3) 扩展逻辑卷物理边界 

[root@localhost ~]# lvextend -L +30G /dev/data/nginx 
  Size of logical volume data/nginx changed from 800.00 MiB (200 extents) to
 30.78 GiB (7880 extents).  Logical volume data/nginx successfully resized.
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [25.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [30.78 GiB] inherit

    4) 扩展文件系统

[root@localhost ~]# tail -1 /etc/fstab 
/dev/data/nginx    /data/nginx    xfs    defaults    0 0
[root@localhost ~]# mkfs -t xfs /dev/data/nginx
meta-data=/dev/data/nginx        isize=512    agcount=4, agsize=2017280 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=8069120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=3940, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# df -hT
文件系统             类型      容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root  xfs        17G  1.7G   16G   10% /
devtmpfs             devtmpfs  902M     0  902M    0% /dev
tmpfs                tmpfs     912M     0  912M    0% /dev/shm
tmpfs                tmpfs     912M  8.7M  904M    1% /run
tmpfs                tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sda1            xfs      1014M  139M  876M   14% /boot
tmpfs                tmpfs     183M     0  183M    0% /run/user/0
/dev/mapper/data-web ext4       25G   28M   24G    1% /data/web
[root@localhost ~]# tail -1 /etc/fstab
/dev/data/nginx    /data/nginx    xfs    defaults    0 0

五、逻辑卷缩减

  流程:
      1、先缩减文件系统
      2、再缩减物理边界
    
  注意事项:
      1、逻辑卷要处于卸载状态  
      2、建议备份数据
      3、xfs文件系统不支持缩减
      4、缩减后的空间要可以保存之前数据

  缩减逻辑卷/dev/data/web的空间到10G

    1) 备份数据

[root@localhost data]# ls /tmp/
systemd-private-4c0fe213859943b998b92d994362d0c4-vmtoolsd.service-RGsyh9
web_2020_12_26.tar.gz
web_.tar.gz

    2) 卸载逻辑卷

[root@localhost ~]# umount /dev/data/web

    3) 检测逻辑卷文件系统是否有损坏

[root@localhost ~]# fsck -f /dev/data/web
fsck,来自 util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/data-web: 21/1638400 files (0.0% non-contiguous), 142625/6553600
 blocks

    4) 缩减文件系统到10GB

[root@localhost ~]# resize2fs -f /dev/data/web 10G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/data/web to 2621440 (4k) blocks.
The filesystem on /dev/data/web is now 2621440 blocks long.

    5) 缩减物理边界

[root@localhost ~]# lvreduce -L-5GB /dev/data/web 
  WARNING: Reducing active logical volume to 20.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data/web? [y/n]: y
  Size of logical volume data/web changed from 25.00 GiB (6400 extents) to 2
0.00 GiB (5120 extents).  Logical volume data/web successfully resized.
[root@localhost ~]# lvscan
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [20.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [30.78 GiB] inherit

    6) 重新挂载逻辑卷, 验证可正常查看数据

[root@localhost ~]# mount /dev/data/web /data/web/
[root@localhost ~]# ls /data/web/
10.html  2.html  4.html  6.html  8.html  lost+found
1.html   3.html  5.html  7.html  9.html

六、逻辑卷快照

  作用:结合特定应用程序, 方便备份数据

  基于cow(copy on write 写时复制)机制

    创建快照时,快照中只保存数据的指针信息(元数据信息), 只有当原逻辑卷数据发    生变化时,才会将真实的数据复制到快照中来

  基于快照备份数据

    1) 创建快照

[root@localhost ~]# lvcreate -s -p r -n web_snap -L 500M /dev/data/web
  Using default stripesize 64.00 KiB.
  Logical volume "web_snap" created.
[root@localhost ~]# lvscan
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE   Original '/dev/data/web' [20.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [30.78 GiB] inherit
  ACTIVE   Snapshot '/dev/data/web_snap' [500.00 MiB] inherit  

    2) 挂载快照,备份数据 

[root@localhost ~]# mount /dev/data/web_snap /mnt/
mount: /dev/mapper/data-web_snap 写保护,将以只读方式挂载
[root@localhost ~]# tar czf /tmp/web_$(date +%Y_%m_%d).tar.gz /mnt/

    3) 卸载、删除快照

[root@localhost ~]# umount /dev/data/web_snap
[root@localhost ~]# lvremove /dev/data/web_snap 
Do you really want to remove active logical volume data/web_snap? [y/n]: y
  Logical volume "web_snap" successfully removed
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [17.00 GiB] inherit
  ACTIVE            '/dev/data/web' [20.00 GiB] inherit
  ACTIVE            '/dev/data/nginx' [30.78 GiB] inherit

 

posted on 2020-12-26 10:51  Lyon-w  阅读(208)  评论(0编辑  收藏  举报