VMware虚拟机中Centos 6.x系统磁盘空间扩容实战

环境:
CentOS 6.9

平常在VMware上创建Linux系统虚拟机的时候,往往当时不会给太多的磁盘空间,在后期的使用过程中经常会遇到磁盘空间不足的情况,所以需要对Linux系统扩展磁盘空间。

扩展磁盘步骤:
1.磁盘扩展

注意:只能在虚拟机关机的情况下操作,并且这个虚拟机上没有磁盘快照文件,如有需要删除才可以扩展。

此时虽然在VMware虚拟机管理界面的扩容操作已经完成,但是这还只是扩容的第一步,后面还需要到操作系统内部进行操作。

2.查看分区信息

[root@test-centos6 ~]# fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000a16bc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM

Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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: 0x00000000

Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

[root@test-centos6 ~]# lsblk //列出块设备信息
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 447M 0 rom
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─vg_testcentos6-lv_root (dm-0) 253:0 0 17.5G 0 lvm /
└─vg_testcentos6-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]

可以看到目前磁盘/dev/sda已经有两个分区,分别为sda1、sda2。

3.新建分区sda3

[root@test-centos6 ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): m //列出可以执行的命令
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition //选择分区,删除现有分区
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n //建立新的磁盘分区
Command action
e extended //逻辑分区
p primary partition (1-4) //主分区
p //建立主分区
Partition number (1-4): 3 //输入分区号
First cylinder (2611-7832, default 2611): //分区起始位置(可直接回车也可以根据情况输入)
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-7832, default 7832): //回车
Using default value 7832

Command (m for help): p //列出磁盘目前的分区情况

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000a16bc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 83 Linux

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: 设备或资源忙.
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. //正在同步磁盘

[root@test-centos6 ~]# fdisk -l //查看分区创建情况

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000a16bc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 83 Linux

Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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: 0x00000000

Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

4.重启系统并格式化分区sda3文件系统

[root@test-centos6 ~]# reboot

[root@test-centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 447M 0 rom
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 19.5G 0 part
│ ├─vg_testcentos6-lv_root (dm-0) 253:0 0 17.5G 0 lvm /
│ └─vg_testcentos6-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
└─sda3 8:3 0 40G 0 part

[root@test-centos6 ~]# mkfs.ext4 /dev/sda3 //将/dev/sda3分区文件系统格式化为ext4
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484755 blocks
524237 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

5.创建物理卷(pv)

此步骤可省略:当将分区直接添加到卷组(vg)时,LVM会自动将分区转换为物理卷(pv)。

[root@test-centos6 ~]# lvm
lvm> pvcreate /dev/sda3 //创建物理卷pv
Physical volume “/dev/sda3” successfully created
lvm> pvdisplay //显示物理卷pv信息
— Physical volume —
PV Name /dev/sda2
VG Name vg_testcentos6
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC

“/dev/sda3” is a new physical volume of “40.00 GiB”
— NEW Physical volume —
PV Name /dev/sda3
VG Name
PV Size 40.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp

lvm> pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name vg_testcentos6
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC

“/dev/sda3” is a new physical volume of “40.00 GiB”
— NEW Physical volume —
PV Name /dev/sda3
VG Name
PV Size 40.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp

6.查看并创建卷组(vg)

lvm> vgdisplay //查看需要扩展的卷组vg名称
— Volume group —
VG Name vg_testcentos6 //卷组vg名称
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 59.49 GiB
PE Size 4.00 MiB
Total PE 17790
Alloc PE / Size 15232 / 19.50 GiB
Free PE / Size 2558 / 9.99 GiB
VG UUID yVVWYi-mV3I-f1WI-2QeH-HkVT-oBXP-2KkQ8v

lvm> vgextend vg_testcentos6 /dev/sda3 //扩展卷组(这里的“vg_testcentos6”名称为上面的卷组名称)
Volume group “vg_testcentos6” successfully extended

7.扩展逻辑卷(lv)

[root@test-centos6 ~]# df -h //查看扩展路径,这里也可以用fdisk -l查看
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_testcentos6-lv_root —–》 //**扩展路径**
18G 18G 0 100% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 58M 403M 13% /boot

[root@test-centos6 ~]# lvm //进入LVM管理器

lvm> lvextend -L +39.99G /dev/mapper/vg_testcentos6-lv_root //扩展逻辑卷lv大小(通过fdisk -l/df -h查看要扩展过去的磁盘)
Rounding size to boundary between physical extents: 39.99 GiB.
Size of logical volume vg_testcentos6/lv_root changed from 17.51 GiB (4482 extents) to 57.50 GiB (14720 extents)
Logical volume lv_root successfully resized.

lvm> lvscan
ACTIVE ‘/dev/vg_testcentos6/lv_root’ [57.50 GiB] inherit
ACTIVE ‘/dev/vg_testcentos6/lv_swap’ [2.00 GiB] inherit

lvm> pvscan
PV /dev/sda2 VG vg_testcentos6 lvm2 [19.51 GiB / 0 free]
PV /dev/sda3 VG vg_testcentos6 lvm2 [39.99 GiB / 0 free]
Total: 2 [59.50 GiB] / in use: 2 [59.50 GiB] / in no VG: 0 [0 ]

lvm> exit
Exiting.

8.查看分区情况

[root@test-centos6 ~]# fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x000a16bc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 83 Linux

Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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: 0x00000000

Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

注意:上面虽然显示物理卷/dev/sdb3有40G空闲,但是这里不能全部使用,可能只有39.99G可以使用。

7.激活即重新读取磁盘信息

查看/dev/mapper/vg_testcentos6-lv_root的Type为ext4,所以使用resize2fs;如果文件类型是 xfs ,则使用xfs_growfs命令格式化。

[root@test-centos6 ~]# blkid //查看文件系统
/dev/sda1: UUID=”32a489a3-65c2-43a4-9aff-12c03eabc4dd” TYPE=”ext4″
/dev/sda2: UUID=”jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC” TYPE=”LVM2_member”
/dev/sda3: UUID=”PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp” TYPE=”LVM2_member”
/dev/mapper/vg_testcentos6-lv_root: UUID=”e8adf717-e78c-4845-a750-2a22a60689ed” TYPE=”ext4″ //可以看到文件系统格式化为ext4
/dev/mapper/vg_testcentos6-lv_swap: UUID=”ac33f130-95cc-4046-8bdd-db2d7c450f6d” TYPE=”swap”

[root@test-centos6 ~]# resize2fs /dev/mapper/vg_testcentos6-lv_root //ext4文件系统使用resize2fs
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_testcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/mapper/vg_testcentos6-lv_root to 15073280 (4k) blocks.
The filesystem on /dev/mapper/vg_testcentos6-lv_root is now 15073280 blocks long.

[root@test-centos6 ~]# df -h //查看激活状态
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_testcentos6-lv_root
57G 4.4G 50G 9% /
tmpfs 1.9G 244K 1.9G 1% /dev/shm
/dev/sda1 485M 58M 403M 13% /boot

可以看到现在我们的磁盘已经从最开始的19G扩展到了57G。

Leave a Reply

Your email address will not be published. Required fields are marked *