天天看點

擴充Linux swap分區 兩種方法

先來查詢一下系統的swap

[root@localhost ~]# free -m

total used free shared buffers cached

mem: 375 369 6 0 7 83

-/+ buffers/cache: 278 97

swap: 1027 128 899

[root@localhost ~]#

現在系統中swap是1024m

擴充是swap分區有兩種方法,

(1) 利用磁盤的剩餘空間來擴充swap

首先分區并改變分區的類型

[root@localhost ~]# fdisk /dev/sda

the number of cylinders for this disk is set to 1958.

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): n

first cylinder (1316-1958, default 1316):

using default value 1316

last cylinder or +size or +sizem or +sizek (1316-1958, default 1958): +500m

command (m for help): t

partition number (1-8): 8

hex code (type l to list codes): 82

changed system type of partition 8 to 82 (linux swap / solaris)

command (m for help): wq

格式化swap

[root@localhost ~]# mkswap /dev/sda8

setting up swapspace version 1, size = 509927 kb

啟動swap

[root@localhost ~]# swapon /dev/sda8

現在我在查詢一下swap的大小

mem: 375 369 6 0 7 82

-/+ buffers/cache: 278 96

swap: 1513 128 1385

可以看到的swap分區已經增大了500m

最後,要想下次系統重新開機生效,必須寫在/etc/fstab檔案中

/dev/sda8 swap swap defaults 0 0

~

(2) 利用分區的剩餘空間來擴充swap

首先我一樣要查詢一下系統中swap分區的大小

mem: 375 368 7 0 7 81

然後利用dd工具來建立一個swap檔案

[root@localhost ~]# dd if=/dev/zero of=swapfile bs=1m count=100

100+0 records in

100+0 records out

104857600 bytes (105 mb) copied, 0.632534 seconds, 166 mb/s

[root@localhost ~]# mkswap swapfile

setting up swapspace version 1, size = 104853 kb

把這個檔案當做一個分區來對待

[root@localhost ~]# swapon swapfile

現在來查詢一下swap分區的大小

mem: 375 368 6 0 1 90

-/+ buffers/cache: 276 98

swap: 1613 128 1485

可以看到系統的swap分區已經擴大100m

/swapfile swap swap defaults 0 0

以上的兩種方法就是用來擴充swap分區的

還可以使用swapon -a 激活交換分區,(該指令可以讀取/etc/fstab檔案,并開啟它列出所有的交換條目

還可以利用swapon -s 來查詢交換分區的狀态

[root@localhost ~]# swapon -s

filename type size used priority

/dev/sda5 partition 1052216 131640 -1

/dev/sda8 partition 497972 0 -2

/root/swapfile file 102392 0 -3

在這裡,可以看到swap是有哪些檔案組成的,還有優先級,數字越大,優先級越高。可以在/etc/fstab檔案中default後面加上pri=x(x為優先級)

參考至:blog.sina.com.cn/s/blog_779eecd8010124ta.html

如有錯誤,歡迎指正

郵箱:[email protected]

作者:czmmiao  文章出處:http://czmmiao.iteye.com/blog/2117105

繼續閱讀