lvm

1.从磁盘到逻辑卷
lvm介绍_第1张图片
2.准备磁盘:
lvm介绍_第2张图片
3.将分的磁盘分区格式更改为lvm:

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

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux

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

三个分区都更改完成后如下:

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

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

   设备 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保存退出
4.将分区做成物理卷:
lvm介绍_第3张图片
5.yum命令安装包的时候,如果只知道命令,不知道包的名字,可以搜索:
yum provides "/*/name"

[root@weix-01 ~]# yum provides "/*/pvcreate"
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.shuosc.org
 * extras: mirrors.shuosc.org
 * updates: mirrors.shuosc.org
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

6.安装lvm2包:

[root@weix-01 ~]# yum install -y lvm2
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.shuosc.org
 * extras: mirrors.shuosc.org
 * updates: mirrors.shuosc.org
正在解决依赖关系
--> 正在检查事务
---> 软件包 lvm2.x86_64.7.2.02.171-8.el7 将被 安装

7.创建物理卷:如果磁盘分区后没有文件,需要运行partprobe命令
lvm介绍_第4张图片
然后创建

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

8.使用pvdisplay命令查看物理卷:pvdisplay或者pvs

[root@weix-01 ~]# 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               V5EDGp-LO0f-3dJI-1SFW-lkjL-bLal-mFiYJ0

  "/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               wMgrB1-W7Tf-isWH-yrY0-lSiq-hIGj-NtsUW6

  "/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               YCe5HH-QmZW-98C3-unsi-y4G1-ozHP-Pkb5rL

9.创建物理卷组:

[root@weix-01 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
  Volume group "vg1" successfully created

10.查看卷组:vgdisplay或者vgs

[root@weix-01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  vg1   2   0   0 wz--n- 1.99g 1.99g

11.删除卷组:vgremove:

[root@weix-01 ~]# vgremove vg1
  Volume group "vg1" successfully removed

12.创建逻辑卷:

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

13.格式化:

[root@weix-01 ~]# 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: 完成

14.挂载:

[root@weix-01 ~]# mount /dev/vg1/lv1 /mnt/
[root@weix-01 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             23G  1.1G   22G    5% /
devtmpfs             479M     0  479M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.8M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            197M   97M  100M   50% /boot
tmpfs                 98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M    2% /mnt
[root@weix-01 ~]# ls -l /dev/mapper/vg1-lv1                              #两个文件指向同一个文件
lrwxrwxrwx. 1 root root 7 1月   2 14:42 /dev/mapper/vg1-lv1 -> ../dm-0
[root@weix-01 ~]# ls -l /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 1月   2 14:42 /dev/vg1/lv1 -> ../dm-0

15.扩容逻辑卷:
lvm介绍_第5张图片
16.按照步骤执行:ext4格式执行

[root@weix-01 ~]# umount /mnt/                                 #解除挂载
[root@weix-01 ~]# 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@weix-01 ~]# 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@weix-01 ~]# resize2fs !$                                          #更新信息
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@weix-01 ~]# !mount                    #重新挂载
mount /dev/vg1/lv1 /mnt/
[root@weix-01 ~]# df -h                          #查看现在大小
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             23G  1.1G   22G    5% /
devtmpfs             479M     0  479M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.8M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            197M   97M  100M   50% /boot
tmpfs                 98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1  190M  1.6M  175M    1% /mnt
[root@weix-01 ~]# ls /mnt/                                 #文件存在,未被改动
1212  23.txt  lost+found
[root@weix-01 ~]# cat /mnt/23.txt
1111111111

17.按步骤执行:xfs格式(不需要卸载)

[root@weix-01 ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-ao---- 100.00m                                                   
[root@weix-01 ~]# 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@weix-01 ~]# df -h                                       #查看大小
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             23G  1.1G   22G    5% /
devtmpfs             479M     0  479M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.7M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            197M   97M  100M   50% /boot
tmpfs                 98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1   97M  5.2M   92M    6% /mnt
[root@weix-01 ~]# 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@weix-01 ~]# df -h                                #查看大小
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             23G  1.1G   22G    5% /
devtmpfs             479M     0  479M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.7M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            197M   97M  100M   50% /boot
tmpfs                 98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1  297M  5.5M  292M    2% /mnt

18.缩减逻辑卷:xfs不支持:
lvm介绍_第6张图片
19.按照步骤执行:

[root@weix-01 ~]# umount /mnt
[root@weix-01 ~]# 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@weix-01 ~]# 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@weix-01 ~]# 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@weix-01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                fMohN5-rM07-VayT-dYvT-rDZm-dPYN-WM071w
  LV Write Access        read/write
  LV Creation host, time weix-01, 2018-01-02 14:40:12 +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

20.扩展卷组:
lvm介绍_第7张图片

[root@weix-01 ~]# vgextend vg1 /dev/sdb3                 #扩展卷组,将分区3加进来
  Volume group "vg1" successfully extended
[root@weix-01 ~]# vgdiplay
-bash: vgdiplay: 未找到命令
[root@weix-01 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       75 / 300.00 MiB
  Free  PE / Size       690 / <2.70 GiB
  VG UUID               T9WpLW-5tBm-arLr-jb70-HoO0-JP81-cMuqrx

[root@weix-01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize  VFree
  vg1   3   1   0 wz--n- <2.99g <2.70g