一、交换分区的建立与启用:
交换分区是作为内存的一个辅助分区,在内存吃紧时,交换分区会作为内存的一部分使用,但是效率比起内存来说会很低下,在一个硬盘分区时最好使用柱面较小的柱面作为交换分区,因为在机械硬盘转速一定的情况下,越靠近外面的磁道,线速度越大,读取到的扇区越多,一定程度上有利于提高性能
交换分区需要在分区时指定分区类型为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