扩容swap空间

添加swap空间

适用场景:

  • 安装系统时未分区swap,完成安装后又需使用swap的
  • swap空间不足,需要扩容

解决方法:

一、添加磁盘作为swap使用

添加磁盘

[root@test ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000

[root@test ~]#

格式化为swap格式

[root@test ~]# mkswap -f /dev/sdb  

Setting up swapspace version 1, size = 20971516 KiB

no label, UUID=300b63d6-3f2b-49c0-8447-02d4e7671cf6

[root@test ~]# blkid /dev/sdb

/dev/sdb: UUID="300b63d6-3f2b-49c0-8447-02d4e7671cf6" TYPE="swap"

目前的swap空间

[root@test ~]# free -h

             total       used       free     shared    buffers     cached

Mem:          1.9G       160M       1.8G       236K       5.6M        34M

-/+ buffers/cache:       120M       1.8G

Swap:         3.0G         0B       3.0G

启用新添加的swap空间

[root@test ~]# swapon /dev/sdb

验证

[root@test ~]# free -h

             total       used       free     shared    buffers     cached

Mem:          1.9G       175M       1.8G       236K       5.6M        34M

-/+ buffers/cache:       135M       1.8G

Swap:          22G         0B        22G

开机自动挂载

[root@test ~]# echo "UUID="300b63d6-3f2b-49c0-8447-02d4e7671cf6"   swap  swap  0  0 " >> /etc/fstab

tips:

停止使用添加的swap空间

[root@test ~]# swapoff /dev/sdb

启用所有的swap空间

[root@test ~]# swapon -a

二、dd一个大文件扩容swap

创建大文件

[root@test ~]# dd if=/dev/zero of=/tmp/test bs=4k count=10240

10240+0 records in

10240+0 records out

41943040 bytes (42 MB) copied, 0.0480515 s, 873 MB/s

[root@test ~]# ll -h /tmp/test

-rw-r--r--. 1 root root 40M Jan  5 18:37 /tmp/test

格式化文件为swap格式启用验证(同上方法)

[root@test ~]# mkswap -f /tmp/test

Setting up swapspace version 1, size = 40956 KiB

no label, UUID=2e9d96e0-985c-4125-99c5-4a329a5929eb

[root@test ~]# swapon /tmp/test

[root@test ~]# blkid /tmp/test

/tmp/test: UUID="2e9d96e0-985c-4125-99c5-4a329a5929eb" TYPE="swap"

[root@test ~]# swapon /tmp/test

开机自动挂载同上

两种方法都可以,根据自身情况自行选择。

猜你喜欢

转载自www.cnblogs.com/chai-/p/10246347.html
今日推荐