`

扩展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 ~]#

现在系统中swap1024M

扩展是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

[root@localhost ~]#

启动swap

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

[root@localhost ~]#

现在我在查询一下swap的大小

[root@localhost ~]# free -m

total used free shared buffers cached

Mem: 375 369 6 0 7 82

-/+ buffers/cache: 278 96

Swap: 1513 128 1385

[root@localhost ~]#

可以看到的swap分区已经增大了500M

最后,要想下次系统重启生效,必须写在/etc/fstab文件中

/dev/sda8 swap swap defaults 0 0

~

(2) 利用分区的剩余空间来扩展swap

首先我一样要查询一下系统中swap分区的大小

[root@localhost ~]# free -m

total used free shared buffers cached

Mem: 375 368 7 0 7 81

-/+ buffers/cache: 278 96

Swap: 1513 128 1385

[root@localhost ~]#

然后利用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 ~]#

格式化swap

[root@localhost ~]# mkswap swapfile

Setting up swapspace version 1, size = 104853 kB

[root@localhost ~]#

把这个文件当做一个分区来对待

启动swap

[root@localhost ~]# swapon swapfile

[root@localhost ~]#

现在来查询一下swap分区的大小

[root@localhost ~]# free -m

total used free shared buffers cached

Mem: 375 368 6 0 1 90

-/+ buffers/cache: 276 98

Swap: 1613 128 1485

[root@localhost ~]#

可以看到系统的swap分区已经扩大100M

最后,要想下次系统重启生效,必须写在/etc/fstab文件中

/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

[root@localhost ~]#

在这里,可以看到swap是有哪些文件组成的,还有优先级,数字越大,优先级越高。可以在/etc/fstab文件中default后面加上pri=XX为优先级)

 

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

如有错误,欢迎指正

邮箱:czmcj@163.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics