利用分区来扩展swap交换分区空间

author author     2022-08-01     482

关键词:

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指定一个032767的优先级

    如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一。

    先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)

    优化性能:分布存放,高性能磁盘存放

 

swap分区的创建



步骤

    1、分区

    2、格式化

    3、挂载

 

查看当前交换分区总空间

[[email protected] ~]# free

             total       used       free       shared     buff/cache   available

Mem:         1868660     172672     1376176     9176       319812      1517840

Swap:        3905532     0          3905532

 

[[email protected] ~]# 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


添加硬盘,并对硬盘进行分区

[[email protected] ~]# echo "- - -" > /sys/class/scsi_host/host2/scan        #扫描新添加的硬盘

 

[[email protected]alhost ~]# 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

 

[[email protected] ~]# 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        #指定修改的文件类型为82Linux 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.

 

[[email protected] ~]# 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分区

[[email protected] ~]# 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配置文件,实现挂载

[[email protected] ~]# 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

 

查看新分区挂载之前的情况

[[email protected] ~]# swapon -s

Filename               Type          Size       Used    Priority

/dev/sda2              partition     3905532     0       -1

 

[[email protected] ~]# free

         total        used        free         shared      buff/cache   available

Mem:     1868660      172672     1376176       9176        319812       1517840

Swap:    3905532      0          3905532

 

执行“swapon -a”激活交换分区后,再次重新查看交换分区情况

[[email protected] ~]# swapon -a

[[email protected] ~]# 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的优先级调高,有利于提供系统的性能。

 

[[email protected] ~]# free

total        used         free      shared     buff/cache   available

Mem:        1868660    174028     1374788   9176      319844     1516436

Swap:       6002680      0     6002680

 

添加pri选项修改优先级

[[email protected] ~]# 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

 

要使优先级生效,需要把之前添加的交换分区卸载,然后再重新挂载

[[email protected] ~]# swapoff /dev/sdb1

[[email protected] ~]# swapon -a

[[email protected] ~]# swapon -s

Filename            Type            Size     Used    Priority

/dev/sdb1           partition       2097148  0       100

/dev/sda2           partition       3905532  0       -1

 

临时挂载

除了上面的方法,也可以使用下面的命令手动挂载,并且指定优先级,这种方法只适合临时挂载,重新启动就会失效。

[[email protected] ~]# swapon -s

Filename               Type            Size     Used    Priority

/dev/sda2              partition       3905532   0       -1

[[email protected] ~]# swapon /dev/sdb1 -p 100

[[email protected] ~]# swapon -s

Filename              Type            Size     Used    Priority

/dev/sdb1             partition       2097148  0       100

/dev/sda2             partition       3905532  0       -1


swap分区的删除



取消/dev/sdb1交换分区的挂载

[[email protected] ~]# swapon -s

Filename                                Type            Size    Used    Priority

/dev/sda2                               partition       3905532 0       -1

/dev/sdb1                               partition       2097148 0       100

 

[[email protected] ~]# swapoff /dev/sdb1

 

[[email protected] ~]# swapon -s

Filename                                Type            Size    Used    Priority

/dev/sda2                               partition       3905532 0       -1

 

删除/etc/fatab中的配置信息

[[email protected] ~]# 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分区

[[email protected] ~]# 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.

[[email protected] ~]# 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

扩展swap分区

swap即交换分区,类似于windows虚拟内存功能,就是在硬盘上分配出一块空间作为虚拟内存来使用.通常swap都是在装系统硬盘分区时设定,这里只是探讨一下系统已经安装好后,扩展swap分区的方法.下面我们来将下如何在安装好的lin... 查看详情

linux系统下创建swap交换分区

...一个小实验,来演示下如何创建swap分区并令其生效首先利用fdisk/dev/sdb来添加一个分区650)this.width= 查看详情

swap空间不足,如何进行添加

交换分区值过小,可以有两种方法来增加swap分区,一种是将新的分区来作为swap,另一种是在磁盘中创建一个大的文件来作swap.我们以文件扩展swap[[email protected]~]#cd/var[[email protected]var]#ddif=/dev/zeroof=filebs=1Mcount=1024[[email ... 查看详情

swap交换分区

swap交换分区虚拟内存(Vitrualmemory):即用软件模拟出来的内存,是将硬盘空间模拟成内存来用,用来补充物理内存不足的问题。在Linux系统中的虚拟内存是用交换分区来实现的。虚拟内存的参考值:物理内存的1.5~2倍注:当物理... 查看详情

swap分区(代码片段)

swap分区作者:张首富时间:2020-06-18wx:y18163201什么是swap分区Linux中Swap(即:交换分区),类似于Windows的虚拟内存,就是当内存不足的时候,把一部分硬盘空间虚拟成内存使用,从而解决内存容量不足的情况。Android是基于Linux的... 查看详情

linux系统kvm虚拟机实战lvm逻辑卷之扩展swap交换分区(代码片段)

【Linux系统KVM虚拟机实战】LVM逻辑卷之扩展Swap交换分区一、Swap分区介绍二、检查本地系统环境1.检查系统版本2.检查系统内核三、检查本地磁盘状态1.检查swap大小2.检查物理磁盘分区3.查看PV状态4.查看VG卷组状态5.查看LVM逻辑卷状... 查看详情

虚拟机无法分配内存virtualmemoryexhausted:cannotallocatememory

1.内存交换空间(swap)的构建安装Linux时一定需要的两个分区:根目录和swap(内存交换空间)。swap的功能:在应付物理内存不足的情况下所造成的内存扩展记录的功能。物理内存不足的时候,swap使用硬盘来暂时放置内存中的信... 查看详情

大内存服务器swap分区应该多大,红帽7系统

...件系统数据(pagecache),当内存耗尽时,系统内核只会回收利用这些pagecache内存。考虑到以下情况:1)安装 查看详情

阿里云云主机添加swap分区与swap性能优化

...大小是可用的虚拟内存的总量。交换空间通常是一个磁盘分区(此分区在安装操作系统时,系统通常会默认划分出一段空间用于交换分区,默认将交换空间的大小设定为内存的1倍到2倍),也可以是一个文件。内核参数中有一 查看详情

安装linux至少需要哪两个分区?作用分别是啥?

参考技术A一个为根分区,一个为交换分区。1,/,根分区,一般所有文件都放在根目录下。2,swap,虚拟内存,交换分区,一般大小为机器内存的1-2倍。起码有如上两个分区才可以安装linux系统。建议再增加一个/boot分区,200M左... 查看详情

centos设置虚拟内存swap

...,只可以使用文件来当作虚拟内存。而linux可以文件或者分区来当作虚拟内存。首先查看当前的内存和swap空间大小(默认单位为k,-m单位为M):free-m此处可以看到总内存是503M,SWAP不存在。查看swap信息,包括文件和分区的详细信息swap... 查看详情

swap分区的创建(代码片段)

swap分区的创建swap交换分区是系统RAM的补充,Swap分区支持虚拟内存。当没有足够的RAM保存系统处理的数据时会将数据写入swap分区当系统缺乏swap空间时,内核会因RAM内存耗尽而终止进程。配置过多swap空间会造成存储设备处于分配... 查看详情

37swap交换分区的配置

swap交换分区是系统ram不足的时候,使用系统磁盘作为ram的补充,也叫虚拟内存。因使用的是系统磁盘,在性能上与ram上有所差异,当配置过多的swap空间时,会掩盖内存泄漏问题以及造成存储空间浪费情况;一般情况下,swap空间... 查看详情

安装linux系统必要的分区是啥?

...,剩下的全分给家目录。这样的好处是分区少,磁盘空间利用率大,以前也尝试过多分几个区,每个分区小了,同一部动画放进去放不下了,这就叫空间利用率低。  3、复杂一些的要根据用户的硬件资源和使用Linux目的进行单... 查看详情

makecollect2:ldterminatedwithsignal9错误解决办法

...l  增加swap大小的方法有两种,一种是已经分过swap交换分区,不过分配不合理,所以可以通过增加swap文件来增加交换分区的大小;另一种方法是通过增加swap分区大小来增加swap的大小。 方法一 查看详情

centos创建swap交换分区

阿里云的服务器是没有交换分区的,如[[email protected]~]$free-mtotalusedfreesharedbuff/cacheavailableMem:78238541242568436661Swap:0001.创建swap分区,关于这个swap应该设置多大呢?4GB或4GB以下内存的系统,最小需要2GB交换空间;大于4GB而小于16GB内... 查看详情

liunx-系统配置及服务管理-交换分区管理swap

...升‘内存的容量,防止OOM(OutOfMemory)2.推荐:设置交换分区大小为内存的2倍3.大于4GB而小于16GB内存的系统,最小需要4GB交换空间;大于16GB而小于64GB内存的系统,最小需要8GB交换空间;大于64GB而小于256GB内存的系统,最小需要16... 查看详情

系统篇:linux下增加交换分区(代码片段)

查看目前交换分区的情况droid@droid-server:~$sudoswapon-sFilename Type Size Used Priority/swapfile file 2097148 98816 -2增加交换分区#创建分区路径sudomkdir-p/var/cache/swap/#设置分区的大小#bs=64M是块大小,count=64是块数量,所以swap空... 查看详情