概述
LVM即逻辑卷管理器的意思,相对于传统的物理磁盘上划分分区(文件系统)、格式化操作来讲,LVM需要先将磁盘或者分区(块设备)转换成PV,由一个或者多PV组成VG,在VG上再划分成LV,最后将LV格式化,挂载,供使用。
那么,LVM相比传统的磁盘管理有什么不同呢,LVM的逻辑卷管理能够灵活的调整文件系统的大小。从而管理更加方便。
一、LVM相关的概念
PV:physical Volume, 物理卷,将一个块设备的SystemID调整为8E(LVM),再通过lvcreate转换,即成为一个PV。
VG:Volume Group,将一个或多个PV组合起来形成的逻辑设备
LV:Logical Volume,逻辑卷,在VG上进行划分,最终分配给用户使用的分区(文件系统),可以进行格式化和挂载
PE:Physical Extend,物理块,LVM的最小存储单元,默认大小为4M,每个VG最多能包含65534个PE:即如果使用默认大小,VG大小最大只有256G。PE大小在创建VG时指定。
LE: Logical Extend,逻辑块,其大小和PE一致
上三张图,阐述下这些概念的关系
(图片来源于网络)
二、LVM实验
下面,我们用一个综合实验,来看看LVM是如何实现和管理的
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
实验目标:
- 创建至少有2个PV组成的,大小为20G的,名为testvg的VG, 要求PE大小为16M,. 在卷组中创建大小为5G的逻辑卷testlv,. 挂载到/users目录,开机自动挂载
- 新建立用户archlinux,要求家目录为/users/archlinux, 切换到archlinux用户,复制/etc/pam.d目录下的所有文件到自己的家目录
- 扩展testlv至7G,要求archlinux的文件不能丢失, 收缩testlv至3G,要求文件不能丢失
- 对testlv创建快照,并尝试基于快照备份数据,验证快照的功能。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接下来我们就上面的实验要求,进行逐步的实现,我们将机器上的/dev/sdb,划分两个分区,来创建lv
一、 按要求创建LV
- 磁盘分区
# 使用parted对/dev/sdb进行分区,先使用print显示已有的分区
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 136GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 10.0GB 9999MB primary lvm
2 10.0GB 20.0GB 9999MB primary lvm
3 20.0GB 30.0GB 10.0GB primary lvm
4 30.0GB 40.0GB 10.0GB primary lvm
5 40.0GB 50.0GB 10.0GB primary lvm
(parted)
#将这个磁盘上现有的分区全部删除
(parted) rm 1
(parted) rm 2
(parted) rm 3
(parted) rm 4
(parted) rm 5
(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 136GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
(parted) mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
(parted) mkpart primary 0G 10G
(parted) mkpart primary 10G 20G
(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 136GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 10.0GB 9999MB primary
2 10.0GB 20.0GB 9999MB primary
(parted) toggle 1 lvm
(parted) toggle 2 lvm
(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 136GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 10.0GB 9999MB primary lvm
2 10.0GB 20.0GB 9999MB primary lvm
(parted) q
信息: You may need to update /etc/fstab.
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
2 0 4 fd0
8 0 133169152 sda
8 1 1048576 sda1
8 2 90176512 sda2
8 16 133169152 sdb
8 17 9764864 sdb1
8 18 9764864 sdb2
11 0 1048575 sr0
253 0 52428800 dm-0
253 1 2097152 dm-1
253 3 35639296 dm-3
[root@localhost ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
- 创建PV
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created.
[root@localhost ~]# pvcreate /dev/sdb2
Physical volume “/dev/sdb2” successfully created.
- 创建VG
[root@localhost ~]# vgcreate -s 16M testvg /dev/sdb{1,2}
Physical volume “/dev/sdb1” successfully created.
Volume group “testvg” successfully created
# 使用vgdisplay或者pvdisplay可以查看到VG和PV之间已经进行了关联
[root@localhost ~]# pvdisplay
— Physical volume —
PV Name /dev/sdb2
VG Name testvg
PV Size 9.31 GiB / not usable 16.00 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 595
Free PE 595
Allocated PE 0
PV UUID sY33VY-xCHC-xv2M-doha-RInF-Pgz5-KfrfCj
— Physical volume —
PV Name /dev/sdb1
VG Name testvg
PV Size 9.31 GiB / not usable 16.00 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 595
Free PE 595
Allocated PE 0
PV UUID jCSfUE-81oO-1jUi-n5YQ-AwtY-W3eo-4vsldi
- 创建LV
[root@localhost ~]# lvcreate -L 5G -n testlv /dev/mapper/testvg
Logical volume “testlv” created.
[root@localhost ~]# lvdisplay /dev/mapper/testvg-testlv
— Logical volume —
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID Xx78rs-xafF-5uqA-aNtM-IT55-oMp4-Awdrzk
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-12-28 11:42:34 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 320
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 8192
Block device 253:2
- 挂载
A. 格式化
[root@localhost ~]# mke2fs -t ext4 /dev/testvg/testlv
B.挂载
[root@localhost ~]# mkdir /users
[root@localhost ~]# mount /dev/mapper/testvg-testlv /users
[root@localhost users]# mount | grep “testlv”
/dev/mapper/testvg-testlv on /users type ext4 (rw,relatime,seclabel,data=ordered)
C. 自动挂载
vim /etc/fstab
/dev/mapper/testvg-testlv /users ext4 defaults 0 0
二、 创建用户并将用户的家目录指向testlv
[root@localhost /]# useradd -d /users/archlinux archlinux
[archlinux@localhost ~]$ cp -r /etc/pam.d/* .
三、 LV的扩展和收缩
- 扩展LV
# 对于LV的扩展,我们先应该保证VG有足够的剩余空间,然后,扩展LV,再修改文件系统的大小,扩展LV应该先备份数据,再在线扩展
# 扩展LV到7G
[root@localhost ~]# lvextend -L 7G /dev/mapper/testvg-testlv
Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
Logical volume testvg/testlv successfully resized.
# 修改文件系统大小
[root@localhost /]# resize2fs /dev/testvg/testlv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/testvg-testlv 6.8G 23M 6.4G 1% /users
- 收缩LV
# 对于缩减LV的大小,应该先卸载文件系统,再进行文件系统的检查,接着先将文件系统的大小进行修改,再修改LV的大小
[root@localhost ~]# umount /users
[root@localhost ~]# e2fsck -f /dev/mapper/testvg-testlv
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/testvg-testlv: 53/458752 files (0.0% non-contiguous), 67363/1835008 blocks
[root@localhost ~]# resize2fs /dev/mapper/testvg-testlv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 786432 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 786432 blocks long.
[root@localhost ~]# lvreduce -L 3G /dev/mapper/testvg-testlv
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
Logical volume testvg/testlv successfully resized.
[root@localhost ~]# mount -a
[root@localhost ~]# mount | grep “testvg”
/dev/mapper/testvg-testlv on /users type ext4 (rw,relatime,seclabel,data=ordered)
[root@localhost ~]# cd /users/archlinux/
[root@localhost archlinux]# ll
总用量 104
-rw-r–r–. 1 archlinux archlinux 272 12月 28 12:15 atd
-rw-r–r–. 1 archlinux archlinux 192 12月 28 12:15 chfn
-rw-r–r–. 1 archlinux archlinux 192 12月 28 12:15 chsh
-rw-r–r–. 1 archlinux archlinux 232 12月 28 12:15 config-util
-rw-r–r–. 1 archlinux archlinux 293 12月 28 12:15 crond
lrwxrwxrwx. 1 archlinux archlinux 19 12月 28 12:15 fingerprint-auth -> fingerprint-auth-ac
-rw-r–r–. 1 archlinux archlinux 702 12月 28 12:15 fingerprint-auth-ac
-rw-r–r–. 1 archlinux archlinux 796 12月 28 12:15 login
-rw-r–r–. 1 archlinux archlinux 154 12月 28 12:15 other
-rw-r–r–. 1 archlinux archlinux 188 12月 28 12:15 passwd
……………
三、 LVM的快照功能
- 对testlv创建一个快照卷
[root@localhost archlinux]# lvcreate -s -L 1G -n testlv-snap /dev/mapper/testvg-testlv
Using default stripesize 64.00 KiB.
Logical volume “testlv-snap” created.
- 挂载并查看快照卷里头的A文件内容
[root@localhost /]# mount -r /dev/testvg/testlv-snap /mnt/snap/
[root@localhost /]# cd /mnt/snap/archlinux/
[root@localhost archlinux]# cat vlock
#%PAM-1.0
auth include system-auth
account required pam_permit.so
[root@localhost archlinux]# wc -l vlock
3 vlock
- 修改源卷testlv里头的A文件内容,再查看快照卷的内容
[root@localhost archlinux]# cat vlock
#%PAM-1.0
auth include system-auth
account required pam_permit.so
This is first new line.
This is second new line.
This is the end line.
[root@localhost archlinux]# cd /mnt/snap/archlinux/
[root@localhost archlinux]# pwd
/mnt/snap/archlinux
[root@localhost archlinux]# cat vlock
#%PAM-1.0
auth include system-auth
account required pam_permit.so
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/90587