LVM添加硬盘和扩容
测试环境:CentOS7 64位(VM虚拟机)
LVM版本:lvm2-2.02.105-14.el7.x86_64
1、添加一块硬盘(8GB)到系统中
使用 fdisk –l 查看这块硬盘为 /dev/sdb
[root@mongodb ~]# fdisk -l Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
2、对新盘分区
使用 fdisk 命令对新盘进行分区,这里建立了一个主分区/dev/sdb1,大小20GB
在分区的过程中,注意设置格式为8e,这是LVM的分区格式
3、创建物理卷(PV)
使用 pvcreate 命令创建物理卷,pvdisplay 查看物理卷信息
[root@mongodb ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@mongodb ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg_mongodb1 PV Size 39.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 10114 Free PE 0 Allocated PE 10114 PV UUID sQKucQ-FECc-5XZB-podl-F6QT-rlTd-D9DCzG "/dev/sdb1" is a new physical volume of "19.99 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 19.99 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID GOi4ZC-JpiA-5nXt-TAzJ-jHVV-8Tp5-ylwuPf
4、创建VG
使用 vgcreate 命令创建vg_mongodb1:
[root@mongodb ~]# vgcreate vg_mongodb1 /dev/sdb1 Volume group "vg_mongodb1" successfully extended
查看一下卷组信息,发现空闲空间为8GB,说明VG已经成功创建:
[root@mongodb ~]# vgdisplay --- Volume group --- VG Name vg_mongodb1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 59.50 GiB PE Size 4.00 MiB Total PE 15232 Alloc PE / Size 10114 / 39.51 GiB Free PE / Size 5118 / 19.99 GiB VG UUID Dw9k5w-HdQd-klbx-fi3D-lJIu-Vd74-uf9h7u
5、创建逻辑卷(LV)
使用 lvcreate 命令从卷组里划分一个新的逻辑卷,这里创建了名称为newlv,大小8GB的逻辑卷分区;使用lvdisplay 查看逻辑卷信息:
[root@mongodb ~]# lvcreate -L 8G -n newlv vg_mongodb1 (lvcreate -l +100%FREE -n newlv vg_mongodb1 使用全部空闲大小) Logical volume "newlv" created [root@mongodb ~]# lvdisplay --- Logical volume --- LV Path /dev/vg_mongodb1/newlv LV Name newlv VG Name vg_mongodb1 LV UUID 4Ui8Lu-ewVM-sl9x-uAtf-M9mK-JzkZ-qzo7M3 LV Write Access read/write LV Creation host, time mongodb, 2017-06-30 17:22:14 +0800 LV Status available # open 0 LV Size 8.00 GiB Current LE 2048 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
我们再查看一下卷组信息,卷组剩余12GB空间了:
[root@mongodb ~]# vgdisplay --- Volume group --- VG Name vg_mongodb1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 59.50 GiB PE Size 4.00 MiB Total PE 15232 Alloc PE / Size 12162 / 47.51 GiB Free PE / Size 3070 / 11.99 GiB VG UUID Dw9k5w-HdQd-klbx-fi3D-lJIu-Vd74-uf9h7u
6、格式化逻辑卷并挂载
新逻辑卷经过格式化就可以挂载到系统里存储数据了。使用 mkfs.ext4 格式化为CentOS7的ext4文件系统:
[root@mongodb ~]# mkfs.ext4 /dev/vg_mongodb1/newlv mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 524288 inodes, 2097152 blocks 104857 blocks (5.00{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418}) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=2147483648 64 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 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
挂载到/test目录下(你可以挂载到自己需要的目录下),挂载后看到容量为8GB:
[root@mongodb ~]# mount /dev/vg_mongodb1/newlv /test/ [root@mongodb ~]# df -h Filesystem Size Used Avail Use{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} Mounted on /dev/mapper/vg_mongodb1-lv_root 37G 5.6G 30G 16{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} / tmpfs 931M 0 931M 0{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /dev/shm /dev/sda1 485M 39M 421M 9{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /boot /dev/mapper/vg_mongodb1-newlv 7.9G 146M 7.4G 2{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /test
设置开机自动挂载,编辑 /etc/fstab 文件,加入最后一行
第五列是dump备份设置。
当其值设置为1时,将允许dump备份程序备份;设置为0时,忽略备份操作;
第六列是fsck磁盘检查设置。
其值是一个顺序。当其值为0时,永远不检查;而 / 根目录分区永远都为1。其它分区从2开始,数字越小越先检查,如果两个分区的数字相同,则同时检查。
/dev/vg_mongodb1/newlv /test/ ext4 defaults 1 3
7、逻辑卷扩容
首先对新加磁盘进行格式化操作
将新加的分区 /dev/sdb1 创建为物理卷
[root@mongodb ~]# pvcreate /dev/sdb1
给卷组‘vg_mongodb1’扩容,将物理卷 /dev/sdb1 扩展至‘vg_mongodb1’卷组
[root@mongodb ~]# vgextend vg_mongodb1 /dev/sdb1
使用 lvextend 命令进行逻辑卷扩容。我把所有剩余空间都分配给了newlv,增大到了20GB,
[root@mongodb ~]# lvextend -l +100%FREE /dev/vg_mongodb1/newlv Extending logical volume newlv to 19.99 GiB Logical volume newlv successfully resized
使用resize2fs 命令在线调整xfs格式文件系统大小
[root@mongodb ~]# resize2fs /dev/vg_mongodb1/newlv resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg_mongodb1/newlv is mounted on /test; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/vg_mongodb1/newlv to 5240832 (4k) blocks. The filesystem on /dev/vg_mongodb1/newlv is now 5240832 blocks long.
最后我们看到逻辑卷分区已经动态扩容到了20GB:
[root@mongodb ~]# df -h Filesystem Size Used Avail Use{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} Mounted on /dev/mapper/vg_mongodb1-lv_root 37G 5.6G 30G 16{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} / tmpfs 931M 0 931M 0{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /dev/shm /dev/sda1 485M 39M 421M 9{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /boot /dev/mapper/vg_mongodb1-newlv 20G 150M 19G 1{5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418} /test