swap的作用
swap分区是必须有的,首先,它是日志文件系统得以发挥作用的依赖,在系统意外关闭的情况下,靠它来保存系统中的数据。其次,在运行一些比较耗内存的程序的时候,也要用到它。在这两种情况之外,swap分区处于一种闲置状态,
处理交换文件和分区
交换分区是系统RAM的补充
基本设置包括:
创建交换分区或者文件
使用mkswap写入特殊签名
在/etc/fstab文件中添加适当的条目
使用swapon -a 激活交换空间
挂载交换分区
启用: swapon
swapon [OPTION]... [DEVICE]
-a:激活/etc/fstab文件中所有的交换分区;
-p PRIORITY:指定优先级
/etc/fstab:pri=value
禁用: swapoff [OPTION]... [DEVICE]
SWAP的优先级
用户可以给某个swap指定一个0到32767的优先级
如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一。
先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)。
优化性能:分布存放,高性能磁盘存放
swap分区的创建
步骤
1、分区
2、格式化
3、挂载
查看当前交换分区总空间
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1868660 172672 1376176 9176 319812 1517840
Swap: 3905532 0 3905532
[root@localhost ~]# free -h #人性化显示swap容量
total used free shared buff/cache available
Mem: 1.8G 169M 1.3G 9.0M 308M 1.4G
Swap: 3.7G 0B 3.7G
添加硬盘,并对硬盘进行分区
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan #扫描新添加的硬盘
[root@localhost ~]# fdisk -l /dev/sdb #查看硬盘情况
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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
[root@localhost ~]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xbdc9991b.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-419430399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +2G
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0xbdc9991b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
Command (m for help): t #修改文件系统类型
Selected partition 1
Hex code (type L to list all codes): L #使用L可以查看系统所有的文件类型
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext‘d (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82 #指定修改的文件类型为82( Linux swap / So)
Changed type of partition ‘Linux‘ to ‘Linux swap / Solaris‘
Command (m for help): p
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0xbdc9991b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 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@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0xbdc9991b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 82 Linux swap / Solaris
格式化新建swap分区
[root@localhost ~]# mkswap -L swap_sdb1 /dev/sdb1 #为新分区进行格式化,针对交换分区需要使用mkswap命令,与格式化ext系列等文件系统不同
Setting up swapspace version 1, size = 2097148 KiB
LABEL=swap_sdb1, UUID=ee3b8097-7c2e-47d0-8188-d6d69a341cb6
修改/etc/fstab配置文件,实现挂载
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:23:38 2016
#
# 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
#
UUID=80371af2-ee0e-481c-b71a-c9d25b11800a / xfs defaults 0 0
UUID=33974954-468b-490d-b84a-c955716d7146 /boot xfs defaults 0 0
UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir xfs defaults 0 0
UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap swap defaults 0 0
UUID="ee3b8097-7c2e-47d0-8188-d6d69a341cb6" swap swap defaults 0 0
查看新分区挂载之前的情况
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1868660 172672 1376176 9176 319812 1517840
Swap: 3905532 0 3905532
执行“swapon -a”激活交换分区后,再次重新查看交换分区情况
[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sdb1 partition 2097148 0 -2
/dev/sda2 partition 3905532 0 -1
注意:虽然/dev/sdb1已经挂载成功了,当sdb1的优先级比sda2低,系统还是会优先使用sda2,这样并不能提高系统的性能,应该将sdb1的优先级调高,有利于提供系统的性能。
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1868660 174028 1374788 9176 319844 1516436
Swap: 6002680 0 6002680
添加pri选项修改优先级
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:23:38 2016
#
# 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
#
UUID=80371af2-ee0e-481c-b71a-c9d25b11800a / xfs defaults 0 0
UUID=33974954-468b-490d-b84a-c955716d7146 /boot xfs defaults 0 0
UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir xfs defaults 0 0
UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap swap defaults 0 0
UUID="ee3b8097-7c2e-47d0-8188-d6d69a341cb6" swap swap pri=100 0 0
要使优先级生效,需要把之前添加的交换分区卸载,然后再重新挂载
[root@localhost ~]# swapoff /dev/sdb1
[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sdb1 partition 2097148 0 100
/dev/sda2 partition 3905532 0 -1
临时挂载
除了上面的方法,也可以使用下面的命令手动挂载,并且指定优先级,这种方法只适合临时挂载,重新启动就会失效。
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
[root@localhost ~]# swapon /dev/sdb1 -p 100
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sdb1 partition 2097148 0 100
/dev/sda2 partition 3905532 0 -1
swap分区的删除
取消/dev/sdb1交换分区的挂载
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
/dev/sdb1 partition 2097148 0 100
[root@localhost ~]# swapoff /dev/sdb1
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
删除/etc/fatab中的配置信息
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:23:38 2016
#
# 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
#
UUID=80371af2-ee0e-481c-b71a-c9d25b11800a / xfs defaults 0 0
UUID=33974954-468b-490d-b84a-c955716d7146 /boot xfs defaults 0 0
UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir xfs defaults 0 0
UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap swap defaults 0 0
UUID="ee3b8097-7c2e-47d0-8188-d6d69a341cb6" swap swap pri=100 0 0 #删除改行
删除/dev/sdb1分区
[root@localhost ~]# 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): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0xbdc9991b
Device Boot Start End Blocks Id System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0xbdc9991b
Device Boot Start End Blocks Id System
本文出自 “Linux路上” 博客,请务必保留此出处http://dreamlinuxc.blog.51cto.com/5733156/1844566
原文地址:http://dreamlinuxc.blog.51cto.com/5733156/1844566