Linux下的存儲管理-swap分區和磁盤配額(RHEL8.0)

一.swap分區

1.swap作用

程序在運行時所有數據是在RAM。當RAM使用量超過了限額,爲了使系統更加穩定,我們在硬盤上劃分一部分空間來作內存緩衝區swap。
當內存使用超過限額,內核會把內存中閒置的數據存放到swap中
當程序需要swap分區中的數據時內核將swap分區中的數據在交還給內存進程處理

2.swap分區大小建議


說明:HIBERNATE是一個開源框架,做數據的插件。HIBERNATE開啓之後,會暫存系統的電源信息。開機之後會把電源信息交還給內存,使得開機速度變快

3.swap管理

①查看swap分區

[root@rhel8 ~]# swapon -s   ##查看swap分區信息
Filename		   Type		 Size	 Used	Priority
/dev/nvme0n1p2   partition	511996	  0	      -2

②創建swap分區
創建分區並設定分區的類型爲Linuxswap

[root@rhel8 ~]# fdisk /dev/sda 

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.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x034d90a2.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +100M

Created a new partition 1 of type 'Linux' and of size 100 MiB.

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): wq
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@rhel8 ~]# mkswap /dev/sda1  		##格式化設備位swap格式
Setting up swapspace version 1, size = 100 MiB (104853504 bytes)
no label, UUID=10cb10c6-fd54-4fc3-8871-bf084a991bf8
[root@rhel8 ~]# swapon -a /dev/sda1 	##激活
[root@rhel8 ~]# swapon -s	##查看swap信息,/dev/sda1優先級是-3
Filename		  Type		Size  Used	Priority
/dev/nvme0n1p2  partition	511996	0	-2
/dev/sda1       partition	102396	0	-3
[root@rhel8 ~]# swapoff /dev/sda1 		##卸載swap
[root@rhel8 ~]# swapon /dev/sda1 -p 1	##-p表示指定swap的優先級
[root@rhel8 ~]# swapon -s	##查看swap信息,/dev/sda1優先級1
Filename				Type		Size	Used    Priority
/dev/nvme0n1p2        partition	   511996	 0	     -2
/dev/sda1             partition	   102396	 0	      1

以上操作都是臨時操作

③永久添加swap分區

[root@rhel8 ~]# vim /etc/fstab 
/dev/sda1 	swap 	swap 	pri=4 	0 	0
[root@rhel8 ~]# swapon -a

④刪除swap

[root@rhel8 ~]# vim /etc/fstab 
/dev/sda1 swap swap pri=4 0 0 ##刪除此行
[root@rhel8 ~]# swapoff /dev/sda1 

二.磁盤配額

1.磁盤配額作用

設定用戶能寫入指定設備的最大額度

2.磁盤配額設定方法

①激活配額

mount /dev/sda1 /pub/ -o usrquota ##掛載設備並激活配額參數
quotaon -uv /dev/sda1 ##激活配額

②設定用戶配額

edquota -u tony ##設定用戶tony配額
Disk quotas for user tony (uid 1001):
設備 	用戶已經創建數據 軟限   硬限 創建文件個數 軟限 	硬限
Filesystem   blocks  	soft  hard   inodes   soft   hard
/dev/sda1      0      	 0    20480     0      0      0

③永久開啓配額

vim /etc/fstab
/dev/sda1 /pub xfs defaults,usrquota 0 0

測試:截取超過20M數據失敗,只能寫入20M數據

[root@rhel8 ~]# su - tony		##切換到tony用戶
[tony@rhel8 pub]$ dd if=/dev/zero of=/pub/tonyfile bs=1M count=40
dd: error writing '/pub/tonyfile': Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB, 20 MiB) copied, 0.00919345 s, 2.3 GB/s

④關閉配額

quotaoff -uv /dev/sda1
vim /etc/fstab ##去掉配額參數usrquota
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章