09/27/13

linux swap 添加 删除 开机启动

1、查看swap

cat /proc/swaps

[root@tianjin swap_2]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       2031608 269832  -1

2、创建文件

[root@tianjin swap_2]# dd if=/dev/zero of=/opt/swap_2/swap bs=512 count=200000
200000+0 records in
200000+0 records out
102400000 bytes (102 MB) copied, 0.818854 seconds, 125 MB/s

[root@tianjin swap_2]# ll
总计 100108
-rw-r–r– 1 root root 102400000 09-27 09:55 swap

3、转化为swap格式

[root@tianjin swap_2]# mkswap /opt/swap_2/swap
Setting up swapspace version 1, size = 102395 kB

[root@tianjin swap_2]# free
total       used       free     shared    buffers     cached
Mem:       4046504     481600    3564904          0      11924     119204
-/+ buffers/cache:     350472    3696032
Swap:      2031608     269832    1761776

4、启用swap,加入swap池

[root@tianjin swap_2]# swapon /opt/swap_2/swap

[root@tianjin swap_2]# free
total       used       free     shared    buffers     cached
Mem:       4046504     482092    3564412          0      12016     119408
-/+ buffers/cache:     350668    3695836
Swap:      2131600     269832    1861768
[root@tianjin swap_2]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       2031608 269832  -1
/opt/swap_2/swap                        file            99992   0       -2   //swap添加成功

5、卸载swap

[root@tianjin swap_2]# swapoff /opt/swap_2/swap
[root@tianjin swap_2]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       2031608 269832  -1
[root@tianjin swap_2]# free
total       used       free     shared    buffers     cached
Mem:       4046504     483484    3563020          0      12308     120472
-/+ buffers/cache:     350704    3695800
Swap:      2031608     269832    1761776

6、添加开机启动

[root@tianjin swap_2]# cat /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0

[root@tianjin swap_2]# echo “/opt/swap_2/swap swap swap defaults 0 0″>>/etc/fstab
[root@tianjin swap_2]# cat /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/opt/swap_2/swap swap swap defaults 0 0
这样系统重启之后,swap可以自动加载。