Linux系统LVM扩容一个分区相对于Windows来说没有那么直观,但是熟悉命令后,扩容起来也是蛮方便的。
扩容场景如下:
[root@rhel06 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 9.9G 2.2G 7.3G 23% /
tmpfs tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 ext4 194M 29M 155M 16% /boot
/dev/sr0 iso9660 3.6G 3.6G 0 100% /mnt
/dev/mapper/VG001-lv1 ext4 2.0G 67M 1.9G 4% /lv1
现有一个lvm分区lv1容量2G,希望扩容此分区空间
[root@rhel06 home]# cat /proc/partitions
major minor #blocks name 8 0 20971520 sda
8 1 204800 sda1
8 2 10485760 sda2
8 3 2097152 sda3
8 4 1 sda4
8 5 2103487 sda5
253 0 2097152 dm-0
查看partitions 获取当前分区情况和磁盘总空间情况。需要计算20971520-204800-10485760-2097152-2103487-1=6080320,约6G的未使用空间。(dm-0是一个lv分区)
当然使用这种手动方式获取未分区空间太过麻烦。可以用这个shell来完成大量的计算
[root@rhel06 home]# cat sda.sh
#!/bin/bash
total=$(grep 'sda$' /proc/partitions |awk '{print $3}');
used=0
for i in $(grep 'sda[[:digit:]]\+$' /proc/partitions |awk '{print $3}' |xargs)
do
used=$(( used + i ));
done
echo $((( total - used ) / 1024 )) "MB"
[root@rhel06 home]# ./sda.sh
5937 MB
接下来执行具体的分区步骤:
一、划分一个新分区并格式化
[root@rhel06 ~]# fdisk /dev/sda Command (m for help): p Disk /dev/sda: 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: 0x00029f55 Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1332 10485760 83 Linux
/dev/sda3 1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4 1593 2610 8176089 5 Extended
/dev/sda5 1593 1854 2103487+ 8e Linux LVM Command (m for help): n #新建分区,由于我已新建了四个分区(主分区和逻辑分区),所以这里只能新建扩展分区,并没有出现分区类型选项
First cylinder (1855-2610, default 1855): #开始点,直接回车即可
Using default value 1855
Last cylinder, +cylinders or +size{K,M,G} (1855-2610, default 2610): +2G #+2G,划分一个2G的新分区 Command (m for help): p Disk /dev/sda: 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: 0x00029f55 Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1332 10485760 83 Linux
/dev/sda3 1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4 1593 2610 8176089 5 Extended
/dev/sda5 1593 1854 2103487+ 8e Linux LVM
/dev/sda6 1855 2116 2104483+ 83 Linux Command (m for help): w #保存退出
[root@rhel06 ~]# partprobe #不要忘了刷新分区表,虚拟机还是需要重启才能刷新。
[root@rhel06 ~]# mkfs.ext4 /dev/sda6 #格式化
二、转换sda6分区为pv
[root@rhel06 ~]# pvcreate /dev/sda6 #转换sda6分区
dev_is_mpath: failed to get device for 8:6
Physical volume "/dev/sda6" successfully created
[root@rhel06 ~]# pvdisplay #查看系统所有pv,这里sda6已成功转换
--- Physical volume ---
PV Name /dev/sda5
VG Name VG001
PV Size 2.01 GiB / not usable 2.19 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 513
Free PE 1
Allocated PE 512
PV UUID dSHFLX-9Lui-0ZDb-6J2g-pp6A-azuX-vg5yGV "/dev/sda6" is a new physical volume of "2.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sda6
VG Name
PV Size 2.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID TEGKht-876S-8MMd-vWkX-rjeI-4UqG-JqRsT2
三、添加新pv到vg
[root@rhel06 ~]# vgdisplay |grep Name #查看当前vg名称
VG Name VG001
[root@rhel06 ~]# vgextend VG001 /dev/sda6 #添加“sda6”pv到此vg
Volume group "VG001" successfully extended
[root@rhel06 ~]# vgdisplay #查看vg详情,可以看到vg已经扩容到4g
--- Volume group ---
VG Name VG001
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 4.01 GiB
PE Size 4.00 MiB
Total PE 1026
Alloc PE / Size 512 / 2.00 GiB
Free PE / Size 514 / 2.01 GiB
VG UUID qG18Fp-agAP-gaTp-0crS-Ughm-UilM-2ZftQt
四、调整lv1容量
[root@rhel06 ~]# lvextend -L 4G /dev/VG001/lv1 #调整lv1分区容量为4G,原来是2G
Extending logical volume lv1 to 4.00 GiB
Logical volume lv1 successfully resized
[root@rhel06 ~]# resize2fs /dev/VG001/lv1 #执行重设大小,对当前lv1有效
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VG001/lv1 is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VG001/lv1 to 1048576 (4k) blocks.
The filesystem on /dev/VG001/lv1 is now 1048576 blocks long.
五、到此设置已完成,df命令查看已扩容到4G
[root@rhel06 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.9G 2.2G 7.3G 23% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sr0 3.6G 3.6G 0 100% /mnt
/dev/mapper/VG001-lv1 4.0G 68M 3.7G 2% /lv1
六、新增lv分区
[root@rhel06 ~]# lvcreate -L 4G -n lv0data lv1 #在lv1逻辑卷新增lv0data分区 -L指定大小 -n指定名称
问题1:执行partprobe刷新分区表时会出现以下报错:
[root@rhel06 ~]# 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格式化是提示“没有这个文件或目录”,那是分区表没有刷新或者刷新失败,刷新步骤可参考上一个问题。
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1332 10485760 83 Linux
/dev/sda3 1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4 1593 2610 8176089 5 Extended
/dev/sda5 1593 1854 2103487+ 83 Linux
[root@rhel06 ~]# mkfs.ext4 /dev/sda5
mke2fs 1.41.12 (17-May-2010)
无法对 /dev/sda5 进行 stat 调用 --- 没有那个文件或目录