LVM介绍及相关操作
一、逻辑卷管理器介绍
逻辑卷管理器(英语:Logical Volume Manager,缩写为LVM),又译为逻辑卷宗管理器、逻辑扇区管理器、逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Logical volume management)功能。它在硬盘的硬盘分区之上,又创建一个逻辑层,以方便系统管理硬盘分割系统。
最先由IBM开发,在AIX系统上实现,OS/2 操作系统与 HP-UX也支持这个功能。在1998年,Heinz Mauelshagen 根据在 HP-UX 上的逻辑卷管理器,写作出第一个 Linux 版本的逻辑卷管理器。
LVM基本术语:
- PV:物理卷,PV处于LVM系统最低层,它可以是整个硬盘,或者与磁盘分区具有相同功能的设备(如RAID),但和基本的物理存储介质相比较,多了与LVM相关管理参数
- VG:卷组,创建在PV之上,由一个或多个PV组成,可以在VG上创建一个或多个“LVM分区”(逻辑卷),功能类似非LVM系统的物理硬盘
- LV:逻辑卷,从VG中分割出的一块空间,创建之后其大小可以伸缩,在LV上可以创建文件系统(如/var,/home)
- PE:物理区域,每一个PV被划分为基本单元(也被称为PE),具有唯一编号的PE是可以被LVM寻址的最小存储单元,默认为4MB
LVM用于以下目的:
- 创建多个物理卷或整个硬盘的单个逻辑卷(有点类似于RAID 0,但更类似于JBOD),从而可以动态调整卷大小。
- 通过结合热插拔,允许添加和更换磁盘而不会造成停机或服务中断,从而管理大型硬盘场。
- 在小型系统(例如台式机)上,LVM不必在安装时估计分区可能需要多大,而是可以根据需要轻松调整文件系统的大小。
- 通过对逻辑卷进行快照来执行一致的备份。
- 使用一个密码加密多个物理分区。
LVM可被视为硬盘和分区之上的薄软件层,它创建了连续性和易用性的抽象,用于管理硬盘驱动器的更换,重新分区和备份。
二、逻辑卷管理器操作
1、LVM进行准备 #添加一块新硬盘/dev/sdb [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 20G 0 disk sr0 11:0 1 1024M 0 rom #对新建硬盘/dev/sdb进行分区 [root@localhost ~]# fdisk /dev/sdb 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 0xd1c551dc. 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): 1 First sector (2048-41943039, default 2048): 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +3G Partition 1 of type Linux and of size 3 GiB 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 (6293504-41943039, default 6293504): Using default value 6293504 Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +4G Partition 2 of type Linux and of size 4 GiB is set 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 (14682112-41943039, default 14682112): Using default value 14682112 Last sector, +sectors or +size{K,M,G} (14682112-41943039, default 41943039): +2G Partition 3 of type Linux and of size 2 GiB is set #对新建的三个主分区更改硬盘属性为“Linux LVM”(代码:8e) Command (m for help): t Partition number (1-3, default 3): 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): 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): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. #查看新硬盘的分区,及因硬盘属性 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 3G 0 part ├─sdb2 8:18 0 4G 0 part └─sdb3 8:19 0 2G 0 part sr0 11:0 1 1024M 0 rom [root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xd1c551dc Device Boot Start End Blocks Id System /dev/sdb1 2048 6293503 3145728 8e Linux LVM /dev/sdb2 6293504 14682111 4194304 8e Linux LVM /dev/sdb3 14682112 18876415 2097152 8e Linux LVM 2、创建LVM #对三个主分区进行PV(物理卷)创建 [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created. [root@localhost ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created. #查看三个主分区是否存在PV [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 /dev/sdb1 lvm2 --- 3.00g 3.00g /dev/sdb2 lvm2 --- 4.00g 4.00g /dev/sdb3 lvm2 --- 2.00g 2.00g #将三个PV添加到LVMtest的VG(卷组)中,并设置PE(物理区域)的大小为12M [root@localhost ~]# vgcreate -s 12M LVMtest /dev/sdb{1..3} Volume group "LVMtest" successfully created #查看LVMtest这个VG是否存在。 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree LVMtest 3 0 0 wz--n- <8.98g <8.98g centos 1 2 0 wz--n- <19.00g 0 #创建大小为4G的LVMtest的逻辑分区 [root@localhost ~]# lvcreate -L 4G -n LVMtest LVMtest Rounding up size to full physical extent <4.01 GiB Logical volume "LVMtest" created. #查看LVMtest这个逻辑分区是否存在 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert LVMtest LVMtest -wi-a----- <4.01g root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g #格式化分区/dev/LVMtest/LVMtest,设置分区类型为ext4,并将其挂载至/data/目录下 [root@localhost ~]# mkfs.ext4 /dev/LVMtest/LVMtest 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 262944 inodes, 1050624 blocks 52531 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1075838976 33 block groups 32768 blocks per group, 32768 fragments per group 7968 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@localhost ~]# partprobe //在不重启机器的情况下系统能够重新读取分区 Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 3G 0 part │ └─LVMtest-LVMtest 253:2 0 4G 0 lvm ├─sdb2 8:18 0 4G 0 part │ └─LVMtest-LVMtest 253:2 0 4G 0 lvm └─sdb3 8:19 0 2G 0 part sr0 11:0 1 4.2G 0 rom /mnt [root@localhost ~]# mkdir /data [root@localhost ~]# mount /dev/LVMtest/LVMtest /data #复制一定的数据用于后面的测试 [root@localhost ~]# cp -R /etc/pam.d/ /data/ [root@localhost ~]# ll -d /data/ drwxr-xr-x. 4 root root 4096 Dec 24 02:16 /data/ [root@localhost ~]# ll -dl /data/ drwxr-xr-x. 4 root root 4096 Dec 24 02:16 /data/ [root@localhost ~]# ll /data/ total 20 drwx------. 2 root root 16384 Dec 24 02:15 lost+found drwxr-xr-x. 2 root root 4096 Dec 24 02:16 pam.d 3、对LVM进行增加容量实验 #为LVMtest分区新增2G容量,并验证数据完整性 1)扩展物理边界 2)扩展逻辑边界 [root@localhost ~]# lvextend -L +2G /dev/LVMtest/LVMtest //扩展物理边界 Rounding size to boundary between physical extents: 2.00 GiB. Size of logical volume LVMtest/LVMtest changed from <4.01 GiB (342 extents) to 6.01 GiB (513 extents). Logical volume LVMtest/LVMtest successfully resized. [root@localhost ~]# resize2fs /dev/LVMtest/LVMtest //扩展逻辑边界 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/LVMtest/LVMtest is mounted on /data; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/LVMtest/LVMtest is now 1575936 blocks long. [root@localhost ~]# ls -l /data/ total 20 drwx------. 2 root root 16384 Dec 24 02:15 lost+found drwxr-xr-x. 2 root root 4096 Dec 24 02:16 pam.d 4、对LVM进行缩减容量 #将LVMtest缩减至2G大小,并验证数据完整性(在生产环境中,不建议进行此操作!!!!!) #在这里采用ext4的文件系统类型, 1)先卸载物理文件系统 2)对文件系统进行强制检测和修复 3)缩减逻辑边界 4)缩减物理边界 5)重新挂载LVM分区 [root@localhost ~]# umount /dev/LVMtest/LVMtest/ [root@localhost ~]# e2fsck -f /dev/LVMtest/LVMtest //对文件系统进行强制检测和修复 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/LVMtest/LVMtest: 43/390432 files (0.0% non-contiguous), 61412/1575936 blocks [root@localhost ~]# resize2fs /dev/LVMtest/LVMtest 2G //缩减逻辑边界为2G大小 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/LVMtest/LVMtest to 524288 (4k) blocks. The filesystem on /dev/LVMtest/LVMtest is now 524288 blocks long. [root@localhost ~]# lvreduce -L 2G /dev/LVMtest/LVMtest //缩减物理边界为2G大小 Rounding size to boundary between physical extents: 2.00 GiB. WARNING: Reducing active logical volume to 2.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce LVMtest/LVMtest? [y/n]: y Size of logical volume LVMtest/LVMtest changed from 6.01 GiB (513 extents) to 2.00 GiB (171 extents). Logical volume LVMtest/LVMtest successfully resized. [root@localhost ~]# mount /dev/LVMtest/LVMtest /data //重新挂载LVM分区 #检查LVM分区大小 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 3G 0 part ├─sdb2 8:18 0 4G 0 part │ └─LVMtest-LVMtest 253:2 0 2G 0 lvm /data └─sdb3 8:19 0 2G 0 part sr0 11:0 1 4.2G 0 rom /mnt #检查数据完整性 [root@localhost ~]# ll /data/ total 20 drwx------. 2 root root 16384 Dec 24 02:15 lost+found drwxr-xr-x. 2 root root 4096 Dec 24 02:16 pam.d 5、删除LVM案例 #卸载LVM的分区 [root@localhost ~]# umount /data/ #删除在LVMtest上的LVM分区 [root@localhost ~]# lvremove /dev/LVMtest/LVMtest Do you really want to remove active logical volume LVMtest/LVMtest? [y/n]: y Logical volume "LVMtest" successfully removed #删除在LVMtest上的VG(卷组) [root@localhost ~]# vgremove LVMtest Volume group "LVMtest" successfully removed #删除在硬盘分区的PV [root@localhost ~]# pvremove /dev/sdb1 Labels on physical volume "/dev/sdb1" successfully wiped. [root@localhost ~]# pvremove /dev/sdb2 Labels on physical volume "/dev/sdb2" successfully wiped. [root@localhost ~]# pvremove /dev/sdb3 Labels on physical volume "/dev/sdb3" successfully wiped. [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <19.00g 0 [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g