使用DD 创建SWAP

时间:2022-03-01 20:25:37

1创建所需swap空间的文件

            if=获取数据  of=输出位置   块大小(每次复制大小)  块个数(复制次数)  
[root@web01 ~]# dd if=/dev/zero of=/tmp/100m   bs=1M       count=
+ records in
+ records out
bytes ( MB) copied, 2.01386 s, 52.1 MB/s
[root@web01 ~]# ll -h /tmp/100m
-rw-r--r--. 1 root root 100M Apr 21 13:44 /tmp/100m

2 格式化文件成为SWAP

[root@web01 ~]# mkswap  /tmp/100m
mkswap: /tmp/100m: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version , size = KiB
no label, UUID=f90d8d0c--42fc-83f8-4b98c651554e
[root@web01 ~]# file /tmp/100m 查看文件类型是否更改为swap
/tmp/100m: Linux/i386 swap file (new style) (4K pages) size pages

3 让文件生效成SWAP

[root@web01 ~]# free -h
total used free shared buffers cached
Mem: .7G .2G .6G 232K .0G 164M
-/+ buffers/cache: 972M .8G
Swap: 1.5G 0B 1.5G [root@web01 ~]# swapon /tmp/100m
[root@web01 ~]# free -h
total used free shared buffers cached
Mem: 3.7G 2.2G 1.6G 232K 1.0G 164M
-/+ buffers/cache: 972M 2.8G
Swap: 1.6G 0B 1.6G

4 检查swap 是由及部分组成

[root@web01 ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition -
/tmp/500m file -
/tmp/100m file -3

5 永久挂载/开机挂载

方法1:写入 /etc/rc.local
[root@web01 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff. touch /var/lock/subsys/local
#bin/mount /dev/sdc1 /date
swapon /tmp/100m
方法2
[root@web01 ~]# cat /etc/fstab
/tmp/100m swap swap defaults 0 0