目录

说明

用parted来调整分区

1.进入parted命令行模式

2.输入print输出分区结构

3.输入resizepart 调整分区大小

4.查看调整后结构

5.退出parted命令行模式

6.更新系统分区

输入lsblk查看

输入df -hT显示磁盘分区:

输入resize2fs命令:

重新df -hT命令进行确认:


说明

虚拟机加了磁盘空间后系统里还要调整一下磁盘空间才可以使用,如果是本地主机那么用GParted Live启动盘引导后进行调整很方便。

如图,先调整 /dev/sda2 的容量,再调整/dev/sda5 的容量点应用就可以了。但是有时候无法用启动盘该怎么做呢?

用parted来调整分区

1.进入parted命令行模式

输入命令:

parted
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

2.输入print输出分区结构

(parted) print                                                            
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 275GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  538MB  537MB  primary   fat32        boot
 2      539MB   275GB  136GB  extended
 5      539MB   275GB  136GB  logical   ext4

(parted)   

 这里可以看到Number  2是Extended扩展分区,后面的Number  5 和 2是一样的,根据前面的GParted图形界面就可以理解分区结构,我们要先调整2的容量后才可以调整5的容量。

3.输入resizepart 调整分区大小

(parted) resizepart                                                       
Partition number? 2                                                       
Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
Yes/No? yes                                                               
End?  [136GB]? -0                                                         
(parted) resizepart                                                       
Partition number? 5                                                       
Warning: Partition /dev/sda5 is being used. Are you sure you want to continue?
Yes/No? yes                                                               
End?  [136GB]? -0                                                         
(parted) quit                                                             
Information: You may need to update /etc/fstab.

 

这里End?  [136GB]?后输入了-0 ,代表分配所有剩余空间,也可以输入 100%

注意:这里必须是先2再5,不然会报Error: Can't have overlapping partitions.错误。

4.查看调整后结构

输入print

(parted) print                                                            
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 275GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  538MB  537MB  primary   fat32        boot
 2      539MB   275GB  274GB  extended
 5      539MB   275GB  274GB  logical   ext4

(parted) 

5.退出parted命令行模式

输入quit

(parted) print                                                            
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 275GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  538MB  537MB  primary   fat32        boot
 2      539MB   275GB  274GB  extended
 5      539MB   275GB  274GB  logical   ext4

(parted) quit                                                             
Information: You may need to update /etc/fstab.

6.更新系统分区

输入lsblk查看

lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
fd0      2:0    1     4K  0 disk 
loop0    7:0    0  97.9M  1 loop /snap/core/10583
loop1    7:1    0  98.4M  1 loop /snap/core/10823
loop2    7:2    0  55.4M  1 loop /snap/core18/1944
loop3    7:3    0  55.5M  1 loop /snap/core18/1988
loop4    7:4    0 162.9M  1 loop /snap/gnome-3-28-1804/145
loop5    7:5    0 217.9M  1 loop /snap/gnome-3-34-1804/60
loop6    7:6    0   219M  1 loop /snap/gnome-3-34-1804/66
loop7    7:7    0  64.8M  1 loop /snap/gtk-common-themes/1514
loop8    7:8    0  64.4M  1 loop /snap/gtk-common-themes/1513
loop9    7:9    0 663.1M  1 loop /snap/intellij-idea-community/270
loop10   7:10   0 424.7M  1 loop /snap/pycharm-community/224
loop11   7:11   0   662M  1 loop /snap/intellij-idea-community/273
loop12   7:12   0    51M  1 loop /snap/snap-store/498
loop13   7:13   0    51M  1 loop /snap/snap-store/518
loop14   7:14   0  31.1M  1 loop /snap/snapd/10707
loop15   7:15   0 424.8M  1 loop /snap/pycharm-community/226
loop16   7:16   0  31.1M  1 loop /snap/snapd/11036
sda      8:0    0   256G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0 255.5G  0 part /

可以看到sda/sda5已经是扩容成功了

输入df -hT显示磁盘分区:

df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  919M     0  919M   0% /dev
tmpfs          tmpfs     190M  2.6M  187M   2% /run
/dev/sda5      ext4      125G  110G  8.2G  94% /
tmpfs          tmpfs     948M     0  948M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     948M     0  948M   0% /sys/fs/cgroup
/dev/loop1     squashfs   99M   99M     0 100% /snap/core/10823
/dev/loop0     squashfs   98M   98M     0 100% /snap/core/10583
/dev/loop2     squashfs   56M   56M     0 100% /snap/core18/1944
/dev/loop3     squashfs   56M   56M     0 100% /snap/core18/1988
/dev/loop5     squashfs  218M  218M     0 100% /snap/gnome-3-34-1804/60
/dev/loop4     squashfs  163M  163M     0 100% /snap/gnome-3-28-1804/145
/dev/loop6     squashfs  219M  219M     0 100% /snap/gnome-3-34-1804/66
/dev/loop7     squashfs   65M   65M     0 100% /snap/gtk-common-themes/1514
/dev/loop8     squashfs   65M   65M     0 100% /snap/gtk-common-themes/1513
/dev/loop9     squashfs  664M  664M     0 100% /snap/intellij-idea-community/270
tmpfs          tmpfs     7.9G     0  7.9G   0% /www/tmp
/dev/loop10    squashfs  425M  425M     0 100% /snap/pycharm-community/224
/dev/loop11    squashfs  663M  663M     0 100% /snap/intellij-idea-community/273
/dev/loop12    squashfs   52M   52M     0 100% /snap/snap-store/498
/dev/loop13    squashfs   52M   52M     0 100% /snap/snap-store/518
/dev/loop14    squashfs   32M   32M     0 100% /snap/snapd/10707
/dev/loop15    squashfs  425M  425M     0 100% /snap/pycharm-community/226
/dev/loop16    squashfs   32M   32M     0 100% /snap/snapd/11036
/dev/sda1      vfat      511M  4.0K  511M   1% /boot/efi
tmpfs          tmpfs     190M     0  190M   0% /run/user/0

这里发现容量还是没有变

输入resize2fs命令:

sudo resize2fs /dev/sda5
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/sda5 is mounted on /; on-line resizing required
old_desc_blocks = 16, new_desc_blocks = 32
The filesystem on /dev/sda5 is now 66977280 (4k) blocks long.

进行进行刷新。

重新df -hT命令进行确认:

Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  919M     0  919M   0% /dev
tmpfs          tmpfs     190M  2.6M  187M   2% /run
/dev/sda5      ext4      251G  110G  131G  46% /
tmpfs          tmpfs     948M     0  948M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     948M     0  948M   0% /sys/fs/cgroup
/dev/loop1     squashfs   99M   99M     0 100% /snap/core/10823
/dev/loop0     squashfs   98M   98M     0 100% /snap/core/10583
/dev/loop2     squashfs   56M   56M     0 100% /snap/core18/1944
/dev/loop3     squashfs   56M   56M     0 100% /snap/core18/1988
/dev/loop5     squashfs  218M  218M     0 100% /snap/gnome-3-34-1804/60
/dev/loop4     squashfs  163M  163M     0 100% /snap/gnome-3-28-1804/145
/dev/loop6     squashfs  219M  219M     0 100% /snap/gnome-3-34-1804/66
/dev/loop7     squashfs   65M   65M     0 100% /snap/gtk-common-themes/1514
/dev/loop8     squashfs   65M   65M     0 100% /snap/gtk-common-themes/1513
/dev/loop9     squashfs  664M  664M     0 100% /snap/intellij-idea-community/270
tmpfs          tmpfs     7.9G     0  7.9G   0% /www/tmp
/dev/loop10    squashfs  425M  425M     0 100% /snap/pycharm-community/224
/dev/loop11    squashfs  663M  663M     0 100% /snap/intellij-idea-community/273
/dev/loop12    squashfs   52M   52M     0 100% /snap/snap-store/498
/dev/loop13    squashfs   52M   52M     0 100% /snap/snap-store/518
/dev/loop14    squashfs   32M   32M     0 100% /snap/snapd/10707
/dev/loop15    squashfs  425M  425M     0 100% /snap/pycharm-community/226
/dev/loop16    squashfs   32M   32M     0 100% /snap/snapd/11036
/dev/sda1      vfat      511M  4.0K  511M   1% /boot/efi
tmpfs          tmpfs     190M     0  190M   0% /run/user/0

可以看到空间已经变大了。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐