添加交换分区
SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。
交换分区的创建过程与前文讲到的挂载并使用存储设备的过程非常相似。在对/dev/sdb存储设备进行分区操作前,有必要先说一下交换分区的划分建议:在生产环境中,交换分区的大小一般为真实物理内存的1.5~2倍,为了让大家更明显地感受交换分区空间的变化,这里取出一个大小为5GB的主分区作为交换分区资源。在分区创建完毕后保存并退出即可:
第一步:划分5G大小的磁盘
[root@ken ~]# fdisk /dev/sdb 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): Using default response p Partition number (2-4, default 2): First sector (206848-41943039, default 206848): Using default value 206848 Last sector, +sectors or +size{K,M,G} (206848-41943039, default 41943039): +5G Partition 2 of type Linux and of size 5 GiB is set Command (m for help): P Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x52997d99 Device Boot Start End Blocks Id System /dev/sdb1 2048 206847 102400 83 Linux /dev/sdb2 206848 10692607 5242880 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 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) Syncing disks. [root@ken ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@ken ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2
第二步:格式化操作
使用SWAP分区专用的格式化命令mkswap,对新建的主分区进行格式化操作:
[root@ken ~]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 5242876 KiB no label, UUID=92dbf6b7-4635-46a4-a813-0241098766d5
第三步:
使用swapon命令把准备好的SWAP分区设备正式挂载到系统中。我们可以使用free -m命令查看交换分区的大小变化
[root@ken ~]# free -h total used free shared buff/cache available Mem: 974M 89M 755M 7.6M 129M 735M Swap: 2.0G 0B 2.0G [root@ken ~]# swapon /dev/sdb2 [root@ken ~]# free -h total used free shared buff/cache available Mem: 974M 93M 751M 7.6M 129M 731M Swap: 7.0G 0B 7.0G
第四步:写入到配置文件中
[root@ken ~]# echo "/dev/sdb2 swap swap defaults 0 0" >> /etc/fstab
第五步:停止swap
使用swapoff可以停止swap
[root@ken ~]# free -h total used free shared buff/cache available Mem: 974M 93M 751M 7.6M 129M 731M Swap: 7.0G 0B 7.0G [root@ken ~]# 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). [root@ken ~]# swapoff -a [root@ken ~]# free -h total used free shared buff/cache available Mem: 974M 88M 758M 7.6M 127M 737M Swap: 0B 0B 0B