菜鸟学习Linux LVM及配置实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菜鸟学习Linux LVM及配置实战相关的知识,希望对你有一定的参考价值。

每当我们生产上线服务器的时候,总是无法正确的预估磁盘大小该给多少,这样就产生了一个技术LVM,什么是LVM?
LVM是 Logical Volume Manager(逻辑卷管理),它是Linux环境下对磁盘分区进行管理的一种机制。
这里菜鸡哥哥画一个图,是我自己的理解,如果有错误请指正!!
技术分享图片
那么如何使用lvm呢?

[[email protected] ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00009be7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        2611    18668544   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes——实验演示使用此盘
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[[email protected] ~]# fdisk /dev/sdb——对需要进行lvm的磁盘进行分区

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 
Value out of range.
Partition number (1-4): 1
First cylinder (1-652, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-652, default 133): 
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-652, default 652): +1G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb8acf654

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         264     1060290   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks
[[email protected] ~]# pvcreate /dev/sdb{1,2}——创建pv
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[[email protected] ~]# vgcreate vg01 /dev/sdb{1,2}——创建vg,vg名为vg01
  Volume group "vg01" successfully created
[[email protected] ~]# lvcreate -n lv01 -L 1G vg01——创建lv,lv名为lv01,大小为1G
  Logical volume "lv01" created
[[email protected] ~]# 
[[email protected] ~]# pvs——查看pv
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1  vg01 lvm2 a--  1.01g 8.00m
  /dev/sdb2  vg01 lvm2 a--  1.01g 1.01g
[[email protected] ~]# vgs——查看vg
  VG   #PV #LV #SN Attr   VSize VFree
  vg01   2   1   0 wz--n- 2.02g 1.02g
[[email protected] ~]# lvs——查看lv
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-a----- 1.00g                                             
[[email protected] ~]# 
[[email protected] ~]# pvscan 
  PV /dev/sdb1   VG vg01   lvm2 [1.01 GiB / 8.00 MiB free]
  PV /dev/sdb2   VG vg01   lvm2 [1.01 GiB / 1.01 GiB free]
  Total: 2 [2.02 GiB] / in use: 2 [2.02 GiB] / in no VG: 0 [0   ]
[[email protected] ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg01" using metadata type lvm2
[[email protected] ~]# lvscan
  ACTIVE            ‘/dev/vg01/lv01‘ [1.00 GiB] inherit
[[email protected] ~]# 
[[email protected] ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg01
  PV Size               1.01 GiB / not usable 3.41 MiB
  Allocatable           yes 
  PE Size               4.00 MiB——默认PE大小为4M
  Total PE              258
  Free PE               2
  Allocated PE          256
  PV UUID               CvlZqo-1Bxi-BKzg-cgGC-W7hZ-43np-SsdyEq

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg01
  PV Size               1.01 GiB / not usable 3.44 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              258
  Free PE               258
  Allocated PE          0
  PV UUID               pDa7Mj-dCs4-cY0V-4JZY-viUG-BSNI-aspjlN

[[email protected] ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.02 GiB
  PE Size               4.00 MiB
  Total PE              516
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       260 / 1.02 GiB
  VG UUID               FUhTIi-lmOr-eVOi-JxMA-0pMJ-yMQq-8gSlCa

[[email protected] ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                S4HQMH-jxjp-0WbT-TkiV-Ftc7-dn58-7l3nsy
  LV Write Access        read/write
  LV Creation host, time jw, 2017-12-22 23:06:19 +0800
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
[[email protected] ~]# mkfs.ext4 /dev/vg01/lv01   ——格式化lvm,文件系统为ext4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# mkdir /testlvm
[[email protected] ~]# mount /dev/vg01/lv01 /testlvm/ 挂载lvm
[[email protected] ~]# df -TH
Filesystem            Type   Size  Used Avail Use% Mounted on
/dev/sda3             ext4    19G  4.7G   14G  27% /
tmpfs                 tmpfs  515M     0  515M   0% /dev/shm
/dev/sda1             ext4   204M   31M  163M  16% /boot
/dev/mapper/vg01-lv01 ext4   **1.1G**   35M  969M   4% /testlvm
[[email protected] ~]#
这里演示lv扩容,把/testlvm加大500M
[[email protected] ~]# vgs——首先查看vg是否有可用空间
  VG   #PV #LV #SN Attr   VSize VFree——VFree还有1G空闲
  vg01   2   1   0 wz--n- 2.02g 1.02g
[[email protected] ~]# lvextend -L +500M /dev/vg01/lv01  ——有空间直接扩展
  Extending logical volume lv01 to 1.49 GiB
  Logical volume lv01 successfully resized
[[email protected] ~]# 
---------------------------------------------------
如果vgs查看没有空间,则需要添加vg,新格式化一个分区sdc1,这里不演示格式化步骤;
vgextend vg01 /dev/sdc1
Lvextend -L 500M /dev/vg01/lv01
————————————————————
执行以上lv扩容后,执行df -Th 发现还是没有扩容,
linux7对文件系统进行扩容执行以下命令
[[email protected] ~]# xfs_growfs /dev/vg01/lv01
linux6对文件系统进行扩容执行以下命令
[[email protected] ~]# resize2fs /dev/vg01/lv01
这里演示的是格式化ext4,所以使用resize2fs
[[email protected] ~]# resize2fs /dev/vg01/lv01 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lv01 is mounted on /testlvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg01/lv01 to 390144 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 390144 blocks long.

[[email protected] ~]# df -TH
Filesystem            Type   Size  Used Avail Use% Mounted on
/dev/sda3             ext4    19G  4.7G   14G  27% /
tmpfs                 tmpfs  515M     0  515M   0% /dev/shm
/dev/sda1             ext4   204M   31M  163M  16% /boot
/dev/mapper/vg01-lv01 ext4   **1.6G**   36M  1.5G   3% /testlvm
[[email protected] ~]# 

删除lvm需要先umount
umount /dev/vg01/lv01
移除lv
lvremove /dev/vg01/lv01
移除vg
vgremove vg01
移除pv
pvremove /dev/sdb{1,2}

以上是关于菜鸟学习Linux LVM及配置实战的主要内容,如果未能解决你的问题,请参考以下文章

Linux7/Centos7磁盘分区格式化及LVM管理

《Linux菜鸟入门2》LVM

linux学习33 运维高级技能-在Linux实现LVM逻辑卷及快照

linux LVM 实战运用

linux CentOS 7 中LVM讲解配置实例及磁盘扩容等

Liunx系统下进行LVM的创建以及相关磁盘配额——实战篇