Linux之LVM篇

一、什么是LVM?

LVM是 Logical Volume Manager(逻辑卷管理)的简写。
它是Linux环境下对磁盘分区进行管理的一种机制,它由HeinzMauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

二、LVM的建立。

首先,先给大家普及以下Linux中lvm中的一些常见符号及意义。

pv		##物理卷:被lv命令处理过的物理分区

vg		##物理卷组:被组装到一起的物理卷

pe		##实体延伸区块:PE是LVM最小的存储单位,一般为4MB大小,它是构成VG的基本单位

lvm		##逻辑卷:直接使用的设备,可以增大缩减并保持原有数据不变

1、xfs文件系统的LVM管理

1)分区时定分区标签为8e。

[root@localhost ~]# fdisk /dev/vdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9ee14f08.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (206848-20971519, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +100M
Partition 2 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (411648-20971519, default 411648): 
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +100M
Partition 3 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): 
Using default response e
Selected partition 4
First sector (616448-20971519, default 616448): 
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): 
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 
[root@localhost ~]# fdisk /dev/vdb
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): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496): 
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): 
Using default value 20971519
Partition 5 of type Linux and of size 9.7 GiB is set

Command (m for help): d
Partition number (1-5, default 5): 5
Partition 5 is deleted

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496): 
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +500M
Partition 5 of type Linux and of size 500 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (1644544-20971519, default 1644544): 
Using default value 1644544
Last sector, +sectors or +size{K,M,G} (1644544-20971519, default 20971519): +500M
Partition 6 of type Linux and of size 500 MiB is set

Command (m for help): t 
Partition number (1-6, default 6): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496     1642495      512000   8e  Linux LVM
/dev/vdb6         1644544     2668543      512000   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

在这里插入图片描述

2)创建物理卷

pvcreate /dev/vdb5		##创建物理卷
pvcreate /dev/vdb6

示例:
采用以下命令监视物理卷、物理卷组、逻辑卷、以及逻辑卷挂载点的有关数据。

watch -n 1 "pvs;echo ===;vgs;echo ===;lvs;echo ===;df -h /mnt"

在这里插入图片描述

3)创建物理卷组

vgcreate vg0 /dev/vdb5		##创建物理卷组
vgextend vg0 /dev/vdb6

在这里插入图片描述
分析:把第一块物理卷加入物理卷组时用vgcreate,之后增添物理卷组都用vgextend。

4)创建逻辑卷及挂载逻辑卷

lvcreate -L 20M -n lv0 vg0	##创建逻辑卷
mkfs.xfs /dev/vg0/lv0		##格式化为xfs的文件系统
mount /dev/vg0/lv0 /mnt		##挂载到mnt

在这里插入图片描述
测试:

[root@localhost ~]# df -H /mnt
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   18M  1.2M   17M   7% /mnt

5)如何增添逻辑卷

pvcreate /dev/vdb7				##创建物理卷标签为8e
vgextend vg0 /dev/vdb9			##增添物理卷组
lvextend -L 1500M /dev/vg0/lv0	##拉伸逻辑卷为1500M
xfs_growfs /dev/vg0/lv0			##拉伸文件系统使1500M的逻辑卷可用

示例:

[root@localhost ~]# vgcreate vg0 /dev/vdb5
  Volume group "vg0" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb6
  Volume group "vg0" successfully extended
[root@localhost ~]# lvcreate -L 20M -n lv0 vg0
  Logical volume "lv0" created
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0 
meta-data=/dev/vg0/lv0           isize=256    agcount=1, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/vg0/lv0 /mnt
[root@localhost ~]# df -H /mnt
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   18M  1.2M   17M   7% /mnt
[root@localhost ~]# fdisk /dev/vdb
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): n
All primary partitions are in use
Adding logical partition 7
First sector (2670592-20971519, default 2670592): 
Using default value 2670592
Last sector, +sectors or +size{K,M,G} (2670592-20971519, default 20971519): +1G
Partition 7 of type Linux and of size 1 GiB is set

Command (m for help): t
Partition number (1-7, default 7): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe 
[root@localhost ~]# pvcreate /dev/vdb7
  Physical volume "/dev/vdb7" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb7
  Volume group "vg0" successfully extended
[root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0 
  Extending logical volume lv0 to 1.46 GiB
  Logical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs /dev/vg0/lv0 
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=1, agsize=5120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 5120 to 384000

在这里插入图片描述

2、ext4文件系统的硬盘缩减及拉伸

前几步与xfs文件系统的相同就不重复介绍了。

1、创建逻辑卷,将逻辑卷格式化为ext4的文件系统。

mkfs.ext4 /dev/vg0/lv0		##lv0格式化为ext4文件系统

在这里插入图片描述

2、挂载ext4文件系统的lv0到/mnt,及拉伸逻辑卷、拉伸文件系统。

mount /dev/vg0/lv0 /mnt/		##挂载
lvextend -L 1800 /dev/vg0/lv0	##拉伸逻辑卷
resize2fs /dev/vg0/lv0 			##拉伸ext4文件系统

在这里插入图片描述

3、缩减逻辑卷及文件系统

umount /mnt/					##卸载
e2fsck -f /dev/vg0/lv0 			##检查lv0数据
resize2fs /dev/vg0/lv0 1000M	##缩减ext4文件系统
mount /dev/vg0/lv0 /mnt/		##挂载
lvreduce -L 1000M /dev/vg0/lv0 	##缩减逻辑卷
pvmove /dev/vdb6 /dev/vdb7 		##把vdb6的数据移动到vdb7
vgreduce vg0 /dev/vdb6			##把vdb6移除vg0物理卷组
pvremove /dev/vdb6				##把vdb6移除物理卷

示例:

[root@localhost ~]# umount /mnt/
[root@localhost ~]# e2fsck -f /dev/vg0/lv0 
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/vg0/lv0: 11/120000 files (9.1% non-contiguous), 16862/460800 blocks
[root@localhost ~]# resize2fs /dev/vg0/lv0 1000M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 256000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 256000 blocks long.

[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# lvreduce -L 1000M /dev/vg0/lv0 
  WARNING: Reducing active and open logical volume to 1000.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 1000.00 MiB
  Logical volume lv0 successfully resized
[root@localhost ~]# pvmove /dev/vdb6 /dev/vdb7
  No data to move for vg0
[root@localhost ~]# vgreduce vg0 /dev/vdb6
  Removed "/dev/vdb6" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb6
  Labels on physical volume "/dev/vdb6" successfully wiped
[root@localhost ~]# 

在这里插入图片描述
在这里插入图片描述

三、创建逻辑卷快照。

lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0	##创建逻辑卷快照
mount /dev/vg0/lv0_snap /mnt/ 							##挂载快照

在这里插入图片描述
在这里插入图片描述小测试:模拟逻辑卷快照的损坏及重新拍摄快照。

[root@localhost ~]# umount /mnt/
[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# touch /mnt/file{1..9}
[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "lv0_snap" created
[root@localhost ~]# mount /dev/vg0/lv0_snap /mnt/
[root@localhost ~]# ls /mnt/*
/mnt/file1  /mnt/file3  /mnt/file5  /mnt/file7  /mnt/file9
/mnt/file2  /mnt/file4  /mnt/file6  /mnt/file8

/mnt/lost+found:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# ls /mnt/*
ls: cannot access /mnt/*: No such file or directory
[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvremove /dev/vg0/lv0_snap
Do you really want to remove active logical volume lv0_snap? [y/n]: y
  Logical volume "lv0_snap" successfully removed
[root@localhost ~]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "lv0_snap" created
[root@localhost ~]# mount /dev/vg0/lv0_snap /mnt/
[root@localhost ~]# ls /mnt/*
/mnt/file1  /mnt/file3  /mnt/file5  /mnt/file7  /mnt/file9
/mnt/file2  /mnt/file4  /mnt/file6  /mnt/file8

/mnt/lost+found:
[root@localhost ~]# 

在这里插入图片描述
在这里插入图片描述

四、删除LVM

lvremove  /dev/vg0/lv0_snap 		##删除逻辑卷快照
lvremove  /dev/vg0/lv0 				##删除逻辑卷
vgremove vg0						##删除物理卷组 
pvremove  /dev/vdb5				##移除物理卷
pvremove  /dev/vdb7					##移除物理卷
fdisk  /dev/vdb						##删除LVM分区
partprobe 							##重新加载分区表

示例:

[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvremove /dev/vg0/lv0_snap
Do you really want to remove active logical volume lv0_snap? [y/n]: y
  Logical volume "lv0_snap" successfully removed
[root@localhost ~]# lvremove /dev/vg0/lv0
Do you really want to remove active logical volume lv0? [y/n]: y
  Logical volume "lv0" successfully removed
[root@localhost ~]# vgremove vg0
  Volume group "vg0" successfully removed
[root@localhost ~]# pvremove /dev/vdb5
  Labels on physical volume "/dev/vdb5" successfully wiped
[root@localhost ~]# pvremove /dev/vdb7
  Labels on physical volume "/dev/vdb7" successfully wiped
[root@localhost ~]# fdisk /dev/vdb
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): d
Partition number (1-7, default 7): 5
Partition 5 is deleted

Command (m for help): d
Partition number (1-6, default 6): 6
Partition 6 is deleted

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5         1644544     2668543      512000   8e  Linux LVM

Command (m for help): d
Partition number (1-5, default 5): 5
Partition 5 is deleted

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值