什么是swap分區(qū):
Swap分區(qū)在系統(tǒng)的物理內(nèi)存不夠用的時候,把硬盤空間中的一部分空間釋放出來靠闭,以供當(dāng)前運行的程序使用钳宪。那些被釋放的空間可能來自一些很長時間沒有什么操作的程序,這些被釋放的空間被臨時保存到Swap分區(qū)中子眶,等到那些程序要運行時膝捞,再從Swap分區(qū)中恢復(fù)保存的數(shù)據(jù)到內(nèi)存中坦刀。在特殊情況下連swap 分區(qū)不夠用時我們則需要建立更大的swap分區(qū)來供系統(tǒng)使用。
一般來說建立swap有三種方法:
1.刪掉原來的swap分區(qū)重新建立一個更大的swap分區(qū),但在實際情況中不推薦使用這種方式鲤遥,如果系統(tǒng)正在使用swap分區(qū)而刪掉則可能帶來數(shù)據(jù)的丟失央渣!
2.新建磁盤做swap,供系統(tǒng)使用渴频;
3.用文件做swap分區(qū)使用芽丹。
一.新建磁盤做swap分區(qū):
這里以虛擬機為例
2.讓系統(tǒng)識別新增加的磁盤(有兩種方法)
1.重啟虛擬機
2.使用命令
[root@centos7 ~]#echo '- - -' >/sys/class/scsi_host/host2/scan
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 10G 0 disk 注:這里就識別到了剛創(chuàng)建的10G硬盤
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
3.創(chuàng)建一個2G大小的swap分區(qū):
[root@centos7 ~]#fdisk /dev/sdb 注:用fdisk對/dev/sdb創(chuàng)建分區(qū)
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 0xa83f56d3.
Command (m for help): n 注:n為增加一個分區(qū)
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p 注:這里選擇主分區(qū)
Partition number (1-4, default 1): 1 注:分區(qū)編號默認(rèn)就1
First sector (2048-20971519, default 2048): 注:選擇默認(rèn)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G 注:增加一個2G的分區(qū)
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): p 注:打印分區(qū)表
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xa83f56d3
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
Command (m for help): t 注:默認(rèn)的ID是82是Linux分區(qū)我們要更改為82 專門用于swap的
Selected partition 1
Hex code (type L to list all codes): 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
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xa83f56d3 注:這里ID就變更為82
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@centos7 ~]#mkswap /dev/sdb1 -L swap2 使用mkswap寫入特殊簽名卜朗,卷標(biāo)設(shè)置為swap2
Setting up swapspace version 1, size = 2097148 KiB
LABEL=swap2, UUID=1f0e66b2-6423-4115-a20d-c0d4957586ff
[root@centos7 ~]#blkid 注:查看是否創(chuàng)建成功
/dev/sda2: UUID="e8d7cbe2-0fd0-4355-b535-a7b54c3febd9" TYPE="xfs"
/dev/sda1: UUID="637077c6-3003-40e0-8689-1556bb4fa00e" TYPE="xfs"
/dev/sda5: UUID="52ceaf06-70a8-4ce2-9bea-e4be30b6af18" TYPE="swap"
/dev/sr0: UUID="2016-12-05-13-52-39-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda3: UUID="4ca027ad-66c8-4132-809f-9ef47aa068b8" TYPE="xfs"
/dev/sdb1: LABEL="swap2" UUID="1f0e66b2-6423-4115-a20d-c0d4957586ff" TYPE="swap"
4.對/etc/fatab進行增加寫入拔第,讓系統(tǒng)開機進行識別掛載。
對應(yīng)相應(yīng)的格式寫入
5.查看swap的分區(qū)掛載场钉。
[root@centos7 ~]#swapon -s 注:默認(rèn)修改fstab沒有生效
Filename Type Size Used Priority
/dev/sda5 partition 2097148 4 -1
6.使用swapon -a 激活交換空間
[root@centos7 ~]#swapon -a
[root@centos7 ~]#swapon -s 注:成功激活swap
Filename Type Size Used Priority
/dev/sda5 partition 2097148 4 -1
/dev/sdb1 partition 2097148 0 -2
到這里基本完成蚊俺,如需更改優(yōu)先級
則可在fstab里更改
如:
這里的數(shù)字越大表示優(yōu)先級越高,設(shè)置完后使之生效逛万,這里先禁用swap分區(qū)
[root@centos7 ~]#swapoff /dev/sdb1
[root@centos7 ~]#swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 4 -1
[root@centos7 ~]#swapon -a 注:重新激活swap泳猬,優(yōu)先級更改
[root@centos7 ~]#swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 4 -1
/dev/sdb1 partition 2097148 0 1
到這里就設(shè)置完畢了。
二宇植,用文件做swap分區(qū)使用
1得封,先創(chuàng)建一個大文件我們可以用dd這條命令來實現(xiàn)
[root@centos7 ~]#dd if=/dev/zero of=swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.59941 s, 192 MB/s
[root@centos7 ~]#ll -h swapfile
-rw-r--r--. 1 root root 1.0G Jun 27 13:36 swapfile
注:
if=/dev/zero : /dev/zero文件代表一個永遠(yuǎn)輸出 0的設(shè)備文件
of=swapfile :寫入到以swapfile命名的文件中
2.使用mkswap寫入特殊簽名
[root@centos7 ~]#mkswap swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=d07f3864-5bd1-4a08-b456-bd29506f0684
[root@centos7 ~]#vim /etc/fstab 注:添加到fstab中
UUID=e8d7cbe2-0fd0-4355-b535-a7b54c3febd9 / xfs defaults 0 0
UUID=4ca027ad-66c8-4132-809f-9ef47aa068b8 /app xfs defaults 0 0
UUID=637077c6-3003-40e0-8689-1556bb4fa00e /boot xfs defaults 0 0
UUID=52ceaf06-70a8-4ce2-9bea-e4be30b6af18 swap swap defaults 0 0
UUID=1f0e66b2-6423-4115-a20d-c0d4957586ff swap swap defaults,pri=1 0 0
UUID=d07f3864-5bd1-4a08-b456-bd29506f0684 swap swap defaults 0 0
[root@centos7 ~]#swapon -a 注:激活swapfile分區(qū)
swapon: /root/swapfile: insecure permissions 0644, 0600 suggested.
[root@centos7 ~]#swapon -s 注:查看是否激活成功
Filename Type Size Used Priority
/dev/sda5 partition 2097148 4 -1
/dev/sdb1 partition 2097148 0 1
/root/swapfile file 1048572 0 -2
到了這里兩種方法就設(shè)置完畢了......
如有不足請多多指教!