一.LVM逻辑卷
LVM是一种基于内核的逻辑卷管理器,适用于管理大存储设备,并允许用户动态调整文件系统的大小。
需要了解的概念有
物理存储介质:指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等
物理卷PV:是LVM逻辑存储块,物理卷与磁盘分区是逻辑对应关系。多个LVM物理卷可以合并或拆分,从而实现容量的扩容与缩减
卷组VG:是LVM逻辑概念上的磁盘设备,通过将单个或者多个物理卷组合成卷组。卷组的大小取决于物理卷的容量及个数
逻辑卷LV:逻辑卷就是LVM逻辑意义上的分区,可以指定从卷组中提取多少容量来创建逻辑卷,最后对逻辑卷挂载及使用
物理长度PE:将物理卷组合成卷组后,所划分的最小存储单位即逻辑意义上磁盘的最小存储单元。LVM中默认PE大小为4MB
逻辑块LE:逻辑卷LV也被划分为称为LE的基本单位,在同一个卷组中,LE的大小和PE是相同的,并且一一对应
二.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 0x29d83a76.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): 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): 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): +500M
Partition 2 of type Linux and of size 500 MiB is set
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: 0x29d83a76
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 83 Linux
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e ##LVM标签为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: 0x29d83a76
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 8e Linux LVM ##更改成功
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe ##同步磁盘分区
2.pv.vg.lv及其挂载的监控
watch -n 1 'pvs;echo ====;vgs;echo ====;lvs;echo ====;df -h /mnt ##监控pv vg lv 以及挂载情况
pvs ##查看物理卷
vgs ##查看逻辑卷组
lvs ##查看逻辑卷
由于未建立逻辑组逻辑卷所以此时监控到的逻辑组逻辑卷无数据
3.创建逻辑组逻辑卷
(1)物理卷
pvcreate /dev/vdb1 #把物理分区做成物理卷
(2)物理卷组
vgcreate vg0 /dev/vdb1 #制作一个物理卷组vg0
(3)逻辑卷
lvcreate -L 100M -n lv0 vg0 #在vg0这个组中建立出逻辑卷 -L 指定大小 -n 指定名字
4.逻辑卷lv0格式化及挂载,可以正常使用
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0 ##格式化
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, 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 ~]# blkid ##查看可用设备
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb2: UUID="82l7Ac-WkLd-1TpK-H18R-lHJL-5nUO-tcipdV" TYPE="LVM2_member"
/dev/sr0: UUID="2016-10-19-18-32-06-00" LABEL="RHEL-7.3 Server.x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/vg0-lv0: UUID="4a6745d6-9657-451e-89c6-6a287dd2f310" TYPE="xfs" ##/可用则格式化成功
[root@localhost ~]# mount /dev/vg0/lv0 /mnt ##挂载在/mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3176344 7297556 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17072 925576 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
/dev/mapper/vg0-lv0 98988 5280 93708 6% /mnt ##挂载成功
注意:/dev/mapper/vg0-lv0与/dev/vg0/lv0为同一设备
[root@localhost ~]# ls -l /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Apr 22 07:23 /dev/vg0/lv0 -> ../dm-0
[root@localhost ~]# ls -l /dev/mapper/vg0-lv0
lrwxrwxrwx. 1 root root 7 Apr 22 07:23 /dev/mapper/vg0-lv0 -> ../dm-0
5.逻辑卷的扩展
当lv为xfs文件系统只支持增加不支持缩减;当文件系统类型为ext2、ext3、ext4 时候,既可以增加可以以减少
未扩展之前lv0分区未100M,文件系统大小为97M
(1)将lv0分区扩展至300M
lvextend -L 300M /dev/vg0/lv0 #扩大lv分区到300M
(2)改变文件系统大小与分区匹配
xfs_growfs /dev/vg0/lv0 ##扩大lv分区文件系统大小与分区大小一致
6.ext格式下逻辑卷的扩大与缩减
将设备格式化成xfs后是不能缩减的,只能扩展,所以需重新格式化逻辑卷的类型
(1)重新格式化
[root@localhost ~]# umount /mnt ##卸载
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3176680 7297220 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17072 925576 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
[root@localhost ~]# mkfs.ext4 /dev/vg0/lv0 ##格式为ext格式
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
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33947648
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/vg0/lv0 /mnt ##重新挂载
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3176680 7297220 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17072 925576 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
/dev/mapper/vg0-lv0 289285 2062 267767 1% /mnt
(2)ext格式下逻辑卷的扩增
a.lvextend -L 400M /dev/vg0/lv0 #增加lv分区的大小到400M
b.resize2fs /dev/vg0/lv0 #扩大lv分区文件系统大小与分区大小一致
(3)ext格式下逻辑卷的缩减
文件系统的缩减不支持在挂载状态下进行,所以需要进行lv设备的缩减时需要先进行卸载设备,进行分区缩减后在进行挂载。
a.卸载
[root@localhost ~]# umount /mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3176692 7297208 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17072 925576 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
b.缩减lv分区为100M
e2fsck -f /dev/vg0/lv0 #检测文件系统,确定文件系统中已有数据大小防止过度缩减
[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/101200 files (0.0% non-contiguous), 23297/409600 blocks
c.resize2fs /dev/vg0/lv0 100M #缩减文件系统的大小为100M
注意:在缩减时,先缩减文件系统大小,再缩减分区;即先软件再硬件,否则会出现缩减文件大小不成功和挂载出现问题
[root@localhost ~]# mount /dev/vg0/lv0 /mnt
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg0-lv0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try ##挂载失败
dmesg | tail or so.
d.重新挂载
[root@localhost ~]# mount /dev/vg0/lv0 /mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3176844 7297056 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17044 925604 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
/dev/mapper/vg0-lv0 90880 1550 82460 2% /mnt
7.逻辑卷组vg大小的增加与减少
vg是由多个pv设备组成的,对vg设备大小的增加与减少其实就是对组成vg的pv设备的增加或者减少
(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.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (1230848-20971519, default 1230848):
Using default value 1230848
Last sector, +sectors or +size{K,M,G} (1230848-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
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: 0x29d83a76
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 8e Linux LVM
/dev/vdb3 1230848 3327999 1048576 83 Linux
Command (m for help): t
Partition number (1-3, default 3): 3
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: 0x29d83a76
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 8e Linux LVM
/dev/vdb3 1230848 3327999 1048576 8e Linux LVM
[root@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# fdisk -l /dev/vdb
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: 0x29d83a76
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 1230847 512000 8e Linux LVM
/dev/vdb3 1230848 3327999 1048576 8e Linux LVM
(2)对新建分区进行物理卷 物理卷组 逻辑卷组的划分
pvcreate /dev/vdb3 ##对磁盘新的物理分区id进行修改,将其做成物理卷pv
vgextend vg0 /dev/vdb3 ##将新做成的pv添加到需要增加大小的逻辑卷组中来
(3)vg(逻辑卷组)大小的增加与减少
移除前
移除后
pvmove /dev/vdb2 /dev/vdb3 #根据各个pv空间的使用情况,将被占用的空间都集中到一个设备中,空出一个空设备
vgreduce vg0 /dev/vdb2 ##将该pv设从该vg中移出
pvremove /dev/vdb2 #再将该空pv设备恢复成物理磁盘分区
8.Lv逻辑卷快照
作用:当我们存储信息在lv分区中以后,不想分区中的文件被损坏,可以通过快照的方式创建一个副本,通过该副本可以看到源分区中的内容,但是该副本中只记录对源分区的修改,不会带源分区中的文件造成任何影响
格式:
lvcreate -L 50M -n lv0-backup -s /dev/vg0/lv0
给/dev/vg0/lv0建立快照,-L 指定快照大小,-n指定快照名字 -s指定快照模板设备
[root@localhost ~]# lvcreate -L 50M -n lv0-kz -s /dev/vg0/lv0 ##创建快照
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0-kz" created
[root@localhost ~]# ls /dev/mapper
control vg0-lv0 vg0-lv0--kz vg0-lv0--kz-cow vg0-lv0-real ##vg0-lv0-kz出现则创建成功
[root@localhost ~]# df ##查看挂载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3177152 7296748 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17064 925584 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
/dev/mapper/vg0-lv0 90880 1550 82460 2% /mnt
[root@localhost ~]# touch /mnt/file{1..3}
[root@localhost ~]# ls /mnt ##通过快照的挂载可以看到原设备中的文件
file1 file2 file3 lost+found
将快照设备挂载之后,可以看到源设备中的文件,然后在快照设备中将文件删除,卸载设备之后,删除该lv分区的快照,重新生成一个,重新挂载,仍能看到源设备中的文件,说明对源设备文件没有影响
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
file1 file2 file3 lost+found
[root@localhost mnt]# rm -fr /mnt/* ##在快照中删除文件
[root@localhost mnt]# ls
[root@localhost mnt]# cd
[root@localhost ~]# umount /mnt ##卸载
[root@localhost ~]# lvremove /dev/vg0/lv0-kz ##删除快照
Do you really want to remove active logical volume lv0-kz? [y/n]: y
Logical volume "lv0-kz" successfully removed
[root@localhost ~]# lvcreate -L 50M -n lv0-kz -s /dev/vg0/lv0 ##重新创建快照
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0-kz" created
[root@localhost ~]# mount /dev/vg0/lv0-kz /mnt ##重新挂载
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls ##仍旧可以看到设备中的文件
file1 file2 file3 lost+found
6.LVM的删除
删除顺序:lvremove ##删除逻辑卷–vgremove ##删除逻辑卷组–pvremove ##删除物理卷
[root@localhost ~]# umount /mnt
[root@localhost ~]# lvremove /dev/vg0/lv0-kz ##删除快照
Do you really want to remove active logical volume lv0-backup? [y/n]: y
Logical volume "lv0-kz" 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/vdb3 ##删除物理卷
Labels on physical volume "/dev/vdb3" successfully wiped
[root@localhost ~]# lvs ##查看逻辑卷组
No volume groups found
[root@localhost ~]# vgs ##查看逻辑卷
No volume groups found
[root@localhost ~]# pvs ##查看物理卷