站长原创,版权所有ITEEDU,2011-5-8
[root@www ~]# fdisk /dev/hdc
Command (m for help): n
First cylinder (2303-5005, default 2303): <==这里按[enter]
Using default value 2303
Last cylinder or +size or +sizeM or +sizeK (2303-5005, default 5005): +256M
Command (m for help): p
Device Boot Start End Blocks Id System
……
/dev/hdc6 2053 2302 2008093+ 83 Linux
/dev/hdc7 2303 2334 257008+ 83 Linux <==新增的项目
Command (m for help): t <==修改系统 ID
Partition number (1-7): 7 <==从上结果看到的,七号partition
Hex code (type L to list codes): 82 <==改成 swap 的 ID
Changed system type of partition 7 to 82 (Linux swap / Solaris)
Command (m for help): p
Device Boot Start End Blocks Id System
……
/dev/hdc6 2053 2302 2008093+ 83 Linux
/dev/hdc7 2303 2334 257008+ 82 Linux swap / Solaris
Command (m for help): w
# 此时就将 partition table 更新了!
[root@www ~]# partprobe
# 不要忘记让核心更新 partition table !
[root@www ~]# mkswap /dev/hdc7
Setting up swapspace version 1, size = 263172 kB <==非常快速!
[root@www ~]# free
total used free shared buffers cached
Mem: 742664 684592 58072 0 43820 497144
-/+ buffers/cache: 143628 599036
Swap: 1020088 96 1019992
# 我有 742664K 的实体记忆体,使用 684592K 剩余 58072K ,使用掉的记忆体有
# 43820K / 497144K 用在缓冲/快取的用途中。
# 至于 swap 已经存在了 1020088K !
[root@www ~]# swapon /dev/hdc7
[root@www ~]# free
total used free shared buffers cached
Mem: 742664 684712 57952 0 43872 497180
-/+ buffers/cache: 143660 599004
Swap: 1277088 96 1276992 <==有增加!
[root@www ~]# swapon -s
Filename Type Size Used Priority
/dev/hdc5 partition 1020088 96 -1
/dev/hdc7 partition 257000 0 -2
如果是在实体分割槽无法支援的环境下,此时前一小节提到的 loop 装置建置方法就派的上用场啦! 与实体分割槽不一样的只是利用 dd 去建置一个大档案而已。多说无益,我们就再透过档案建置的方法建立一个 128 MB 的记忆体置换空间吧!
使用 dd 这个指令来新增一个 128MB 的档案在 /tmp 底下:
[root@www ~]# dd if=/dev/zero of=/tmp/swap bs=1M count=128
128+0 records in
128+0 records out
134217728 bytes (134 MB) copied, 1.7066 seconds, 78.6 MB/s
[root@www ~]# ls -l /tmp/swap
-rw-r--r-- 1 root root 128M Oct 28 15:33 /tmp/swap
这样一个 128MB 的档案就建置妥当。
使用 mkswap 将 /tmp/swap 这个档案格式化为 swap 的档案格式:
[root@www ~]# mkswap /tmp/swap
Setting up swapspace version 1, size = 134213 kB
# 这个指令下达时请'特别小心',因为下错字元控制,将可能使您的档案系统挂掉!
使用 swapon 来将 /tmp/swap 启动。
[root@www ~]# free
total used free shared buffers cached
Mem: 742664 450860 291804 0 45584 261284
-/+ buffers/cache: 143992 598672
Swap: 1277088 96 1276992
[root@www ~]# swapon /tmp/swap
[root@www ~]# free
total used free shared buffers cached
Mem: 742664 450860 291804 0 45604 261284
-/+ buffers/cache: 143972 598692
Swap: 1408152 96 1408056
[root@www ~]# swapon -s
Filename Type Size Used Priority
/dev/hdc5 partition 1020088 96 -1
/dev/hdc7 partition 257000 0 -2
/tmp/swap file 131064 0 -3
使用 swapoff 关掉 swap file
[root@www ~]# swapoff /tmp/swap
[root@www ~]# swapoff /dev/hdc7
[root@www ~]# free
total used free shared buffers cached
Mem: 742664 450860 291804 0 45660 261284
-/+ buffers/cache: 143916 598748
Swap: 1020088 96 1019992 <==回复成最原始的样子了!
笔者在安装ubuntu系统之时,本来已经分好了一个专门的交换区,但是后来一不小心,把交换区给重新格式化了,在xp下作为一个2.5G的磁盘使用了。所以目前只能在ubuntu下边建立一个swap交换文件,以当做swap交换区来使用了。
那么,我采用上述的方法二,但是每次已重新启动就通过free一查看,swap交换区就变为0了。哎……
后来,经过摸索,终于搞定了,现在跟大家再次分享出来:
我们需要将swap挂载在/etc/fstab中。
利用命令:sudo gedit /etc/fatab,打开fatab文件,在其中添加:
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/home/rock/swapfile none swap defaults 0 0
source /etc/fstab即可即时生效,也可以保证下次重新启动的时候依然生效。呵呵,就这么简单。