七、逻辑卷管理器
一、 lvm
一、LVM(logical volume manager 逻辑卷管理器):内核支持的功能 以分区为单位
1.)企业生产当中遇到的问题:
1.分好区之后,某个业务不断扩张,某个分区磁盘空间不够啦。但是 其他分区还有剩余空间,如何做内部调整
2.整个磁盘空间都不够用了,难道我要买一块新的硬盘,重新安装操作系统,然后把数据 COPY 到新的硬盘上边吗?
3.多个磁盘上边有剩余空间,如何整合剩余空间成为一块新的磁盘?
4.能不能在磁盘管理上扩展性更加灵活?
IBM,Linux kernel 2.4 版本的时候引入到 Linux 当中。目前使用的是 LVM V2 版本
2.)举例说明什么是 LVM:
逻辑卷管理器的优点:
1.分区动态扩充,业务不中断
2.可以给分区拍快照
3.存储的基础
4.不需要安装额外的软件包 是内核所支持的功能!!!!
3.)创建逻辑卷管理器的概念
pv:物理卷,物理的分区,逻辑卷管理器物理存储设备。
vg:卷组,虚拟磁盘。一个或若干物理卷的集合
lv:逻辑卷,一个模拟出来的分区 在使用的时候 和正常的分区无差别--------创建文件系统挂载、使用
可以动态调整分区创建快照
4.)使用逻辑卷的过程:
1.创建物理卷-->2.创建卷组-->3.创建逻辑卷-->4.创建文件系统-->5.挂载使用
5.)LVM 中的基本命令
二、 实验
[root@localhost ~]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xfb374980.
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-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +512M
Partition 1 of type Linux and of size 512 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 (1050624-10485759, default 1050624):
Using default value 1050624
Last sector, +sectors or +size{K,M,G} (1050624-10485759, default 10485759): +512M
Partition 2 of type Linux and of size 512 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 (2099200-10485759, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-10485759, default 10485759): +512M
Partition 3 of type Linux and of size 512 MiB is set
Command (m for help): p
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xfb374980
Device Boot Start End Blocks Id System
/dev/sdc1 2048 1050623 524288 83 Linux
/dev/sdc2 1050624 2099199 524288 83 Linux
/dev/sdc3 2099200 3147775 524288 83 Linux
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): l
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xfb374980
Device Boot Start End Blocks Id System
/dev/sdc1 2048 1050623 524288 83 Linux
/dev/sdc2 1050624 2099199 524288 83 Linux
/dev/sdc3 2099200 3147775 524288 8e Linux LVM
Command (m for help): t
Partition number (1-3, default 3): 2
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-3, default 3): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xfb374980
Device Boot Start End Blocks Id System
/dev/sdc1 2048 1050623 524288 8e Linux LVM
/dev/sdc2 1050624 2099199 524288 8e Linux LVM
/dev/sdc3 2099200 3147775 524288 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/sdc
[root@localhost ~]# pvcreate /dev/sdc[1-3] //连续创建物理卷
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdc2" successfully created.
Physical volume "/dev/sdc3" successfully created.
[root@localhost ~]# vgcreate mainvg /dev/sdc[1-3] //创建卷组名称为 mainvg
Volume group "mainvg" successfully created
[root@localhost ~]# pvs //扫描系统中的物理卷
PV VG Fmt Attr PSize PFree
/dev/sdc1 mainvg lvm2 a-- 508.00m 508.00m
/dev/sdc2 mainvg lvm2 a-- 508.00m 508.00m
/dev/sdc3 mainvg lvm2 a-- 508.00m 508.00m
[root@localhost ~]# vgs //检测系统中的卷组
VG #PV #LV #SN Attr VSize VFree
mainvg 3 0 0 wz--n- 1.49g 1.49g
[root@localhost ~]# lvcreate -n 1stlv -L 400M mainvg //-n 指定名字 -L 指定大小 卷组名称 分配逻辑卷
Logical volume "1stlv" created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdc1 mainvg lvm2 a-- 508.00m 108.00m
/dev/sdc2 mainvg lvm2 a-- 508.00m 508.00m
/dev/sdc3 mainvg lvm2 a-- 508.00m 508.00m
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
mainvg 3 1 0 wz--n- 1.49g 1.10g
逻辑卷的设备标识符
[root@localhost ~]# ll /dev/mainvg/1stlv
lrwxrwxrwx. 1 root root 7 Jan 8 02:52 /dev/mainvg/1stlv -> ../dm-0
[root@localhost ~]# ll /dev/mapper/mainvg-1stlv
lrwxrwxrwx. 1 root root 7 Jan 8 02:52 /dev/mapper/mainvg-1stlv -> ../dm-0
[root@localhost ~]# mkfs.ext4 /dev/mainvg/1stlv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# mkdir wu
[root@localhost tmp]# echo "/dev/mainvg/1stlv /tmp/wu ext4 defaults 0 0" >>/etc/fstab
[root@localhost tmp]# mount -a
[root@localhost tmp]# mount |grep /tmp/wu
/dev/mapper/mainvg-1stlv on /tmp/wu type ext4 (rw,relatime,seclabel,data=ordered)
[root@localhost tmp]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 18G 4.5G 14G 25% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 144K 489M 1% /dev/shm
tmpfs 489M 7.2M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 509M 26M 483M 6% /home
/dev/sda1 509M 162M 347M 32% /boot
tmpfs 98M 12K 98M 1% /run/user/0
/dev/mapper/mainvg-1stlv 380M 2.3M 354M 1% /tmp/wu
逻辑卷的设备标识符
1、/dev/卷组名/逻辑卷名
2、/dev/mapper/卷组名-逻辑卷名
[root@localhost tmp]# lvcreate -L 500M -n 2edlv mainvg //创建逻辑卷
Logical volume "2edlv" created.
[root@localhost tmp]# mkfs.xfs /dev/mainvg/2edlv //为逻辑卷创建一个 xfs 系统
[root@localhost tmp]# mkdir /tmp/2edlv
[root@localhost tmp]# echo "/dev/mapper/mainvg-2edlv /tmp/2edlv xfs defaults 0 0" >> /etc/fstab
[root@localhost tmp]# mount -a
[root@localhost tmp]# vgs
VG #PV #LV #SN Attr VSize VFree
mainvg 3 2 0 wz--n- 1.49g 624.00m
[root@localhost tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdc1 mainvg lvm2 a-- 508.00m 108.00m
/dev/sdc2 mainvg lvm2 a-- 508.00m 8.00m
/dev/sdc3 mainvg lvm2 a-- 508.00m 508.00m
调整 lvm 及包含文件系统
1.ext4 文件系统 lvm 的扩大
(1)扩大 LV(2)扩大文件系统
[root@localhost tmp]# lvresize -L 520M /dev/mainvg/1stlv // 扩大 LV
Size of logical volume mainvg/1stlv changed from 400.00 MiB (100 extents) to 520.00 MiB (130 extents).
Logical volume mainvg/1stlv successfully resized.
[root@localhost tmp]# lvresize -L +140M /dev/mainvg/1stlv // 扩大 LV
Size of logical volume mainvg/1stlv changed from 400.00 MiB (100 extents) to 520.00 MiB (130 extents).
Logical volume mainvg/1stlv successfully resized.
以上两种方式相同
[root@localhost tmp]# resize2fs /dev/mainvg/1stlv //扩大文件系统
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mainvg/1stlv is mounted on /tmp/wu; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/mainvg/1stlv is now 532480 blocks long.
[root@localhost tmp]# df -lh | grep 1st
/dev/mapper/mainvg-1stlv 496M 2.3M 465M 1% /tmp/wu
2.包含 ext 文件系统的 lv 缩小
(1)卸载文件系统(2)缩小文件系统(e2fsck)(3)缩小逻辑卷
[root@localhost tmp]# umount /tmp/wu/ //卸载文件系统
[root@localhost tmp]# e2fsck -f /dev/mainvg/1stlv // 检查分区
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/mainvg/1stlv: 11/133120 files (0.0% non-contiguous), 27326/532480 blocks
[root@localhost tmp]# resize2fs /dev/mainvg/1stlv 100M //缩小文件系统
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mainvg/1stlv to 102400 (1k) blocks.
The filesystem on /dev/mainvg/1stlv is now 102400 blocks long.
[root@localhost tmp]# lvresize /dev/mainvg/1stlv -L 120M //缩小逻辑卷
WARNING: Reducing active logical volume to 120.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mainvg/1stlv? [y/n]: y
Size of logical volume mainvg/1stlv changed from 400.00 MiB (100 extents) to 120.00 MiB (30 extents).
Logical volume mainvg/1stlv successfully resized.
[root@localhost tmp]# mount -a
包含 ext 文件系统的调整:在线放大、离线缩小、缩小的时候一旦顺序颠倒,文件系统将会损坏,文件将会丢失,
不可恢复!在实际生产当中,几乎不缩小 LV
调整包含 xfs 文件系统的 lv
(1)扩大 LV(2)扩大文件系统
[root@localhost tmp]# lvresize /dev/mainvg/2edlv -L 800M //扩大 lv
Size of logical volume mainvg/2edlv changed from 500.00 MiB (125 extents) to 800.00 MiB (200 extents).
Logical volume mainvg/2edlv successfully resized.
[root@localhost tmp]# xfs_growfs /dev/mainvg/2edlv //扩大 xfs 的文件系统
[root@localhost tmp]# df -lh | grep 2ed
/dev/mapper/mainvg-2edlv 797M 26M 772M 4% /tmp/2edlv
xfs 系统无法缩小
卷组 vg 的调整
VG 的扩充:通过 pv 的方式扩充物理卷
[root@localhost tmp]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@localhost tmp]# vgextend mainvg /dev/sdb1
Volume group "mainvg" successfully extended
[root@localhost tmp]# vgreduce mainvg /dev/sdb1 //删除没被使用的物理卷
Removed "/dev/sdb1" from volume group "mainvg"
通过向卷组添加物理卷 vgextend 的方式扩大卷组也可以吧没有使用的物理卷通过 vgreduce 命令剔除出去
三、 pv 的迁移
[root@localhost tmp]# vgextend mainvg /dev/sdb1 //把需要替换的其他物理设备添加到物理卷中
Volume group "mainvg" successfully extended
[root@localhost tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 mainvg lvm2 a-- 2.00g 2.00g
/dev/sdc1 mainvg lvm2 a-- 508.00m 8.00m
/dev/sdc2 mainvg lvm2 a-- 508.00m 0
/dev/sdc3 mainvg lvm2 a-- 508.00m 216.00m
[root@localhost tmp]# pvmove /dev/sdc2 /dev/sdb1 //把 sdc2 数据迁移到 sdb1 上
/dev/sdc2: Moved: 2.36%
/dev/sdc2: Moved: 100.00%
[root@localhost tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 mainvg lvm2 a-- 2.00g 1.50g
/dev/sdc1 mainvg lvm2 a-- 508.00m 8.00m
/dev/sdc2 mainvg lvm2 a-- 508.00m 508.00m
/dev/sdc3 mainvg lvm2 a-- 508.00m 216.00m
[root@localhost tmp]# vgreduce mainvg /dev/sdc2 //把 sdc2 移除卷组中
Removed "/dev/sdc2" from volume group "mainvg"
[root@localhost tmp]# pvremove /dev/sdc2 //把删除物理卷
Labels on physical volume "/dev/sdc2" successfully wiped.
[root@localhost tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 mainvg lvm2 a-- 2.00g 1.50g
/dev/sdc1 mainvg lvm2 a-- 508.00m 8.00m
/dev/sdc3 mainvg lvm2 a-- 508.00m 216.00m
四、 卷组的迁移
单位:卷组
1.业务离线
[root@localhost tmp]# umount /dev/mainvg/1stlv
[root@localhost tmp]# umount /dev/mainvg/2edlv
2.去激活业务(卷组)
[root@localhost tmp]# vgchange -a n mainvg
0 logical volume(s) in volume group "mainvg" now active
3.导出设备(卷组)
[root@localhost tmp]# vgexport mainvg
Volume group "mainvg" successfully exported
4.把硬盘物理卸载掉 在另外一台电脑上关机插入这些硬盘 开机
在另一台机器上
1.做磁盘检索
[root@localhost tmp]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 mainvg lvm2 ax- 2.00g 1.50g
/dev/sdc1 mainvg lvm2 ax- 508.00m 8.00m
/dev/sdc3 mainvg lvm2 ax- 508.00m 216.00m
2.导入卷组
[root@localhost tmp]# vgimport mainvg
Volume group "mainvg" successfully imported
3.激活卷组
[root@localhost tmp]# vgchange -a y mainvg
2 logical volume(s) in volume group "mainvg" now active
4.挂载正常使用
作业:以上练习外删除 pv/vg/lv 各种 display【查看设备详细信息】
[root@localhost tmp]# umount /dev/mainvg/a
[root@localhost tmp]# umount /dev/mainvg/b
umount: /dev/mainvg/b: not mounted
[root@localhost tmp]# lvremove /dev/mainvg/a
Do you really want to remove active logical volume mainvg/a? [y/n]: y
Logical volume "a" successfully removed
[root@localhost tmp]# vgremove mainvg
Volume group "mainvg" successfully removed
[root@localhost tmp]# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@localhost tmp]# pvremove /dev/sdc2
Labels on physical volume "/dev/sdc2" successfully wiped.
[root@localhost tmp]# pvremove /dev/sdc3
Labels on physical volume "/dev/sdc3" successfully wiped.