linux下的交换空间
交换分区的作用:
当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行
的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被
临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系
统总是在物理内存不够时,才进行Swap交换。
======================================================================================
一般来说可以按照如下规则设置swap大小:
4G以内的物理内存,SWAP 设置为内存的2倍。
4-8G的物理内存,SWAP 等于内存大小。
8-64G 的物理内存,SWAP 设置为8G。
64-256G物理内存,SWAP 设置为16G。
实际上,系统中交换分区的大小并不取决于物理内存的量,而是取决于系统中内存的负荷,所以在
安装系统时要根据具体的业务来设置SWAP的值。
======================================================================================
系统在什么情况下才会使用SWAP?
实际上,并不是等所有的物理内存都消耗完毕之后,才去使用swap的空间,什么时候使用是由
swappiness 参数值控制。
ubuntu@VM-24-30-ubuntu:~$ cat /proc/sys/vm/swappiness
60
该值默认值是60.
swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,
swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里
面
======================================================================================
如何修改swappiness参数?
--临时性修改:
[root@rhce ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@rhce ~]# cat /proc/sys/vm/swappiness
10
这里我们的修改已经生效,但是如果我们重启了系统,又会变成60.
--永久修改:
在/etc/sysctl.conf 文件里添加如下参数:
vm.swappiness=10
或者:
[root@rhce ~]# echo 'vm.swappiness=10' >>/etc/sysctl.conf
保存,重启,就生效了。
=====================================================================================
交换分区的设置:
Linux下可以创建两种类型的交换空间,一种是swap分区,一种是swap文件。前者适合有空闲
的分区可以使用,后者适合于没有空的硬盘分区,硬盘的空间都已经分配完毕。
=====================================================================================
增加交换分区:
根据交换分区的设置,增加交换分区的方案可分为两种:
1.使用分区:
在安装OS时划分出专门的交换分区,空间大小要事先规划好,启动系统时自动进行mount。这
种方法只能在安装OS时设定,一旦设定好不容易改变,除非重装系统。
2.使用swapfile:(或者是整个空闲分区)
新建临时swapfile或者是空闲分区,在需要时设定为交换空间,最多可以增加8个swapfile。
交换空间的大小,与CPU密切相关,在i386系中,最多可以使用2GB的空间。在系统启动后根据需要
在2G的总容量下进行增减。这种方法比较灵活,也比较方便,缺点是启动系统后需要手工设置。
=====================================================================================
使用swapfile增加交换分区的实例:
使用命令详解:
free:查看内存状态命令,可显示memory,swap等的大小和使用情况,默认单位为k, -m
单位为M
或者检查meminfo文件
grep SwapTotal /proc/meminfo
dd:读取,转换并输出数据命令;
if:读取的源空闲空间;
of:输出的交换文件的路径和名称;
bs:块大小,单位为byte,一般为1k即1024个byte;
count:总块数,即空间总大小,单位为k
mkswap:设置交换分区,相当于格式化交换分区
swapon:启用交换分区,相当于mount
swapoff:关闭交换分区,相当于umount
=====================================================================================
查看原交换空间大小
ubuntu@VM-24-30-ubuntu:~$ free -m
total used free shared buffers cached
Mem: 3952 3374 578 0 324 1419
-/+ buffers/cache: 1630 2322
Swap: 2047 1016 1031
=====================================================================================
创建交换空间文件:大小为1000M
ubuntu@VM-24-30-ubuntu:~$ sudo dd if=/dev/zero of=/myswap bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 544.709 s, 1.9 MB/s
====================================================================================
将swapfile设置为swap空间
ubuntu@VM-24-30-ubuntu:~$ sudo mkswap /myswap
Setting up swapspace version 1, size = 999996 KiB
no label, UUID=ce09301f-2bf1-4101-9e09-f93a8e75323f
启用交换空间
ubuntu@VM-24-30-ubuntu:~$ sudo swapon /myswap
=====================================================================================
查看增加后的交换空间大小
ubuntu@VM-24-30-ubuntu:~$ free -m
total used free shared buffers cached
Mem: 3952 3824 128 0 66 2170
-/+ buffers/cache: 1587 2364
Swap: 3024 1025 1999
到此,增加交换空间结束
======================================================================================
如果不再使用空间可以选择关闭交换空间
ubuntu@VM-24-30-ubuntu:~$ sudo swapoff /myswap
ubuntu@VM-24-30-ubuntu:~$ free -m
total used free shared buffers cached
Mem: 3952 3824 128 0 66 2170
-/+ buffers/cache: 1587 2365
Swap: 2047 1025 1022
====================================================================================
使用这种方法在每次系统启动时都需要手动设置、开启swapfile,比较麻烦,解决方法:
在 /etc/rc.d/rc.local 文件的末行下追加加以下内容:(编辑这个文件当然是用vi了~)
/sbin/swapon /myswap
保存后退出,这样在系统启动后,swap空间就会自动加载了;
修改/etc/fstab文件,使得新加的2G交换空间在系统重新启动后自动生效
在文件最后加入:
/myswap swap swap defaults 0 0
======================================================================================
删除交换文件
ubuntu@VM-24-30-ubuntu:~$ sudo swapoff /myswap
ubuntu@VM-24-30-ubuntu:~$ rm -rf /myswap
rm: cannot remove `/myswap': Permission denied
ubuntu@VM-24-30-ubuntu:~$ sudo rm -rf /myswap
=====================================================================================
查看交换分区:
ubuntu@VM-24-30-ubuntu:~$ swapon -s
Filename Type Size Used Priority
/dev/vdc partition 2097148 1049704 -1
/myswap file 982112 0 -2
======================================================================================
使用磁盘添加swap
先划分一个1G的磁盘分区出来:
[root@rhce /]# fdisk /dev/sdb 查看磁盘
Device contains neither a valid DOS partitiontable, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier0x65edb587.
Changes will remain in memory only, until youdecide to write them.
After that, of course, the previous content won'tbe recoverable.
Warning: invalid flag 0x0000 of partition table 4will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n 创建新分区
Command action
e extended
p primary partition (1-4)
p 创建主分区
Partition number (1-4): 1 主分区序号为1
First cylinder (1-652, default 1):
Using default value 1 使用默认值
Last cylinder, +cylinders or +size{K,M,G} (1-652,default 652): +1G 大小为1G
Command (m for help): w 保存修改内容
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
=====================================================================================
[root@rhce /]# fdisk -l 查看所有磁盘分区
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000bcd24
DeviceBoot Start End Blocks Id System
/dev/sda1 * 1 128 1024000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 128 511 3072000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 511 2611 16874496 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x65edb587
DeviceBoot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux 新建的分区
…
注意这里fdisk ID的编号。 这里显示的83,它代表这个分区是ext的分区,这个是不能用来做swap
的。
=====================================================================================
在fdisk 命令可以看到具体数据的含义:
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linuxplaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 HiddenFAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaqdiagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 HiddenFAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 HiddenFAT16 64 Novell Netware af HFS/ HFS+ fb VMware VMFS
17 HiddenHPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 ASTSmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b HiddenW95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c HiddenW95 FAT3 80 Old Minix be Solaris boot ff BBT
1e HiddenW95 FAT1
ext的分区ID号:0x83
swap的分区ID号:0x82
======================================================================================
将/dev/sdb1分区改成swap 分区:
[root@rhce ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): h 帮助命令
h: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): t 修改类型命令
Selected partition 1
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS /.
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linuxplaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 HiddenFAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaqdiagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 HiddenFAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 HiddenFAT16 64 Novell Netware af HFS/ HFS+ fb VMware VMFS
17 HiddenHPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 ASTSmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b HiddenW95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c HiddenW95 FAT3 80 Old Minix be Solaris boot ff BBT
1e HiddenW95 FAT1
Hex code (type L to list codes):82
Changed system type of partition1 to 82 (Linux swap / Solaris)
--注意这里更改磁盘分区的ID类型,如果不更在,在系统重启时会根据ID来判断磁盘的类型,那样
在使用时就会出现错误。
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 witherror 16: Device or resource busy.
The kernel still uses the old table. The new tablewill be used at
the next reboot or after you run partprobe(8) orkpartx(8)
Syncing disks.
======================================================================================
格式化swap分区:
[root@rhce /]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID= bc913e6b-209a-49a7-b561-9b8c57294681
===================================================================================
[root@rhce /]# mkswap –L swap-disk/dev/sdc1 设置Lable
一个设置了Lable,一个没设置。我们在添加/etc/fstab时,可是用LABLE名称,或者使用UUID来配
置。 具体如上。
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x65edb587
DeviceBoot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 82 Linux swap / Solaris
Disk /dev/sdc: 5368 MB, 5368709120 bytes
181 heads, 40 sectors/track, 1448 cylinders
Units = cylinders of 7240 * 512 = 3706880 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
DeviceBoot Start End Blocks Id System
/dev/sdc1 1 291 1053400 82 Linux swap / Solaris
================================================================================
注意: 使用blkid 查看类型已经变成了swap:
[root@rhce ~]# blkid
/dev/sda1: UUID="935f7fb2-2ef4-486f-ae4e-265eaf9f580f"TYPE="ext4"
/dev/sda2:UUID="43343402-9188-4cdc-9c79-0e36ab737517" TYPE="swap"
/dev/sda3:UUID="7e49fb2c-6fd8-461a-95d9-65d1f3994160" TYPE="ext4"
/dev/sdb1:UUID="bc913e6b-209a-49a7-b561-9b8c57294681" TYPE="swap"
/dev/sdc1:LABEL="swap-disk"UUID="799b4379-ca59-4508-ad04-2d18847b3fd9" TYPE="swap"
这里的类型已经改变,我们可以尝试手工的启动swap:
[root@rhce ~]# swapon /dev/sdc1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
这里可以直接启用swap。
我们这里把/dev/dbb1和/dev/sdc1都添加/etc/fstab 文件,在重启一下看看。 添加内容如下:
UUID=bc913e6b-209a-49a7-b561-9b8c57294681 swap swap defaults
0 0
LABEL=swap-disk swap swap defaults 0 0
========================================================================
重启系统后查看:
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
=====================================================================================
对/dev/sdb1 进行测试:
[root@rhce ~]# swapoff /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
[root@rhce ~]# swapon /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2