本文以 VMware® Workstation 15 Pro -15.5.6 build-16341506
+ CentOS Linux release 7.5.1804 (Core)
为测试机,本文为扩展原有硬盘空间,如需增加新硬盘请参考 链接: VMware下的Linux扩展磁盘空间(增加全新的硬盘空间)
。
如下图所示,此时可以看到虚拟机硬盘目前为25G,点击编辑虚拟机设置
,进入虚拟机设置界面。
分别按下图步骤点击硬盘
→扩展
→扩展内存→扩展
→确定
。
硬盘扩展成功后,须要在客户机(即Linux虚拟机)上进行分区硬盘和扩展文件系统。
执行fdisk -l
查看当前的系统的磁盘空间的情况:
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c591b
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
执行fdisk /dev/sda
键入n
,增加一个分区,得到:
键入 p
,主分区,并键入3
(编号):
默认起始扇区和结束扇区即可(键入两次Enter) 键入t
,
修改分区类型为8e
键入w
,写分区表
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (41943040-104857599, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-104857599, default 104857599):
Using default value 104857599
Partition 3 of type Linux and of size 30 GiB is set
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to '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 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
执行fdisk -l
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c591b
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
发现此时/dev/sda
空间已经扩大,执行reboot
重启
执行pvcreate /dev/sda3
初始化刚才的分区
[root@localhost ~]# pvcreate /dev/sda3
WARNING: dos signature detected on /dev/sda3 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
执行vgdisplay
查看卷组信息
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos #卷组名称
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID hCBaNk-KEkz-SKvR-gRP2-btG9-KqhR-BKtfTX
执行vgextend centos /dev/sda3
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
执行lvextend -L +30G /dev/mapper/centos-root
1
[root@localhost ~]# lvextend -L +30G /dev/mapper/centos-root
Insufficient free space: 7680 extents needed, but only 7679 available
#此处提示 available 不足,没有去研究具体问题,直接执行下步操作,
[root@localhost ~]# lvextend -L +29.9G /dev/mapper/centos-root
Rounding size to boundary between physical extents: 29.90 GiB.
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <46.90 GiB (12006 extents).
Logical volume centos/root successfully resized.
resize2fs /dev/mapper/centos-root
[root@localhost ~]# resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.
若执行这上一步有尝试打开/dev/mapper/centos-root
找不到有效的文件系统超级块的报错的时候,需要使用的是xfs_growfs /dev/mapper/centos-root
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 12294144
ok,看空间去吧。
/dev/mapper/centos-root 这个可以在linux中的df -h中看到,根据要扩展的具体逻辑卷的名称决定
/dev/mapper/centos-root 这个可以在linux中的df -h中看到,根据要扩展的具体逻辑卷的名称决定 ↩︎