您的位置:首页 > 其它

内存交换空间(swap)的构建

2016-03-29 17:47 399 查看
一、使用物理分区构建swap

1、先进行分区的行为。

[root@iZ255cppmtxZ ~]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended
Select (default p): p
Partition number (1,3,4, default 1): 4
First sector (20973568-62914559, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +512M Partition 4 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/xvdb: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x25a2caf5

Device Boot      Start         End      Blocks   Id  System
/dev/xvdb2            2048    20973567    10485760   83  Linux
/dev/xvdb4        20973568    41945087    10485760   83  Linux

Command (m for help): t           <==修改系统ID
Partition number (2,4, default 4): 4
Hex code (type L to list all codes): 82           <==改成swap的ID
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/xvdb: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x25a2caf5

Device Boot      Start         End      Blocks   Id  System
/dev/xvdb2            2048    20973567    10485760   83  Linux
/dev/xvdb4        20973568    41945087    10485760   82  Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


强制内核更新分区表:

[root@iZ255cppmtxZ ~]# partprobe


2、开始构建swap格式。

[root@iZ255cppmtxZ ~]# mkswap /dev/xvdb4
Setting up swapspace version 1, size = 5485756 KiB
no label, UUID=033a1d75-88fd-4347-8726-08d856cb9ad1


3、查实查看与加载。

[root@iZ255cppmtxZ ~]# swapon /dev/xvdb4
[root@iZ255cppmtxZ ~]# free
total       used       free     shared    buffers     cached
Mem:       1016656     152956     863700       6624      11036      75828
-/+ buffers/cache:      66092     950564
Swap:     10485756          0   10485756


[root@iZ255cppmtxZ ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/xvdb4                                 partition    10485756    0    -1


二、使用文件系统构建swap

1、使用dd这个命令来新增一个128MB的文件在/data下面:

[root@iZ255cppmtxZ ~]# dd if=/dev/zero of=/data/swap bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 7.82149 s, 68.6 MB/s


2、使用mkswap将/data/swap这个文件格式化为swap的文件格式:

[root@iZ255cppmtxZ ~]# mkswap /data/swap
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=ede63d44-cf78-4d08-92ca-15fb38a625f4


3、使用swapon来将/data/swap启动

[root@iZ255cppmtxZ ~]# swapon /data/swap
swapon: /data/swap: insecure permissions 0644, 0600 suggested.
[root@iZ255cppmtxZ ~]# free -m
total       used       free     shared    buffers     cached
Mem:           992        670        322          6         11        586
-/+ buffers/cache:         72        919
Swap:          511          0        511
[root@iZ255cppmtxZ ~]# swapon -s
Filename                Type        Size    Used    Priority
/data/swap                                 file    524284    0    -1


使用swapoff关掉swapfile

[root@iZ255cppmtxZ ~]# swapoff /dev/xvdb4
[root@iZ255cppmtxZ ~]# swapoff /data/swap


为了能够让swap自动挂载,要修改/etc/fstab文件

在文件末尾加上

/data/swap swap swap default 0 0
这样就算重启系统,swap分区就不用手动挂载了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: