多语言展示
当前在线:1481今日阅读:27今日分享:41

添加交换分区

SWAP交换分区是一种类似于Windows系统虚拟内存的功能,通过把一部分硬盘空间虚拟成内存来使用,从而解决内存容量不足的情况。但由于SWAP毕竟是用硬盘资源虚拟的,速度上比真实物理内存要慢很多,所以一般只有当真实物理内存耗尽时才会调用SWAP交换分区,把内存中暂时不常用的数据临时存放到硬盘中,腾出内存空间让更活跃的程序服务来使用。详情请关注《linux就该这么学》。
工具/原料

一台装有RHEL 7.0操作系统的电脑

方法/步骤
1

SWAP交换分区的创建过程非常类似于上一个小节所讲到的分区设备挂载使用的方法,首先第一步就是再进行对/dev/sdb存储设备分区操作,取出一个大小为5GB的主存储分区然后保存退出即可:[root@linuxprobe ~]# fdisk /dev/sdbWelcome 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xb3d27ce1. Command (m for help): nPartition type: p primary (1 primary, 0 extended, 3 free) e extendedSelect (default p): pPartition number (2-4, default 2): First sector (4196352-41943039, default 4196352): 此处敲击回车Using default value 4196352 Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +5GPartition 2 of type Linux and of size 5 GiB is set Command (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = 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: 0xb0ced57f Device Boot Start End Blocks Id System /dev/sdb1 2 2097152 83 Linux/dev/sdb2 4196352 14682111 5242880 83 LinuxCommand (m for help): wThe 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.

2

把新建的存储分区使用SWAP交换分区专用的格式化mkswap命令进行格式化操作:[root@linuxprobe ~]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 5242876 KiB no label, UUID=2972f9cb-17f0-4113-84c6-c64b97c40c75

3

接下来使用swapon命令把准备好的SWAP交换分区设备正式的挂载到系统中,并可以使用free -m 命令来看到交换分区大小的变化(由2047M提升至了7167M):[root@linuxprobe ~]# free -m total used free shared buffers cached Mem: 1483 782 701 9 0 254 -/+ buffers/cache: 526 957Swap: 2047 0 2047[root@linuxprobe ~]# swapon /dev/sdb2 [root@linuxprobe ~]# free -m total used free shared buffers cached Mem: 1483 785 697 9 0 254 -/+ buffers/cache: 530 953Swap: 7167 0 7167

4

对了~为了能够让新的SWAP交换分区设备在重启后依然生效,需要按照下面的格式写入到配置文件中,记得保存哦~[root@linuxprobe ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Wed May 4 19:26:23 2017 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/rhel-root / xfs defaults 1 1 UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2 /dev/mapper/rhel-swap swap swap defaults 0 0 /dev/cdrom /media/cdrom iso9660 defaults 0 0 /dev/sdb1 /newFS xfs defaults 0 0 /dev/sdb2 swap swap defaults 0 0

推荐信息