linux系統中添加swap交換分區、刪除swap交換分區


      swap交換分區是一種通過在硬盤中預先划分一定的空間,然后把內存中暫時不常用的數據臨時存放在硬盤中,以便騰出物理內存空間讓更活躍的程序服務來

使用的技術,其設計的目的是解決真實物理內存不足的問題。但由於交換分區畢竟是通過硬盤設備讀寫數據的,速度肯定要比物理內存滿,所以只有當真實的

物理內存耗盡后才會調用交換分區的資源。在生產環境中,交換分區的大小一般為真實物理內存的1.5~2倍。(linux就該這么學p128)

      swap及交換分區,類似於windows虛擬內存的作用,其作用可簡單描述為,在系統物理內存不足時,將暫時不用的數據存放到交換空間所在的硬盤上,

從而可以騰出內存來讓別的程序運行。(https://blog.csdn.net/qq_37344125/article/details/107446554)

      通常swap都是在裝系統硬盤分區時設定,這里只是探討一下系統已經安裝好后,擴展swap分區的方法.下面我們來將下如何在安裝好的linux下增加swap交換分區.

可以有兩種方法來增加swap分區,一種是將新的分區來作為swap,另一種是在磁盤中創建一個大的文件來作swap.(http://www.ttlsa.com/linux/update-linux-swap/)

 

1、如何查看當前的swap分區,swapon -s  命令

[root@linuxprobe dev]# swapon -s Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1

 

2、使用新的分區來做swap (增加交換分區有兩種方法,一種是新的交換分區來做swap,另外一種是在磁盤中創建一個大的文件來做swap)

[root@linuxprobe dev]# pwd ##查看當前路徑 /dev [root@linuxprobe dev]# find sd*  ## 查看當前的硬盤及分區情況,兩塊同接口硬盤sda、sdb,sdb一個主分區sdb1 sda sda1 sda2 sdb sdb1 [root@linuxprobe dev]# fdisk /dev/sdb ## 對/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): p ## 查看分區,一個分區 Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System /dev/sdb1            2048     6293503     3145728   83 Linux Command (m for help): n ## 新增分區 Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p ## 選擇p,新增主分區 Partition number (2-4, default 2): 2 ## 選擇主分區編號2 First sector (6293504-41943039, default 6293504): ## 定義起始的扇區位置,默認敲回車鍵即可 Using default value 6293504 Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +5G ## 定義分區結束的扇區位置,實際是定義分區大小,輸入+5G,新的分區5G Partition 2 of type Linux and of size 5 GiB is set Command (m for help): p ## 輸入p查看分區情況,可見新增了一個分區 Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System /dev/sdb1            2048     6293503     3145728   83 Linux /dev/sdb2         6293504    16779263     5242880   83 Linux Command (m for help): w ## 輸入w保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@linuxprobe dev]# pwd ## 查看當前路徑 /dev [root@linuxprobe dev]# find sd*  ## 查看硬盤及分區情況,發現新增的分區未及時同步 sda sda1 sda2 sdb sdb1 [root@linuxprobe dev]# partprobe ## 這個命令雷士刷新 Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. [root@linuxprobe dev]# find sd*  ## 再次查看硬盤及分區情況,新的分區已經同步
sda sda1 sda2 sdb sdb1 sdb2 [root@linuxprobe dev]# mkswap /dev/sdb2 ## 使用swap分區專用的格式化命令mkswap進行格式話 mkswap: /dev/sdb2: warning: wiping old swap signature. Setting up swapspace version 1, size = 5242876 KiB no label, UUID=b9fd91b4-b51e-4e9f-81c1-0d1446820dd7 [root@linuxprobe dev]# free -h ## 查看未掛載前swap分區大小 total used free shared buffers cached Mem: 1.9G 1.3G 686M 9.2M 1.7M 549M -/+ buffers/cache: 750M 1.2G Swap: 2.0G 0B 2.0G [root@linuxprobe dev]# swapon -s ## 查看當前swap分區 Filename Type Size Used Priority /dev/dm-0 partition 2097148 0 -1 [root@linuxprobe dev]# swapon /dev/sdb2 ## 使用swapon命令把准備好的swap分區設備正式掛載到系統上 [root@linuxprobe dev]# free -h ## 查看swap分區大小,發現已經曾加了5G total used free shared buffers cached Mem: 1.9G 1.3G 682M 9.2M 1.7M 549M -/+ buffers/cache: 754M 1.2G Swap: 7.0G 0B 7.0G [root@linuxprobe dev]# swapon -s ## 查看當前swap分區 Filename Type Size Used Priority /dev/dm-0 partition 2097148 0 -1 /dev/sdb2 partition 5242876 0 -2

將掛載后的swap分區寫入開機自動啟動,修改 /etc/fstab配置文件

[root@linuxprobe dev]# cat /etc/fstab ## 查看修改前情況 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0 [root@linuxprobe dev]# echo -e "/dev/sdb2\tswap\tswap\tdefaults\t0\t0" >> /etc/fstab ## 修改,也可以使用vim編輯器 [root@linuxprobe dev]# cat /etc/fstab ## 查看修改后 /etc/fstab文件 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0

 

 

3、創建文件來做swap分區

[root@linuxprobe mnt]# cd /mnt/  ## 選擇一個目錄用於創建swap文件 [root@linuxprobe mnt]# ls sdb1test [root@linuxprobe mnt]# mkdir fileswap ## 創建放置swap文件的目錄 [root@linuxprobe mnt]# ls fileswap sdb1test [root@linuxprobe mnt]# cd fileswap/ [root@linuxprobe fileswap]# ls [root@linuxprobe fileswap]# dd if=/dev/zero bs=1M count=2048 of=swapfile ## 創建大小為2G的swap文件 2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 12.6299 s, 170 MB/s [root@linuxprobe fileswap]# ll -h ## 查看 total 2.0G -rw-r--r--. 1 root root 2.0G Oct 24 13:46 swapfile [root@linuxprobe fileswap]# mkswap swapfile ## 使用mkswap命令對創建的swap文件進行格式話 Setting up swapspace version 1, size = 2097148 KiB no label, UUID=64e6c009-9e1e-4e23-9881-28ec85f13ef0 [root@linuxprobe fileswap]# ll -h ## 查看,沒啥變化 total 2.0G -rw-r--r--. 1 root root 2.0G Oct 24 13:51 swapfile [root@linuxprobe fileswap]# chmod 600 swapfile ## 修改權限 ?? [root@linuxprobe fileswap]# ll -h ## 查看 total 2.0G -rw-------. 1 root root 2.0G Oct 24 13:51 swapfile [root@linuxprobe fileswap]# free -h ## 查看掛載前的swap分區空間大小 total used free shared buffers cached Mem: 1.9G       1.9G        73M       9.2M       220K       1.1G -/+ buffers/cache:       761M       1.2G Swap: 7.0G         0B       7.0G [root@linuxprobe fileswap]# swapon -s ## 查看掛載前的swap分區 Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2 [root@linuxprobe fileswap]# swapon swapfile ## 掛載創建的swap分區 [root@linuxprobe fileswap]# free -h ## 查看掛載后的swap分區大小,發現增加了2G total used free shared buffers cached Mem: 1.9G       1.9G        71M       9.2M       220K       1.1G -/+ buffers/cache:       763M       1.2G Swap: 9.0G         0B       9.0G [root@linuxprobe fileswap]# swapon -s ## 查看掛載后swap分區,多出一個分區 Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
/mnt/fileswap/swapfile                  file    2097148 0       -3

設置為開機自動啟動,修改/etc/fstab配置文件:

[root@linuxprobe fileswap]# cat /etc/fstab ## 查看修改前的配置文件 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0 [root@linuxprobe fileswap]# echo -e "/mnt/fileswap/swapfile\tswap\tswap\tdefaults\t0\t0" >> /etc/fstab ## 修改配置文件,也可以使用vim編輯器 [root@linuxprobe fileswap]# cat /etc/fstab ## 查看修改后的配置文件 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
/mnt/fileswap/swapfile  swap    swap    defaults        0       0

 

4、如何刪除swap分區

首先刪除開啟自動啟動的配置文件對應行:

[root@linuxprobe fileswap]# cat /etc/fstab ## 首先查看配置文件 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
/mnt/fileswap/swapfile  swap    swap    defaults        0       0 [root@linuxprobe fileswap]# wc -l /etc/fstab ## 統計行數,一共15行 15 /etc/fstab [root@linuxprobe fileswap]# sed '14,15d' /etc/fstab -i ## 刪除最后兩行(這里根據swap分區所在行數而定),也可以使用vim編輯器刪除 [root@linuxprobe fileswap]# cat /etc/fstab ## 查看刪除效果 # # /etc/fstab # Created by anaconda on Thu Oct 15 18:36:35 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 # /dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1 UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0

 

使用swapoff命令刪除交換分區

[root@linuxprobe mnt]# swapon -s ## 查看刪除前的分區 Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
/mnt/fileswap/swapfile                  file    2097148 0       -3 [root@linuxprobe fileswap]# free -h ## 查看刪除前交換分區大小 total used free shared buffers cached Mem: 1.9G       1.9G        71M       9.2M       220K       1.1G -/+ buffers/cache:       763M       1.2G Swap: 9.0G         0B       9.0G [root@linuxprobe mnt]# swapoff /mnt/fileswap/swapfile ## 使用swapoff 刪除一個交換分區 [root@linuxprobe mnt]# swapon -s ## 查看交換分區,已經少了一個交換分區 Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2 [root@linuxprobe mnt]# free -h ## 查看刪除后交換分區大小,減少了2G total used free shared buffers cached Mem: 1.9G       1.9G        72M       9.2M       220K       1.1G -/+ buffers/cache:       761M       1.2G Swap: 7.0G         0B       7.0G [root@linuxprobe mnt]# swapoff /dev/sdb2 ## 刪除 /dev/sdb2分區 [root@linuxprobe mnt]# swapon -s ## 產看分區 Filename Type Size Used Priority /dev/dm-0                               partition       2097148 0       -1 [root@linuxprobe mnt]# free -h ## 減少了5G total used free shared buffers cached Mem: 1.9G       1.9G        76M       9.2M       220K       1.1G -/+ buffers/cache:       757M       1.2G Swap: 2.0G         0B       2.0G

 

刪除/dev/sdb2磁盤分區

[root@linuxprobe mnt]# cd /dev ## 切換目錄 [root@linuxprobe dev]# find sdb*  ## 查看當前的磁盤分區 sdb sdb1 sdb2 [root@linuxprobe dev]# fdisk /dev/sdb ## 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): p ## 查看,共兩個磁盤分區 Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System /dev/sdb1            2048     6293503     3145728   83 Linux /dev/sdb2         6293504    16779263     5242880   83 Linux Command (m for help): d ## 輸入刪除選項 Partition number (1,2, default 2): 2 ## 選擇2 Partition 2 is deleted Command (m for help): p ## 查看磁盤分區,已剩一個磁盤分區 Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System /dev/sdb1            2048     6293503     3145728   83 Linux 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 with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@linuxprobe dev]# pwd /dev [root@linuxprobe dev]# find sdb*  ## 查看 /dev/sdb磁盤分區 sdb sdb1 sdb2 [root@linuxprobe dev]# partprobe ## 刷新 Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only. [root@linuxprobe dev]# find sdb*  ## /dev/sdb2已經刪除 sdb sdb1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM