1, 可能遇到的需求: 某一个程序需要大一点的 swap 分区, 需要我们手动增加 swap 分区
首先在文件系统中创建一个模拟的磁盘出来:
dd if=/dev/zero of=/tmp/newdisk bs=1M count=100
dd 命令: 是用来操作磁盘的, 可以读, 写; if 指定从哪里去读; of 指定将 0 写到哪里去; bs 指定每一个块的大小为 1M;newdisk 大小 = count 乘以 100(即 newdisk 大小为 100M)
- [root@linux-01 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=100
- 100+0 records in
- 100+0 records out
- 104857600 bytes (105 MB) copied, 4.65032 s, 22.5 MB/s
- [root@linux-01 ~]# du -sh /tmp/newdisk
100M /tmp/newdisk
2, 创建完虚拟磁盘后需要格式化:
- [root@linux-01 ~]# mkswap -f /tmp/newdisk
- Setting up swapspace version 1, size = 102396 KiB
- no label, UUID=a211ca68-dba6-4e06-8e64-999c31ba4555
3, 把新加的 100Mswap 加载到现在的 swap 上, 使用 swapon 命令:
- [root@linux-01 ~]# free -m
- total used free shared buff/cache available
- Mem: 976 131 584 6 259 660
- Swap: 2047 0 2047 // 未加载前 swap 分区大小 2047
- [root@linux-01 ~]# swapon /tmp/newdisk
- swapon: /tmp/newdisk: insecure permissions 0644, 0600 suggested. // 提示权限不安全
- [root@linux-01 ~]# free -m
- total used free shared buff/cache available
- Mem: 976 131 585 6 259 660
- Swap: 2147 0 2147 // 加载完 100M 之后可以查看到 swap 大小为 2147M
- [root@linux-01 ~]# chmod 0600 /tmp/newdisk // 更改 newdisk 权限为 0600
4, 卸载新增加的 swap: 使用 swapoff 命令:
- [root@linux-01 ~]# swapoff /tmp/newdisk
- [root@linux-01 ~]# free -m
- total used free shared buff/cache available
- Mem: 976 129 587 6 259 663
- Swap: 2047 0 2047 // 卸载完成之后 swap 大小恢复为 2047M
不想要可以删除掉 newdisk:
[root@linux-01 ~]# rm -f /tmp/newdisk
来源: http://www.bubuko.com/infodetail-2557985.html