第一步:划分2G大小的磁盘
╭─[email?protected] ~
╰─? fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n 新建
Partition type:
p primary (1 primary,1 extended,2 free)
l logical (numbered from 5)
Select (default p): p 查看
分区号 (3,4,默认 3): 默认 回车即可
起始 扇区 (20973568-41943039,默认为 20973568): 默认回车即可
将使用默认值 20973568
Last 扇区,+扇区 or +size{K,M,G} (20973568-41943039,默认为 41943039):+2G
分区 3 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):p 查看分区
磁盘 /dev/sdb:21.5 GB,21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x915dc123
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended
/dev/sdb3 20973568 25167871 2097152 83 Linux
/dev/sdb5 10489856 14684159 2097152 83 Linux
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
第二步:识别新的分区
╭─[email?protected] ~
╰─? ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5
╭─[email?protected] ~
╰─? partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
╭─[email?protected] ~
╰─? ls /dev/sd* 再次列出分区 1 ?
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb5
╭─[email?protected] ~
第三步:格式化为swap类型
─[email?protected] ~
╰─? mkswap /dev/sdb3
正在设置交换空间版本 1,大小 = 2097148 KiB
无标签,UUID=47ed5ae2-ee9b-4b4b-83b3-563c00fe51ae
第四步:启动swap
╭─[email?protected] ~
╰─? free -h
total used free shared buff/cache available
Mem: 1.8G 448M 927M 10M 447M 1.2G
Swap: 2.0G 0B 2.0G
╭─[email?protected] ~
╰─? swapon /dev/sdb3
╭─[email?protected] ~
╰─? free -h
total used free shared buff/cache available
Mem: 1.8G 450M 925M 10M 447M 1.2G
Swap: 4.0G 0B 4.0G
?
第四步:写入到配置文件中
─[email?protected] ~
╰─? echo "swapon /dev/sdb3" >> /etc/rc.local
╭─[email?protected] ~
╰─? vim /etc/rc.local
?
第一步:创建目录
[[email?protected] ~]# mkdir /swap
?
第二步:创建文件
[[email?protected] ~]# dd if=/dev/zero of=/swap/swap bs=2M count=2014
2014+0 records in
2014+0 records out
4223664128 bytes (4.2 GB) copied,29.3039 s,144 MB/s
?
第三步:格式化
[[email?protected] ~]# mkswap /swap/swap
Setting up swapspace version 1,size = 4124668 KiB
no label,UUID=211cb23a-49b5-46fc-b568-f167d4b6d06a
?
第四步:写入文件
[[email?protected] ~]# echo “/swap/swap swap swap defaults 0 0” >> /etc/fstab
[[email?protected] ~]# mount -a
[[email?protected] ~]# free -h
total used free shared buff/cache available
Mem: 974M 85M 69M 7.6M 819M 712M
Swap: 4.0G 0B 4.0G
[[email?protected] ~]# swapon -a
swapon: /swap/swap: insecure permissions 0644,0600 suggested.
[[email?protected] ~]# free -h
total used free shared buff/cache available
Mem: 974M 88M 66M 7.6M 819M 709M
Swap: 7.9G 0B 7.9G
?
第五步:修改权限
[[email?protected] ~]# chmod 0600 /swap/swap
?
第五步:停止swap
使用swapoff可以停止swap
[[email?protected] ~]# free -h
total used free shared buff/cache available
Mem: 974M 93M 751M 7.6M 129M 731M
Swap: 7.0G 0B 7.0G
[[email?protected] ~]# swapoff
Usage:
swapoff [options] [<spec>]
Options:
-a,--all disable all swaps from /proc/swaps
-v,--verbose verbose mode
-h,--help display this help and exit
-V,--version output version information and exit
The <spec> parameter:
-L <label> LABEL of device to be used
-U <uuid> UUID of device to be used
LABEL=<label> LABEL of device to be used
UUID=<uuid> UUID of device to be used
<device> name of device to be used
<file> name of file to be used
For more details see swapoff(8).
[[email?protected]~]# swapoff -a
[[email?protected] ~]# free -h total used free shared buff/cache availableMem: 974M 88M 758M 7.6M 127M 737MSwap: 0B 0B 0B