写这篇博文的目的是为了记住如何在已经安装好的Linux中扩展Swap的大小,让你能够不再重新安装操作系统时完成你的业务的部署。
这里我采用两种方法来扩容Swap的大小
第一种:利用linux中未分配的硬盘空间,新建立一个swap分区来扩容swap的大小
[root@RAC02 ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1
* 1 64 514048+ 83 Linux
/dev/sda2
65 2231 17406427+ 83 Linux
/dev/sda3
2232 2492 2096482+ 82 Linux swap / Solaris
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (2493-2610, default 2493):
Using default value 2493
Last cylinder or +size or +sizeM or +sizeK (2493-2610, default 2610):
Using default value 2610
Command (m for help): p
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1
* 1 64 514048+ 83 Linux
/dev/sda2
65 2231 17406427+ 83 Linux
/dev/sda3
2232 2492 2096482+ 82 Linux swap / Solaris
/dev/sda4
2493 2610 947835 83 Linux
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 82
Changed system type of partition 4 to 82 (Linux swap / Solaris)
Command (m for help):w
[root@RAC02 ~]# partprobe
[root@RAC02 ~]# mkswap /dev/sda4
Setting up swapspace version 1, size = 970575 kB
[root@RAC02 ~]# swapon /dev/sda4
[root@RAC02 ~]#
[root@RAC02 ~]# free
total used free shared buffers cached
Mem:
2075500 498424 1577076 0 29712 347880
-/+ buffers/cache:
120832 1954668
Swap:
304429
为了在系统重启后依然生效,还需要在/etc/fstab中添加下面一句话: /dev/sda4 swap swap defaults 0 0
第二种方法是创建一个swap文件,来扩容swap的大小
1) 首先确定你要添加的swap的大小,比如是2G,那么就是2048000k
2)以root用户登录到终端,dd一个文件,大小就是你期望的swap文件大小,比如:
dd if=/dev/zero of=/home/swap bs=1024000k count=2
这样就创建了一个大小是2G的文件
3)将/home/swap文件转化成swap文件
mkswap /home/swap
4)激活当前的swap file,这样在swapon -s就可以看到当前swap已经生效了
swapon /swapfile
5)为了在系统重启后依然生效,还需要在/etc/fstab中添加一行:
/home/swap swap swap defaults 0 0
6)利用free来查看是否swap的大小是否已经增加