Linux----LVM扩容磁盘空间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux----LVM扩容磁盘空间相关的知识,希望对你有一定的参考价值。
Linux系统LVM扩容一个分区相对于Windows来说没有那么直观,但是熟悉命令后,扩容起来也是蛮方便的。
扩容场景如下:
1 [[email protected] ~]# df -Th 2 Filesystem Type Size Used Avail Use% Mounted on 3 /dev/sda2 ext4 9.9G 2.2G 7.3G 23% / 4 tmpfs tmpfs 491M 0 491M 0% /dev/shm 5 /dev/sda1 ext4 194M 29M 155M 16% /boot 6 /dev/sr0 iso9660 3.6G 3.6G 0 100% /mnt 7 /dev/mapper/VG001-lv1 ext4 2.0G 67M 1.9G 4% /lv1
现有一个lvm分区lv1容量2G,希望扩容此分区空间
1 [[email protected] home]# cat /proc/partitions 2 major minor #blocks name 3 4 8 0 20971520 sda 5 8 1 204800 sda1 6 8 2 10485760 sda2 7 8 3 2097152 sda3 8 8 4 1 sda4 9 8 5 2103487 sda5 10 253 0 2097152 dm-0
查看partitions 获取当前分区情况和磁盘总空间情况。需要计算20971520-204800-10485760-2097152-2103487-1=6080320,约6G的未使用空间。(dm-0是一个lv分区)
当然使用这种手动方式获取未分区空间太过麻烦。可以用这个shell来完成大量的计算
1 [[email protected] home]# cat sda.sh 2 #!/bin/bash 3 total=$(grep ‘sda$‘ /proc/partitions |awk ‘{print $3}‘); 4 used=0 5 for i in $(grep ‘sda[[:digit:]]\+$‘ /proc/partitions |awk ‘{print $3}‘ |xargs) 6 do 7 used=$(( used + i )); 8 done 9 echo $((( total - used ) / 1024 )) "MB" 10 [[email protected] home]# ./sda.sh 11 5937 MB
接下来执行具体的分区步骤:
一、划分一个新分区并格式化
1 [[email protected] ~]# fdisk /dev/sda 2 3 Command (m for help): p 4 5 Disk /dev/sda: 21.5 GB, 21474836480 bytes 6 255 heads, 63 sectors/track, 2610 cylinders 7 Units = cylinders of 16065 * 512 = 8225280 bytes 8 Sector size (logical/physical): 512 bytes / 512 bytes 9 I/O size (minimum/optimal): 512 bytes / 512 bytes 10 Disk identifier: 0x00029f55 11 12 Device Boot Start End Blocks Id System 13 /dev/sda1 * 1 26 204800 83 Linux 14 Partition 1 does not end on cylinder boundary. 15 /dev/sda2 26 1332 10485760 83 Linux 16 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 17 /dev/sda4 1593 2610 8176089 5 Extended 18 /dev/sda5 1593 1854 2103487+ 8e Linux LVM 19 20 Command (m for help): n #新建分区,由于我已新建了四个分区(主分区和逻辑分区),所以这里只能新建扩展分区,并没有出现分区类型选项 21 First cylinder (1855-2610, default 1855): #开始点,直接回车即可 22 Using default value 1855 23 Last cylinder, +cylinders or +size{K,M,G} (1855-2610, default 2610): +2G #+2G,划分一个2G的新分区 24 25 Command (m for help): p 26 27 Disk /dev/sda: 21.5 GB, 21474836480 bytes 28 255 heads, 63 sectors/track, 2610 cylinders 29 Units = cylinders of 16065 * 512 = 8225280 bytes 30 Sector size (logical/physical): 512 bytes / 512 bytes 31 I/O size (minimum/optimal): 512 bytes / 512 bytes 32 Disk identifier: 0x00029f55 33 34 Device Boot Start End Blocks Id System 35 /dev/sda1 * 1 26 204800 83 Linux 36 Partition 1 does not end on cylinder boundary. 37 /dev/sda2 26 1332 10485760 83 Linux 38 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 39 /dev/sda4 1593 2610 8176089 5 Extended 40 /dev/sda5 1593 1854 2103487+ 8e Linux LVM 41 /dev/sda6 1855 2116 2104483+ 83 Linux 42 43 Command (m for help): w #保存退出
1 [[email protected] ~]# partprobe #不要忘了刷新分区表,虚拟机还是需要重启才能刷新。
1 [[email protected] ~]# mkfs.ext4 /dev/sda6 #格式化
二、转换sda6分区为pv
1 [[email protected] ~]# pvcreate /dev/sda6 #转换sda6分区 2 dev_is_mpath: failed to get device for 8:6 3 Physical volume "/dev/sda6" successfully created 4 [[email protected] ~]# pvdisplay #查看系统所有pv,这里sda6已成功转换 5 --- Physical volume --- 6 PV Name /dev/sda5 7 VG Name VG001 8 PV Size 2.01 GiB / not usable 2.19 MiB 9 Allocatable yes 10 PE Size 4.00 MiB 11 Total PE 513 12 Free PE 1 13 Allocated PE 512 14 PV UUID dSHFLX-9Lui-0ZDb-6J2g-pp6A-azuX-vg5yGV 15 16 "/dev/sda6" is a new physical volume of "2.01 GiB" 17 --- NEW Physical volume --- 18 PV Name /dev/sda6 19 VG Name 20 PV Size 2.01 GiB 21 Allocatable NO 22 PE Size 0 23 Total PE 0 24 Free PE 0 25 Allocated PE 0 26 PV UUID TEGKht-876S-8MMd-vWkX-rjeI-4UqG-JqRsT2
三、添加新pv到vg
1 [[email protected] ~]# vgdisplay |grep Name #查看当前vg名称 2 VG Name VG001 3 [[email protected] ~]# vgextend VG001 /dev/sda6 #添加“sda6”pv到此vg 4 Volume group "VG001" successfully extended 5 [[email protected] ~]# vgdisplay #查看vg详情,可以看到vg已经扩容到4g 6 --- Volume group --- 7 VG Name VG001 8 System ID 9 Format lvm2 10 Metadata Areas 2 11 Metadata Sequence No 3 12 VG Access read/write 13 VG Status resizable 14 MAX LV 0 15 Cur LV 1 16 Open LV 1 17 Max PV 0 18 Cur PV 2 19 Act PV 2 20 VG Size 4.01 GiB 21 PE Size 4.00 MiB 22 Total PE 1026 23 Alloc PE / Size 512 / 2.00 GiB 24 Free PE / Size 514 / 2.01 GiB 25 VG UUID qG18Fp-agAP-gaTp-0crS-Ughm-UilM-2ZftQt
四、调整lv1容量
1 [[email protected] ~]# lvextend -L 4G /dev/VG001/lv1 #调整lv1分区容量为4G,原来是2G 2 Extending logical volume lv1 to 4.00 GiB 3 Logical volume lv1 successfully resized 4 [[email protected] ~]# resize2fs /dev/VG001/lv1 #执行重设大小,对当前lv1有效 5 resize2fs 1.41.12 (17-May-2010) 6 Filesystem at /dev/VG001/lv1 is mounted on /lv1; on-line resizing required 7 old desc_blocks = 1, new_desc_blocks = 1 8 Performing an on-line resize of /dev/VG001/lv1 to 1048576 (4k) blocks. 9 The filesystem on /dev/VG001/lv1 is now 1048576 blocks long.
五、到此设置已完成,df命令查看已扩容到4G
1 [[email protected] ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/sda2 9.9G 2.2G 7.3G 23% / 4 tmpfs 491M 0 491M 0% /dev/shm 5 /dev/sda1 194M 29M 155M 16% /boot 6 /dev/sr0 3.6G 3.6G 0 100% /mnt 7 /dev/mapper/VG001-lv1 4.0G 68M 3.7G 2% /lv1
问题1:执行partprobe刷新分区表时会出现以下报错:
[[email protected] ~]# partprobe Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your changes until after reboot.
这个问题貌似只存在于虚拟机上面,物理机上并没有发现这个问题。重启虚拟机即可。
问题2:fdisk可以查看到sda5分区,但是使用mkfs格式化是提示“没有这个文件或目录”,那是分区表没有刷新或者刷新失败,刷新步骤可参考上一个问题。
1 Device Boot Start End Blocks Id System 2 /dev/sda1 * 1 26 204800 83 Linux 3 Partition 1 does not end on cylinder boundary. 4 /dev/sda2 26 1332 10485760 83 Linux 5 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 6 /dev/sda4 1593 2610 8176089 5 Extended 7 /dev/sda5 1593 1854 2103487+ 83 Linux 8 [[email protected] ~]# mkfs.ext4 /dev/sda5 9 mke2fs 1.41.12 (17-May-2010) 10 无法对 /dev/sda5 进行 stat 调用 --- 没有那个文件或目录
以上是关于Linux----LVM扩容磁盘空间的主要内容,如果未能解决你的问题,请参考以下文章