Linux学习(十五)LVM

一、前言

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

看它的原理图:

Linux学习(十五)LVM

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

二、实现一个lvm

步骤:

2.1 做物理卷。

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

[root@ruanwenwu02 ~]# fdisk -l /dev/sdb

磁盘 /dev/sdb: MB,  字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x2f45f776 设备 Boot Start End Blocks Id System
/dev/sdb1 Linux
/dev/sdb2 Linux
/dev/sdb3 Extended
/dev/sdb5 Linux
/dev/sdb6 Linux

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

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

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

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

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

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

[root@ruanwenwu02 ~]# yum install lvm2
已加载插件:fastestmirror
base | 3.6 kB ::
extras | 3.4 kB ::
updates | 3.4 kB ::
(/): extras//x86_64/primary_db | kB ::
(/): updates//x86_64/primary_db

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

[root@ruanwenwu02 ~]# pvcreate /dev/sdb1
WARNING: ext2 signature detected on /dev/sdb1 at offset . 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.

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

[root@ruanwenwu02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- .00g .00g
/dev/sdb2 lvm2 --- 800.00m 800.00m
/dev/sdb5 lvm2 --- .00g .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
Total PE
Free PE
Allocated PE
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
Total PE
Free PE
Allocated PE
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
Total PE
Free PE
Allocated PE
PV UUID lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc

2.2 创建卷组

[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 wz--n- .77g .77g
vg2 wz--n- 1020.00m 1020.00m
[root@ruanwenwu02 ~]# vgdisplay
--- Volume group ---
VG Name vg2
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / 1020.00 MiB
VG UUID aDXygA-rltJ-JWCQ-Nbvb-JML9-W3Hf-cVjG63 --- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1.77 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / 1.77 GiB
VG UUID qxqwsD-oNGT-8ltJ-1Itj-IZfO-0rui-Yxvhh7

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

2.3 创建逻辑卷

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

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

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

[root@ruanwenwu02 ~]# mke2fs -t ext4 /dev/vg1/lv1
mke2fs 1.42. (-Dec-)
文件系统标签=
OS type: Linux
块大小= (log=)
分块大小= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
第一个数据块=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , Allocating group tables: 完成
正在写入inode表: 完成
Creating journal ( blocks): 完成
Writing superblocks and filesystem accounting information: 完成

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

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

[root@ruanwenwu02 ~]# mkdir /mnt/vg1
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 190M 1.6M 175M % /mnt/vg1
[root@ruanwenwu02 ~]# cat /etc/passwd >> /mnt/vg1/passwd
[root@ruanwenwu02 ~]# cat !$
cat /mnt/vg1/passwd
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
mail:x:::mail:/var/spool/mail:/sbin/nologin
operator:x:::operator:/root:/sbin/nologin
games:x:::games:/usr/games:/sbin/nologin
ftp:x:::FTP User:/var/ftp:/sbin/nologin
nobody:x:::Nobody:/:/sbin/nologin
systemd-bus-proxy:x:::systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:::systemd Network Management:/:/sbin/nologin
dbus:x:::System message bus:/:/sbin/nologin
polkitd:x:::User for polkitd:/:/sbin/nologin
tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x::::/var/spool/postfix:/sbin/nologin
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ruanwenwu:x::::/home/ruanwenwu:/bin/bash

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

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

[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 wz--n- 1020.00m 820.00m
vg2 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

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

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

[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

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

[root@ruanwenwu02 ~]# history|grep lvcreate
lvcreate -L 200M -n lv1 vg1
history|lvcreate
history|grep lvcreate
[root@ruanwenwu02 ~]# lvcreate -L 200M -n lv1 vg1
WARNING: ext4 signature detected on /dev/vg1/lv1 at offset . 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. (-Dec-)
文件系统标签=
OS type: Linux
块大小= (log=)
分块大小= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
第一个数据块=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , Allocating group tables: 完成
正在写入inode表: 完成
Creating journal ( 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 .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 190M 1.6M 175M % /mnt/vg1

到这里,我们的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 wz--n- 1020.00m 820.00m
vg2 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文件系统举例)

[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 ( extents) to 300.00 MiB ( extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 190M 1.6M 175M % /mnt/vg1
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42. (-Dec-)
/dev/vg1/lv1 is mounted.
e2fsck: 无法继续, 中止.

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

[root@ruanwenwu02 ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 200.00 MiB ( extents) to 300.00 MiB ( extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 190M 1.6M 175M % /mnt/vg1
[root@ruanwenwu02 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42. (-Dec-)
/dev/vg1/lv1 is mounted.
e2fsck: 无法继续, 中止. [root@ruanwenwu02 ~]# umount /dev/vg1/lv1
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
[root@ruanwenwu02 ~]# lvresize -L 300M /dev/vg1/lv1
New size ( extents) matches existing size ( extents).
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1
mount: 在 /etc/fstab 中找不到 /dev/vg1/lv1
[root@ruanwenwu02 ~]# e2fsck /dev/vg1/lv1
e2fsck 1.42. (-Dec-)
/dev/vg1/lv1: clean, / files, / blocks
[root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42. (-Dec-)
请先运行 'e2fsck -f /dev/vg1/lv1'. [root@ruanwenwu02 ~]# resize2fs -f /dev/vg1/lv1
resize2fs 1.42. (-Dec-)
Resizing the filesystem on /dev/vg1/lv1 to (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 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. (-Dec-)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass : Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: / files (0.0% non-contiguous), / 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. (-Dec-)
The filesystem is already blocks long. Nothing to do! [root@ruanwenwu02 ~]# ls -ld /dev/vg1/lv1
lrwxrwxrwx. root root 11月 : /dev/vg1/lv1 -> ../dm-
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 287M 2.1M 266M % /mnt/vg1

总结一下步骤:

 lvresize -L 300M  /dev/vg1/lv1  重新设置卷大小

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

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

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

到这里扩容就成功了。

四、缩容

步骤:

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

实验:

[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. (-Dec-)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass : Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: / files (9.1% non-contiguous), / blocks
[root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42. (-Dec-)
The filesystem is already blocks long. Nothing to do! [root@ruanwenwu02 ~]# resize2fs /dev/vg1/lv1 50M
resize2fs 1.42. (-Dec-)
Resizing the filesystem on /dev/vg1/lv1 to (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 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 ( extents) to 52.00 MiB ( extents).
Logical volume vg1/lv1 successfully resized.
[root@ruanwenwu02 ~]# mount /dev/vg1/lv1 /mnt/vg1/
[root@ruanwenwu02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G .4G 17G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda1 197M 109M 88M % /boot
tmpfs 98M 98M % /run/user/
/dev/mapper/vg1-lv1 45M 1.1M 40M % /mnt/vg1

五、扩展卷组

步骤:

fdisk /dev/sdb  新增/dev/sdb5(逻辑分区8e)  2G

 pvcreate /dev/sdb5

 vgextend vg1 /dev/sdb5

 lvresize -L 100M  /dev/vg1/lv1  重新设置卷大小

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

[root@ruanwenwu02 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 命令(输入 m 获取帮助):t
分区号 (-,,,默认 ):
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):q [root@ruanwenwu02 ~]# pvcreate /dev/sdb6
WARNING: ext4 signature detected on /dev/sdb6 at offset . 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
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / 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 / --- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1.77 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / 52.00 MiB
Free PE / Size / 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, -- :: +
LV Status available
# open
LV Size 52.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : --- Physical volumes ---
PV Name /dev/sdb1
PV UUID lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc
PV Status allocatable
Total PE / Free PE / PV Name /dev/sdb6
PV UUID QsRgrb-3vmW-Jte1-o5Qv-JvgZ-oNnO-MJkOoG
PV Status allocatable
Total PE / Free PE /

六、命令整理:

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 缩容操作

先umount

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

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

 lvresize -L 100M  /dev/vg1/lv1  重新设置卷大小

6.9 列出卷组的详细信息

[root@ruanwenwu02 ~]# vgdisplay -v
--- Volume group ---
VG Name vg2
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1020.00 MiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size /
Free PE / Size / 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 / --- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 1.77 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / 52.00 MiB
Free PE / Size / 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, -- :: +
LV Status available
# open
LV Size 52.00 MiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : --- Physical volumes ---
PV Name /dev/sdb1
PV UUID lttH1f-3tce-mEpF-2ULq-nqmL-15SI-2ImIxc
PV Status allocatable
Total PE / Free PE / PV Name /dev/sdb6
PV UUID QsRgrb-3vmW-Jte1-o5Qv-JvgZ-oNnO-MJkOoG
PV Status allocatable
Total PE / Free PE /

七、总结

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

上一篇:BZOJ_3944_Sum_杜教筛


下一篇:为什么要使用Ajax