ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux下手把手教你使用LVM

2021-11-05 11:31:39  阅读:172  来源: 互联网

标签:shuaige 手把手 haha 1.9 dev Linux LVM xiudaochengxian root


LVM磁盘阵列技术

· 什么是LVM

LVM(Logical Volume Manager,逻辑卷管理器),动态调整分区大小

LVM是Linux系统用于对硬盘分区进行管理的一种机制,理论性较强

LVM技术是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整

逻辑卷管理器的技术结构

在这里插入图片描述

逻辑卷管理器使用流程图

在这里插入图片描述
在这里插入图片描述

· 相关LVM命令

常用的LVM部署命令
在这里插入图片描述
在这里插入图片描述

· LVM部署

部署逻辑卷

添加两块硬盘设备

在这里插入图片描述
1.让新添加的两块硬盘设备支持LVM

[root@xiudaochengxian ~]# ls -l /dev/sd*			#查看硬盘设备
brw-rw----. 1 root disk 8,  0 Nov  1 16:34 /dev/sda
brw-rw----. 1 root disk 8,  1 Nov  1 16:34 /dev/sda1
brw-rw----. 1 root disk 8,  2 Nov  1 16:34 /dev/sda2
brw-rw----. 1 root disk 8,  3 Nov  1 16:34 /dev/sda3
brw-rw----. 1 root disk 8, 16 Nov  1 16:34 /dev/sdb
brw-rw----. 1 root disk 8, 32 Nov  1 16:34 /dev/sdc
[root@xiudaochengxian ~]# pvcreate /dev/sdb				#使新硬盘支持LVM
  Physical volume "/dev/sdb" successfully created.
[root@xiudaochengxian ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.

2.将创建的两块硬盘设备加入卷组里

[root@xiudaochengxian ~]# vgcreate shuaige /dev/sdb /dev/sdc		#将两块硬盘加入卷组(shuaige)里
  Volume group "shuaige" successfully created

3.创建逻辑卷

[root@xiudaochengxian ~]# lvcreate -n haha -l 100 shuaige		#切割出一个约为400MB的逻辑卷设备
  Logical volume "haha" created.
[root@xiudaochengxian ~]# lvdisplay 		#查看状态
  --- Logical volume ---
  LV Path                /dev/shuaige/haha
  LV Name                haha
  VG Name                shuaige
  LV UUID                SKbcGd-DcP1-WJB9-pgQt-mt9e-ORgc-ZeA7xs
  LV Write Access        read/write
  LV Creation host, time xiudaochengxian, 2021-11-01 16:42:57 +0800
  LV Status              available
  # open                 0
  LV Size                400.00 MiB
  Current LE             100
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

4.格式化生成好的逻辑卷,然后挂载使用

Linux系统会把LVM中的逻辑卷设备存放在/dev设备目录中(实际上就是个快捷方式),同时会以卷组的名称来建立一个目录,其中保存了逻辑卷的设备映射文件(即/dev/卷组名称/逻辑卷名称)
例:/dev/shuaige/haha

[root@xiudaochengxian ~]# mkfs.ext4 /dev/shuaige/haha 		#格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@xiudaochengxian ~]# mkdir /haha		#创建目录
[root@xiudaochengxian ~]# vim /etc/fstab 		#写入配置文件,永久生效
 13 /dev/shuaige/haha /haha ext4 defaults 0 0
 
[root@xiudaochengxian ~]# mount -a			#挂载
[root@xiudaochengxian ~]# df -h				#查看挂载情况
Filesystem                Size  Used Avail Use% Mounted on
/dev/sda3                  17G  3.5G   14G  21% /
devtmpfs                  1.9G     0  1.9G   0% /dev
tmpfs                     1.9G     0  1.9G   0% /dev/shm
tmpfs                     1.9G   13M  1.9G   1% /run
tmpfs                     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sr0                  4.2G  4.2G     0 100% /media/cdrom
/dev/sda1                1014M  165M  850M  17% /boot
tmpfs                     378M  4.0K  378M   1% /run/user/42
tmpfs                     378M   20K  378M   1% /run/user/0
/dev/mapper/shuaige-haha  380M  2.3M  354M   1% /haha                                    

注: 如果使用了逻辑卷管理器,则不建议用XFS文件系统
因为XFS文件系统自身就可以使用xfs_growfs命令进行磁盘扩容
在实测阶段发现,在有一些服务器上,XFS与LVM的兼容性并不好

※ 扩容逻辑卷(红帽考试内容之一)

注: 扩容前请一定要记得卸载设备和挂载点的关联

1.卸载文件系统

[root@xiudaochengxian ~]# umount /haha

2.扩容

[root@xiudaochengxian ~]# lvextend -L 800M /dev/shuaige/haha 	#扩容800MB
  Size of logical volume shuaige/haha changed from 400.00 MiB (100 extents) to 800.00 MiB (200 extents).
  Logical volume shuaige/haha successfully resized.

3.检查硬盘的完整性,确认目录结构、内容和文件内容没有丢失,一般情况下是要没有报错,均为正常情况

[root@xiudaochengxian ~]# e2fsck -f /dev/shuaige/haha 
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/shuaige/haha: 11/102400 files (0.0% non-contiguous), 23456/409600 blocks

4.通知文件系统发生变化,重置设备在系统中的容量(手动进行同步系统内核还没有同步到这部分新修改的信息)

[root@xiudaochengxian ~]# resize2fs /dev/shuaige/haha 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/shuaige/haha to 819200 (1k) blocks.
The filesystem on /dev/shuaige/haha is now 819200 blocks long.

5.重新挂载硬盘设备并查看挂载状态

[root@xiudaochengxian ~]# mount -a
[root@xiudaochengxian ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/sda3                  17G  3.5G   14G  21% /
devtmpfs                  1.9G     0  1.9G   0% /dev
tmpfs                     1.9G     0  1.9G   0% /dev/shm
tmpfs                     1.9G   13M  1.9G   1% /run
tmpfs                     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sr0                  4.2G  4.2G     0 100% /media/cdrom
/dev/sda1                1014M  165M  850M  17% /boot
tmpfs                     378M  4.0K  378M   1% /run/user/42
tmpfs                     378M   24K  378M   1% /run/user/0
/dev/mapper/shuaige-haha  767M  2.5M  721M   1% /haha

相关命令: 在这里插入图片描述
在这里插入图片描述

缩小逻辑卷

1.卸载文件系统

[root@xiudaochengxian ~]# umount /haha

2.检查文件系统的完整性

[root@xiudaochengxian ~]# e2fsck -f /dev/shuaige/haha 
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/shuaige/haha: 11/204800 files (0.0% non-contiguous), 36617/819200 blocks

3.通知文件系统缩小

[root@xiudaochengxian ~]# resize2fs /dev/shuaige/haha 300M			#缩小300M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/shuaige/haha to 307200 (1k) blocks.
The filesystem on /dev/shuaige/haha is now 307200 blocks long.

4.缩小(先通知系统内核自己想缩小逻辑卷,如果resize2fs命令执行后没有报错,系统允许了,再正式操作)

[root@xiudaochengxian ~]# lvreduce -L 300M /dev/shuaige/haha 
  WARNING: Reducing active logical volume to 300.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce shuaige/haha? [y/n]: y
  Size of logical volume shuaige/haha changed from 800.00 MiB (200 extents) to 300.00 MiB (75 extents).
  Logical volume shuaige/haha successfully resized.

5.重新挂载文件系统并查看系统状态

[root@xiudaochengxian ~]# mount -a
[root@xiudaochengxian ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/sda3                  17G  3.5G   14G  21% /
devtmpfs                  1.9G     0  1.9G   0% /dev
tmpfs                     1.9G     0  1.9G   0% /dev/shm
tmpfs                     1.9G   13M  1.9G   1% /run
tmpfs                     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sr0                  4.2G  4.2G     0 100% /media/cdrom
/dev/sda1                1014M  165M  850M  17% /boot
tmpfs                     378M  4.0K  378M   1% /run/user/42
tmpfs                     378M   24K  378M   1% /run/user/0
/dev/mapper/shuaige-haha  283M  2.1M  262M   1% /haha

删除逻辑卷

1.卸载文件系统

[root@xiudaochengxian ~]# umount /haha/

2.删除文件挂载信息

[root@xiudaochengxian ~]# vim /etc/fstab
13 /dev/shuaige/haha /haha ext4 defaults 0 0

3.移除LVM逻辑卷

[root@xiudaochengxian ~]# lvremove /dev/shuaige/haha 
Do you really want to remove active logical volume shuaige/haha? [y/n]: y
  Logical volume "haha" successfully removed

4.删除卷组

[root@xiudaochengxian ~]# vgremove shuaige 
  Volume group "shuaige" successfully removed

5.让物理卷不再支持LVM技术

[root@xiudaochengxian ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
[root@xiudaochengxian ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped.

· 逻辑卷快照

LVM的快照卷功能类似于虚拟机软件的还原时间点功能
例:对某一个逻辑卷设备做一次快照,如果日后发现数据被改错了,就可以利用之前做好的快照卷进行覆盖还原

LVM的快照卷功能有两个特点:
1.快照卷的容量必须等同于逻辑卷的容量
2.快照卷仅一次有效,一旦执行还原操作后则会被立即自动删除

1.创建快照卷

[root@xiudaochengxian ~]# cd /haha
[root@xiudaochengxian haha]# ls
lost+found
[root@xiudaochengxian haha]# cp -rf /etc/* ./		#复制一些文件到目录里
[root@xiudaochengxian haha]# lvcreate -L 300M -s -n SNAP /dev/shuaige/haha	创建快照卷
  Logical volume "SNAP" created.

查看SNAP

[root@xiudaochengxian haha]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/shuaige/haha
  LV Name                haha
  VG Name                shuaige
  LV UUID                SKbcGd-DcP1-WJB9-pgQt-mt9e-ORgc-ZeA7xs
  LV Write Access        read/write
  LV Creation host, time xiudaochengxian, 2021-11-01 16:42:57 +0800
  LV snapshot status     source of
                         SNAP [active]
  LV Status              available
  # open                 1
  LV Size                300.00 MiB
  Current LE             75
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/shuaige/SNAP
  LV Name                SNAP
  VG Name                shuaige
  LV UUID                yZHVcw-wvb1-mVft-ZKGd-XNai-32xW-S4OPD1
  LV Write Access        read/write
  LV Creation host, time xiudaochengxian, 2021-11-05 11:02:46 +0800
  LV snapshot status     active destination for haha
  LV Status              available
  # open                 0
  LV Size                300.00 MiB
  Current LE             75
  COW-table size         300.00 MiB
  COW-table LE           75
  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:3

2.检验SNAP快照效果

[root@xiudaochengxian haha]# rm -rf *		#删除目录下所有文件
[root@xiudaochengxian haha]# ls
[root@xiudaochengxian haha]# 
[root@xiudaochengxian haha]# cd
[root@xiudaochengxian ~]# umount /haha			#卸载挂载的目录
[root@xiudaochengxian ~]# lvconvert --merge /dev/shuaige/SNAP 	#恢复数据
  Merging of volume shuaige/SNAP started.
  shuaige/haha: Merged: 100.00%

[root@xiudaochengxian ~]# mount -a			#查看文件是否恢复
[root@xiudaochengxian ~]# cd /haha/
[root@xiudaochengxian haha]# ls
abrt                        maven
adjtime                     mcelog
aliases                     mime.types
aliases.db                  mke2fs.conf
alsa                        modprobe.d
alternatives                modules-load.d
anacrontab                  motd
asound.conf                 mtab
at.deny                     mtools.conf
audisp                      multipath
audit                       my.cnf
avahi                       my.cnf.d
bash_completion.d           nanorc
bashrc                      netconfig
binfmt.d                    NetworkManager
bluetooth                   networks
.......

标签:shuaige,手把手,haha,1.9,dev,Linux,LVM,xiudaochengxian,root
来源: https://blog.csdn.net/m0_59331971/article/details/120031813

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有