方法一:建立分區
使用fdisk工具建立一個分區
[root@VM_168_102_centos ~]# fdisk /dev/xvdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building 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): p
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xad066822
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G
Command (m for help): p
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xad066822
Device Boot Start End Blocks Id System
/dev/xvdb1 1 262 2104483+ 83 Linux
Command (m for help): t #調整分區類型
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xad066822
Device Boot Start End Blocks Id System
/dev/xvdb1 1 262 2104483+ 82 Linux swap / Solaris
将/dev/xvdb1分區格式為交換分區
[root@VM_168_102_centos ~]# mkswap /dev/xvdb1
Setting up swapspace version 1, size = 2104476 KiB
no 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 cached
Mem: 1018532 123916 894616 0 14624 41096
-/+ buffers/cache: 68196 950336
Swap: 0 0 0
[root@VM_168_102_centos ~]# swapon -s
Filename Type Size Used Priority
方法二:建立虛拟記憶體檔案
建立1G的空檔案
[root@VM_168_102_centos tmp]# dd if=/dev/zero of=/tmp/swap1 bs=1M count=1024 #建立1G的空檔案swap1
1024+0 records in
1024+0 records out
1073741824 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
[root@VM_168_102_centos tmp]# mkswap /tmp/swap1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=dbbc7b8d-d77e-426d-aa9f-fe1e4204a09e
[root@VM_168_102_centos tmp]# free
total used free shared buffers cached
Mem: 1018532 930276 88256 0 11420 830364
-/+ buffers/cache: 88492 930040
Swap: 0 0 0
[root@VM_168_102_centos tmp]# swapon /tmp/swap1
[root@VM_168_102_centos tmp]# swapon -s
Filename Type Size Used Priority
/tmp/swap1 file 1048568 0 -1
[root@VM_168_102_centos tmp]# free
total used free shared buffers cached
Mem: 1018532 931516 87016 0 11596 830548
-/+ buffers/cache: 89372 929160
Swap: 1048568 0 1048568
[root@VM_168_102_centos tmp]# swapoff /tmp/swap1
[root@VM_168_102_centos tmp]# swapon -s
Filename Type Size Used Priority
[root@VM_168_102_centos tmp]# free
total used free shared buffers cached
Mem: 1018532 931044 87488 0 11696 830620
-/+ buffers/cache: 88728 929804
Swap: 0 0 0