Method 1: Create a partition
Use fdisk to create a partition
[[Email protected] _ 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 stronugly 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 ): 1 first cylinder (1-1305, default 1): Using default value 1 last cylinder, + cylinders or + size {K, M, g} (1-1305, default 1305 ): + 2 gcommand (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 # adjust the partition type selected partition 1hex code (type L to list codes): 82 changed system type of partition 1 to 82 (Linux swap/Solaris) 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 Linux swap/Solaris
Format/dev/xvdb1 partition as swap Partition
[[email protected]_168_102_centos ~]# mkswap /dev/xvdb1Setting up swapspace version 1, size = 2104476 KiBno label, UUID=4bd9be30-3473-4db3-8b3d-0cd930347ba1
Mount swap Partition
[[email protected]_168_102_centos ~]# swapon /dev/xvdb1[[email protected]_168_102_centos ~]# swapon –s Filename Type Size Used Priority/dev/xvdb1 partition 2104472 0 -1
Unmount swap Partition
[[email protected]_168_102_centos ~]# swapoff /dev/xvdb1[[email protected]_168_102_centos ~]# free total used free shared buffers cachedMem: 1018532 123916 894616 0 14624 41096-/+ buffers/cache: 68196 950336Swap: 0 0 0[[email protected]_168_102_centos ~]# swapon -sFilename Type Size Used Priority
Method 2: create a virtual memory file
Create 1 GB empty file
[[Email protected] _ 168_102_centos TMP] # dd If =/dev/Zero of =/tmp/swap1 BS = 1 m COUNT = 1024 # create an empty 1g file swap11024 + 0 records in1024 + 0 records outparts 3741824 bytes (1.1 GB) copied, 4.66541 S, 230 MB/S
[[email protected]_168_102_centos tmp]# ls -lh s* -rw-r--r-- 1 root root 1.0G Aug 22 16:01 swap1
Convert/tmp/swap1 to swap Partition
[[email protected]_168_102_centos tmp]# mkswap /tmp/swap1Setting up swapspace version 1, size = 1048572 KiBno label, UUID=dbbc7b8d-d77e-426d-aa9f-fe1e4204a09e[[email protected]_168_102_centos tmp]# free total used free shared buffers cachedMem: 1018532 930276 88256 0 11420 830364-/+ buffers/cache: 88492 930040Swap: 0 0 0
Mount swap Partition
[[email protected]_168_102_centos tmp]# swapon /tmp/swap1[[email protected]_168_102_centos tmp]# swapon -sFilename Type Size Used Priority/tmp/swap1 file 1048568 0 -1[[email protected]_168_102_centos tmp]# free total used free shared buffers cachedMem: 1018532 931516 87016 0 11596 830548-/+ buffers/cache: 89372 929160Swap: 1048568 0 1048568
Unmount swap Partition
[[email protected]_168_102_centos tmp]# swapoff /tmp/swap1[[email protected]_168_102_centos tmp]# swapon -sFilename Type Size Used Priority[[email protected]_168_102_centos tmp]# free total used free shared buffers cachedMem: 1018532 931044 87488 0 11696 830620-/+ buffers/cache: 88728 929804Swap: 0 0 0
Add swap partition swap