引入
已经反复多次遇到了,ESXi/kvm虚拟机磁盘在运行过程中,容量不够,需要扩容。应该如何操作呢?
方法
物理扩容
首先在ESXi/kvm管理程序上将磁盘进行物理扩容,这一步不需要重启,立即可以生效
重新扫描块设备
以ESXi设备为例,扩容的设备为sda,执行以下命令重新扫描块设备
1 2 |
echo 1 > /sys/block/sdb/device/rescan |
经过这一步之后,执行fdisk -l /dev/sda,即可看到容量已经增大
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# fdisk -l /dev/sda GPT PMBR size mismatch (2097151999 != 6291455999) will be corrected by write. The backup GPT table is not on the end of the device. Disk /dev/sda: 2.93 TiB, 3221225472000 bytes, 6291456000 sectors Disk model: Virtual 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: 19047C30-81DE-41FF-80F2-6EBFF074BF82 Device Start End Sectors Size Type /dev/sda1 2048 1050623 1048576 512M EFI System /dev/sda2 1050624 2097151966 2096101343 999.5G Linux filesystem |
因为块设备容量扩大,所以最后扇区的GPT的备份分区表就不存在了
重建分区表
以GPT分区表为例,首先重建GPT的备份分区表
使用gdisk /dev/sda编辑分区表, 按x进入专业模式, 按e重建备份分区表, 按p确认效果, 按w写入到设备, 按y确认
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# gdisk /dev/sda GPT fdisk (gdisk) version 1.0.8 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): x Expert command (? for help): ? a set attributes b byte-swap a partition's name c change partition GUID d display the sector alignment value e relocate backup data structures to the end of the disk f randomize disk and partition unique GUIDs g change disk GUID h recompute CHS values in protective/hybrid MBR i show detailed information on a partition j move the main partition table l set the sector alignment value m return to main menu n create a new protective MBR o print protective MBR data p print the partition table q quit without saving changes r recovery and transformation options (experts only) s resize partition table t transpose two partition table entries u replicate partition table on new device v verify disk w write table to disk and exit z zap (destroy) GPT data structures and exit ? print this menu Expert command (? for help): e Relocating backup data structures to the end of the disk Expert command (? for help): p Disk /dev/sda: 6291456000 sectors, 2.9 TiB Model: Virtual disk Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 19047C30-81DE-41FF-80F2-6EBFF074BF82 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 6291455966 Partitions will be aligned on 2048-sector boundaries Total free space is 4194306014 sectors (2.0 TiB) Number Start (sector) End (sector) Size Code Name 1 2048 1050623 512.0 MiB EF00 EFI System Partition 2 1050624 2097151966 999.5 GiB 8300 Linux filesystem Expert command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sda. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully. |
然后扩大对应分区的容量, 这里是/dev/sda2, 从1T扩容到3T
还是使用gdisk /dev/sda, 按d, 2删除分区, 按n, 按2, 回车确认分区类型8300, 回车确认在原有起始扇区创建分区, 回车确认结束地址是磁盘结尾扇区。按w写入, 按y确认。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
tosee-tap-0005# gdisk /dev/sda GPT fdisk (gdisk) version 1.0.8 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p Disk /dev/sda: 6291456000 sectors, 2.9 TiB Model: Virtual disk Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 19047C30-81DE-41FF-80F2-6EBFF074BF82 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 6291455966 Partitions will be aligned on 2048-sector boundaries Total free space is 4194306014 sectors (2.0 TiB) Number Start (sector) End (sector) Size Code Name 1 2048 1050623 512.0 MiB EF00 EFI System Partition 2 1050624 2097151966 999.5 GiB 8300 Linux filesystem Command (? for help): d Partition number (1-2): 2 Command (? for help): n Partition number (2-128, default 2): First sector (34-6291455966, default = 1050624) or {+-}size{KMGTP}: Last sector (1050624-6291455966, default = 6291455966) or {+-}size{KMGTP}: Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): p Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sda: 6291456000 sectors, 2.9 TiB Model: Virtual disk Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 19047C30-81DE-41FF-80F2-6EBFF074BF82 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 6291455966 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 1050623 512.0 MiB EF00 EFI System Partition 2 1050624 6291455966 2.9 TiB 8300 Linux filesystem Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y OK; writing new GUID partition table (GPT) to /dev/sda. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully. |
刷新系统分区信息
使用partprobe来重新刷新系统分区信息,让系统读取到/dev/sda2的容量已经扩大
1 2 |
partprobe |
扩充分区文件系统容量
这里以ext4文件系统为例,执行resize2fs来进行在线扩容
1 2 3 4 5 6 |
# resize2fs /dev/sda2 resize2fs 1.46.5 (30-Dec-2021) Filesystem at /dev/sda2 is mounted on /; on-line resizing required old_desc_blocks = 125, new_desc_blocks = 375 The filesystem on /dev/sda2 is now 786300667 (4k) blocks long. |
执行成功后,会看到提示新的文件系统扇区数
再执行df -hl确认对应文件系统的挂载后的容量符合预期即可