您的位置:首页 > 其它

4.10/4.11/4.12 lvm讲解 4.13 磁盘故障小案例

2018-01-02 18:25 501 查看

4.10/4.11/4.12 lvm讲解

操作流程:
磁盘分区-->创建物理卷-->划分为卷组-->划分成逻辑卷-->格式化、挂载-->扩容。

磁盘分区
注: 创建分区时需要更改其文件类型为lvm(代码8e)
创建物理卷

#### pvcreate命令

pvcreate=physical volume create

[root@martinlinux ~]# pvcreate /dev/sdb1
-bash: pvcreate: 未找到命令
命令不存在,需要安装该命令包!

[root@martinlinux ~]# yum install -y lvm
已加载插件:fastestmirror

Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                | 3.6 kB  00:00:00
extras                                                              | 3.4 kB  00:00:00
updates                                                             | 3.4 kB  00:00:00
(1/2): extras/7/x86_64/primary_db                                   | 168 kB  00:00:00
(2/2): updates/7/x86_64/primary_db                                  | 5.7 MB  00:00:04
Determining fastest mirrors
* base: mirrors.btte.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com

没有可用软件包 lvm。
错误:无须任何处理
即,‘lvm’不是该命令所在包的正确名称,此时不知道正确的包的名称,需要进行查找,进行如下操作:

[root@martinlinux ~]# yum provides "/*/pvcreate"
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
7:lvm2-2.02.166-1.el7_3.4.x86_64 : Userland logical volume management tools
源    :updates
匹配来源:
文件名    :/usr/sbin/pvcreate

即,该包的名称应该是“lvm2”!!!

[root@martinlinux ~]# yum install -y lvm2
yum provides命令: 通过模块查找包

安装完成后继续创建物理卷:

[root@martinlinux ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@martinlinux ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@martinlinux ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.

注: 一般情况在创建完分区后系统会自动生成相应的分区文件“/dev/sdbn”,但是当系统未自动生成时无法完成物理卷创建任务,需要执行命令 “partprobe” 生成相应文件再执行pvcreate命令。

pvdisplay命令&pvs命令

物理卷创建完成后可以使用命令pvdisplay或pvs进行查看:(在此只展示一个物理卷)

[root@martinlinux ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb1
VG Name
PV Size               1.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               pnei02-gNA9-4CfU-FPJL-8b0H-OCId-bmtCDT
[root@martinlinux ~]# pvs
PV         VG Fmt  Attr PSize PFree
/dev/sdb1     lvm2 ---  1.00g 1.00g
/dev/sdb2     lvm2 ---  1.00g 1.00g
/dev/sdb3     lvm2 ---  1.00g 1.00g

创建物理卷组

vgcreate命令

vgcreate=volume group create

语法: vgcreate [组名] [参数] 参数指分区

创建卷组:

[root@martinlinux ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created
创建完成后同样可以使用vgdisplay命令和vgs命令进行查看:

[root@martinlinux ~]# vgdisplay
--- Volume group ---
VG Name               vg1
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  1
……
[root@martinlinux ~]# vgs
VG  #PV #LV #SN Attr   VSize VFree
vg1   2   0   0 wz--n- 1.99g 1.99g

vgremove命令

vgremove=volume group remove 删除卷组

创建逻辑卷

lvcreate命令

lvcreate=logical volume create 创建逻辑卷

语法: lvcreate [options] [参数] 此处参数指物理卷组
选项:
-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节
-n:指定逻辑卷的名称。

[root@martinlinux ~]# lvcreate -L 100M -n lv1 vg1
Logical volume "lv1" created.
创建完成后同样可以使用lvdisplay命令和lvs命令进行查看:

[root@martinlinux ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID 9Yd03t-y7TJ-PzRR-lCza-dKNl-PkQM-IeTgVP
……
[root@martinlinux ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m
ext4格式

格式化&挂载

eg1:格式化成ext4格式

[root@martinlinux ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
……
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
格式化完成后开始挂载:

[root@martinlinux ~]# mount /dev/vg1/lv1 /mnt/
[root@martinlinux ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.2G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt
[root@martinlinux ~]# ls -l /dev/vg1/lv1
lrwxrwxrwx 1 root root 7 6月 19 19:47 /dev/vg1/lv1 -> ../dm-0
[root@martinlinux ~]# ls -l /dev/mapper/vg1-lv1
lrwxrwxrwx 1 root root 7 6月 19 19:47 /dev/mapper/vg1-lv1 -> ../dm-0
扩容

流程:更改逻辑卷信息(lvresize)-->检测磁盘错误(e2fsck -f)-->更新逻辑卷信息(resize2fs)

先在/mnt/目录下写入一些东西,备用:

[root@martinlinux ~]# mkdir /mnt/1212
[root@martinlinux ~]# touch /mnt/23.txt
[root@martinlinux ~]# echo "11111111111">!$
echo "11111111111">/mnt/23.txt
开始扩容:

lvresize命令

lvresize命令(=logical volume resize)用于调整LVM逻辑卷的空间大小,可以增大空间和缩小空间。使用lvresize命令调整逻辑卷空间大小和缩小空间时需要谨慎,因为它有可能导致数据丢失。

注: 正常情况下该命令应该在卸载后再执行。
语法: lvresize [optiones] [参数] 参数指逻辑卷
选项:
-L:指定大小

[root@martinlinux ~]# lvresize -L 200 /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
Logical volume vg1/lv1 successfully resized.
e2fsck命令

检测磁盘错误(ext4执行)

[root@martinlinux ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8898/102400 blocks
resize2fs命令

更新逻辑卷信息(ext4执行)

[root@martinlinux ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
更新逻辑卷信息后新分区在挂载的时候才能被识别新的大小。
挂载:

[root@martinlinux ~]# !mount
mount /dev/vg1/lv1 /mnt/
[root@martinlinux ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.2G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt
扩容后变成190M
系统扩容不会使文件丢失:

[root@martinlinux ~]# ls /mnt/
1212 23.txt lost+found
缩容(xfs格式不支持)

流程:卸载(umount)-->检测(e2fsck -f)-->更新逻辑卷信息(大小)(resize2fs)-->重置逻辑卷大小(lvresize)

[root@martinlinux ~]# umount /mnt/
[root@martinlinux ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11886/204800 blocks
[root@martinlinux ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
[root@martinlinux ~]# lvresize -L 100 /dev/vg1/lv1 重置逻辑卷大小
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv1 successfully resized.
[root@martinlinux ~]# lvs 查看逻辑卷大小
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m
[root@martinlinux ~]# ls /mnt/
1212 23.txt lost+found
即,合理缩容也不会使系统内文件丢失。
xfs格式

格式化&挂载

格式化成xfs格式

[root@martinlinux ~]# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

扩容

流程:更改逻辑卷信息(lvresize)-->更新逻辑卷信息(xfs_growfs)

更改逻辑卷信息(lvresize)

[root@martinlinux ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
Logical volume vg1/lv1 successfully resized.
xfs_growfs命令(该命令必须在挂载状态执行)

[root@martinlinux ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@martinlinux ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/vg1-lv1 297M 16M 282M 6% /mnt
扩容卷组

eg:将/dev/sdb3增加到vg1

vgextend命令

语法: vgextend [卷组名] [物理卷]

[root@martinlinux ~]# vgextend vg1 /dev/sdb3
Volume group "vg1" successfully extended
[root@martinlinux ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 3 1 0 wz--n- 2.99g 2.70g

4.13 磁盘故障小案例

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lvm 讲解