简介

1
手里有几台小鸡,安装某些服务内存不够,所以就扩大swap分区。本次实验均使用dd命令

本次实验环境

序号 环境 版本 内存(M)
1 Ubuntu16.04 minimal 4.4.0-109-generic 512
2 Ubuntu16.04 4.4.0-130-generic 1024

1.创建文件

我这里都是创建内存的两倍


1
2
3
4
5
6
7
8
root@VM-16-13-ubuntu:~# sudo dd if=/dev/zero of=/swapspace bs=1G count=2
dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)
root@VM-16-13-ubuntu:~# du /swapspace
0 /swapspace
root@VM-16-13-ubuntu:~# free -h
total used free shared buff/cache available
Mem: 864M 90M 653M 9.4M 119M 636M
Swap: 0B 0B 0B

bs不能超过可用内存。*dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)*

1
2
3
4
5
6
root@VM-16-13-ubuntu:~# sudo dd if=/dev/zero of=/swapspace bs=636M count=2
2+0 records in
2+0 records out
1333788672 bytes (1.3 GB, 1.2 GiB) copied, 12.8161 s, 104 MB/s
root@VM-16-13-ubuntu:~# du /swapspace
1302532 /swapspace

2.设置权限

1
sudo chmod 600 /swapspace

3.设置 启动Linux交换区

1
2
3
4
5
6
7
8
root@VM-16-13-ubuntu:~# sudo mkswap /swapspace
Setting up swapspace version 1, size = 2 GiB (2097147904 bytes)
no label, UUID=c8861fd4-f125-4d6f-abad-8854079fec5c
root@VM-16-13-ubuntu:~# sudo swapon /swapspace
root@VM-16-13-ubuntu:~# free -h
total used free shared buff/cache available
Mem: 864M 92M 665M 9.4M 106M 641M
Swap: 2.0G 0B 2.0G

写入/etc/fstab文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@VM-16-13-ubuntu:~# cat /etc/fstab
/dev/vda1 / ext4 noatime,acl,user_xattr 1 1
proc /proc proc defaults 0 0
sysfs /sys sysfs noauto 0 0
debugfs /sys/kernel/debug debugfs noauto 0 0
devpts /dev/pts devpts mode=0620,gid=5 0 0
root@VM-16-13-ubuntu:~# echo "/swapspace swap swap defaults 0 0" >> /etc/fstab
root@VM-16-13-ubuntu:~# cat /etc/fstab
/dev/vda1 / ext4 noatime,acl,user_xattr 1 1
proc /proc proc defaults 0 0
sysfs /sys sysfs noauto 0 0
debugfs /sys/kernel/debug debugfs noauto 0 0
devpts /dev/pts devpts mode=0620,gid=5 0 0
/swapspace swap swap defaults 0 0
1
2
3
echo "/swapspace swap swap defaults    0  0" >> /etc/fstab

vim /etc/fstab手动添加进去

总结


  • sudo dd if=/dev/zero of=/{filename}.swap bs=1M count={swap_size}

  • sudo chmod 600 /{filename}.swap

  • sudo mkswap /{filename}.swap

  • sudo swapon /{filename}.swap

  • echo “/{filename}.swap swap swap defaults 0 0” >> /etc/fstab


1
2
3
4
5
sudo dd if=/dev/zero of=/{filename}.swap bs=1M count={swap_size} 
sudo chmod 600 /{filename}.swap
sudo mkswap /{filename}.swap
sudo swapon /{filename}.swap
echo "/{filename}.swap swap swap defaults 0 0" >> /etc/fstab

参考链接: