方法一:新建分区

使用fdisk工具新建一个分区

[root@VM_168_102_centos ~]# fdisk /dev/xvdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0xad066822.Changes will remain in memory only, until you decide to write them.After that, of course, the previous content won't be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. It's strongly recommended to         switch off the mode (command 'c') and change display units to         sectors (command 'u').Command (m for help): pDisk /dev/xvdb: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xad066822    Device Boot      Start         End      Blocks   Id  SystemCommand (m for help): n        Command action   e   extended   p   primary partition (1-4)p     Partition number (1-4): 1First cylinder (1-1305, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2GCommand (m for help): pDisk /dev/xvdb: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xad066822    Device Boot      Start         End      Blocks   Id  System/dev/xvdb1               1         262     2104483+  83  LinuxCommand (m for help): t Selected partition 1Hex code (type L to list codes): 82Changed system type of partition 1 to 82 ()Command (m for help): pDisk /dev/xvdb: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xad066822    Device Boot      Start         End      Blocks   Id  System/dev/xvdb1               1         262     2104483+  82

将/dev/xvdb1分区格式为交换分区

[root@VM_168_102_centos ~]# Setting up swapspace version 1, size = 2104476 KiBno label, UUID=4bd9be30-3473-4db3-8b3d-0cd930347ba1

挂载交换分区

[root@VM_168_102_centos ~]# swapon /dev/xvdb1[root@VM_168_102_centos ~]# swapon –s Filename                Type        Size    Used    Priority/dev/xvdb1                              partition    2104472    0    -1

卸载交换分区

[root@VM_168_102_centos ~]# swapoff /dev/xvdb1[root@VM_168_102_centos ~]# free             total       used       free     shared    buffers     cachedMem:       1018532     123916     894616          0      14624      41096-/+ buffers/cache:      68196     950336Swap:            0          0          0[root@VM_168_102_centos ~]# swapon -sFilename                Type        Size    Used    Priority

 

方法二:新建虚拟内存文件

创建1G的空文件

[root@VM_168_102_centos tmp]# dd if=/dev/zero of=/tmp/swap1 bs=1M count=1024 1024+0 records in1024+0 records out1073741824 bytes (1.1 GB) copied, 4.66541 s, 230 MB/s
[root@VM_168_102_centos tmp]# ls -lh s* -rw-r--r-- 1 root root 1.0G Aug 22 16:01 swap1

将/tmp/swap1格式为交换分区

[root@VM_168_102_centos tmp]# mkswap /tmp/swap1Setting up swapspace version 1, size = 1048572 KiBno label, UUID=dbbc7b8d-d77e-426d-aa9f-fe1e4204a09e[root@VM_168_102_centos tmp]# free             total       used       free     shared    buffers     cachedMem:       1018532     930276      88256          0      11420     830364-/+ buffers/cache:      88492     930040

挂载交换分区

[root@VM_168_102_centos tmp]# swapon [root@VM_168_102_centos tmp]# swapon -sFilename                Type        Size    Used    Priority[root@VM_168_102_centos tmp]# free             total       used       free     shared    buffers     cachedMem:       1018532     931516      87016          0      11596     830548-/+ buffers/cache:      89372     929160

卸载交换分区

[root@VM_168_102_centos tmp]# swapoff /tmp/swap1[root@VM_168_102_centos tmp]# swapon -sFilename                Type        Size    Used    Priority[root@VM_168_102_centos tmp]# free             total       used       free     shared    buffers     cachedMem:       1018532     931044      87488          0      11696     830620-/+ buffers/cache:      88728     929804