1、首先查看Swap位置:swapon -s
[email protected]:~# swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2094076 0 -1
2、关闭Swap:/sbin/swapoff /dev/sda5
[email protected]:~# /sbin/swapoff /dev/sda5
3、将其删除开机启动:nano /etc/fstab
找到并删除类似于:
UUID=e882xe3f-48xx-4bxx-9cea-01f1af74exxx none swap sw 0 $ 0
4、保存,reboot 重启,然后删除Swap占用硬盘容量的分区
[email protected]:~# fdisk -l #查看分区
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xb4b4e733
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 37750783 37748736 18G 83 Linux
/dev/sda2 37752830 41940991 4188162 2G 5 Extended
/dev/sda5 37752832 41940991 4188160 2G 82 Linux swap / Solaris
[email protected]:~# fdisk /dev/sda #此处"/dev/sda" 根据你的显示进行修改
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):d #删除
Partition number (1,2,5, default 5):5 #序号为"5"的分区
Partition 5 has been deleted.
Command (m for help):d #删除
Partition number (1,2, default 2): 2 #序号为"2"的分区
Partition 2 has been deleted.
Command (m for help):w #保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
5、将删除的空间扩容至主空间
安装growpart软件
# centos:
yum install -y cloud-utils-growpart
# Debian:
apt install -y cloud-guest-utils
* 如果你硬盘是GPT格式的请安装以下软件,MBR格式请忽略。——怎么查看硬盘格式是GPT,还是MBR?这个见本文最下方。
# centos:
yum install gdisk -y
# Debian:
apt-get install gdisk -y
[email protected]:~# growpart /dev/sda 1
failed [pt_update:1] pt_update /dev/sda 1
partx: /dev/sda: error updating partition 1
FAILED: pt_resize failed
[email protected]:~# resize2fs /dev/sda1
resize2fs 1.43.4 (31-Jan-2017)
The filesystem is already 4718592 (4k) blocks long. Nothing to do!
[email protected]:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 992M 0 992M 0% /dev
tmpfs 201M 2.9M 198M 2% /run
/dev/sda1 18G 742M 16G 5% /
tmpfs 1003M 0 1003M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1003M 0 1003M 0% /sys/fs/cgroup
tmpfs 201M 0 201M 0% /run/user/0