感谢支持
我们一直在努力

LVM 逻辑卷的扩容/缩容

LVM 逻辑卷的扩容/缩容

[1] 创建逻辑卷
[root@hyperStor ~]# vgs //查看当前卷组
VG #PV #LV #SN Attr VSize VFree
vg0 2 2 0 wz–n- 698.38g 98.29g
[root@hyperStor ~]# lvcreate -L 2GB -n lv1 vg0 //在卷组vg0上建立2GB的逻辑卷lv1
Logical volume “lv1″created
[root@hyperStor ~]# lvs |grep lv1
lv1 vg0 -wi-a- 2.00g
[root@hyperStor ~]# mkfs.ext4 /dev/vg0/lv1 > /dev/null 2>& 1 //格式化lv1
[root@hyperStor ~]# mkdir /lv1
[root@hyperStor ~]# mount /dev/vg0/lv1 /lv1 //挂载lv1到/lv1目录下
[root@hyperStor ~]# df -H |grep lv1 //查看挂载状态
/dev/mapper/vg0-lv12.2G 71M 2.0G 4% /lv1
[root@hyperStor ~]# mount |grep lv1
/dev/mapper/vg0-lv1on /lv1typeext4 (rw)
[root@hyperStor ~]# dd if=/dev/zero of=/lv1/test1 bs=100M count=5 //想/lv1中写入500M数据
5+0 records in
5+0 records out
524288000 bytes (524 MB) copied, 1.92862 s, 272 MB/s
[root@hyperStor ~]# du -sh /lv1/* //查看文件状态
16K/lv1/lost+found
500M/lv1/test1

[2] lvextend命令扩容lv1到3GB
//将lv1扩到3GB,如果是从2GB到5GB,则为lvextend -L +3GB
[root@hyperStor ~]# lvextend  -L 3GB -n  /dev/vg0/lv1
  Extending logical volume lv1 to 3.00 GiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# lvs |grep lv1  //查看当前逻辑卷大小,已经为3GB
  lv1            vg0  -wi-ao  3.00g                           
[root@hyperStor ~]# df -H |grep lv1  //查看文件系统状态,发现文件系统仍没有扩容
/dev/mapper/vg0-lv1    2.2G  595M  1.5G  30% /lv1
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  //调整文件系统大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv1 is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv1 to 786432 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 786432 blocks long.
[root@hyperStor ~]# df -H |grep lv1  //重新查看文件系统大小,成功在线扩容到3GB
/dev/mapper/vg0-lv1    3.2G  595M  2.5G  20% /lv1
[root@hyperStor ~]# lvs |grep lv1
  lv1            vg0  -wi-ao  3.00g                           
[root@hyperStor ~]# du -sh /lv1/*    //查看数据是否完整
16K/lv1/lost+found
501M/lv1/test1

[3] lvreduce 缩容,不可“在线”操作 lv1:3GB–>1GB
[root@hyperStor ~]# umount /lv1/      //卸载文件系统
[root@hyperStor ~]# mount |grep lv1|wc -l  //查看是否成功卸载
0
[root@hyperStor ~]# lvs |grep lv1  //查看当前lv1 状态
  lv1            vg0  -wi-a-  3.00g                           
[root@hyperStor ~]# e2fsck -f /dev/vg0/lv1  //进行磁盘检查
e2fsck 1.41.12 (17-May-2010)
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/lv1: 12/196608 files (0.0% non-contiguous), 157501/786432 blocks
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  1G  //调整文件系统
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv1 to 262144 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 262144 blocks long.
[root@hyperStor ~]# lvs |grep lv1    //查看lv1 大小,未变
  lv1            vg0  -wi-a-  3.00g                           
[root@hyperStor ~]# lvreduce -L 1GB /dev/vg0/lv1  //通过lvduce命令进行缩容
  WARNING: Reducing active logical volume to 1.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 1.00 GiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# lvs |grep lv1  //再次查看lv1大小,减少
  lv1            vg0  -wi-a-  1.00g                           
[root@hyperStor ~]# mount /dev/vg0/lv1  /lv1/  //重新挂载
[root@hyperStor ~]# df -H |grep lv1  //查看当前状态
/dev/mapper/vg0-lv1    1.1G  595M  410M  60% /lv1
[root@hyperStor ~]# du -sh /lv1/* //查看数据情况
16K/lv1/lost+found
501M/lv1/test1

注意:在进行扩容,缩容之后进行原有文件完整性校验的时候最好通过比较文件的md5值是否保持不变以判断操作对原有文件没有造成影响
如:
【1】扩容:两次/lv1/test1 的md5值均为d8b61b2c0025919d5321461045c8226f
[root@hyperStor ~]# md5sum  /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1
[root@hyperStor ~]# lvextend -L 2GB /dev/vg0/lv1
  Extending logical volume lv1 to 2.00 GiB
  Logical volume lv1 successfully resized
r[root@hyperStor ~]# resize2fs  /dev/vg0/lv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv1 is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv1 to 524288 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 524288 blocks long.
[root@hyperStor ~]# df -H |grep lv1
/dev/mapper/vg0-lv1    2.2G  595M  1.5G  30% /lv1
[root@hyperStor ~]# md5sum  /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1

【2】缩容
[root@hyperStor ~]# md5sum  /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1
[root@hyperStor ~]# umount /lv1/
[root@hyperStor ~]# e2fsck  -f /dev/vg0/lv1
e2fsck 1.41.12 (17-May-2010)
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/lv1: 12/131072 files (0.0% non-contiguous), 153389/524288 blocks
[root@hyperStor ~]# resize2fs  /dev/vg0/lv1  800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv1 to 204800 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 204800 blocks long.
[root@hyperStor ~]# lvreduce  -L 800M /dev/vg0/lv1
  WARNING: Reducing active logical volume to 800.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 800.00 MiB
  Logical volume lv1 successfully resized
[root@hyperStor ~]# mount  /dev/vg0/lv1  /lv1
[root@hyperStor ~]# md5sum  /lv1/test1
d8b61b2c0025919d5321461045c8226f  /lv1/test1

拓展:LVM 卷组“扩容/缩容”
上面说了如果对LVM 逻辑卷进行扩容以及缩容,这边说下如何对卷组进行所谓的扩容跟缩容,大家都知道卷组由物理卷组成,物理卷是卷组的最小组成单位,那么卷组的扩容跟缩容无非就是物理卷加入卷组以及移除卷组的过程。
vgextend vg0 new_pv //将新的pv设备加入vg0
vgreduce vg0 old_pv //将pv从vg0中删除,注意:是vgreduce 不是vgremove

相关阅读:

mysqldump和LVM逻辑卷快照 http://www.linuxidc.com/Linux/2013-09/90382.htm

LVM的综合性介绍及实现 http://www.linuxidc.com/Linux/2013-03/81482.htm

MySQL高性能备份方案解决数据不间断访问(LVM快照方式备份) http://www.linuxidc.com/Linux/2013-07/87887.htm

LVM上实施ASM http://www.linuxidc.com/Linux/2012-11/73475.htm

赞(0) 打赏
转载请注明出处:服务器评测 » LVM 逻辑卷的扩容/缩容
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏