lvm讲解、磁盘故障小案例

  • lvm讲解上中下
    • 准备磁盘
    • 安装lvmpvcreate命令创建物理卷pvdisplay命令或pvs命令列出当前物理卷pvremove命令删除物理卷
    • vgcreate命令创建卷组vgdisplay命令或者vgs列出卷组vgremove命令删除卷组
    • lvcreate命令创建逻辑卷
    • 挂载
    • 扩容ext4逻辑卷
    • ext4缩减逻辑卷
    • xfs扩容不支持缩容
  • 磁盘故障小案例

410 lvm讲解(上)(中)(下)

LVM是一个复杂结构的软件,但是方便扩容和缩容。后期出现问题恢复数据比较麻烦。
Disk(磁盘)-> Physical Volume(物理卷 /磁盘分区)-> Volume Group(卷组) -> Logical Volume(逻辑卷)

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

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

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

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbf9521ab

   设备 Boot      Start         End      Blocks   Id  System

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

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbf9521ab

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
(Id为83是普通磁盘分区,需要更改为LVM分区)

准备磁盘

命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e

WARNING: If you have created or modified any DOS 6.xpartitions, please see the fdisk manual page for additionalinformation.

已将分区“FAT12”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):p

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbf9521ab

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   8e  Linux LVM
(改完之后Id变为8e,System变为Linux LVM)
命令(输入 m 获取帮助):p

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xceba1b55

   设备 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    10485759     3144704   8e  Linux LVM
命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘
[root@localhost ~]# fdisk -l

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     9005055     4194304   82  Linux swap / Solaris
/dev/sda3         9005056    41943039    16468992   83  Linux

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xceba1b55

   设备 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    10485759     3144704   8e  Linux LVM

安装lvm,pvcreate命令创建物理卷,pvdisplay命令或pvs命令列出当前物理卷,pvremove命令删除物理卷

[root@localhost ~]# yum install -y lvm
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
没有可用软件包 lvm。
错误:无须任何处理
[root@localhost ~]# yum provides "/*/pvcreate"(通过查找命令确定包名)
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
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
[root@localhost ~]# yum install -y lvm2
[root@localhost ~]# ls /dev/sdb1
/dev/sdb1
[root@localhost ~]# partprobe(如果ls /dev/sdb1没有才运行该命令生成系统文件,一般创建完分区就会自动生成)
[root@localhost ~]# 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@localhost ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvcreate /dev/sdb3
WARNING: xfs signature detected on /dev/sdb3 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb3.
  Physical volume "/dev/sdb3" successfully created.
[root@localhost ~]# 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               U3wYJo-U4oD-07E5-9rOs-qK5w-1HBR-liBkO2

  "/dev/sdb3" is a new physical volume of "<3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               <3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               vQ0yI6-BlAw-Zdnz-CrHV-1gHc-BGA3-0u94B6

  "/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               eDi6k2-jLs7-w1nJ-ZbLc-8HcC-uLBW-8ckcaB
[root@localhost ~]# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb1     lvm2 ---   1.00g  1.00g
  /dev/sdb2     lvm2 ---   1.00g  1.00g
  /dev/sdb3     lvm2 ---  <3.00g <3.00g

vgcreate命令创建卷组,vgdisplay命令或者vgs列出卷组,vgremove命令删除卷组

[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2(将/dev/sdb1 和/dev/sdb2合成一个叫vg1的卷组)
  Volume group "vg1" successfully created
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1
  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               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       0 / 0   
  Free  PE / Size       510 / 1.99 GiB
  VG UUID               bIo5rl-pkmg-0ts0-KVWq-NcLw-0DYs-SZvxwY
[root@localhost ~]# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  vg1   2   0   0 wz--n- 1.99g 1.99g

lvcreate命令创建逻辑卷

[root@localhost ~]# lvcreate -L 100M -n lv1 vg1
  Logical volume "lv1" created.

格式化

[root@localhost ~]# 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
第一个数据块=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@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             16G  1.1G   15G    7% /
devtmpfs             903M     0  903M    0% /dev
tmpfs                912M     0  912M    0% /dev/shm
tmpfs                912M  8.7M  904M    1% /run
tmpfs                912M     0  912M    0% /sys/fs/cgroup
/dev/sda1            297M  118M  180M   40% /boot
tmpfs                183M     0  183M    0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M    2% /mnt
[root@localhost ~]# ls -l /dev/vg1/lv1 
lrwxrwxrwx. 1 root root 7 12 23:56 /dev/vg1/lv1 -> ../dm-0
[root@localhost ~]# ls -l /dev/mapper/vg1-lv1 
lrwxrwxrwx. 1 root root 7 12 23:56 /dev/mapper/vg1-lv1 -> ../dm-0

操作新挂载磁盘

[root@localhost ~]# mkdir /mnt/123
[root@localhost ~]# touch /mnt/123/1.txt
[root@localhost ~]# echo "123213esads" > /mnt/123/1.txt

扩容ext4逻辑卷


ext4扩容

[root@localhost ~]# umount /mnt/(先取消挂载)
[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1 (扩容至200M)
  New size (50 extents) matches existing size (50 extents).
[root@localhost ~]# e2fsck -f /dev/vg1/lv1 (检查磁盘错误(ext4执行))
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@localhost ~]# resize2fs /dev/vg1/lv1 (更新逻辑卷信息(ext4执行))
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@localhost ~]# !mount
mount /dev/vg1/lv1 /mnt/

ext4缩减逻辑卷

[root@localhost ~]# umount /mnt/(取消挂载)
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# 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@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-a----- 100.00m    
[root@localhost ~]# !mount
mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             16G  1.1G   15G    7% /
devtmpfs             903M     0  903M    0% /dev
tmpfs                912M     0  912M    0% /dev/shm
tmpfs                912M  8.7M  904M    1% /run
tmpfs                912M     0  912M    0% /sys/fs/cgroup
/dev/sda1            297M  118M  180M   40% /boot
tmpfs                183M     0  183M    0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M    2% /mnt 

xfs扩容,不支持缩容

[root@localhost ~]# umount /mnt/(取消挂载)
[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1 (格式化为xfs)
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@localhost ~]# mount /dev/vg1/lv1 /mnt/(挂载)
[root@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-ao---- 100.00m  
[root@localhost ~]# lvresize -L 300M /dev/vg1/lv1 (xfs扩容不需要取消挂载)
  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@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-ao---- 300.00m  
[root@localhost ~]# df -h 
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             16G  1.1G   15G    7% /
devtmpfs             903M     0  903M    0% /dev
tmpfs                912M     0  912M    0% /dev/shm
tmpfs                912M  8.7M  904M    1% /run
tmpfs                912M     0  912M    0% /sys/fs/cgroup
/dev/sda1            297M  118M  180M   40% /boot
tmpfs                183M     0  183M    0% /run/user/0
/dev/mapper/vg1-lv1   97M  5.2M   92M    6% /mnt(挂载点还没改变,需要更新)
[root@localhost ~]# xfs_growfs  /dev/vg1/lv1(更新/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@localhost ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             16G  1.1G   15G    7% /
devtmpfs             903M     0  903M    0% /dev
tmpfs                912M     0  912M    0% /dev/shm
tmpfs                912M  8.7M  904M    1% /run
tmpfs                912M     0  912M    0% /sys/fs/cgroup
/dev/sda1            297M  118M  180M   40% /boot
tmpfs                183M     0  183M    0% /run/user/0
/dev/mapper/vg1-lv1  297M  5.5M  292M    2% /mnt(更新成功)

注意:扩容、缩容都不影响之前的文件

卷组扩容

[root@localhost ~]# fdisk -l /dev/sdb

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xceba1b55

   设备 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    10485759     3144704   8e  Linux LVM
(/dev/sdb3 未使用的分区)
[root@localhost ~]# vgextend vg1 /dev/sdb3(将sdb3物理卷增加到vg1卷组)
  Volume group "vg1" successfully extended
[root@localhost ~]# vgs 
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg1   3   1   0 wz--n- <4.99g <4.70g(vg1已经增加到了5G)
(然后再重新设置卷大小就可以了)
[root@localhost ~]# lvresize -L 500M /dev/vg1/lv1 (增加卷组到500M)

413 磁盘故障小案例

因为之前实验更改了/etc/fstab里面去,重启物理机发现进入不了系统

先输入root密码

[root@localhost ~]# vi /etc/fstab

删掉最后一行并保存,再重启物理机

正常

你可能感兴趣的:(lvm讲解、磁盘故障小案例)