Linux LVM分区与应用
1.什么是LVM
LVM(Logical volume Manager)逻辑卷管理是通过将底层物理硬盘抽象封装起来,以逻辑卷的形式表现给上层系统,逻辑卷的大小可以动态调整,而且不会丢失现有数据.新加入的硬盘也不会改变现有上层的逻辑卷.
作为一种动态磁盘管理机制,逻辑卷技术打打提高了磁盘管理的灵活性.
2.四个基本概念
-
PE(physical Extend) 物理拓展
- LVM最基本单位
- 默认4M大小
-
PV(physical volume) 物理卷
-
物理设备
-
拆分为多个PE
-
-
VG(physical group) 卷组(空间池 )
- 由PE组成
-
LV(logical volume) 逻辑卷,操作系统最终使用逻辑卷
- 从VG中取出一部分PE组成
3.图示
4.创建LVM的过程
逻辑卷的大小必须是PE的整数倍
- 将物理磁盘设备初始化为物理卷
[root@serv02 ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
- 查看物理卷信息
[root@serv02 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <15.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3839
Free PE 0
Allocated PE 3839
PV UUID vPo1Cg-9ZD5-XMZe-mkj4-1lsv-8BrT-UhxhnF
"/dev/sdb" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID BfipOc-FnD5-UN2t-Pwud-sfMe-D4Qa-5wxsWb
[root@serv02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <15.00g 0
/dev/sdb lvm2 --- 10.00g 10.00g
- 创建卷组,并将PV加入卷组中
[root@serv02 ~]# vgcreate Nnuxcast /dev/sdb
Volume group "Nnuxcast" successfully created
- 查看卷组
[root@serv02 ~]# vgdisplay
--- Volume group ---
VG Name Nnuxcast
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 <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / <10.00 GiB
VG UUID H5dRdF-21wm-cski-z1Zi-3n5W-2b4x-THK2n1
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <15.00 GiB
PE Size 4.00 MiB
Total PE 3839
Alloc PE / Size 3839 / <15.00 GiB
Free PE / Size 0 / 0
VG UUID g5wLSY-M6cf-IJCW-tyfd-TJPf-rx7c-JFB9sc
[root@serv02 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
Nnuxcast 1 0 0 wz--n- <10.00g <10.00g
centos 1 2 0 wz--n- <15.00g 0
- 基于卷组创建逻辑卷
[root@serv02 ~]# lvcreate -n mylv -L 5G Nnuxcast
Logical volume "mylv" created.
#-n 逻辑卷名
#-L 逻辑卷大小
- 查看逻辑卷
[root@serv02 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/Nnuxcast/mylv
LV Name mylv
VG Name Nnuxcast
LV UUID 8989Mx-40nm-UCOR-dbGF-1UUA-Qoqe-IGI77F
LV Write Access read/write
LV Creation host, time serv02.wangc.com, 2021-10-19 07:46:31 -0400
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID oUPpmQ-vbH4-KdsY-7Klu-dszH-2vYw-RuhXeg
LV Write Access read/write
LV Creation host, time localhost, 2021-09-25 07:14:18 -0400
LV Status available
# open 1
LV Size 13.39 GiB
Current LE 3429
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID uJsDxF-9Ycd-4mq0-LkLU-Lbpn-rI1T-wicfjY
LV Write Access read/write
LV Creation host, time localhost, 2021-09-25 07:14:18 -0400
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
[root@serv02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv Nnuxcast -wi-a----- 5.00g
root centos -wi-ao---- 13.39g
swap centos -wi-ao---- 1.60g
- 为创建好的逻辑卷创建文件系统
[root@serv02 ~]# mkfs.ext4 /dev/Nnuxcast/mylv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
- 将格式化好的逻辑卷挂载使用
[root@serv02 ~]# mount /dev/Nnuxcast/mylv /mnt
[root@serv02 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/mapper/Nnuxcast-mylv 4.8G 20M 4.6G 1% /mnt
[root@serv02 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=495592k,nr_inodes=123898,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=22,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12435)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=101512k,mode=700)
/dev/mapper/Nnuxcast-mylv on /mnt type ext4 (rw,relatime,seclabel,data=ordered)
5.删除操作
-
删除LV
删除前先卸载已经挂载的目录
[root@serv02 mapper]# umount /mnt/
[root@serv02 mapper]# lvremove /dev/Nnuxcast/mylv
Do you really want to remove active logical volume Nnuxcast/mylv? [y/n]: y
Logical volume "mylv" successfully removed
- 删除VG
[root@serv02 mapper]# vgremove /dev/Nnuxcast
Volume group "Nnuxcast" successfully removed
[root@serv02 mapper]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <15.00g 0
- 删除物理卷
[root@serv02 mapper]# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped.
[root@serv02 mapper]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <15.00g 0
6逻辑卷的大小调整
6.1拉伸逻辑卷
逻辑卷可以在线拉伸,不会丢失数据
- 保证VG中有足够的空闲空间
[root@serv02 mapper]# vgdisplay
--- Volume group ---
VG Name Mypv
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 512 / 2.00 GiB
Free PE / Size 2047 / <8.00 GiB
VG UUID wikKOb-Obfy-nsCL-956f-ufsc-QJzi-0CTFh3
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <15.00 GiB
PE Size 4.00 MiB
Total PE 3839
Alloc PE / Size 3839 / <15.00 GiB
Free PE / Size 0 / 0
VG UUID g5wLSY-M6cf-IJCW-tyfd-TJPf-rx7c-JFB9sc
- 扩充逻辑卷(此处原逻辑卷为2G)
[root@serv02 mapper]# lvextend -L +1G /dev/Mypv/mylv
Size of logical volume Mypv/mylv changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
Logical volume Mypv/mylv successfully resized.
#此时查看磁盘/mnt仍是2G
[root@serv02 mapper]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/mapper/Mypv-mylv 2.0G 6.0M 1.8G 1% /mnt
- 更新文件系统
[root@serv02 mapper]# resize2fs /dev/Mypv/mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/Mypv/mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/Mypv/mylv is now 786432 blocks long.
#此时查看已经增加
[root@serv02 mapper]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/mapper/Mypv-mylv 2.9G 6.0M 2.8G 1% /mnt
#查看文件系统
cat /etc/fstab
#resize2fs命令针对ext2、ext3、ext4文件系统
#xfs_growfs 针对文件系统xfs
6.2扩充卷组
- 查看一下卷组
[root@serv02 mapper]# vgs
VG #PV #LV #SN Attr VSize VFree
Mypv 1 1 0 wz--n- <10.00g <7.00g
centos 1 2 0 wz--n- <15.00g 0
- 添加物理硬盘
- 将硬盘格式化为PV
[root@serv02 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
#查看一下PV
[root@serv02 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <15.00g 0
/dev/sdb Mypv lvm2 a-- <10.00g <7.00g
/dev/sdc lvm2 --- 10.00g 10.00g
- 将新的PV加入到VG中
[root@serv02 ~]# vgs #先看一下
VG #PV #LV #SN Attr VSize VFree
Mypv 1 1 0 wz--n- <10.00g <7.00g
centos 1 2 0 wz--n- <15.00g 0
[root@serv02 ~]# vgextend Mypv /dev/sdc #添加
Volume group "Mypv" successfully extended
[root@serv02 ~]# vgs #再看一眼.有了
VG #PV #LV #SN Attr VSize VFree
Mypv 2 1 0 wz--n- 19.99g 16.99g
centos 1 2 0 wz--n- <15.00g 0
6.3缩小逻辑卷
缩小操作无法在线执行,需要卸载已经挂载的逻辑卷
- 卸载已经挂载的逻辑卷
#老规矩,看一眼在操作,操作完了再看一眼
[root@serv02 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/mapper/Mypv-mylv 2.9G 9.0M 2.8G 1% /mnt
[root@serv02 ~]# umount /mnt/
[root@serv02 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
- 缩小文件系统
[root@serv02 ~]# resize2fs /dev/Mypv/mylv 1G
resize2fs 1.42.9 (28-Dec-2013)
Please run 'e2fsck -f /dev/Mypv/mylv' first.
#此处提示需要先运行e2fsck -f /dev/Mypv/mylv对文件系统进行检查,-f参数是强制检查,不修复
[root@serv02 ~]# e2fsck -f /dev/Mypv/mylv
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/Mypv/mylv: 11/196608 files (0.0% non-contiguous), 31036/786432 blocks
[root@serv02 ~]# resize2fs /dev/Mypv/mylv 1G #
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/Mypv/mylv to 262144 (4k) blocks.
The filesystem on /dev/Mypv/mylv is now 262144 blocks long.
- 缩小LV
[root@serv02 ~]# lvreduce -L -2G /dev/Mypv/mylv
WARNING: Reducing active logical volume to 1.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce Mypv/mylv? [y/n]: y
Size of logical volume Mypv/mylv changed from 3.00 GiB (768 extents) to 1.00 GiB (256 extents).
Logical volume Mypv/mylv successfully resized.
[root@serv02 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv Mypv -wi-a----- 1.00g
root centos -wi-ao---- 13.39g
swap centos -wi-ao---- 1.60g
#缩小时一定要检查空间是否正确,否则会损坏文件数据
- 重新挂载
[root@serv02 ~]# mount /dev/Mypv/mylv /mnt/
[root@serv02 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 7.1M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/mapper/Mypv-mylv 944M 7.6M 870M 1% /mnt
6.4缩小卷组
缩小卷组直接从卷组中取出PV即可
[root@serv02 ~]# vgreduce Mypv /dev/sdc
Removed "/dev/sdc" from volume group "Mypv"
[root@serv02 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
Mypv 1 1 0 wz--n- <10.00g <9.00g
centos 1 2 0 wz--n- <15.00g 0
#此时可以看到VG中少了一个PV
#特别注意:VG的空闲空间必须大于要取出的PV的空间大小