日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

使用磁盘为Linux添加swap

發(fā)布時(shí)間:2025/3/17 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用磁盘为Linux添加swap 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一.SWAP 說明
?
1、SWAP 概述
  當(dāng)系統(tǒng)的物理內(nèi)存不夠用的時(shí)候,就需要將物理內(nèi)存中的一部分空間釋放出來,以供當(dāng)前運(yùn)行的程序使用。那些被釋放的空間可能來自一些很長(zhǎng)時(shí)間沒有什么操作的程序,這些被釋放的空間被臨時(shí)保存到Swap空間中,等到那些程序要運(yùn)行時(shí),再?gòu)腟wap中恢復(fù)保存的數(shù)據(jù)到內(nèi)存中。這樣,系統(tǒng)總是在物理內(nèi)存不夠時(shí),才進(jìn)行Swap交換。
  以上是SWAP 交換分區(qū)的作用。 實(shí)際上,我們更關(guān)注的應(yīng)該是SWAP分區(qū)的大小問題。 設(shè)置多大才是最優(yōu)的。一般來說可以按照如下規(guī)則設(shè)置swap大小:
    

    4G以內(nèi)的物理內(nèi)存,SWAP 設(shè)置為內(nèi)存的2倍。
    4-8G的物理內(nèi)存,SWAP 等于內(nèi)存大小。
    8-64G 的物理內(nèi)存,SWAP 設(shè)置為8G。
    64-256G物理內(nèi)存,SWAP 設(shè)置為16G。
  

  實(shí)際上,系統(tǒng)中交換分區(qū)的大小并不取決于物理內(nèi)存的量,而是取決于系統(tǒng)中內(nèi)存的負(fù)荷,所以在安裝系統(tǒng)時(shí)要根據(jù)具體的業(yè)務(wù)來設(shè)置SWAP的值。


2、系統(tǒng)在什么情況下才會(huì)使用SWAP?
  實(shí)際上,并不是等所有的物理內(nèi)存都消耗完畢之后,才去使用swap的空間,什么時(shí)候使用是由swappiness 參數(shù)值控制。

[root@rhce ~]# cat /proc/sys/vm/swappiness 60

  該值默認(rèn)值是60。swappiness=0的時(shí)候表示最大限度使用物理內(nèi)存,然后才是 swap空間,swappiness=100的時(shí)候表示積極的使用swap分區(qū),并且把內(nèi)存上的數(shù)據(jù)及時(shí)的搬運(yùn)到swap空間里面。現(xiàn)在服務(wù)器的內(nèi)存動(dòng)不動(dòng)就是上百G,所以我們可以把這個(gè)參數(shù)值設(shè)置的低一些,讓操作系統(tǒng)盡可能的使用物理內(nèi)存,降低系統(tǒng)對(duì)swap的使用,從而提高系統(tǒng)的性能。

二.使用磁盤為L(zhǎng)inux添加swap

?  查看磁盤及分區(qū)信息

[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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


  添加新的分區(qū)

[root@ip-192-168-31-161 ~]# fdisk /dev/xvdf 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 0x8fb93abe.Command (m for help): m Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)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): 1 First sector (2048-8388607, default 2048): (直接回車) Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): ?(直接回車) Using default value 8388607 Partition 1 of type Linux and of size 4 GiB is setCommand (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks.


  查看磁盤及分區(qū)信息

[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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: 0x8fb93abeDevice Boot Start End Blocks Id System /dev/xvdf1 2048 8388607 4193280 83 Linux

?

  可以看到/dev/xvdf1是新創(chuàng)建的分區(qū),注意這里fdisk ID的編號(hào)。 這里顯示的83,它代表這個(gè)分區(qū)是ext的分區(qū),這個(gè)是不能用來做swap的。我們繼續(xù)更改

[root@ip-192-168-31-161 ~]# fdisk /dev/xvdf 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): l (list known partition types)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 Command (m for help): m Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris'Command (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks.


  查看磁盤及分區(qū)信息

[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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: 0x8fb93abeDevice Boot Start End Blocks Id System /dev/xvdf1 2048 8388607 4193280 82 Linux swap / Solaris

?

  格式化swap分區(qū):

[root@rhce /]# mkswap /dev/xvdf1 Setting up swapspace version 1, size = 1060252 KiB no label, UUID=39c73cf6-1848-4a78-83ec-35089f0eebcb

[root@ip-192-168-31-161 ~]# blkid

/dev/xvda1: LABEL="centos7_root" UUID="179c37d9-9ba3-47f2-ab50-cf76dec5ef8e" TYPE="xfs" /dev/loop0: UUID="0c7e9760-c4df-4d5b-b0b6-b2f4487e757b" TYPE="ext4" /dev/mapper/docker-202:1-385876097-pool: UUID="0c7e9760-c4df-4d5b-b0b6-b2f4487e757b" TYPE="ext4" /dev/xvdf1: UUID="39c73cf6-1848-4a78-83ec-35089f0eebcb" TYPE="swap"

?

  手工的啟動(dòng)swap

[root@ip-192-168-31-161 ~]# swapon /dev/xvdf1
[root@ip-192-168-31-161 ~]# swapon -s

?

  將/dev/xvdf1添加/etc/fstab文件,系統(tǒng)啟動(dòng)自動(dòng)加載swap

[root@ip-192-168-31-161 ~]# vi /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 8 16:48:19 2014 # # 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 # LABEL=centos7_root / xfs defaults 1 1 UUID=39c73cf6-1848-4a78-83ec-35089f0eebcb swap swap defaults 0 0

?

總結(jié)

以上是生活随笔為你收集整理的使用磁盘为Linux添加swap的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。