环境:
CentOS 6.9
平常在VMware上创建Linux系统虚拟机的时候,往往当时不会给太多的磁盘空间,在后期的使用过程中经常会遇到磁盘空间不足的情况,所以需要对Linux系统扩展磁盘空间。
扩展磁盘步骤:
1.磁盘扩展
注意:只能在虚拟机关机的情况下操作,并且这个虚拟机上没有磁盘快照文件,如有需要删除才可以扩展。


此时虽然在VMware虚拟机管理界面的扩容操作已经完成,但是这还只是扩容的第一步,后面还需要到操作系统内部进行操作。
2.查看分区信息
[root@test-centos6~]#fdisk-l
Disk/dev/sda:64.4GB,64424509440bytes
255heads,63sectors/track,7832cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x000a16bc
DeviceBootStartEndBlocksIdSystem
/dev/sda1*16451200083Linux
Partition1doesnotendoncylinderboundary.
/dev/sda2642611204584968eLinuxLVM
Disk/dev/mapper/vg_testcentos6-lv_root:18.8GB,18798870528bytes
255heads,63sectors/track,2285cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x00000000
Disk/dev/mapper/vg_testcentos6-lv_swap:2147MB,2147483648bytes
255heads,63sectors/track,261cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x00000000
[root@test-centos6~]#lsblk//列出块设备信息
NAMEMAJ:MINRMSIZEROTYPEMOUNTPOINT
sr011:01447M0rom
sda8:0060G0disk
├─sda18:10500M0part/boot
└─sda28:2019.5G0part
├─vg_testcentos6-lv_root(dm-0)253:0017.5G0lvm/
└─vg_testcentos6-lv_swap(dm-1)253:102G0lvm[SWAP]
可以看到目前磁盘/dev/sda已经有两个分区,分别为sda1、sda2。
3.新建分区sda3
[root@test-centos6~]#fdisk/dev/sda
WARNING:DOS-compatiblemodeisdeprecated.It'sstronglyrecommendedto
switchoffthemode(command'c')andchangedisplayunitsto
sectors(command'u').
Command(mforhelp):m//列出可以执行的命令
Commandaction
atoggleabootableflag
beditbsddisklabel
ctogglethedoscompatibilityflag
ddeleteapartition
llistknownpartitiontypes
mprintthismenu
naddanewpartition//选择分区,删除现有分区
ocreateanewemptyDOSpartitiontable
pprintthepartitiontable
qquitwithoutsavingchanges
screateanewemptySundisklabel
tchangeapartition'ssystemid
uchangedisplay/entryunits
vverifythepartitiontable
wwritetabletodiskandexit
xextrafunctionality(expertsonly)
Command(mforhelp):n//建立新的磁盘分区
Commandaction
eextended//逻辑分区
pprimarypartition(1-4)//主分区
p//建立主分区
Partitionnumber(1-4):3//输入分区号
Firstcylinder(2611-7832,default2611)://分区起始位置(可直接回车也可以根据情况输入)
Usingdefaultvalue2611
Lastcylinder,+cylindersor+size{K,M,G}(2611-7832,default7832)://回车
Usingdefaultvalue7832
Command(mforhelp):p//列出磁盘目前的分区情况
Disk/dev/sda:64.4GB,64424509440bytes
255heads,63sectors/track,7832cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x000a16bc
DeviceBootStartEndBlocksIdSystem
/dev/sda1*16451200083Linux
Partition1doesnotendoncylinderboundary.
/dev/sda2642611204584968eLinuxLVM
/dev/sda3261178324193902083Linux
Command(mforhelp):w//对分区操作进行保存
Thepartitiontablehasbeenaltered!
Callingioctl()tore-readpartitiontable.
WARNING:Re-readingthepartitiontablefailedwitherror16:设备或资源忙.
Thekernelstillusestheoldtable.Thenewtablewillbeusedat
thenextrebootorafteryourunpartprobe(8)orkpartx(8)
Syncingdisks.//正在同步磁盘
[root@test-centos6~]#fdisk-l//查看分区创建情况
Disk/dev/sda:64.4GB,64424509440bytes
255heads,63sectors/track,7832cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x000a16bc
DeviceBootStartEndBlocksIdSystem
/dev/sda1*16451200083Linux
Partition1doesnotendoncylinderboundary.
/dev/sda2642611204584968eLinuxLVM
/dev/sda3261178324193902083Linux
Disk/dev/mapper/vg_testcentos6-lv_root:18.8GB,18798870528bytes
255heads,63sectors/track,2285cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x00000000
Disk/dev/mapper/vg_testcentos6-lv_swap:2147MB,2147483648bytes
255heads,63sectors/track,261cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x00000000
4.重启系统并格式化分区sda3文件系统
[root@test-centos6~]#reboot
[root@test-centos6~]#lsblk
NAMEMAJ:MINRMSIZEROTYPEMOUNTPOINT
sr011:01447M0rom
sda8:0060G0disk
├─sda18:10500M0part/boot
├─sda28:2019.5G0part
│├─vg_testcentos6-lv_root(dm-0)253:0017.5G0lvm/
│└─vg_testcentos6-lv_swap(dm-1)253:102G0lvm[SWAP]
└─sda38:3040G0part
[root@test-centos6~]#mkfs.ext4/dev/sda3//将/dev/sda3分区文件系统格式化为ext4
mke2fs1.41.12(17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096(log=2)
分块大小=4096(log=2)
Stride=0blocks,Stripewidth=0blocks
2621440inodes,10484755blocks
524237blocks(5.00%)reservedforthesuperuser
第一个数据块=0
Maximumfilesystemblocks=4294967296
320blockgroups
32768blockspergroup,32768fragmentspergroup
8192inodespergroup
Superblockbackupsstoredonblocks:
32768,98304,163840,229376,294912,819200,884736,1605632,2654208,
4096000,7962624
正在写入inode表:完成
Creatingjournal(32768blocks):完成
Writingsuperblocksandfilesystemaccountinginformation:完成
Thisfilesystemwillbeautomaticallycheckedevery35mountsor
180days,whichevercomesfirst.Usetune2fs-cor-itooverride.
5.创建物理卷(pv)
此步骤可省略:当将分区直接添加到卷组(vg)时,LVM会自动将分区转换为物理卷(pv)。
[root@test-centos6~]#lvm
lvm>pvcreate/dev/sda3//创建物理卷pv
Physicalvolume"/dev/sda3"successfullycreated
lvm>pvdisplay//显示物理卷pv信息
---Physicalvolume---
PVName/dev/sda2
VGNamevg_testcentos6
PVSize19.51GiB/notusable3.00MiB
Allocatableyes(butfull)
PESize4.00MiB
TotalPE4994
FreePE0
AllocatedPE4994
PVUUIDjMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC
"/dev/sda3"isanewphysicalvolumeof"40.00GiB"
---NEWPhysicalvolume---
PVName/dev/sda3
VGName
PVSize40.00GiB
AllocatableNO
PESize0
TotalPE0
FreePE0
AllocatedPE0
PVUUIDPVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp
lvm>pvdisplay
---Physicalvolume---
PVName/dev/sda2
VGNamevg_testcentos6
PVSize19.51GiB/notusable3.00MiB
Allocatableyes(butfull)
PESize4.00MiB
TotalPE4994
FreePE0
AllocatedPE4994
PVUUIDjMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC
"/dev/sda3"isanewphysicalvolumeof"40.00GiB"
---NEWPhysicalvolume---
PVName/dev/sda3
VGName
PVSize40.00GiB
AllocatableNO
PESize0
TotalPE0
FreePE0
AllocatedPE0
PVUUIDPVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp
6.查看并创建卷组(vg)
lvm>vgdisplay//查看需要扩展的卷组vg名称
---Volumegroup---
VGNamevg_testcentos6//卷组vg名称
SystemID
Formatlvm2
MetadataAreas3
MetadataSequenceNo6
VGAccessread/write
VGStatusresizable
MAXLV0
CurLV2
OpenLV2
MaxPV0
CurPV3
ActPV3
VGSize59.49GiB
PESize4.00MiB
TotalPE17790
AllocPE/Size15232/19.50GiB
FreePE/Size2558/9.99GiB
VGUUIDyVVWYi-mV3I-f1WI-2QeH-HkVT-oBXP-2KkQ8v
lvm>vgextendvg_testcentos6/dev/sda3//扩展卷组(这里的“vg_testcentos6”名称为上面的卷组名称)
Volumegroup"vg_testcentos6"successfullyextended
7.扩展逻辑卷(lv)
[root@test-centos6~]#df-h//查看扩展路径,这里也可以用fdisk-l查看
FilesystemSizeUsedAvailUse%Mountedon
/dev/mapper/vg_testcentos6-lv_root-----》//**扩展路径**
18G18G0100%/
tmpfs1.9G01.9G0%/dev/shm
/dev/sda1485M58M403M13%/boot
[root@test-centos6~]#lvm//进入LVM管理器
lvm>lvextend-L+39.99G/dev/mapper/vg_testcentos6-lv_root//扩展逻辑卷lv大小(通过fdisk-l/df-h查看要扩展过去的磁盘)
Roundingsizetoboundarybetweenphysicalextents:39.99GiB.
Sizeoflogicalvolumevg_testcentos6/lv_rootchangedfrom17.51GiB(4482extents)to57.50GiB(14720extents)
Logicalvolumelv_rootsuccessfullyresized.
lvm>lvscan
ACTIVE'/dev/vg_testcentos6/lv_root'[57.50GiB]inherit
ACTIVE'/dev/vg_testcentos6/lv_swap'[2.00GiB]inherit
lvm>pvscan
PV/dev/sda2VGvg_testcentos6lvm2[19.51GiB/0free]
PV/dev/sda3VGvg_testcentos6lvm2[39.99GiB/0free]
Total:2[59.50GiB]/inuse:2[59.50GiB]/innoVG:0[0]
lvm>exit
Exiting.
8.查看分区情况
[root@test-centos6~]#fdisk-l
Disk/dev/sda:64.4GB,64424509440bytes
255heads,63sectors/track,7832cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x000a16bc
DeviceBootStartEndBlocksIdSystem
/dev/sda1*16451200083Linux
Partition1doesnotendoncylinderboundary.
/dev/sda2642611204584968eLinuxLVM
/dev/sda3261178324193902083Linux
Disk/dev/mapper/vg_testcentos6-lv_root:18.8GB,18798870528bytes
255heads,63sectors/track,2285cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier:0x00000000
Disk/dev/mapper/vg_testcentos6-lv_swap:2147MB,2147483648bytes
255heads,63sectors/track,261cylinders
Units=cylindersof16065*512=8225280bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Diskidentifier: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
resize2fs1.41.12(17-May-2010)
Filesystemat/dev/mapper/vg_testcentos6-lv_rootismountedon/;on-lineresizingrequired
olddesc_blocks=2,new_desc_blocks=4
Performinganon-lineresizeof/dev/mapper/vg_testcentos6-lv_rootto15073280(4k)blocks.
Thefilesystemon/dev/mapper/vg_testcentos6-lv_rootisnow15073280blockslong.
[root@test-centos6~]#df-h//查看激活状态
FilesystemSizeUsedAvailUse%Mountedon
/dev/mapper/vg_testcentos6-lv_root
57G4.4G50G9%/
tmpfs1.9G244K1.9G1%/dev/shm
/dev/sda1485M58M403M13%/boot
可以看到现在我们的磁盘已经从最开始的19G扩展到了57G。