Linux学习 LVM ***

时间:2022-05-20 21:43:10

一、前言

LVM,逻辑卷管理工具,它的作用是提供一种灵活的磁盘管理办法。通常我们的某个分区用完了,想要扩容,很麻烦。但是用lvm就可以很方便的扩容,收缩。

看它的原理图:

Linux学习 LVM ***

它的原理大致是:首先将磁盘做成物理卷,然后物理卷再组成卷组,卷组下面再划分逻辑卷。

二、实现一个lvm

步骤:

2.1 做物理卷。

首先看看我们的磁盘/dev/sdb的分区情况:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# fdisk -l /dev/sdb

磁盘 /dev/sdb:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x2f45f776 设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 6293504 7931903 819200 83 Linux
/dev/sdb3 2099200 6293503 2097152 5 Extended
/dev/sdb5 2101248 4198399 1048576 83 Linux
/dev/sdb6 4200448 5838847 819200 83 Linux
Linux学习 LVM ***

接下来,我们把sdb1,sdb2,sdb5的分区id修改为8e:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 命令(输入 m 获取帮助):t
分区号 (1-3,5,6,默认 6):1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):t
分区号 (1-3,5,6,默认 6):2
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):t
分区号 (1-3,5,6,默认 6):5
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x2f45f776 设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 6293504 7931903 819200 8e Linux LVM
/dev/sdb3 2099200 6293503 2097152 5 Extended
/dev/sdb5 2101248 4198399 1048576 8e Linux LVM
/dev/sdb6 4200448 5838847 819200 83 Linux Partition table entries are not in disk order
Linux学习 LVM ***

如果是新创建的分区,找不到的话,使用partprobe命令刷新一下。

运行pvcreate命令,创建物理卷(physical volum)

[root@ruanwenwu02 ~]# pvs
-bash: pvs: 未找到命令
[root@ruanwenwu02 ~]# pvdisplay
-bash: pvdisplay: 未找到命令

发现这些命令都没有的话,那么就需要安装lvm2:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# yum install lvm2
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 129 kB 00:00:00
(2/2): updates/7/x86_64/primary_db
Linux学习 LVM ***

安装好之后,就可以使用pvcreate命令了:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# pvcreate /dev/sdb1
WARNING: ext2 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext2 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@ruanwenwu02 ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@ruanwenwu02 ~]# pvcreate /dev/sdb5
Physical volume "/dev/sdb5" successfully created.
Linux学习 LVM ***

运行pvs或者pvdisplay命令能查看当前的物理卷:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 800.00m 800.00m
/dev/sdb5 lvm2 --- 1.00g 1.00g
[root@ruanwenwu02 ~]# pvdisplay
"/dev/sdb2" is a new physical volume of "800.00 MiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 800.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID FS2sy9-BTxn-XzCz-6ZBH-fL04-r1mB-p2EH91 "/dev/sdb5" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb5
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 6iKVQF-W01Z-1aMF-m1af-jmhX-Ff7c-r5QHcD "/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 lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc
Linux学习 LVM ***

2.2 创建卷组

Linux学习 LVM ***
[root@ruanwenwu02 ~]# vgcreate vg1 /dev/sdb
sdb sdb1 sdb2 sdb3 sdb5 sdb6
[root@ruanwenwu02 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created
[root@ruanwenwu02 ~]# vgcreate vg2 /dev/sdb5
Volume group "vg2" successfully created
[root@ruanwenwu02 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- 1.77g 1.77g
vg2 1 0 0 wz--n- 1020.00m 1020.00m
[root@ruanwenwu02 ~]# vgdisplay
--- Volume group ---
VG Name vg2
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE 255
Alloc PE / Size 0 / 0
Free PE / Size 255 / 1020.00 MiB
VG UUID aDXygA-rltJ-JWCQ-Nbvb-JML9-W3Hf-cVjG63 --- Volume group ---
VG Name vg1
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 1.77 GiB
PE Size 4.00 MiB
Total PE 454
Alloc PE / Size 0 / 0
Free PE / Size 454 / 1.77 GiB
VG UUID qxqwsD-oNGT-8ltJ-1Itj-IZfO-0rui-Yxvhh7
Linux学习 LVM ***

使用vgs或者vgdisplay可以看到卷组。

2.3 创建逻辑卷

现在我们创建了两个卷组,一个是vg1的1.7G,一个是vg2的1G。接下来我们创建逻辑卷:

[root@ruanwenwu02 ~]# lvcreate -L 200M -n lv1 vg1
Logical volume "lv1" created.

创建的逻辑需要被格式化之后才可以使用:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# mke2fs -t 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
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 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: 完成
Linux学习 LVM ***

我们采用的ext4的文件系统格式化的,使用xfs的文件系统格式化也是可以的。

我们把格式化完成之后/dev/vg1/lv1挂载到/mnt目录下

Linux学习 LVM ***
[root@ruanwenwu02 ~]# mkdir /mnt/vg1
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt/vg1
[root@ruanwenwu02 ~]# cat /etc/passwd >> /mnt/vg1/passwd
[root@ruanwenwu02 ~]# cat !$
cat /mnt/vg1/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ruanwenwu:x:1000:1000::/home/ruanwenwu:/bin/bash
Linux学习 LVM ***

到这里,我们的逻辑卷创建也结束了。

如果,我们要删除一个物理卷怎么操作呢?

Linux学习 LVM ***
[root@ruanwenwu02 ~]# pvremove /dev/sdb2
PV /dev/sdb2 is used by VG vg1 so please use vgreduce first.
(If you are certain you need pvremove, then confirm by using --force twice.)
/dev/sdb2: physical volume label not removed.
[root@ruanwenwu02 ~]# man vdreduce
没有 vdreduce 的手册页条目
[root@ruanwenwu02 ~]# man vgreduce
[root@ruanwenwu02 ~]# man vgreduce
[root@ruanwenwu02 ~]# vgreduce vg1 /dev/sdb2
Removed "/dev/sdb2" from volume group "vg1"
[root@ruanwenwu02 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 1 1 0 wz--n- 1020.00m 820.00m
vg2 1 0 0 wz--n- 1020.00m 1020.00m
[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- 1020.00m 820.00m
/dev/sdb2 lvm2 --- 800.00m 800.00m
/dev/sdb5 vg2 lvm2 a-- 1020.00m 1020.00m
[root@ruanwenwu02 ~]# pvremove /dev/sdb2
Labels on physical volume "/dev/sdb2" successfully wiped.
[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- 1020.00m 820.00m
/dev/sdb5 vg2 lvm2 a-- 1020.00m 1020.00m
Linux学习 LVM ***

代码里碰到一个问题。如果一个物理卷已经被加入一个卷组里,需要使用vgreduce命令把这个物理卷从卷组里删除,然后再运行pvremove命令。

除了删除物理卷之外,逻辑卷也是可以删除的,使用命令lvremove /dev/vg1/lv1。

Linux学习 LVM ***
[root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 200.00m
[root@ruanwenwu02 ~]# lvremove /dev/vg1/lv1
Logical volume vg1/lv1 contains a filesystem in use.
[root@ruanwenwu02 ~]# pwd
/root
[root@ruanwenwu02 ~]# rm -rf /dev/vg1/lv1/*
[root@ruanwenwu02 ~]# !lv
lvremove /dev/vg1/lv1
Logical volume vg1/lv1 contains a filesystem in use.
[root@ruanwenwu02 ~]# umount /dev/vg1/lv1
[root@ruanwenwu02 ~]# !lv
lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed
[root@ruanwenwu02 ~]# lvs
[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- 1020.00m 1020.00m
/dev/sdb5 vg2 lvm2 a-- 1020.00m 1020.00m
Linux学习 LVM ***

现在我们再次将逻辑卷加回来:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# history|grep lvcreate
235 lvcreate -L 200M -n lv1 vg1
273 history|lvcreate
274 history|grep lvcreate
[root@ruanwenwu02 ~]# lvcreate -L 200M -n lv1 vg1
WARNING: ext4 signature detected on /dev/vg1/lv1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg1/lv1.
Logical volume "lv1" created.
[root@ruanwenwu02 ~]# mke2fs -t 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
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 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: 完成 [root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 200.00m
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt/vg1
Linux学习 LVM ***

到这里,我们的LVM基本上做完了。我们来梳理一下我们现有有了那些东西:

物理卷:

[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- 1020.00m 820.00m
/dev/sdb5 vg2 lvm2 a-- 1020.00m 1020.00m

卷组:

[root@ruanwenwu02 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 1 1 0 wz--n- 1020.00m 820.00m
vg2 1 0 0 wz--n- 1020.00m 1020.00m

物理卷:

[root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 200.00m

三、接下来讲如何扩容。

(先以ext4文件系统举例)

Linux学习 LVM ***
[root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 200.00m
[root@ruanwenwu02 ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 300.00 MiB (75 extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt/vg1
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
/dev/vg1/lv1 is mounted.
e2fsck: 无法继续, 中止.
Linux学习 LVM ***

这说明ext4文件系统扩容的话需要先umount:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 300.00 MiB (75 extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt/vg1
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
/dev/vg1/lv1 is mounted.
e2fsck: 无法继续, 中止. [root@ruanwenwu02 ~]# umount /dev/vg1/lv1
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@ruanwenwu02 ~]# lvresize -L 300M /dev/vg1/lv1
New size (75 extents) matches existing size (75 extents).
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1
mount: 在 /etc/fstab 中找不到 /dev/vg1/lv1
[root@ruanwenwu02 ~]# e2fsck /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
/dev/vg1/lv1: clean, 11/51200 files, 12115/204800 blocks
[root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
请先运行 'e2fsck -f /dev/vg1/lv1'. [root@ruanwenwu02 ~]# resize2fs -f /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 307200 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 307200 blocks long. [root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 300.00m
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1
mount: 在 /etc/fstab 中找不到 /dev/vg1/lv1
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 11/77824 files (0.0% non-contiguous), 15987/307200 blocks
[root@ruanwenwu02 ~]# lvresize /dev/vg1/lv1
No command with matching syntax recognised. Run 'lvresize --help' for more information.
[root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 307200 blocks long. Nothing to do! [root@ruanwenwu02 ~]# ls -ld /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 11月 4 12:42 /dev/vg1/lv1 -> ../dm-0
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 287M 2.1M 266M 1% /mnt/vg1
Linux学习 LVM ***

总结一下步骤:

Linux学习 LVM ***
 lvresize -L 300M  /dev/vg1/lv1  重新设置卷大小

 e2fsck -f /dev/vg1/lv1 检查磁盘错误  (ext4执行)

 resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行)

 xfs_growfs /dev/vg1/lv1  xfs文件系统需要执行
Linux学习 LVM ***

到这里扩容就成功了。

四、缩容

步骤:

Linux学习 LVM ***
缩减逻辑卷(xfs不支持)
先umount e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext) resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext) lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
Linux学习 LVM ***

实验:

Linux学习 LVM ***
[root@ruanwenwu02 ~]# umount /dev/vg1/lv1
[root@ruanwenwu02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks
[root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 102400 blocks long. Nothing to do! [root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1 50M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 51200 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 51200 blocks long. [root@ruanwenwu02 ~]# lvresize -L 50M /dev/vg1/lv1
Rounding size to boundary between physical extents: 52.00 MiB.
WARNING: Reducing active logical volume to 52.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 100.00 MiB (25 extents) to 52.00 MiB (13 extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.4G 17G 8% /
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 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 45M 1.1M 40M 3% /mnt/vg1
Linux学习 LVM ***

五、扩展卷组

步骤:

Linux学习 LVM ***
fdisk /dev/sdb  新增/dev/sdb5(逻辑分区8e)  2G

 pvcreate /dev/sdb5

 vgextend vg1 /dev/sdb5

 lvresize -L 100M  /dev/vg1/lv1  重新设置卷大小
Linux学习 LVM ***

先创建一个分区,创建物理分区,将物理分区添加到卷组,然后重新分配卷大小。

Linux学习 LVM ***
[root@ruanwenwu02 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 命令(输入 m 获取帮助):t
分区号 (1-3,5,6,默认 6):6
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):q [root@ruanwenwu02 ~]# pvcreate /dev/sdb6
WARNING: ext4 signature detected on /dev/sdb6 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb6.
Physical volume "/dev/sdb6" successfully created.
[root@ruanwenwu02 ~]# vgex
vgexport vgextend
[root@ruanwenwu02 ~]# vgex
vgexport vgextend
[root@ruanwenwu02 ~]# vgextend vg1 /dev/sdb6
Volume group "vg1" successfully extended
[root@ruanwenwu02 ~]# vgdisplay -v
--- Volume group ---
VG Name vg2
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE 255
Alloc PE / Size 0 / 0
Free PE / Size 255 / 1020.00 MiB
VG UUID aDXygA-rltJ-JWCQ-Nbvb-JML9-W3Hf-cVjG63 --- Physical volumes ---
PV Name /dev/sdb5
PV UUID 6iKVQF-W01Z-1aMF-m1af-jmhX-Ff7c-r5QHcD
PV Status allocatable
Total PE / Free PE 255 / 255 --- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.77 GiB
PE Size 4.00 MiB
Total PE 454
Alloc PE / Size 13 / 52.00 MiB
Free PE / Size 441 / 1.72 GiB
VG UUID qxqwsD-oNGT-8ltJ-1Itj-IZfO-0rui-Yxvhh7 --- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID x6wfyb-ZMDC-eGmK-8TRH-cVX5-JZB2-u0ZYID
LV Write Access read/write
LV Creation host, time ruanwenwu02, 2017-11-04 11:52:34 +0800
LV Status available
# open 1
LV Size 52.00 MiB
Current LE 13
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0 --- Physical volumes ---
PV Name /dev/sdb1
PV UUID lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc
PV Status allocatable
Total PE / Free PE 255 / 242 PV Name /dev/sdb6
PV UUID QsRgrb-3vmW-Jte1-o5Qv-JvgZ-oNnO-MJkOoG
PV Status allocatable
Total PE / Free PE 199 / 199
Linux学习 LVM ***

六、命令整理:

6.1 fdisk diskname -> t ->8e

修改磁盘分区为lvm类型。

6.2 pvcreate partionname

创建物理分区

6.4 pvremove

删除物理分区

6.3 vgcreate vg1 /dev/sdb1 /dev/sdb2

创建卷组

6.4 vgremove

移除卷组

6.5 lvcreate -L 200M -n lv1 vg1

创建逻辑卷

6.6 lvresize -L 300M /dev/vg1/lv1

扩容逻辑卷大小

6.7 扩容之后的操作

 e2fsck -f /dev/vg1/lv1 检查磁盘错误  (ext4执行)

 resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行)

 xfs_growfs /dev/vg1/lv1  xfs文件系统需要执行

6.8 缩容操作

Linux学习 LVM ***
先umount

 e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext)

 resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext)

 lvresize -L 100M  /dev/vg1/lv1  重新设置卷大小
Linux学习 LVM ***

6.9 列出卷组的详细信息

Linux学习 LVM ***
[root@ruanwenwu02 ~]# vgdisplay -v
--- Volume group ---
VG Name vg2
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE 255
Alloc PE / Size 0 / 0
Free PE / Size 255 / 1020.00 MiB
VG UUID aDXygA-rltJ-JWCQ-Nbvb-JML9-W3Hf-cVjG63 --- Physical volumes ---
PV Name /dev/sdb5
PV UUID 6iKVQF-W01Z-1aMF-m1af-jmhX-Ff7c-r5QHcD
PV Status allocatable
Total PE / Free PE 255 / 255 --- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.77 GiB
PE Size 4.00 MiB
Total PE 454
Alloc PE / Size 13 / 52.00 MiB
Free PE / Size 441 / 1.72 GiB
VG UUID qxqwsD-oNGT-8ltJ-1Itj-IZfO-0rui-Yxvhh7 --- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID x6wfyb-ZMDC-eGmK-8TRH-cVX5-JZB2-u0ZYID
LV Write Access read/write
LV Creation host, time ruanwenwu02, 2017-11-04 11:52:34 +0800
LV Status available
# open 1
LV Size 52.00 MiB
Current LE 13
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0 --- Physical volumes ---
PV Name /dev/sdb1
PV UUID lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc
PV Status allocatable
Total PE / Free PE 255 / 242 PV Name /dev/sdb6
PV UUID QsRgrb-3vmW-Jte1-o5Qv-JvgZ-oNnO-MJkOoG
PV Status allocatable
Total PE / Free PE 199 / 199
Linux学习 LVM ***

七、总结

LVM的命令还是比较多的,有机会多看看多练练吧。