虚拟机LVM扩容

时间:2023-12-26 17:58:43

1.先在Vmware上,把虚拟机硬盘做扩展,如果有快照存在,磁盘可能是不可编辑状态,先删除快照后再扩展。

2.现在打开虚拟机发现系统的磁盘空间已经扩了,但是硬盘分区可用空间没变,还是原来的30G

[root@localhost ~]# fdisk -l

Disk /dev/sda: 53.6 GB,  bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes Device Boot Start End Blocks Id System
/dev/sda1 * Linux
/dev/sda2 + 8e Linux LVM

3.使用 fdisk 新建分区,为lvm扩容做准备

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk is set to .
There is nothing wrong with that, but this is larger than ,
and could in certain setups cause problems with:
) software that runs at boot time (e.g., old versions of LILO)
) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/ FDISK) Command (m for help): p Disk /dev/sda: 53.6 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes Device Boot Start End Blocks Id System
/dev/sda1 * Linux
/dev/sda2 + 8e Linux LVM Command (m for help): n
Command action
e extended
p primary partition (-)
p
Partition number (-):
First cylinder (-, default ):
Using default value
Last cylinder or +size or +sizeM or +sizeK (-, default ):
Using default value Command (m for help): p Disk /dev/sda: 53.6 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes Device Boot Start End Blocks Id System
/dev/sda1 * Linux
/dev/sda2 + 8e Linux LVM
/dev/sda3 + Linux

调整磁盘分区/dev/sda3为LVM格式,防止重装系统是不能识别(感觉不调整格式也能用)

Command (m for help): t
Partition number (-):
Hex code (type L to list codes): 8e
Changed system type of partition to 8e (Linux LVM) Command (m for help): p Disk /dev/sda: 53.6 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes Device Boot Start End Blocks Id System
/dev/sda1 * Linux
/dev/sda2 + 8e Linux LVM
/dev/sda3 + 8e Linux LVM Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error : Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@localhost ~]# reboot Broadcast message from root (pts/) (Wed Mar :: ): The system is going down for reboot NOW!

如果Re-reading the partition table 失败,可能分区还未识别,重启下再看。

4.建立 pv

[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
[root@localhost ~]# pvdisplay
/dev/cdrom: open failed: No medium found
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup00
PV Size 29.90 GB / not usable 24.06 MB
Allocatable yes
PE Size (KByte)
Total PE
Free PE
Allocated PE
PV UUID o9H0fA-136u-HmOM-Ga3O-7R0i-VRAS-F1mNxZ --- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 20.00 GB
Allocatable NO
PE Size (KByte)
Total PE
Free PE
Allocated PE
PV UUID Qib59l-uCF5-5fo0-6oqH-5Url-yuSA-Y6N0E0

5.扩展 vg

[root@localhost ~]# vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 49.88 GB
PE Size 32.00 MB
Total PE
Alloc PE / Size / 29.81 GB
Free PE / Size / 20.06 GB
VG UUID 8D7xnq-UPBK-LW2U-OCmc-Dl23-cpP1-qLLSHn

6.扩展lv

输入扩展单元,这里就是上面的Free PE 的大小。

[root@localhost ~]# lvextend -l + /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 47.94 GB
Logical volume LogVol00 successfully resized
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID QDtSfz-1SlB-xl3P-mRZp-H3dx-erAx-mAwrt2
LV Write Access read/write
LV Status available
# open
LV Size 47.94 GB
Current LE
Segments
Allocation inherit
Read ahead sectors
Block device : --- Logical volume ---
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID 6nv9PJ-ho10-LPkA-0GBg-TBZt-z1Tf-F6S2Hu
LV Write Access read/write
LV Status available
# open
LV Size 1.94 GB
Current LE
Segments
Allocation inherit
Read ahead sectors
Block device :

7.调整文件系统

[root@localhost ~]# resize2fs  /dev/mapper/VolGroup00-LogVol00
resize2fs 1.35 (-Feb-)
/dev/mapper/VolGroup00-LogVol00 is mounted; can't resize a mounted filesystem!

不能调整,使用 ext2online,我这边文件系统估计有点不对,所有会有些inode调整,忽略。

[root@localhost ~]# ext2online -v /dev/mapper/VolGroup00-LogVol00
ext2online v1.1.18 - // for EXT2FS .5b
new filesystem size
group inode table has offset , not
group inode table has offset , not
...
using reserved group descriptor blocks
creating group with blocks (rsvd = , newgd = ) cache direct hits: , indirect hits: , misses:

注意,对于新的CentOS系统,可能是使用 xfs 格式的分区,需要用别的命令调整

[root@vt-c7 ~]# resize2fs /dev/mapper/centos-root
resize2fs 1.42. (-Dec-)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.

检查是xfs格式并使用 xfs_growfs 命令来扩展

[root@vt-c7 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Apr ::
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(), findfs(), mount() and/or blkid() for more info
#
/dev/mapper/centos-root / xfs defaults
UUID=7c27bad9-295a--afc8-b1f721ba5a5d /boot xfs defaults
/dev/mapper/centos-swap swap swap defaults
[root@vt-c7 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt= spinodes=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
data blocks changed from to
[root@vt-c7 ~]#

8.打完收工

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
48G 27G 19G % /
/dev/sda1 99M 13M 81M % /boot
none 772M 772M % /dev/shm