LINUX磁盘-LVM

 

作者:MP

归档:学习笔记

2017/11/25

 


格式说明:

蓝色字体:注释

×××背景:重要

绿色背景:注意


 

 

LVM... 1

1LVM原理图... 1

2LVM相关名词解释... 1

1.1 物理存储介质:例如磁盘等... 2

1.2 物理卷PV:磁盘分区、raid... 2

1.3 卷组VG:PV容量组件的池(pv的集合等)、类似于一块待分区的磁盘”. 2

1.4 逻辑卷LVVG的分区,再次上可以建立文件系统挂载等使用。类似于磁盘的分区可以被格式化、挂载使用... 2

1.5 逻辑块LE:逻辑卷上最小的容量单位,默认是4MB.. 2

演示环境:... 2

2 创建LVM过程... 3

2.1 使用fdisk初始化磁盘-选择磁盘格式为8e. 3

2.1.1 初始化/dev/sdb. 3

2.1.2 初始化/dev/sdc. 4

2.2 创建PV.. 4

2.3 创建VG.. 5

2.4 VG中划分LV.. 6

2.5 格式化、挂载分区... 7

3 扩容LVM... 7

3.1 首先逻辑扩展(扩展LV容量)... 7

3.2 其次文件系统的扩展... 8

4 缩小LVM... 9

4.1 卸载、检查... 9

4.2 缩减文件系统的容量... 9

4.3 在缩减LV的容量... 10

4.4 重新挂载... 10

 

LVM

·        LVM Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。

LVM解决了分区大小分配磁盘扩展问题。

例如:

    在创建的系统的时候讲/date 分配挂载在/目录下,后期由于业务需求需要独立出来。重新挂载一个新的分区,期中之前的数据将无法使用,虽然可以使用cp等命令将数据迁移。但是若果新挂载的这个硬盘容量也满了了?挂载一个新的更大容量的硬盘重新挂载?若是长期使用这种办法也是也可以,但是资源浪费严重。

LVM就是打破物理存储限制、可以随意增加容量、缩减容量。



1LVM原理图

LVM原理.jpg


2LVM相关名词解释

 

1.1 物理存储介质:例如磁盘等

1.2 物理卷PV:磁盘分区、raid

1.3 卷组VG:PV容量组件的池(pv的集合等)、类似于一块待分区的磁盘

1.4 逻辑卷LVVG的分区,再次上可以建立文件系统挂载等使用。类似于磁盘的分区可以被格式化、挂载使用

1.5 逻辑块LE:逻辑卷上最小的容量单位,默认是4MB

演示环境:



演示环境:

    VMware workstation 12.0.1 build-3160714

    centos 6.9

        挂载了两块新的虚拟磁盘容量分别为1GB 2GB

[root@linux-edu ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1   *           1          26      204800   83  Linux
/dev/sda2              26         157     1048576   82  Linux swap / Solaris
/dev/sda3             157        2611    19717120   83  Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk /dev/sdc: 2147 MB, 2147483648 bytes
[root@linux-edu ~]#

 LVM常用命令

阶段

显示

创建

删除

增加

缩小

PV

pvdisplay

pvcreat

pvremove



VG

vgdisplay

vgcreat

vgremove

vgextend

vgreduce

LV

lvdisplay

lvcreat

lvremove

lvextend

lvreduce

 



第2章 创建LVM过程

2.1 使用fdisk初始化磁盘-选择磁盘格式为8e

            创建流程:新建主分区-编号为1-容量所有-选择格式为LVM-保存

2.1.1  初始化/dev/sdb

[root@linux-edu ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9396a336.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): 1
First cylinder (1-130, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): 
Using default value 130
Command (m for help): p
Disk /dev/sdb: 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: 0x9396a336
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  83  Linux
Command (m for help): t    ###
选择格式
Selected partition 1
Hex code (type L to list codes): 8e      ##
格式设置为8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 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: 0x9396a336
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  8e  Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@linux-edu ~]#

2.1.2  初始化/dev/sdc

步骤如上

[root@linux-edu ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1   *           1          26      204800   83  Linux
/dev/sda2              26         157     1048576   82  Linux swap / Solaris
/dev/sda3             157        2611    19717120   83  Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
/dev/sdb1               1         130     1044193+  8e  Linux LVM
Disk /dev/sdc: 2147 MB, 2147483648 bytes
/dev/sdc1               1         261     2096451   8e  Linux LVM
[root@linux-edu ~]#

2.2 创建PV

#创建pv
[root@linux-edu ~]# pvcreate /dev/sd[bc]1
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdc1" successfully created
[root@linux-edu ~]# 
#
查看pv
[root@linux-edu ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "1019.72 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name              
  PV Size               1019.72 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               GVfctN-vmJQ-3A6M-ogcd-fVbi-udQD-RRu03q
  
  "/dev/sdc1" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name              
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               74mwlF-SBuT-U19r-JQZk-1BOj-NdMd-cdXIFg
  
[root@linux-edu ~]#

2.3 创建VG

[root@linux-edu ~]# vgcreate disk01 /dev/sd[bc]1     ###创建VG命名为disk01
  Volume group "disk01" successfully created
[root@linux-edu ~]# vgdisplay 
  --- Volume group ---
  VG Name               disk01    #VG
名称
  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               2.99 GiB    ##VG
总容量
  PE Size               4.00 MiB    ##LE
块的大小
  Total PE              765         ##LE
块的数量  
  Alloc PE / Size       0 / 0   
  Free  PE / Size       765 / 2.99 GiB
  VG UUID               fj09w3-RSVt-oRxH-nETB-SsC3-C8i9-CxLA5j
   
[root@linux-edu ~]# 

2.4 VG中划分LV

lvcreate 命令常用参数:

    -L 设置指定大小

    -n 设置指定名称

[root@linux-edu ~]# lvcreate -L 100M -n LVM_date01 disk01
  Logical volume "LVM_date01" created.
[root@linux-edu ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/disk01/LVM_date01
  LV Name                LVM_date01    #LV
名称
  VG Name                disk01        #
所属VG名称
  LV UUID                g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
  LV Write Access        read/write
  LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB     ##LV
的总容量
  Current LE             25                ##LE
的个数
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[root@linux-edu ~]# 

2.5 格式化、挂载分区

[root@linux-edu ~]# mkfs.ext4 /dev/disk01/LVM_date01 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
......
[root@linux-edu /]# mount /dev/disk01/LVM_date01 /data/
[root@linux-edu /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              19G  1.5G   17G   9% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   35M  146M  19% /boot
/dev/mapper/disk01-LVM_date01
                       93M  1.6M   87M   2% /data
[root@linux-edu /]# 

至此LVM分区创建完毕

第3章 扩容LVM

3.1  首先逻辑扩展(扩展LV容量)

        使用的命令:

               lvextend 扩展LV容量

                    常用参数: -L 500M   #将容量扩展到500M

                                    -L +500M  #在原有的基础上加上500M

[root@linux-edu /]# lvextend -L +200M /dev/disk01/LVM_date01 
  Size of logical volume disk01/LVM_date01 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
  Logical volume LVM_date01 successfully resized.
[root@linux-edu /]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/disk01/LVM_date01
  LV Name                LVM_date01
  VG Name                disk01
  LV UUID                g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
  LV Write Access        read/write
  LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
  LV Status              available
  # open                 1
  LV Size                300.00 MiB
  Current LE             75
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[root@linux-edu /]#                                   

3.2  其次文件系统的扩展

        使用的命令是:resize2fs          

[root@linux-edu /]# resize2fs -p /dev/disk01/LVM_date01 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/disk01/LVM_date01 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/disk01/LVM_date01 to 307200 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 307200 blocks long.
[root@linux-edu /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              19G  1.5G   17G   9% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   35M  146M  19% /boot
/dev/mapper/disk01-LVM_date01
                      287M  2.1M  270M   1% /data
[root@linux-edu /]# 

至此扩容成功

第4章 缩小LVM

 缩减容量是一件危险的操作;缩减必须在离线状态下执行;并且必须先强制检查文件系统错误,防止缩减过程损坏数据;

4.1   卸载、检查

[root@linux-edu /]# umount /dev/disk01/LVM_date01  
[root@linux-edu /]# e2fsck -f /dev/disk01/LVM_date01
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/disk01/LVM_date01: 11/75088 files (9.1% non-contiguous), 15637/307200 blocks
[root@linux-edu /]#

4.2   缩减文件系统的容量


[root@linux-edu /]# resize2fs /dev/disk01/LVM_date01 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/disk01/LVM_date01 to 102400 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 102400 blocks long.
[root@linux-edu /]# 

4.3   在缩减LV的容量

[root@linux-edu /]# lvreduce -L 100M /dev/disk01/LVM_date01 
  WARNING: Reducing active logical volume to 100.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce disk01/LVM_date01? [y/n]: y
  Size of logical volume disk01/LVM_date01 changed from 300.00 MiB (75 extents) to 100.00 MiB (25 extents).
  Logical volume LVM_date01 successfully resized.
[root@linux-edu /]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/disk01/LVM_date01
  LV Name                LVM_date01
  VG Name                disk01
  LV UUID                g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
  LV Write Access        read/write
  LV Creation host, time linux-edu, 2017-11-25 20:05:04 +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     256
  Block device           253:0
   
[root@linux-edu /]# 

4.4   重新挂载

 

[root@linux-edu /]# mount /dev/disk01/LVM_date01 /data/
[root@linux-edu /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              19G  1.5G   17G   9% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   35M  146M  19% /boot
/dev/mapper/disk01-LVM_date01
                       93M  1.6M   87M   2% /data
[root@linux-edu /]# 

 

本文借鉴前辈、重新整理。