一、什么是lvm
是对磁盘分区进行管理的一种机制。通过在硬盘和分区之上建立一个逻辑层,提高了磁盘分区管理的灵活性,在线调整磁盘各个分区的大小,动态调整磁盘容量。
PV --- 物理卷,由分区转化
VG --- 卷组,建立在pv之上,有一个或多个组成
LV --- 逻辑卷,建立在vg之上,是一个动态改变大小的分区
PE --- 物理区域,是lvm的最小寻址单位,大小为4MB
二、lvm相关常用命令
命令 | 含义 |
---|---|
pvcreate,vgcreate,lvcreate | 创建 |
pvremove,vgremove,lvremove | 移除 |
pvdisplay,vgdisplay,lvdisplay | 查看属性 |
pvscan,vgscan,lvscan | 扫描 |
pvs,vgs,lvs | 列出所有 |
pvmove | 数据迁移 |
lvextend | 扩容 |
lvreduce | 缩容 |
lvresize | 调整大小 |
三、创建lvm
1.安装软件包
[root@work1 ~]# yum install -y lvm2
2.创建物理卷PV
[root@work1 ~]# pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb.
Physical volume "/dev/sdb" successfully created.
[root@work1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 1.00g 1.00g
3.创建卷组VG
[root@work1 ~]# vgcreate vg_test01 /dev/sdb
Volume group "vg_test01" successfully created
[root@work1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_test01 1 0 0 wz--n- 1020.00m 1020.00m
4.创建逻辑卷lv
[root@work1 ~]# lvcreate -n lv_test01 -L 100MB vg_test01
WARNING: xfs signature detected on /dev/vg_test01/lv_test01 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/vg_test01/lv_test01.
Logical volume "lv_test01" created.
[root@work1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_test01 vg_test01 -wi-a----- 100.00m
5.查看磁盘分区信息
[root@work1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 2G 0 part [SWAP]
└─sda2 8:2 0 48G 0 part /
sdb 8:16 0 1G 0 disk
└─vg_test01-lv_test01 253:0 0 100M 0 lvm
sr0 11:0 1 1024M 0 rom
6.挂载使用
#xfs文件系统,ext4文件系统使用mkfs.ext4
[root@work1 ~]# mkfs.xfs /dev/vg_test01/lv_test01
[root@work1 parted]# mount /dev/vg_test01/lv_test01 data1
7.逻辑卷扩容
查看卷组容量,如果有空闲,则直接使用 lvextend扩容逻辑卷lv;如果没有空闲,则添加磁盘-->转化为pv-->加入卷组vg-->lvextend扩容逻辑卷lv。
[root@work1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_test01 1 1 0 wz--n- 1020.00m 920.00m
#vg容量充足,直接使用lvextend,缩容是lvreduce
[root@work1 ~]# lvextend -L 200MB /dev/vg_test01/lv_test01 # -L 是扩容到
Size of logical volume vg_test01/lv_test01 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
Logical volume vg_test01/lv_test01 successfully resized.
[root@work1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_test01 vg_test01 -wi-a----- 200.00m
#如果已经挂载使用,扩容后,系统是还没有识别的,需要用命令重读文件系统
xfs文件系统类型使用命令 xfs_growfs 扩容逻辑卷的路径
ext4文件系统类型使用命令 resize2fs 扩容逻辑卷的路径
[root@work1 parted]# xfs_growfs /dev/vg_test01/lv_test01
8.逻辑卷缩容
ext4文件系统支持缩容,但不建议进行缩容操作,缩容会损坏文件系统,导致数据丢失。
xfs文件系统不支持缩容,需要转换成ext4支持缩容的文件系统,再进行缩容。
1.查看磁盘挂载情况
[root@work1 parted]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 48G 3.5G 45G 8% /
devtmpfs devtmpfs 476M 0 476M 0% /dev
tmpfs tmpfs 487M 12K 487M 1% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg_test01-lv_test01 xfs 149M 5.4M 144M 4% /root/study/parted/data1
/dev/mapper/vg_test02-lv_test02 ext4 6.8M 77K 6.2M 2% /root/study/parted/data2
2.卸载磁盘
[root@work1 ~]# umount /dev/vg_test02/lv_test02
3.检查磁盘情况
[root@work1 parted]# e2fsck -f /dev/vg_test02/lv_test02
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/vg_test02/lv_test02: 11/2048 files (9.1% non-contiguous), 1362/8192 blocks
4.文件系统容量更新
[root@work1 parted]# resize2fs /dev/vg_test02/lv_test02
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg_test02/lv_test02 to 53248 (1k) blocks.
The filesystem on /dev/vg_test02/lv_test02 is now 53248 blocks long.
5.执行逻辑卷缩容命令,进行缩容,还可以用 lvresize命令
[root@work1 parted]# lvreduce -L 30MB /dev/vg_test02/lv_test02
Rounding size to boundary between physical extents: 32.00 MiB.
WARNING: Reducing active logical volume to 32.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_test02/lv_test02? [y/n]: y
Size of logical volume vg_test02/lv_test02 changed from 52.00 MiB (13 extents) to 32.00 MiB (8 extents).
Logical volume vg_test02/lv_test02 successfully resized.
6.查看逻辑卷信息,缩容成功
[root@work1 parted]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_test01 vg_test01 -wi-ao---- 152.00m
lv_test02 vg_test02 -wi-a----- 32.00m
7.挂载使用,挂载失败,文件系统损坏,数据丢失
[root@work1 parted]# mount /dev/vg_test02/lv_test02 data2
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg_test02-lv_test02,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
[root@work1 parted]# dmesg | tail
[15316.816635] e1000: eth2 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[15317.381916] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[16731.617369] bond0: Releasing backup interface eth2
[16835.339320] e1000: eth1 NIC Link is Down
[16836.329593] e1000: eth0 NIC Link is Down
[16842.622359] e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[16842.624127] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[19352.794060] EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts: (null)
[19998.112564] EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts: (null)
[20238.769157] EXT4-fs (dm-1): bad geometry: block count 53248 exceeds size of device (32768 blocks)
[root@work1 parted]# e2fsck -f /dev/vg_test02/lv_test02
e2fsck 1.42.9 (28-Dec-2013)
The filesystem size (according to the superblock) is 53248 blocks
The physical size of the device is 32768 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes
9.移除逻辑卷,恢复磁盘
1.卸载硬盘
[root@work1 parted]# umount data1
2.移除逻辑卷lv lvremove
[root@work1 parted]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_test01 vg_test01 -wi-a----- 300.00m
[root@work1 parted]# lvremove /dev/vg_test01/lv_test01
Do you really want to remove active logical volume vg_test01/lv_test01? [y/n]: y
Logical volume "lv_test01" successfully removed
[root@work1 parted]# lvs
3.移除卷组vg vgremove
[root@work1 parted]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_test01 1 0 0 wz--n- 1020.00m 1020.00m
[root@work1 parted]# vgremove vg_test01
Volume group "vg_test01" successfully removed
[root@work1 parted]# vgs
4.移除物理卷pv pvremove
[root@work1 parted]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 1.00g 1.00g
[root@work1 parted]# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped.
[root@work1 parted]# pvs