创建 swap 分区的方式也是非常的简单的!通过下面几个步骤就搞定啰:
- 分区:先使用 gdisk 在你的磁盘中分区出一个分区给系统作为 swap 。由于 Linux 的 gdisk 默认会将分区的 ID 设置为 Linux 的文件系统,所以你可能还得要设置一下 system ID 就是了。
- 格式化:利用创建 swap 格式的“mkswap 设备文件名”就能够格式化该分区成为 swap 格式啰
- 使用:最后将该 swap 设备启动,方法为:“swapon 设备文件名”。
- 观察:最终通过 free 与 swapon -s 这个指令来观察一下内存的用量吧!
不啰唆,立刻来实作看看!既然我们还有多余的磁盘容量可以分区,那么让我们继续分区出 512MB 的磁盘分区吧! 然后将这个磁盘分区做成 swap 吧!
先进行分区的行为啰!
[root@study ~]# gdisk /dev/vda Command (? for help): n Partition number (6-128, default 6): First sector (34-83886046, default = 69220352) or {+-}size{KMGTP}: Last sector (69220352-83886046, default = 83886046) or {+-}size{KMGTP}: +512M Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): 8200 Changed type of partition to 'Linux swap'
Command (? for help): p Number Start (sector) End (sector) Size Code Name 6 69220352 70268927 512.0 MiB 8200 Linux swap # 重点就是产生这东西!
Command (? for help): w
Do you want to proceed? (Y/N): y
[root@study ~]# partprobe [root@study ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:0 0 40G 0 disk .....(中间省略)..... `-vda6 252:6 0 512M 0 part # 确定这里是存在的才行!
鸟哥有简化输出喔!结果可以看到我们多了一个 /dev/vda6 可以使用于 swap 喔!
- 开始创建 swap 格式
[root@study ~]# mkswap /dev/vda6
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=6b17e4ab-9bf9-43d6-88a0-73ab47855f9d
[root@study ~]# blkid /dev/vda6
/dev/vda6: UUID="6b17e4ab-9bf9-43d6-88a0-73ab47855f9d" TYPE="swap"
# 确定格式化成功!且使用 blkid 确实可以抓到这个设备了喔!
- 开始观察与载入看看吧!
[root@study ~]# free
total used free shared buff/cache available
Mem: 1275140 227244 330124 7804 717772 875536 # 实体内存
Swap: 1048572 101340 947232 # swap 相关
# 我有 1275140K 的实体内存,使用 227244K 剩余 330124K ,使用掉的内存有
# 717772K 用在缓冲/高速缓存的用途中。至于 swap 已经有 1048572K 啰!这样会看了吧?!
[root@study ~]# swapon /dev/vda6
[root@study ~]# free
total used free shared buff/cache available
Mem: 1275140 227940 329256 7804 717944 874752
Swap: 1572856 101260 1471596
&
lt;==有看到增加了没?
[root@study ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 1048572 101260 -1
/dev/vda6 partition 524284 0 -2
# 上面列出目前使用的 swap 设备有哪些的意思!
[root@study ~]# nano /etc/fstab
UUID="6b17e4ab-9bf9-43d6-88a0-73ab47855f9d" swap swap defaults 0 0
# 当然要写入配置文件,只不过不是文件系统,所以没有挂载点!第二个字段写入 swap 即可。