1.LVM是什么
LVM是一个Linux操作系统的逻辑卷管理器,现在有两个版本的LVM。LVM2是目前最新的版本,LVM 2是几乎完全向后兼容卷LVM。
PS:由LVM升级到LVM2需要删除快照。
2.使用LVM的优点
2.1逻辑卷管理提供了一个高级的较为磁盘管理,我们能够更灵活地分配存储空间(动态扩容,数据迁移,自定义卷组名,而不是以传统物理磁盘命名sd[a-z])。
2.2某分区空间紧缺,比如/home目录空间紧缺,使用传统的分区,解决此问题需要花费很大精力;但是使用LVM可以很快解决问题(在线扩容)。
3.缺点
因为使用LVM(分区,pv,vg,lv,格式化挂载)比传统文件系统管理多出了3个节点,性能必有所损失。
3.LVM的主要组成元素
PE(physicalextent), physical volume(PV), volume group(VG), logical extent(LE), logicalvolume (LV).
·····································
1. lvm创建 1.1. 分区 1.2. 调整分区类型 1.3. 创建 1.4. 格式化挂载 2. LVM相关信息查看(逻辑,卷组,物理卷) 2.1. 逻辑卷查看 2.2. 卷组查看 2.3. 物理卷查看 3. LVM扩容 3.1. 磁盘分区 3.2. 创建物理卷 3.3. 扩展卷组 3.4. lvm扩容 3.5. 重新刷新大小 4. LVM缩减 4.1. 卸载文件系统 4.2. df -h查看文件系统已经成功卸载 4.3. 对文件系统进行强制检测 4.4. 缩减文件系统到10G 4.5. 缩减LVM到10G 4.6. 重新挂载文件系统 4.7. 查看开机是否自动挂载 5. 移除LVM ############################################################# 1.lvm创建 ---
1.1分区 [root@www ~]# fdisk /dev/sdc 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 (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): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): N Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Using default response p Partition number (2-4, default 2): First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G Partition 2 of type Linux and of size 5 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 (20973568-41943039, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +2G Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): N Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): E Selected partition 4 First sector (25167872-41943039, default 25167872): Using default value 25167872 Last sector, +sectors or +size{K,M,G} (25167872-41943039, default 41943039): +3G Partition 4 of type Extended and of size 3 GiB is set
Command (m for help): N All primary partitions are in use Adding logical partition 5 First sector (25169920-31459327, default 25169920): Using default value 25169920 Last sector, +sectors or +size{K,M,G} (25169920-31459327, default 31459327): Using default value 31459327 Partition 5 of type Linux and of size 3 GiB is set
Command (m for help): W The partition table has been altered!
================================================== 1.2调整分区类型 --- 改变分区类型(8e Linux LVM) Command (m for help): t Partition number (1-5, default 5): 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-5, default 5): 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-5, default 5): 2 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-5, default 5): 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-5, default 5): 5 Hex code (type L to list all codes): 8e
You cannot change a partition into an extended one or vice versa. Delete it first.
Type of partition 4 is unchanged: Extended
Command (m for help): w The partition table has been altered!
Calling ioctl() to re-read partition table. Syncing disks. ###################################################################### 1.3 创建 [root@www ~]# pvcreate /dev/sdc{1..3} Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created Physical volume "/dev/sdc3" successfully created [root@www ~]# vgcreate vg0 /dev/sdc{1..3} Volume group "vg0" successfully created [root@www ~]# lvcreate -L 10g -n lv0 vg0 Logical volume "lv0" created. ####################################################################### 1.4格式化挂载 === a.格式化 [root@www ~]# mkfs.ext4 /dev/vg0/lv0 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 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2151677952 80 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, 1605632
Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done =========================================== b.挂载 [root@www ~]# mkdir /mnt/lv_test [root@www ~]# mount /dev/vg0/lv0 /mnt/lv_test 开机自动挂载 [root@www ~]# echo "/dev/mapper/vg0-lv0 /mnt/lv_test ext4 defaults 0 0" >>/etc/fstab [root@www ~]# tail -1 /etc/fstab /dev/mapper/vg0-lv0 /mnt/lv_test ext4 defaults 0 0
######################################################
2. LVM相关信息查看(逻辑,卷组,物理卷) --------------------------------------- 2.1.逻辑卷查看 [root@www ~]# lvdisplay /dev/vg0/lv0 --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID BebWlW-fUxw-se8Q-BfQp-Aodo-1hYT-YbrCQr LV Write Access read/write LV Creation host, time www.centos.com, 2015-09-15 19:20:07 -0400 LV Status available # open 0 LV Size 10.00 GiB Current LE 2560 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 ---------------------------------------- [root@www ~]# lvscan ACTIVE '/dev/centos/swap' [2.00 GiB] inherit ACTIVE '/dev/centos/root' [17.47 GiB] inherit ACTIVE '/dev/vg0/lv0' [10.00 GiB] inherit ==================================== 2.2. 卷组查看 [root@www ~]# vgscan Reading all physical volumes. This may take a while... Found volume group "centos" using metadata type lvm2 Found volume group "vg0" using metadata type lvm2 [root@www ~]# vgdisplay /dev/vg0 --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 3 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 3 Act PV 3 VG Size 11.99 GiB PE Size 4.00 MiB Total PE 3069 Alloc PE / Size 2560 / 10.00 GiB Free PE / Size 509 / 1.99 GiB VG UUID ZWKv1x-v4Ns-FfWl-hqmq-RCGI-1OZD-Olm7za ========================================================== 2.3. 物理卷查看 [root@www ~]# pvscan PV /dev/sda2 VG centos lvm2 [19.51 GiB / 40.00 MiB free] PV /dev/sdc1 VG vg0 lvm2 [5.00 GiB / 0 free] PV /dev/sdc2 VG vg0 lvm2 [5.00 GiB / 0 free] PV /dev/sdc3 VG vg0 lvm2 [2.00 GiB / 1.99 GiB free] [root@www ~]# pvdisplay ... ... ... ... --- Physical volume --- PV Name /dev/sdc1 VG Name vg0 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 1279 Free PE 0 Allocated PE 1279 PV UUID glzMi3-UYaV-YzRF-5Cx8-lwPA-pP6e-Tsx0I6
--- Physical volume --- PV Name /dev/sdc2 VG Name vg0 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 1279 Free PE 0 Allocated PE 1279 PV UUID hcYMzv-IPik-vEI0-pwEW-wo9l-4HCs-zINuRA ... ... ... ...
################################################ 3.LVM扩容 ----- 3.1. 磁盘分区 ... ... 略 参照1.1 ... ... 3.2. 创建物理卷 [root@www ~]# pvcreate /dev/sdc{5..6} //创建物理卷 Physical volume "/dev/sdc5" successfully created Physical volume "/dev/sdc6" successfully created [root@www ~]# pvscan //查看刚创建的物理卷 PV /dev/sda2 VG centos lvm2 [19.51 GiB / 40.00 MiB free] PV /dev/sdc1 VG vg0 lvm2 [5.00 GiB / 0 free] PV /dev/sdc2 VG vg0 lvm2 [5.00 GiB / 0 free] PV /dev/sdc3 VG vg0 lvm2 [2.00 GiB / 1.99 GiB free] PV /dev/sdc5 lvm2 [2.00 GiB] PV /dev/sdc6 lvm2 [6.00 GiB] ---- 3.3. 扩展卷组 [root@www ~]# vgextend vg0 /dev/sdc5 /dev/sdc6 Volume group "vg0" successfully extended [root@www ~]# vgdisplay /dev/vg0 --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 5 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 5 Act PV 5 VG Size 19.98 GiB PE Size 4.00 MiB Total PE 5115 Alloc PE / Size 2560 / 10.00 GiB Free PE / Size 2555 / 9.98 GiB VG UUID ZWKv1x-v4Ns-FfWl-hqmq-RCGI-1OZD-Olm7za ---- 3.4. lvm扩容 [root@www ~]# lvextend -L +5g /dev/vg0/lv0 Size of logical volume vg0/lv0 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents). Logical volume lv0 successfully resized ---- 3.5. 重新刷新大小 [root@www ~]# resize2fs /dev/vg0/lv0 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv_test; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 2 The filesystem on /dev/vg0/lv0 is now 3932160 blocks long. ################################################################ 4. LVM缩减 --- 4.1. 卸载文件系统 [root@www ~]# umount /dev/vg0/lv0 --- 4.2. df -h查看文件系统已经成功卸载 --- 4.3. 对文件系统进行强制检测 [root@www ~]# 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: 15/983040 files (0.0% non-contiguous), 105181/3932160 blocks --- 4.4. 缩减文件系统到10G [root@www ~]# resize2fs /dev/vg0/lv0 10G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg0/lv0 to 2621440 (4k) blocks. The filesystem on /dev/vg0/lv0 is now 2621440 blocks long. --- 4.5. 缩减LVM到10G [root@www ~]# lvreduce -L 10G /dev/vg0/lv0 WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv0? [y/n]: y Size of logical volume vg0/lv0 changed from 15.00 GiB (3840 extents) to 10.00 GiB (2560 extents). Logical volume lv0 successfully resized --- 4.6. 重新挂载文件系统,使用df -h查看是否已经看到所挂载的文件系统 [root@www ~]# mount /dev/mapper/vg0-lv0 /mnt/lv_test --- 4.7. 查看开机是否自动挂载 [root@www ~]# tail -5 /etc/fstab # /dev/mapper/centos-root / xfs defaults 0 0 UUID=f6e1aeb1-d2c8-46bf-bda0-5541250f8163 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 /dev/mapper/vg0-lv0 /mnt/lv_test ext4 defaults 0 0 ################################################################
5.移除LVM [root@www ~]# lvremove /dev/vg0/lv1 //移除文件系统 Do you really want to remove active logical volume lv1? [y/n]: y Logical volume "lv1" successfully removed [root@www ~]# vgremove /dev/vg0 //移除卷组 Volume group "vg0" successfully removed [root@www ~]# pvremove /dev/sdc1 //移除物理卷 Labels on physical volume "/dev/sdc1" successfully wiped =========================================================
|