CentOS7的swap分區(qū)管理(分區(qū)或文件)-創(chuàng)新互聯(lián)

CentOS7的swap分區(qū)管理(分區(qū)或文件)
1、swap可以在安裝操作系統(tǒng)的時(shí)候劃分單獨(dú)分區(qū)創(chuàng)建;也可以安裝好操作系統(tǒng)后劃分剩余硬盤創(chuàng)建;還可以創(chuàng)建一個(gè)文件當(dāng)swap分區(qū)使用

我們提供的服務(wù)有:成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、瑤海ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的瑤海網(wǎng)站制作公司

2、建議創(chuàng)建一個(gè)新的分區(qū)當(dāng)swap使用,如果想使用一個(gè)文件當(dāng)swap分區(qū)用的話最好將文件放在固態(tài)硬盤上,使用文件充當(dāng)swap的話可以隨意變大變小,移動(dòng)也方便,但性能不如分區(qū)好

創(chuàng)建分區(qū)來劃分swap
一、使用free -h查看當(dāng)前swap

當(dāng)前系統(tǒng)有4G的swap空間

[root@centos7 ~]# free -h
total used free shared buff/cache available
Mem: 1.9G 180M 1.2G 19M 545M 1.6G
Swap: 4.0G 12M 4.0G
使用swapon -s顯示當(dāng)前系統(tǒng)的swap

[root@centos7 ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-0 partition 419430012676 -2
二、使用lsblk查看當(dāng)前系統(tǒng)的硬盤

可以看到,sda總共200G,現(xiàn)在才劃分了150多G,還有可以用的空間

[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 /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 4G 0 part
└─centos-swap
253:0 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb2 8:18 0 2G 0 part
三、使用fdisk對(duì)/dev/sda硬盤進(jìn)行創(chuàng)建分區(qū)操作

[root@centos7 ~]# fdisk /dev/sda
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): n
All primary partitions are in use
Adding logical partition 6
First sector (35072896-419430399, default 325072896):
Using default value 325072896
Last sector, +sectors or +size{K,M,G} (325072896-419430399, default 419430399): +2G
Partition 6 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sda: 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: 0x000f14f7

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 316671999 52428800 83 Linux
/dev/sda4 316672000 419430399 51379200 5 Extended
/dev/sda5 316674048 325070847 4198400 8e Linux LVM
/dev/sda6 325072896 329267199 2097152 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.
四、執(zhí)行兩下partprobe(CentOS6執(zhí)行part /dev/sda -a)

[root@centos7 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.
Warning: Unable to open /dev/sr1 read-write (Read-only file system). /dev/sr1 has been opened read-only.
[root@centos7 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.
Warning: Unable to open /dev/sr1 read-write (Read-only file system). /dev/sr1 has been opened read-only.
五、創(chuàng)建文件系統(tǒng)

[root@centos7 ~]# ls /dev/sda6
/dev/sda6
[root@centos7 ~]# mkswap /dev/sda6
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=b98996a2-a548-4266-b636-b2cb75cd0626
六、寫入/etc/fstab,并mount -a測(cè)試

[root@centos7 ~]# blkid /dev/sda6
/dev/sda6: UUID="b98996a2-a548-4266-b636-b2cb75cd0626" TYPE="swap"
[root@centos7 ~]# vim /etc/fstab
[root@centos7 ~]# tail -1 /etc/fstab
UUID=b98996a2-a548-4266-b636-b2cb75cd0626 swap swap defaults 0 0

被掛載的設(shè)備名 掛載點(diǎn) 文件系統(tǒng)類型 掛載選項(xiàng) 轉(zhuǎn)儲(chǔ)頻率 系統(tǒng)自檢選項(xiàng)

[root@centos7 ~]# mount -a
七、swapon -a

此時(shí)執(zhí)行free -h,swap分區(qū)還沒顯示出來,執(zhí)行swapon -a,啟用所有swap

[root@centos7 ~]# free -h
total used free shared buff/cache available
Mem: 1.9G 176M 1.2G 19M 547M 1.6G
Swap: 4.0G 12M 4.0G

執(zhí)行swapon -a

[root@centos7 ~]# swapon -a
[root@centos7 ~]# free -h
total used free shared buff/cache available
Mem: 1.9G 178M 1.2G 19M 547M 1.6G
Swap: 6.0G 12M 6.0G
八、卸載這個(gè)swap

使用swapoff關(guān)閉/dev/sda6這個(gè)swap分區(qū)

[root@centos7 ~]# swapoff /dev/sda6

使用sed刪除fstab最后加的一行

[root@centos7 ~]# sed -ri '$d' /etc/fstab

mount -a

[root@centos7 ~]# mount -a

此時(shí)swap已變成原來的4G

創(chuàng)建一個(gè)文件充當(dāng)swap分區(qū)
一、使用dd命令創(chuàng)建一個(gè)文件100M,當(dāng)swap用

[root@centos7 data]# dd if=/dev/zero of=/data/swapfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 2.74665 s, 38.2 MB/s

swap文件權(quán)限修改為600,安全

[root@centos7 data]# chmod 600 swapfile
二、對(duì)該文件創(chuàng)建文件系統(tǒng)

[root@centos7 data]# mkswap swapfile
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=a16538e7-685e-4141-aaeb-e1b7896cffdf
三、寫入/etc/fstab,掛載(這里要注意,掛載設(shè)備不能寫UUID,只能寫文件名)

[root@centos7 data]# tail -1 /etc/fstab
/data/swapfile swap swap defaults 0 0
[root@centos7 data]# mount -a
四、執(zhí)行swapon -a 開啟所有swap

[root@centos7 data]# swapon -a
[root@centos7 data]# swapon -s
Filename Type Size Used Priority
/dev/dm-0 partition 4194300 12672 -2
/data/swapfile file 102396 0 -3
[root@centos7 data]# free -h
total used free shared buff/cache available
Mem: 1.9G 176M 1.1G 19M 650M 1.6G
Swap: 4.1G 12M 4.1G
五、增大swap,需要先取消掛載

必須先卸載,不然報(bào)錯(cuò)了

[root@centos7 data]# dd if=/dev/zero of=/data/swapfile bs=1M count=200
dd: failed to open ‘/data/swapfile’: Text file busy

卸載

[root@centos7 data]# swapoff /data/swapfile
[root@centos7 data]# dd if=/dev/zero of=/data/swapfile bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 3.23377 s, 64.9 MB/s

創(chuàng)建文件系統(tǒng)

[root@centos7 data]# mkswap swapfile
Setting up swapspace version 1, size = 204796 KiB
no label, UUID=79d0f287-c998-42d3-aabc-b3b2d60b9cb2

執(zhí)行swapon -a啟用所有swap

[root@centos7 data]# swapon -a
[root@centos7 data]# free -h
total used free shared buff/cache available
Mem: 1.9G 176M 1.0G 19M 752M 1.6G
Swap: 4.2G 12M 4.2G
六、卸載

取消掛載

[root@centos7 data]# swapoff /data/swapfile
[root@centos7 data]# sed -ri '$d' /etc/fstab
[root@centos7 data]# mount -a
[root@centos7 data]# rm -f swapfile
[root@centos7 data]# free -h
total used free shared buff/cache available
Mem: 1.9G 176M 1.2G 19M 547M 1.6G
Swap: 4.0G 12M 4.0G

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站標(biāo)題:CentOS7的swap分區(qū)管理(分區(qū)或文件)-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://bm7419.com/article8/disoop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、企業(yè)建站網(wǎng)頁設(shè)計(jì)公司、定制開發(fā)、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)