文章目录
- 环境
- 背景
- 虚拟机磁盘扩容
- 配置
- 参考
环境
- VMWare Workstation 17 Pro
- RHEL 9.4
背景
一个RHEL虚拟机,其 /home
目录大小为30GB。
[ding@192 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 12M 1.5G 1% /run
/dev/mapper/rhel_192-root 61G 5.0G 56G 9% /
/dev/nvme0n1p2 960M 404M 557M 43% /boot
/dev/nvme0n1p1 599M 7.1M 592M 2% /boot/efi
/dev/mapper/rhel_192-home 30G 284M 30G 1% /home
tmpfs 766M 96K 766M 1% /run/user/1000
/dev/sr0 117M 117M 0 100% /run/media/ding/CDROM
/dev/sr1 11G 11G 0 100% /run/media/ding/RHEL-9-4-0-BaseOS-x86_64
现在发现磁盘空间不够用,需要扩展。
虚拟机磁盘扩容
把虚拟机关机,然后打开虚拟机设置:
选择“磁盘”,然后点击“扩展”按钮。
注:上图是开机状态,只有在关机状态,“扩展”按钮才能点击。另外,如果有虚拟机快照,也无法点击“扩展”按钮(会有提示)。
注:磁盘空间是100GB。上图显示磁盘空间为150GB,是扩容操作之后的容量。
在弹出的对话框中,输入150,并点击“扩展”按钮,完成扩展。
但是,在这里给虚拟机扩展了磁盘空间后,在虚拟机里并不会立刻生效,还需要配置一下才行。
配置
打开虚拟机并登录。
下面的操作都使用root账号,为了方便,直接切换到root账号。
sudo su -
使用 df -h
命令,可见磁盘并没有增大(输出结果略)。
使用 fdisk -l
命令查看磁盘状况:
[root@192 ~]# fdisk -l
GPT PMBR size mismatch (209715199 != 314572799) will be corrected by write.
Disk /dev/nvme0n1: 150 GiB, 161061273600 bytes, 314572800 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: FAC28C81-C6F0-477D-AC9F-00905AD8BA34Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 1230847 1228800 600M EFI System
/dev/nvme0n1p2 1230848 3327999 2097152 1G Linux filesystem
/dev/nvme0n1p3 3328000 209713151 206385152 98.4G Linux LVMDisk /dev/mapper/rhel_192-root: 60.85 GiB, 65338867712 bytes, 127614976 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel_192-swap: 7.85 GiB, 8430551040 bytes, 16465920 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel_192-home: 29.71 GiB, 31897681920 bytes, 62300160 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/nvme0n1
已经变成150GB了。
注意最上面的提示:
GPT PMBR size mismatch (209715199 != 314572799) will be corrected by write.
使用 fdisk
命令对其进行配置:
[root@192 ~]# fdisk /dev/nvme0n1Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.GPT PMBR size mismatch (209715199 != 314572799) will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.Command (m for help):
先输入 m
看看帮助:
Command (m for help): mHelp:GPTM enter protective/hybrid MBRGenericd delete a partitionF list free unpartitioned spacel list known partition typesn add a new partitionp print the partition tablet change a partition typev verify the partition tablei print information about a partitionMiscm print this menux extra functionality (experts only)ScriptI load disk layout from sfdisk script fileO dump disk layout to sfdisk script fileSave & Exitw write table to disk and exitq quit without saving changesCreate a new labelg create a new empty GPT partition tableG create a new empty SGI (IRIX) partition tableo create a new empty DOS partition tables create a new empty Sun partition tableCommand (m for help):
可见, n
命令是添加一个新分区。输入 n
:
Command (m for help): n
Partition number (4-128, default 4): <---- 这里直接回车
First sector (209713152-314572766, default 209713152): <---- 这里直接回车
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209713152-314572766, default 314572766): <---- 这里直接回车Created a new partition 4 of type 'Linux filesystem' and of size 50 GiB.Command (m for help):
可见,自动识别了50GB的磁盘空间,分区号是 4
,类型是 Linux filesystem
。
接下来用 t
命令来改变分区类型:
Command (m for help): t
Partition number (1-4, default 4): <---- 这里直接回车
Partition type or alias (type L to list all):
输入 L
,列出所有类型:
Partition type or alias (type L to list all): L1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F3 Intel Fast Flash D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
......30 Linux LVM E6D6D379-F507-44C2-A23C-238F2A3DF928
......
106 Plan 9 partition C91818F9-8025-47AF-89D2-F030D7000C2C
107 HiFive Unleashed FSBL 5B193300-FC78-40CD-8002-E86C45580B47
108 HiFive Unleashed BBL 2E54B353-1271-4842-806F-E436D6AF6985Aliases:linux - 0FC63DAF-8483-4772-8E79-3D69D8477DE4swap - 0657FD6D-A4AB-43C4-84E5-0933C84B4F4Fhome - 933AC7E1-2EB4-4F13-B844-0E14E2AEF915uefi - C12A7328-F81F-11D2-BA4B-00A0C93EC93Braid - A19D880F-05FC-4D3B-A006-743F0F84911Elvm - E6D6D379-F507-44C2-A23C-238F2A3DF928
Partition type or alias (type L to list all):
可见,一共有100多种类型,其中我们所需要的类型是30号 Linux LVM
。该工具很贴心的把常用类型做成了alias,其中30号对应的alias是 lvm
。
输入 lvm
,改变分区类型:
Partition type or alias (type L to list all): lvmChanged type of partition 'Linux filesystem' to 'Linux LVM'.Command (m for help):
最后,输入 w
保存设置:
Command (m for help): w
The partition table has been altered.
Syncing disks.
再次查看磁盘状况:
[root@192 ~]# fdisk -l
Disk /dev/nvme0n1: 150 GiB, 161061273600 bytes, 314572800 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: FAC28C81-C6F0-477D-AC9F-00905AD8BA34Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 1230847 1228800 600M EFI System
/dev/nvme0n1p2 1230848 3327999 2097152 1G Linux filesystem
/dev/nvme0n1p3 3328000 209713151 206385152 98.4G Linux LVM
/dev/nvme0n1p4 209713152 314572766 104859615 50G Linux LVMDisk /dev/mapper/rhel_192-root: 60.85 GiB, 65338867712 bytes, 127614976 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel_192-swap: 7.85 GiB, 8430551040 bytes, 16465920 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel_192-home: 29.71 GiB, 31897681920 bytes, 62300160 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/nvme0n1p4 209713152 314572766 104859615 50G Linux LVM
同时,提示不匹配的信息也没了。
接下来,使用 mkfs
命令来创建文件系统:
[root@192 ~]# mkfs -t ext3 /dev/nvme0n1p4
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 13107200 4k blocks and 3283200 inodes
Filesystem UUID: 11b9ebbf-2058-4b1f-8dc6-9f61167fed09
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
查看physical volume:
[root@192 ~]# pvdisplay--- Physical volume ---PV Name /dev/nvme0n1p3VG Name rhel_192PV Size 98.41 GiB / not usable 2.00 MiBAllocatable yes (but full)PE Size 4.00 MiBTotal PE 25193Free PE 0Allocated PE 25193PV UUID vj5hJb-TT31-2H8v-YqPl-sxre-i3CM-iAsaEa
可见,并没有 /dev/nvme0n1p4
。
创建physical volume:
[root@192 ~]# pvcreate /dev/nvme0n1p4
WARNING: ext3 signature detected on /dev/nvme0n1p4 at offset 1080. Wipe it? [y/n]: yWiping ext3 signature on /dev/nvme0n1p4.Physical volume "/dev/nvme0n1p4" successfully created.
再次查看:
[root@192 ~]# pvdisplay--- Physical volume ---PV Name /dev/nvme0n1p3VG Name rhel_192PV Size 98.41 GiB / not usable 2.00 MiBAllocatable yes (but full)PE Size 4.00 MiBTotal PE 25193Free PE 0Allocated PE 25193PV UUID vj5hJb-TT31-2H8v-YqPl-sxre-i3CM-iAsaEa"/dev/nvme0n1p4" is a new physical volume of "50.00 GiB"--- NEW Physical volume ---PV Name /dev/nvme0n1p4VG Name PV Size 50.00 GiBAllocatable NOPE Size 0 Total PE 0Free PE 0Allocated PE 0PV UUID ldt1oe-P2AT-mein-bIVo-hPN7-M25c-InV2No
这次有了。但是 VG Name
为空。
接下来使用 vgextend
命令来扩展该volume group:
[root@192 ~]# vgextend rhel_192 /dev/nvme0n1p4Volume group "rhel_192" successfully extended
注:volume group rhel_192
是怎么来的呢?从前面 pvdisplay
输出结果可见, /dev/nvme0n1p3
的volume group是 rhel_192
。另外,从 /dev/mapper/rhel_192-home
可见,其命名规则为 /dev/mapper/<volume group>-xxx
。
前面提到, /home
目录只有30GB,需要扩容:
[root@192 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 12M 1.5G 1% /run
/dev/mapper/rhel_192-root 61G 5.1G 56G 9% /
/dev/nvme0n1p2 960M 404M 557M 43% /boot
/dev/nvme0n1p1 599M 7.1M 592M 2% /boot/efi
/dev/mapper/rhel_192-home 30G 284M 30G 1% /home
tmpfs 766M 108K 766M 1% /run/user/1000
/dev/sr0 117M 117M 0 100% /run/media/ding/CDROM
/dev/sr1 11G 11G 0 100% /run/media/ding/RHEL-9-4-0-BaseOS-x86_64
其文件系统为 /dev/mapper/rhel_192-home
。
然后使用 lvextend
命令来扩展其logical volume:
[root@192 ~]# lvextend -L +50G /dev/mapper/rhel_192-homeInsufficient free space: 12800 extents needed, but only 12799 available
可见,虽说是50GB,但是貌似实际要差一点点。换成49.9试试看:
[root@192 ~]# lvextend -L +49.9G /dev/mapper/rhel_192-homeRounding size to boundary between physical extents: 49.90 GiB.Size of logical volume rhel_192/home changed from <29.71 GiB (7605 extents) to <79.61 GiB (20380 extents).Logical volume rhel_192/home successfully resized.
这次成功了。
最后,使用 xfs_growfs
命令:
[root@192 ~]# xfs_growfs /dev/mapper/rhel_192-home
meta-data=/dev/mapper/rhel_192-home isize=512 agcount=4, agsize=1946880 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=7787520, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 7787520 to 20869120
大功告成:
[root@192 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 12M 1.5G 1% /run
/dev/mapper/rhel_192-root 61G 5.1G 56G 9% /
/dev/nvme0n1p2 960M 404M 557M 43% /boot
/dev/nvme0n1p1 599M 7.1M 592M 2% /boot/efi
/dev/mapper/rhel_192-home 80G 641M 79G 1% /home
tmpfs 766M 108K 766M 1% /run/user/1000
/dev/sr0 117M 117M 0 100% /run/media/ding/CDROM
/dev/sr1 11G 11G 0 100% /run/media/ding/RHEL-9-4-0-BaseOS-x86_64
可见, /home
已经变成80GB了。
参考
https://blog.csdn.net/chuixue24/article/details/139856886