您的位置:首页 > 运维架构 > Linux

四周第一次课(2月26日)

2018-02-26 17:42 253 查看
4.10/4.11/4.12 lvm讲解
4.13 磁盘故障小案例


4.10 lvm讲解(上)

图讲解



两块硬盘 A B
A两个分区 B一个分区 共三个分区
三个分区组成一个物理卷(physical volume)
然后物理卷上面再组成卷组(volume group),卷组1有物理卷1,卷组2有物理卷2,3。 卷组就是物理卷的一个集合。
卷组基础上划分逻辑卷,卷组1里有逻辑卷1,卷组有逻辑卷2,3。
最终在逻辑卷上面进行格式化,再挂载到系统上面使用。

分区准备


fdisk /dev/sdb
n创建三个新分区,分别1G
t改变分区类型为8e

分区操作
# fdisk /dev/sdb

创建1G的主分区,
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux

ID为83代表只是普通的分区,如果想使用LVM,需要更改分区ID为8e,8e代表LVM分区

命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM

只有改到这样,才可以做物理卷

接下来,创建另外两个主分区,分别也是LVM类型(具体操作跟创建第一个一样)
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
分区之后,别忘了w保存

#fdisk -l 查看分区是否成功
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM

创建物理卷



安装lvm命令,安装失败。
# yum install -y lvm
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
没有可用软件包 lvm。
错误:无须任何处理

利用#yum通配搜索pvcreate命令
#yum provides "/*/pvcreate"
[root@centos7 ~]# yum provides "/*/pvcreate"
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base/7/x86_64/filelists_db | 6.7 MB 00:00:01
extras/7/x86_64/filelists_db | 636 kB 00:00:00
updates/7/x86_64/filelists_db | 3.5 MB 00:00:01
1:bash-completion-2.1-6.el7.noarch : Programmable completion for Bash
源 :base
匹配来源:
文件名 :/usr/share/bash-completion/completions/pvcreate

7:lvm2-2.02.171-8.el7.x86_64 : Userland logical volume management tools
源 :base
匹配来源:
文件名 :/usr/sbin/pvcreate

查到pvcreate命令来源于lvm2的包,所以安装这个lvm2即可
#yum install -y lvm2
安装成功,由于过程太多信息,就不列出。

继续操作
创建物理卷
(#partprobe命令,生成分区文件,用于系统没有自动生成文件。)
[root@centos7 ~]# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@centos7 ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@centos7 ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.

#pvdisplay 查看物理卷建立状态。

"/dev/sdb2" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID crs9wx-KFZA-JPde-JwD2-fKnA-M6pi-Ci5kuj

"/dev/sdb3" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 5lyipH-eOGt-8je9-N2NQ-82dw-sdqh-RVRbvQ

"/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 ffAd90-QY88-w25I-MSE1-gyA6-FtYc-l4XCyk

4.11 lvm讲解(中)

#pvs 同样也是查看物理卷
# 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 vg1 /dev/sdb1 /dev/sdb2
# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created

#vgs 查看卷组信息(#vgdisplay 也可以查看)
vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- 1.99g 1.99g

创建卷组之后,创建逻辑卷
#lvcreate -L 100M -n lv1 vg1
指定100M 名字 lv1 从vg1里面划分出来
# lvcreate -L 100M -n lv1 vg1
Logical volume "lv1" created.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m
# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID AYUXn9-xIp6-Ti6A-JWiI-hcvR-I1X2-PFoyrI
LV Write Access read/write
LV Creation host, time centos7.4-01, 2018-02-26 15:36:16 +0800
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

格式化ext4
#mkfs.ext4 /dev/vg1/lv1 逻辑卷的路径可以在#lvdisplay查看

挂载到/mnt
[root@centos7 ~]# mount /dev/vg1/lv1 /mnt
[root@centos7 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.1G 27G 4% /
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 187M 97M 91M 52% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt

#ls -l /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 2月 26 15:40 /dev/vg1/lv1 -> ../dm-0
/dev/vg1/lv1是一个软链接文件,指向../dm-0

创建目录,文件,写入内容。
[root@centos7 ~]# mkdir /mnt/1212
[root@centos7 ~]# touch /mnt/23.txt
[root@centos7 ~]# echo "11111" > /mnt/23.txt

扩容逻辑卷



# lvresize 扩容命令 -L指定容量 后面对应盘符路径(扩容之前,应该先#umount)
# lvresize -L 200M /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.
(如果进行#lvresize时忘记了#umount,可以先#umount再来一次#lvresize也是不影响效果的)
# umount /mnt/
# lvresize -L 200M /dev/vg1/lv1
New size (50 extents) matches existing size (50 extents).

检查磁盘错误
#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), 8899/102400 blocks

更新逻辑卷信息
# 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@centos7 ~]# !mount
mount /dev/vg1/lv1 /mnt

查看更新后的分区信息,/dev/vg1/lv1 容量比原来增大了
[root@centos7 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.1G 27G 4% /
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 187M 97M 91M 52% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt

检查设置后文件有没有受到影响,检查正常,无任何问题发现,由此证明扩容没有影响文件。
[root@centos7 ~]# ls /mnt/
1212 23.txt lost+found
[root@centos7 ~]# cat /mnt/23.txt
11111

缩减逻辑卷(xfs不支持)




先umount
#umount /mnt

e2fsck -f /dev/vg1/lv1 检查磁盘错误
# 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), 11887/204800 blocks

resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext)
# 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.

lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
# lvresize -L 100M /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.
此处提醒缩减可能会导致文件损坏,选y,后面再检查。

检查是否缩减变成了100M
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m

挂载,检查文件
#!mount
mount /dev/vg1/lv1 /mnt
[root@centos7 ~]# ls /mnt
1212 23.txt lost+found
[root@centos7 ~]# cat /23.txt
cat: /23.txt: 没有那个文件或目录
[root@centos7 ~]# cat /mnt/23.txt
11111

检查结果,文件可以继续操作和查看,并无影响,由此证明,缩容没有影响文件问题。

将/dev/vg1/lv1 格式化为xfs
#umount /mnt
#mkfs.xfs -f /dev/vg1/lv1
# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, 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

#!mount

[root@centos7 ~]# touch /mnt/233.txt
[root@centos7 ~]# echo "aaaa" > /mnt/233.txt
[root@centos7 ~]# cat !$
cat /mnt/233.txt
aaaa
[root@centos7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 100.00m

xfs扩容300M,不需要卸载挂载
[root@centos7 ~]# lvresize -L 300M
No command with matching syntax recognised. Run 'lvresize --help' for more information.
Nearest similar command has syntax:
lvresize -L|--size [+|-]Size[m|UNIT] LV
Resize an LV by a specified size.

[root@centos7 ~]# 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.
[root@centos7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 300.00m

#df-h 容量没有更新上去
# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.1G 27G 4% /
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 187M 97M 91M 52% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 97M 5.2M 92M 6% /mnt

执行xfs_growfs /dev/vg1/lv1 让容量更新上去
[root@centos7 ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=25600, 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
data blocks changed from 25600 to 76800
[root@centos7 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.1G 27G 4% /
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 187M 97M 91M 52% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 297M 5.5M 292M 2% /mnt

4.12 lvm讲解(下)

扩展卷组



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

容量变为3G,表示添加成功。

4.13 磁盘故障小案例

开机失败,因为修改了文件错误。
这种情况,往往是磁盘挂载错误才出现的问题。

直接输入root的密码



进入/etc/fstab
dd删掉最后一行



重启即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 学习 笔记