Linux 磁盘管理-交换分区管理详解

今天给伙伴们分享一下Linux 磁盘管理-交换分区管理,希望看了有所收获。

我是公众号「想吃西红柿」「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!

如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。

  • 交换(SWAP)分区是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。

  • 由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。

  • swap分区大小设置规则,在生产环境中,交换分区的大小一般为真实物理内存的1.5~2倍,在Linux系统,我们可以参照Redhat公司为RHEL5、RHEL6推荐的SWAP空间的大小划分原则,在你没有其他特别需求时,可以作为很好的参考依据。

    • 内存小于4GB时,推荐不少于2GB的swap空间;

    • 内存4GB~16GB,推荐不少于4GB的swap空间;

    • 内存16GB~64GB,推荐不少于8GB的swap空间;

    • 内存64GB~256GB,推荐不少于16GB的swap空间。

1、fdisk做一个分区

[root@qfedu.com ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 敲击回车即可
First sector (4196352-41943039, default 4196352): 敲击回车即可
Last sector, +sectors or +size{K,M,G,T,P} (4196352-41943039, default 41943039): +5G

Created a new partition 2 of type 'Linux' and of size 5 GiB.

2、更新分区表

修改硬盘的标识码,这里将其改成82(Linux swap)

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p 
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x88b2c2b0

Device     Boot   Start      End  Sectors Size Id Type
/dev/sdb1          2048  4196351  4194304   2G 83 Linux
/dev/sdb2       4196352 14682111 10485760   5G 82 Linux swap / Solaris

w参数退出分区表编辑工具:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3、格式化swap分区

mkswap命令用于对新设备进行交换分区格式化,英文全称为“make swap”,语法格式为“mkswap设备名称”。

[root@qfedu.com ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=45a4047c-49bf-4c88-9b99-f6ac93908485

4、激活交换分区

swapon命令用于激活新的交换分区设备,英文全称为“swap on”,语法格式为“swapon设备名称”。

使用free -m命令查看交换分区的大小变化(由2047MB增加到7167MB):

[root@qfedu.com ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1966        1391         105          12         469         384
Swap:          2047           9        2038
[root@qfedu.com ~]# swapon /dev/sdb2
[root@qfedu.com ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1966        1395         101          12         469         380
Swap:          7167           9        7158

5、永久激活swap分区

为了能够让新的交换分区设备在重启后依然生效,需要按照下面的格式将相关信息写入配置文件中,并记得保存:

[root@qfedu.com ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jul 21 05:03:40 2020
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root                        /               xfs        defaults    1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b    /boot           xfs        defaults    1 2
/dev/mapper/rhel-swap                        swap            swap       defaults    0 0
/dev/cdrom                                   /media/cdrom    iso9660    defaults    0 0 
/dev/sdb1                                    /newFS          xfs        defaults    0 0 
/dev/sdb2                                    swap            swap       defaults    0 0 
[root@qfedu.com ~]# swapon -a	(读取/etc/fstab)

6、关闭swap分区

[root@qfedu.com ~]# swapoff /dev/sdb2

如果对伙伴们有帮助,感谢关注支持**「想吃西红柿」「云原生运维实战派」**后续会有更多实用的运维技术分享给伙伴们,您的关注就是我最大的成就。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘嫣慕曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值