设为首页 收藏本站
查看: 1834|回复: 0

[经验分享] LVM逻辑卷管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-31 08:49:14 | 显示全部楼层 |阅读模式
本次内容
        1、什么是LVM
        2、为什么使用LVM
        3、LVM的操作命令

        4、LVM的具体操作实现


一、什么是LVM
    LVM:Logical Volume Manager,即逻辑卷管理,它是Linux环境下的一种磁盘管理方式,其功能在于能弹性调整文件系统的容量。LVM中有几个专业名词,重点来说以下一个
    PV:物理卷
        LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
    VG:卷组
        由一个或多个PV构成,为上层的逻辑卷提供空间
    LV:逻辑卷
        类似于磁盘的分区,建立在VG之上,可以再其上面创建文件系统
    PE:PV的基本存储单元,通常大小为4MB
    关系如下图所示:
wKioL1fE6rrzWlosAABEK7N3RX0925.jpg
    可以理解为将PV打碎成一个个的PE,然后将这所有的PE组合成一个VG,此时VG就由所有的PE构成,创建LV时,就是将PE分配给LV,在LV上面我们可以创建文件系统并挂载使用了。

二、为什么使用LVM
    想象一下,如果当初装机时候你给其中一个分区20G,并且将它挂载在/home目录上,如果有一天,其中的数据太多,20G已经不够用了,这么搞呢,可能也就是新增加一块硬盘,重新分区并且格式化,然后将/home下的数据完整的复制过来,然后卸载原来的分区,挂载新的分区。好一个繁琐的过程,而且如果下次空间又满了,还得重新重复以上的步骤。
    此时,LVM的光辉就显现出来了,最大的作用就是在于可以弹性的更改LV的的容量。那么问题来了,它是怎样弹性更改LV的容量,如下图所示
wKioL1fFDgfCfPMtAAB2Ce_YG80172.jpg



    如果LV的空间不足时,分三种情况:
        1、当VG还有空间时,直接将VG中的PE分配给LV,扩大其容量
        2、如果VG空间分配完毕,应该向VG中增加PV,从而扩展VG的容量,然后步骤同1
        3、如果现有PV使用完,可以将新的分区或者硬盘创建为PV,然后步骤同2

三、LVM的操作命令
    1、PV管理工具
        创建pv
            pvcreate /dev/DEVICE

       显示pv信息
            pvs:简要pv信息显示
            pvdisplay
    2、vg管理工具

        显示卷组
            vgs
            vgdisplay
        创建卷组
            vgcreate [-s #[kKmMgGtTpPeE]] VGNAME PVPATH
        管理卷组
            vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
            vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
        删除卷组
            先做pvmove,再做vgremove
    3、lv管理工具
        显示逻辑卷
            lvs
            Lvdisplay
        创建逻辑卷
            lvcreate-L #[mMgGtT] -n NAME VolumeGroup
        删除逻辑卷
            lvremove/dev/VG_NAME/LV_NAME
        重设文件系统大小
            fsadm[options] resize device [new_size[BKMGTEP]]
            resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
    4、扩展和缩减逻辑卷
        扩展逻辑卷:
            # lvextend-L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
            # resize2fs /dev/VG_NAME/LV_NAME
        缩减逻辑卷:
            # umount/dev/VG_NAME/LV_NAME
            # e2fsck -f /dev/VG_NAME/LV_NAME
            # resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
            # lvreduce-L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
            # mount


四、LVM具体是如何实现的
    如何在自己的虚拟机上实现LVM逻辑卷管理?步骤如下;
    1)创建分区并且将分区类型更改为8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[iyunv@localhost ~]# fdisk /dev/sdb    //对/dev/sdb硬盘进行分区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n            //创建一个新的分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-167772159, default 2048):  
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): +100M
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e        //更改分区类型为8e
Changed type of partition 'Linux' to 'Linux LVM'    //显示更改为Linux LVM

Command (m for help): p                    //查看一下刚才创建的分区,system类型显示为Linux LVM

Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   8e  Linux LVM

Command (m for help): w        //存盘退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[iyunv@localhost ~]# partprobe //更新内存信息



    重复上面步骤创建/dev/sdb2,/dev/sdb3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@localhost ~]# fdisk /dev/sdb        
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p            //显示分区信息

Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   8e  Linux LVM    //现在共有三个LVM类型的分区
/dev/sdb2          206848      411647      102400   8e  Linux LVM
/dev/sdb3          411648      616447      102400   8e  Linux LVM



    2)创建PV
1
2
3
4
[iyunv@localhost ~]# pvcreate /dev/sdb{1,2,3}    //将/dev/sdb{1,2,3}创建为PV
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created



    3)创建VG
1
2
[iyunv@localhost ~]# vgcreate myvg /dev/sdb{1,2}  //将/dev/sdb{1,2}创建为卷组,卷组名为myvg
  Volume group "myvg" successfully created



    4)创建LV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@localhost ~]# lvcreate -n mylv -L 100M myvg    //创建大小为100M,名字为mylv的LV
  Logical volume "mylv" created.
[iyunv@localhost ~]# lvdisplay         //查看系统上LV的信息,这里仅仅有且只有一个,即刚刚创建的
  --- Logical volume ---
  LV Path                /dev/myvg/mylv    //路径为/dev/myvg/mylv
  LV Name                mylv              //名称为mylv
  VG Name                myvg              //卷组名为myvg
  LV UUID                OkCyEj-sTkC-6Kch-svPr-UAgv-mQuf-uRofNj
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-08-30 14:46:48 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB        //大小为100MB
  Current LE             25                //LE数量,其实就是PE,因为被分配到了LV里面,所以改叫LE           
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0



    5)格式化并挂载使用
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost ~]# mkfs.ext4 /dev/myvg/mylv //将mylv格式化ext4的文件系统
[iyunv@localhost ~]# mount /dev/myvg/mylv /testdir/    //挂载到/testdir目录下
[iyunv@localhost ~]# df
Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/sda2              41922560  931284  40991276   3% /
devtmpfs                 486140       0    486140   0% /dev
tmpfs                    500664       0    500664   0% /dev/shm
tmpfs                    500664    6936    493728   2% /run
tmpfs                    500664       0    500664   0% /sys/fs/cgroup
/dev/sda3              20961280 3149160  17812120  16% /usr
/dev/sda1                496300  140512    355788  29% /boot
tmpfs                    100136       0    100136   0% /run/user/0
/dev/mapper/myvg-mylv     95054    1550     86336   2% /testdir    //挂载成功!



    6)进入挂载目录并且正常使用
1
2
3
4
5
6
7
8
[iyunv@localhost ~]# cd /testdir/    //进入挂载的目录
[iyunv@localhost testdir]# ls
lost+found
[iyunv@localhost testdir]# touch f1    //创建文件
[iyunv@localhost testdir]# ls        //创建成功!
f1  lost+found
[iyunv@localhost testdir]# pwd
/testdir



    如果此分区的空间即将被填满,如何扩展LV的容量?步骤如下
    1)如果VG还有空间
1
2
3
4
5
6
7
[iyunv@localhost testdir]# lvextend -L +50M /dev/myvg/mylv  //增加50M
  Rounding size to boundary between physical extents: 52.00 MiB
  Size of logical volume myvg/mylv changed from 100.00 MiB (25 extents) to 152.00 MiB (38 extents).
  Logical volume mylv successfully resized.
[iyunv@localhost testdir]# lvs    //简略显示LV的信息
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 152.00m   //增加了52M,以为PE是最小存储单元,所以必须是PE的整数倍



     虽然LV显示已经增加,但是已经挂载的目录好像有些问题
1
2
3
4
5
6
7
8
9
10
11
[iyunv@localhost testdir]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               40G  910M   40G   3% /
devtmpfs               475M     0  475M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  483M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda3               20G  3.1G   17G  16% /usr
/dev/sda1              485M  138M  348M  29% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/myvg-mylv   93M  1.6M   85M   2% /testdir    //容量并没有增加



    此时需要从新设置文件系统的容量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@localhost testdir]# resize2fs /dev/myvg/mylv  //重新设置LV的容量
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /testdir; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/myvg/mylv is now 155648 blocks long.

[iyunv@localhost testdir]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               40G  910M   40G   3% /
devtmpfs               475M     0  475M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  483M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda3               20G  3.1G   17G  16% /usr
/dev/sda1              485M  138M  348M  29% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/myvg-mylv  144M  1.6M  133M   2% /testdir    //容量成功增加



    2)如果VG空间分配完毕,此时需要向VG中添加PV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[iyunv@localhost testdir]# vgextend myvg /dev/sdb3     //将之前创建的PV/dev/sdb3添加至卷组myvg
  Volume group "myvg" successfully extended
[iyunv@localhost testdir]# vgdisplay  //查看VG的详细信息
  --- Volume group ---
  VG Name               myvg
  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               288.00 MiB    //增加了/dev/sdb3的容量,现在为288MB的空间。增加成功!
  PE Size               4.00 MiB
  Total PE              72
  Alloc PE / Size       38 / 152.00 MiB    //已经分配的PE/空间大小
  Free  PE / Size       34 / 136.00 MiB    //空闲的的PE/空间大小
  VG UUID               ObuSOV-RmEg-m2Ec-GIe1-JzhC-E3kd-1XgOP



    VG空间成功增加,以下步骤同上1
    3)如果PV使用完毕,那就创建分区,并且更改分区类型为8e,然后pvcreate。(创建PV时候也可以直接使用磁盘)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[iyunv@localhost testdir]# fdisk /dev/sdc   
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd7e4d605

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048      206847      102400   8e  Linux LVM        //LVM类型的分区
[iyunv@localhost testdir]# pvcreate /dev/sdc1    //创建/dev/sdc1为PV
  Physical volume "/dev/sdc1" successfully created
[iyunv@localhost ~]# pvcreate /dev/sd{d,e}    //将/dev/sd{d,e}创建为PV
  Physical volume "/dev/sdd" successfully created
  Physical volume "/dev/sde" successfully created



    如果你觉得逻辑卷分配的空间太多了,也可以缩减逻辑卷
    1、卸载LV
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@localhost ~]# df -h        //查看磁盘挂载信息
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               40G  910M   40G   3% /
devtmpfs               475M     0  475M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.9M  483M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda3               20G  3.1G   17G  16% /usr
/dev/sda1              485M  138M  348M  29% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/myvg-mylv  144M  1.6M  132M   2% /testdir     //已使用1.6M,剩余132M可用
[iyunv@localhost ~]# umount /dev/myvg/mylv         //注意:缩减逻辑卷时候必须先卸载



    2、检测文件系统 (如果不执行这一步,直接执行第三步时候会报错)
1
2
3
4
5
6
7
8
[iyunv@localhost ~]# e2fsck -f /dev/myvg/mylv
e2fsck 1.42.9 (28-Dec-2013)
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/myvg/mylv: 12/37544 files (8.3% non-contiguous), 10390/155648 blocks



    3、重设文件系统的大小
1
2
3
4
[iyunv@localhost ~]# resize2fs /dev/myvg/mylv 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 102400 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 102400 blocks long.



    4、缩减LV的容量
1
2
3
4
5
6
7
8
9
[iyunv@localhost ~]# lvreduce -L 100M /dev/myvg/mylv     //将LV缩减到100M
  WARNING: Reducing active logical volume to 100.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: yes
  Size of logical volume myvg/mylv changed from 152.00 MiB (38 extents) to 100.00 MiB (25 extents).
  Logical volume mylv successfully resized.
[iyunv@localhost ~]# lvs                //查看LV的大小
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 100.00m         //显示100M,缩减成功



    5、重新挂载并查看
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@localhost ~]# mount /dev/myvg/mylv /testdir/
[iyunv@localhost ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               40G  910M   40G   3% /
devtmpfs               475M     0  475M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  483M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda3               20G  3.1G   17G  16% /usr
/dev/sda1              485M  138M  348M  29% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/myvg-mylv   93M  1.6M   85M   2% /testdir        //缩减成功!



    注意缩减时候,计算好要缩减的空间大小,缩减的空间大小如果比已经使用的空间还要小,那就会出问题了。而且,为避免文件损坏,缩减时候一定要首先卸载逻辑卷,然后再检查一次文件系统。所以,缩减前最好对原始数据做好备份。
                                                                                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-265456-1-1.html 上篇帖子: Linux系统通过com口配置交换机 下篇帖子: CentOS 6.8不重启在线增加硬盘
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表