一、添加swap空间:
1、扩展一个现有的swap空间:
<1>、首先禁止正在使用的swap空间:
[root@server4 ~]# swapoff /dev/mapper/tools-swap
<2>、重新
resize /dev/mapper/tools-swap:
[root@server4 ~]# lvm lvresize /dev/tools/swap -L +300M
Extending logical volume swap to 500.00 MB
Logical volume swap successfully resized
Extending logical volume swap to 500.00 MB
Logical volume swap successfully resized
<3>、使用mkswap重新格式化:
[root@server4 ~]# mkswap /dev/tools/swap
Setting up swapspace version 1, size = 524283 kB
Setting up swapspace version 1, size = 524283 kB
<4>、启用
swap:
[root@server4 ~]# swapon -av
swapon on /dev/mapper/tools-swap
swapon on /dev/mapper/tools-swap
<5>查看swap大小:
[root@server4 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 1044216 0 -2
/dev/mapper/tools-swap partition 511992 0 -4
Filename Type Size Used Priority
/dev/sda3 partition 1044216 0 -2
/dev/mapper/tools-swap partition 511992 0 -4
[root@server4 ~]# free -m
total used free shared buffers cached
Mem: 194 172 21 0 19 78
-/+ buffers/cache: 75 119
Swap: 1519 0 1519
total used free shared buffers cached
Mem: 194 172 21 0 19 78
-/+ buffers/cache: 75 119
Swap: 1519 0 1519
2、添加一个新的swap空间(基于LVM):
[root@server4 ~]# lvm lvcreate tools -n swap1 -L 100M
Logical volume "swap1" created
[root@server4 ~]# mkswap /dev/tools/swap1
Setting up swapspace version 1, size = 104853 k
Logical volume "swap1" created
[root@server4 ~]# mkswap /dev/tools/swap1
Setting up swapspace version 1, size = 104853 k
更改/etc/fstab文件,加入/dev/tools/swap1。
启用新创建的swap空间:
[root@server4 ~]# swapon -av
swapon on /dev/mapper/tools-swap1
swapon on /dev/mapper/tools-swap1
[root@server4 ~]# free -m
total used free shared buffers cached
Mem: 194 173 20 0 19 78
-/+ buffers/cache: 75 118
Swap: 1619 0 1619
total used free shared buffers cached
Mem: 194 173 20 0 19 78
-/+ buffers/cache: 75 118
Swap: 1619 0 1619
3、创建swap文件:
[root@server4 /]# dd if=/dev/zero of=/swapfile bs=1k count=102400
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 1.23157 seconds, 85.1 MB/s
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 1.23157 seconds, 85.1 MB/s
[root@server4 /]# mkswap /swapfile
Setting up swapspace version 1, size = 104853 kB
Setting up swapspace version 1, size = 104853 kB
[root@server4 /]# swapon /swapfile
在/etc/fstab文件里加入下行:
/swapfile swap swap defaults 0 0
二、删除swap空间:
1、减小一个swap空间:
[root@server4 /]# swapoff /dev/mapper/tools-swap
[root@server4 /]# lvm lvreduce /dev/tools/swap -L -400M
WARNING: Reducing active logical volume to 100.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce swap? [y/n]: y
Reducing logical volume swap to 100.00 MB
Logical volume swap successfully resized
[root@server4 /]# mkswap /dev/tools/swap
Setting up swapspace version 1, size = 104853 kB
[root@server4 /]# swapon -va
swapon on /dev/mapper/tools-swap
[root@server4 /]# lvm lvreduce /dev/tools/swap -L -400M
WARNING: Reducing active logical volume to 100.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce swap? [y/n]: y
Reducing logical volume swap to 100.00 MB
Logical volume swap successfully resized
[root@server4 /]# mkswap /dev/tools/swap
Setting up swapspace version 1, size = 104853 kB
[root@server4 /]# swapon -va
swapon on /dev/mapper/tools-swap
[root@server4 /]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
swap tools -wi-ao 100.00M
swap1 tools -wi-ao 100.00M
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
swap tools -wi-ao 100.00M
swap1 tools -wi-ao 100.00M
2、删除一个基于LVM的swap:
[root@server4 /]# swapoff /dev/tools/swap1
[root@server4 /]# lvm lvremove /dev/tools/swap1
Do you really want to remove active logical volume "swap1"? [y/n]: y
Logical volume "swap1" successfully removed
[root@server4 /]# lvm lvremove /dev/tools/swap1
Do you really want to remove active logical volume "swap1"? [y/n]: y
Logical volume "swap1" successfully removed
然后将其从/etc/fstab文件里删除即可。
3、删除一个swap文件:
[root@server4 /]# swapoff -v /swapfile
[root@server4 /]# rm -rf /swapfile