1、关闭虚拟机,找到需要扩容的硬盘,修改为新的容量80GB,应用保存。
2、打开VM,进入系统,使用lsblk可以看到硬盘容量已经变为80GB,但硬盘根分区还没有扩容,使用df查看根文件系统也没有扩容。
[192.168.2.108] alpha@mascot.x ~ $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS loop0 7:0 0 111.9M 1 loop /snap/lxd/24322 loop1 7:1 0 53.3M 1 loop /snap/snapd/19457 loop2 7:2 0 63.4M 1 loop /snap/core20/1974 sda 8:0 0 80G 0 disk ├─sda1 8:1 0 1M 0 part └─sda2 8:2 0 25G 0 part / sr0 11:0 1 1024M 0 rom [192.168.2.108] alpha@mascot.x ~ $ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 392M 1.1M 391M 1% /run /dev/sda2 25G 7.7G 16G 33% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 392M 4.0K 392M 1% /run/user/1000
3、使用parted命令修改分区大小:
sudo parted /dev/sda
运行parted命令后,输入help查看命令列表。
[192.168.2.108] alpha@mascot.x ~ $ sudo parted /dev/sda
[sudo] password for alpha:
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help align-check TYPE N check partition N for TYPE(min|opt) alignmenthelp [COMMAND] print general help, or help on COMMANDmklabel,mktable LABEL-TYPE create a new disklabel (partition table)mkpart PART-TYPE [FS-TYPE] START END make a partitionname NUMBER NAME name partition NUMBER as NAMEprint [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particularpartitionquit exit programrescue START END rescue a lost partition near START and ENDresizepart NUMBER END resize partition NUMBERrm NUMBER delete partition NUMBERselect DEVICE choose the device to editdisk_set FLAG STATE change the FLAG on selected devicedisk_toggle [FLAG] toggle the state of FLAG on selected deviceset NUMBER FLAG STATE change the FLAG on partition NUMBERtoggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBERunit UNIT set the default unit to UNITversion display the version number and copyright information of GNU Parted
(parted)
输入print命令,打印当前分区表,会提示有可用空间,需要修复GPT,输入Fix修复:
(parted) print
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 115343360
blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags1 1049kB 2097kB 1049kB bios_grub2 2097kB 26.8GB 26.8GB ext4(parted)
修复GPT之后,可以输入print free查看可用的空间:
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags17.4kB 1049kB 1031kB Free Space1 1049kB 2097kB 1049kB bios_grub2 2097kB 26.8GB 26.8GB ext426.8GB 85.9GB 59.1GB Free Space(parted)
继续 输入resizepart 2 100%命令,将分区2扩容100%可用空间,输入Yes确认操作:
(parted) resizepart 2 100%
Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
Yes/No? Yes
(parted)
输入q退出parted命令,再用lsblk查看,根分区大小已经变成80G,但使用df查看根文件系统,还是25G,没有扩容。
(parted) q
Information: You may need to update /etc/fstab.[192.168.2.108] alpha@mascot.x ~ $ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.4M 1 loop /snap/core20/1974
loop1 7:1 0 111.9M 1 loop /snap/lxd/24322
loop2 7:2 0 53.3M 1 loop /snap/snapd/19457
loop3 7:3 0 38.8M 1 loop /snap/snapd/21759
loop4 7:4 0 63.9M 1 loop /snap/core20/2318
loop5 7:5 0 87M 1 loop /snap/lxd/28373
sda 8:0 0 80G 0 disk
├─sda1 8:1 0 1M 0 part
└─sda2 8:2 0 80G 0 part /
sr0 11:0 1 1024M 0 rom
[192.168.2.108] alpha@mascot.x ~ $ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 392M 1.1M 391M 1% /run
/dev/sda2 25G 7.9G 16G 35% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 392M 4.0K 392M 1% /run/user/1000
4、使用resize2fs命令更新文件系统大小,再用df查看根文件系统大小已经更新:
sudo resize2fs /dev/sda2
[192.168.2.108] alpha@mascot.x ~ $ sudo resize2fs /dev/sda2
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 10
The filesystem on /dev/sda2 is now 20971003 (4k) blocks long.[192.168.2.108] alpha@mascot.x ~ $ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 392M 1.1M 391M 1% /run
/dev/sda2 79G 8.1G 68G 11% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 392M 4.0K 392M 1% /run/user/1000
结束。
参考:
ubuntu 扩展分区并将剩余空间添加到现有逻辑卷中_ubuntu扩展分区大小 将一个磁盘加到另一个当中-CSDN博客