交换分区创建和磁盘配额的使用

一、交换分区的建立与启用:

    交换分区是作为内存的一个辅助分区,在内存吃紧时,交换分区会作为内存的一部分使用,但是效率比起内存来说会很低下,在一个硬盘分区时最好使用柱面较小的柱面作为交换分区,因为在机械硬盘转速一定的情况下,越靠近外面的磁道,线速度越大,读取到的扇区越多,一定程度上有利于提高性能

     交换分区需要在分区时指定分区类型为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.178linux.com/41330

(0)
上一篇 2016-08-29 08:45
下一篇 2016-08-29 08:45

相关推荐

  • awk命令详解

    简介: wak是一个强大的文本分析工具,也可以叫做报告生成工具。相对于grep的查找,sed的编辑,awk在对其数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格 为默认分隔符将每行切片,切开的部分再进行各种分析处理。 awk有三个不同的版本:awk,nawk,gawk。我们目前所使用的awk一般指gawk,gawk是AWK和GN…

    Linux干货 2016-11-28
  • select基本用法和软件包管理

    一、select语句     select语句在脚本中用于创建菜单,按数字顺序排列的菜单项显示在标准输出上,并用PS3提示符,等待用户输入。用户输入菜单中的某个数字,执行相应的命令,并被保存在内置变量REPLY中。 用法:     select NAME [in WORDS &#…

    Linux干货 2016-08-21
  • Linux网络管理命令的使用

    网络管理命令 ip命令 配置Linux网络属性:ip命令,不过该命令的操作只是临时操作,重启以后配置丢失 ip-show / manipulate routing, devices, policy routing and tunnels ip [ OPTIONS ] OBJECT { COMMAND | help }    &n…

    Linux干货 2016-09-11
  • N21沉舟第七周作业

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; mke2fs -t ex…

    Linux干货 2016-08-22
  • LVM逻辑卷管理

    前言     通常情况下,在操作系统上新建了一个分区并在此分区创建文件系统后,该文件系统的大小就固定了。假如要增加此文件系统的大小,我们不得不添加一块硬盘并重新分区,创建文件系统,然后把原文件系统的数据完整复制过来。如果第二次分区时分配的空间太大,用不完又浪费该怎么办呢?重复此前的流程又将花费大量的时间,有没有更便捷的…

    Linux干货 2015-05-04