Day16 LVM(逻辑卷管理)与磁盘故障小案例

lvm详解

简述

LVM的产生是因为传统的分区一旦分区好后就无法在线扩充空间,也存在一些工具能实现在线扩充空间但是还是会面临数据损坏的风险;传统的分区当分区空间不足时,一般的解决办法是再创建一个更大的分区将原分区卸载然后将数据拷贝到新分区,但是在企业的生产系统往往不允许停机或者允许停机的时间很短,LVM就能很好的解决在线扩充空间的问题,而且不会对数据造成影响,LVM还能通过快照在备份的过程中保证日志文件和表空间文件在同一时间点的一致性;本篇文章不会对概念知识做太详细的介绍,主要讲述LVM的扩容、收缩、移除的方法。

创建lvm

1.准备磁盘分区

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

示例:

[root@centos001 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xee32a29c 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+1G 
分区 1 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):N
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): P
分区号 (2-4,默认 2):
起始 扇区 (2099200-20971519,默认为 2099200):
将使用默认值 2099200
Last 扇区, +扇区 or +size{K,M,G} (2099200-20971519,默认为 20971519):+1G
分区 2 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):N
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): P
分区号 (3,4,默认 3):
起始 扇区 (4196352-20971519,默认为 4196352):
将使用默认值 4196352
Last 扇区, +扇区 or +size{K,M,G} (4196352-20971519,默认为 20971519):+1G
分区 3 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):T
分区号 (1-3,默认 3):1
Hex 代码(输入 L 列出所有代码):8E
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):T
分区号 (1-3,默认 3):2
Hex 代码(输入 L 列出所有代码):8E
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):T
分区号 (1-3,默认 3):
Hex 代码(输入 L 列出所有代码):8E
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):W
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@centos001 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c29e8

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41943039    20765696   8e  Linux LVM

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xee32a29c

   设备 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

磁盘 /dev/mapper/cl-root:19.1 GB, 19113443328 字节,37330944 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/cl-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/vg1-lv1:314 MB, 314572800 字节,614400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

2.准备物理卷 先运行一下 partprobe 命令 pvcreate /dev/sdb1 pvcreate /dev/sdb2 pvcreate /dev/sdb3

pvdisplay 或 pvs(更直观一些) 列出当前的物理卷 pvremove /dev/sdb3删除物理卷

pvcreate命令:用于将物理硬盘分区初始化为物理卷,以便LVM使用
partprobe命令:创建物理卷
pvdisplay与pvs 命令:列出当前物理卷后者更直观
pvremove 命令:删除物理卷

3.创建卷组

[root@centos001 ~]# lvcreate -L 100M -n lv1 vg1             //创建一个逻辑卷
  Logical Volume "lv1" already exists in volume group "vg1"
[root@centos001 ~]# mkfs.ext4 /dev/vg1/lv1                 //把这个卷格式化成ext4格式
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
第一个数据块=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 
 
[root@centos001 ~]# mount /dev/vg1/lv1  /mnt/                  //挂载 
[root@centos001 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /                //这里能看到两个不同的路径
devtmpfs             482M     0  482M    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  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M    2% /mnt/              //与我们刚才挂载的路径不同
[root@centos001 ~]# ls -l /dev/vg1/lv1                                   
lrwxrwxrwx. 1 root root 7 11月  6 07:40 /dev/vg1/lv1 -> ../dm-2
[root@centos001 ~]# ls -l /dev/mapper/vg1-lv1 
lrwxrwxrwx. 1 root root 7 11月  6 07:40 /dev/mapper/vg1-lv1 -> ../dm-2
扩充lvm

1.准备

[root@centos001 ~]# mkdir /mnt/1212
[root@centos001 ~]# touch /mnt/23.txt
[root@centos001 ~]# echo “11111111” > /mnt/23.txt 

2.扩容

e2fsck -f检查磁盘错误
resize2fs 更新逻辑卷信息
**xfs_growfs ** 文件系统需要执行

[root@centos001 ~]# 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.
[root@centos001 ~]# umount /mnt/                         //  应该先删除挂载点
[root@centos001 ~]# lvresize -L 200M /dev/vg1/lv1           //重新设定
  New size (50 extents) matches existing size (50 extents).
[root@centos001 ~]# 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
[root@centos001 ~]# !mount                       //下一步再更新逻辑卷的信息
mount /dev/vg1/lv1  /mnt/                         //不更新 的话 无法识别新的大小^
[root@centos001 ~]# df -h                        //查看
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /
devtmpfs             482M     0  482M    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  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M    2% /mnt          //因为没有使用跟新逻辑卷的命令 所以 挂载点上的大小也没有变
 [root@centos001 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg1/lv1 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.

[root@centos001 ~]# !mount
mount /dev/vg1/lv1  /mnt/
mount: /dev/mapper/vg1-lv1 已经挂载或 /mnt 忙
       /dev/mapper/vg1-lv1 已经挂载到 /mnt 上
[root@centos001 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /
devtmpfs             482M     0  482M    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  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1  190M  1.6M  178M    1% /mnt            //实现扩容
[root@centos001 ~]# ls /mnt/
1212  23.txt  lost+found
[root@centos001 ~]# cat /mnt/23.txt 
“11111111”
收缩lvm

先umount 删除挂载点;
e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext4)
resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext4)
lvresize -L 100M /dev/vg1/lv1 重新设置卷大小

  • xfs格式无法使用缩小
[root@centos001 ~]# 缩减逻辑卷(xfs不支持^C
[root@centos001 ~]# umount /mnt/               //删除挂载点
[root@centos001 ~]# 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
[root@centos001 ~]# 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@centos001 ~]# 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.
[root@centos001 ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root cl  -wi-ao----  17.80g                                                    
  swap cl  -wi-ao----   2.00g                                                    
  lv1  vg1 -wi-a----- 100.00m                                                    
[root@centos001 ~]# mount /dev/vg1/lv1  /mnt/             //添加挂载点
[root@centos001 ~]# ls /mnt/
1212  23.txt  lost+found
[root@centos001 ~]# ls /mnt/23.txt 
/mnt/23.txt
[root@centos001 ~]# ls -l /mnt/23.txt 
-rw-r--r--. 1 root root 15 11月  6 07:46 /mnt/23.txt
[root@centos001 ~]# cat /mnt/23.txt 
“11111111”
xfs格式 扩容

**mkfs.xfs -f /dev/vg1/lv1 ** :把磁盘格式成xfs
xfs_growfs /dev/vg1/lv1: 更新设置

 [root@centos001 ~]# umount /mnt/
[root@centos001 ~]# 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
[root@centos001 ~]# !mount
mount /dev/vg1/lv1  /mnt/
[root@centos001 ~]# ls /mnt/            //没有东西是因为mnt下已经没有文件了所以我们就来创建一个
[root@centos001 ~]# touch /mnt/233.txt
[root@centos001 ~]# echo "aaa" > !$
echo "aaa" > /mnt/233.txt
[root@centos001 ~]# cat !$
cat /mnt/233.txt
aaa
[root@centos001 ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root cl  -wi-ao----  17.80g                                                    
  swap cl  -wi-ao----   2.00g                                                    
  lv1  vg1 -wi-ao---- 100.00m                                                    
[root@centos001 ~]# 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@centos001 ~]# lvs 
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root cl  -wi-ao----  17.80g                                                    
  swap cl  -wi-ao----   2.00g                                                    
  lv1  vg1 -wi-ao---- 300.00m                                                    
[root@centos001 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /
devtmpfs             482M     0  482M    0% /dev
tmpfs                493M     0  493M    0% /dev/shm
tmpfs                493M  6.9M  486M    2% /run
tmpfs                493M     0  493M    0% /sys/fs/cgroup
/dev/sda1            197M  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1   97M  5.2M   92M    6% /mnt      //这里能看到还没变 所以用下面这个命令 更新数据
[root@centos001 ~]# xfs_growfs -L 300M /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
xfs_growfs: XFS_IOC_FSGROWFSLOG xfsctl failed: 无效的参数
[root@centos001 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /
devtmpfs             482M     0  482M    0% /dev
tmpfs                493M     0  493M    0% /dev/shm
tmpfs                493M  6.9M  486M    2% /run
tmpfs                493M     0  493M    0% /sys/fs/cgroup
/dev/sda1            197M  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1   97M  5.2M   92M    6% /mnt
[root@centos001 ~]# 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@centos001 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   18G  1.5G   17G    8% /
devtmpfs             482M     0  482M    0% /dev
tmpfs                493M     0  493M    0% /dev/shm
tmpfs                493M  6.9M  486M    2% /run
tmpfs                493M     0  493M    0% /sys/fs/cgroup
/dev/sda1            197M  146M   52M   74% /boot
tmpfs                 99M     0   99M    0% /run/user/0
/dev/mapper/vg1-lv1  297M  5.5M  292M    2% /mnt

排错案例

  • 开启虚拟机是出现乱码

  • 示例:

  • 解决方法:
  1. 输入用户名和密码
  2. 打开配置文件 vi /etc/fstab
  3. 删除最后一行 保存并退出
  4. reboot 重启

转载于:https://my.oschina.net/u/3707523/blog/1832452

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值