寫這篇博文的目的是爲了記住如何在已經安裝好的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的大小是否已經增加