一、交換分區的建立與啟用:
交換分區是作為內存的一個輔助分區,在內存吃緊時,交換分區會作為內存的一部分使用,但是效率比起內存來說會很低下,在一個硬盤分區時最好使用柱面較小的柱面作為交換分區,因為在機械硬盤轉速一定的情況下,越靠近外面的磁道,線速度越大,讀取到的扇區越多,一定程度上有利于提高性能
交換分區需要在分區時指定分區類型為82,分區結束要同步磁盤,最后更改配置文件永久掛載,更改配置文件時可以給指定的分區指定優先級系統默認的優先級為-1開始每增加一個分區,新增加的分區優先級就-1,并且優先級越大,優先級越高,手工指定的優先級可以是0-32768,在配置文件當中default的位置寫優先級pri=#
分區類型和掛載點都是swap,其他的設置與普通分區使用沒有區別。最后改完配置,格式化為swap分區,并激活;
詳細步驟如下:
[root@cnode6_8 ~]# fdisk /dev/sdb //開始新建分區 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (15-2610, default 15): Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): +100M Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 82 //改變分區hex dode為82swap分區 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x4340940f Device Boot Start End Blocks Id System /dev/sdb1 1 14 112423+ fd Linux raid autodetect /dev/sdb2 15 28 112455 82 Linux swap / Solaris 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@cnode6_8 ~]# partx -a /dev/sdb //通知內核設備新增分區 BLKPG: Device or resource busy error adding partition 1 [root@cnode6_8 ~]# mkswap -L new_swap1 /dev/sdb2 //設定label,格式化分區 Setting up swapspace version 1, size = 112448 KiB LABEL=new_swap1, UUID=15c17561-d356-42f4-a5ba-b05d62d6260a [root@cnode6_8 ~]# vim /etc/fstab //添加配置信息永久添加swap [root@cnode6_8 ~]# tail -n1 /etc/fstab UUID=15c17561-d356-42f4-a5ba-b05d62d6260a swap swap defaults,pri=100 0 0 [root@cnode6_8 ~]# mount -a [root@cnode6_8 ~]# free -h total used free shared buffers cached Mem: 980M 294M 686M 216K 30M 119M -/+ buffers/cache: 143M 836M Swap: 2.0G 0B 2.0G 注:buffer存放元數據,cache存放普通緩沖數據 [root@cnode6_8 ~]# swapon -a //激活所有swap分區 [root@cnode6_8 ~]# free -h total used free shared buffers cached Mem: 980M 294M 685M 216K 30M 119M -/+ buffers/cache: 144M 836M Swap: 2.1G 0B 2.1G [root@cnode6_8 ~]# cat /proc/swaps //可以看到指定的優先級100生效 Filename Type Size Used Priority /dev/sda5 partition 2097148 0 -1 /dev/sdb2 partition 112448 0 100 [root@cnode6_8 ~]# swapoff /dev/sdb2 //關閉新建的交換分區 [root@cnode6_8 ~]# cat /proc/swaps Filename Type Size Used Priority /dev/sda5 partition 2097148 0 -1 [root@cnode6_8 ~]# vim /etc/fstab //刪除添加的表項 [root@cnode6_8 ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): d //刪除交換分區的分區 Partition number (1-4): 2 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@cnode6_8 ~]# partx -d /dev/sdb //通知內核同步分區 error deleting partition 1: BLKPG: Device or resource busy error deleting partitions 3-256: BLKPG: No such device or address [root@cnode6_8 ~]# lsblk /dev/sdb //已經徹底刪除分區 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 109.8M 0 part └─md0 9:0 0 217M 0 raid5 [root@cnode6_8 ~]# swapon -s //通過swapon -s查看交換分區信息 Filename Type Size Used Priority /dev/sda5 partition 2097148 0 -1
二、磁盤配額:
quota就是限額的意思,對于linux來說,linux系統是多任務多用戶的操作系統,如果一個用戶使用磁盤過多,必然會導致其他用戶使用磁盤額度收到限制,quota就是針對這種情況應用而生的,但是quota對配額時必須是一個獨立的分區才可以,并且是僅僅針對普通用戶有效,對于root是無效的。
下面將針對家目錄不是某一個獨立的分區,新建分區后對家目錄進行遷移至獨立分區。quota的限制既可以針對用戶,也可以針對針對組來做,可以限制總文件的大小,也可以限制iNode數量來限制。在配置文件當中,有一個soft和hard,這兩個值軟限制值一般小于應限制值,當用戶數據大于soft小于hard時,系統會在用戶每次登陸系統都報警,提示用戶清理磁盤,默認有一個寬限值7天,如果在這7天,用戶都沒有進行清理,soft的值將會取代hard值,用戶將無法建立新文件。主要用途一般被用來例如網頁服務器的每個人需要一定的空間。郵件服務器每個人郵箱都有固定的大小等。
具體示例:(關于分區格式化再次不在贅述格式化分區為/dev/sdb2)
[root@cnode6_8 ~]# mkdir /mnt/home //創建臨時掛載點,提供將原來/home數據復制至新建的分區 [root@cnode6_8 ~]# mount /dev/sdb2 /mnt/home //掛載臨時掛載點 [root@cnode6_8 ~]# cp -Rp /home/* /mnt/home/ //遞歸保留權限復制數據 [root@cnode6_8 ~]# ll /mnt/home/ total 8 drwx------. 4 jack jack 4096 Jul 25 17:22 jack drwx------. 4 rose rose 4096 Jul 25 17:24 rose [root@cnode6_8 ~]# vim /etc/fstab [root@cnode6_8 ~]# tail -n1 /etc/fstab UUID="424b6201-3ee2-4a3b-a288-75e91f52f71b" /home ext4 defaults,usrquota,grpquota 0 0 //fstab文件配置 [root@cnode6_8 ~]# mount -a //掛載分區 [root@cnode6_8 ~]# mount |grep "sdb2" /dev/sdb2 on /home type ext4 (rw,usrquota,grpquota) [root@cnode6_8 ~]# mount |grep "sdb2" /dev/sdb2 on /home type ext4 (rw,usrquota,grpquota) [root@cnode6_8 ~]# quotacheck -cug /home/ quotacheck: Mountpoint (or device) /home not found or has no quota enabled. quotacheck: Cannot find filesystem to check or filesystem not mounted with quota option. //提示掛載選項沒有生效 [root@cnode6_8 ~]# mount -o remount /home //重新掛載 [root@cnode6_8 ~]# mount |grep sdb2 /dev/sdb2 on /home type ext4 (rw,usrquota,grpquota) [root@cnode6_8 ~]# quotacheck -cug /home //重新生成配額數據庫文件 quotacheck: Cannot create new quotafile /home/aquota.user.new: Permission denied //因為selinux的原因創建數據庫文件失敗 quotacheck: Cannot initialize IO on new quotafile: Permission denied quotacheck: Cannot create new quotafile /home/aquota.group.new: Permission denied quotacheck: Cannot initialize IO on new quotafile: Permission denied [root@cnode6_8 ~]# setenforce 0 //臨時關閉selinux [root@cnode6_8 ~]# vim /etc/selinux/config //永久改變selinux狀態 [root@cnode6_8 ~]# grep disable /etc/selinux/config # disabled - No SELinux policy is loaded. SELINUX=disabled //selinux配置文件修改項 [root@cnode6_8 ~]# quotacheck -cug /home [root@cnode6_8 ~]# ll /home //查看用戶和組數據庫文件,是否已經建立 total 24 -rw-------. 1 root root 7168 Jul 28 01:16 aquota.group -rw-------. 1 root root 7168 Jul 28 01:16 aquota.user drwx------. 4 jack jack 4096 Jul 25 17:22 jack drwx------. 4 rose rose 4096 Jul 25 17:24 rose [root@cnode6_8 ~]# quotaon /home //啟用磁盤配額功能,如需關閉quotaoff [root@cnode6_8 ~]# edquota jack //編輯jack的quota限制,此處進入配置后有兩個狀態值是不能修改的分別是blocks和inodes 本別表示當前系統使用了多少k(此處的blocks是以kb為單位!切記)和使用了多少iNode iNodes設置為0 0表示沒有設置iNode限制 [root@cnode6_8 ~]# edquota -p jack rose //可以使用jack為模板為rose設置quota,也可以使用setquota 加用戶名設置 [root@cnode6_8 ~]# setquota user1 100000 200000 0 0 /home [root@cnode6_8 ~]# quota jack //查看jack的quota設置 Disk quotas for user jack (uid 500): Filesystem blocks quota limit grace files quota limit grace /dev/sdb2 40 50000 100000 11 0 0 [root@cnode6_8 ~]# repquota /home //查看/home分區設置的用戶磁盤限制 *** Report for user quotas on device /dev/sdb2 Block grace time: 7days; Inode grace time: 7days Block limits File limits User used soft hard grace used soft hard grace ---------------------------------------------------------------------- root -- 4 0 0 1 0 0 jack -- 40 50000 100000 11 0 0 rose -- 32 50000 100000 8 0 0 user1 -- 32 100000 200000 8 0 0 [root@cnode6_8 ~]# su - jack [jack@cnode6_8 ~]$ cd /home/home //進入jack家目錄做測試 [jack@cnode6_8 ~]$ dd if=/dev/zero of=jack_zero bs=1M count=50 sdb2: warning, user block quota exceeded. //超過50M報警 50+0 records in 50+0 records out 52428800 bytes (52 MB) copied, 1.22038 s, 43.0 MB/s [jack@cnode6_8 ~]$ dd if=/dev/zero of=jack_zero2 bs=1M count=50 sdb2: write failed, user block limit reached. //再次創建50M提示失敗 dd: writing `jack_zero2': Disk quota exceeded 48+0 records in 47+0 records out 49930240 bytes (50 MB) copied, 0.685798 s, 72.8 MB/s [jack@cnode6_8 ~]$ quota jack //查看jack的quota,寬限期已經變為6天 Disk quotas for user jack (uid 500): Filesystem blocks quota limit grace files quota limit grace /dev/sdb2 100000* 50000 100000 6days 13 0 0 [jack@cnode6_8 ~]$ du -sh //jack的家目錄一共有的數據 98M .
原創文章,作者:jack_cui,如若轉載,請注明出處:http://www.www58058.com/41330