磁盘划分——(下)

  • 添加交换分区
  • 磁盘容量配额
  • 软硬方式链接

部署SWAP交换分区、配置quota磁盘配额服务,以及掌握ln命令带来的软硬链接。希望各位会对Linux系统的磁盘存储以及文件系统有深入的理解

2.1 添加交换分区

SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。

交换分区的创建过程与前文讲到的挂载并使用存储设备的过程非常相似。在分区创建完毕后保存并退出即可:

[root@centos ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xb3d27ce1.
##输入n进行选择
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
##此处查看信息输入P
e extendedSelect (default p): p
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): ##此处敲击回车
Using default value 4196352
##设置大小为5G
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
##设置完毕 输入p查看信息
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xb0ced57f
 Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 14682111 5242880 83 Linux
##此处输入W表示保存
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.

使用SWAP分区专用的格式化命令mkswap,对新建的主分区进行格式化操作:

[root@centos ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=2972f9cb-17f0-4113-84c6-c64b97c40c75

使用swapon命令把准备好的SWAP分区设备正式挂载到系统中。我们可以使用free -m命令查看交换分区的大小变化(由2047MB增加到7167MB)

[root@centos ~]# free -m
total used free shared buffers cached
Mem: 1483 782 701 9 0 254
-/+ buffers/cache: 526 957
Swap: 2047 0 2047
[root@linuxprobe ~]# swapon /dev/sdb2
[root@linuxprobe ~]# free -m
total	 used 	free 	shared 	buffers 	cached  -/+ buffers/cache
Mem:	 1483 	785		 697	 9 			0		 254  530   953
Swap: 7167 0 7167

为了能够让新的交换分区设备在重启后依然生效,需要按照下面的格式将相关信息写入到配置文件中,并记得保存:

[root@centos ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0 
/dev/sdb2 swap swap defaults 0 0

2.2 磁盘容量配额

使用磁盘容量配额服务来限制某位用户或某个用户组针对特定文件夹可以使用的最大硬盘空间或最大文件个数,一旦达到这个最大值就不再允许继续使用

但是我们可以使用quota命令进行磁盘容量配额管理,从而限制用户的硬盘可用容量或所能创建的最大文件个数。quota命令还有软限制和硬限制的功能

名称

内容

软限制

当达到软限制时会提示用户,但仍允许用户在限定的额度内继续使用。

硬限制

当达到硬限制时会提示用户,且强制终止用户的操作

若已经安装了quota磁盘容量配额服务程序包,在/etc/fstab文件中开启对quota的支持,需要手动配置文件,让系统中的/root能够支持quota磁盘容量配额服务技术

[root@centos ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults,uquota 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0 
/dev/sdb1 /newFS xfs defaults 0 0 
/dev/sdb2 swap swap defaults 0 0 
[root@linuxprobe ~]# reboot
[root@linuxprobe ~]# mount | grep boot
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)

接下来做一个检查quota磁盘容量配额效果的测试,创建一个用户ahei,并且针对/root目录来加入其他人的写权限,保证用户能够正常写入数据

[root@centos ~]# useradd ahei
[root@centos ~]# chmod -Rf o+w /boot
2.2.1 xfs_quota命令

xfs_quota命令是专门针对XFS文件系统来管理quota磁盘容量配额服务而设计的命令,格式:xfs_quota [参数] 配额 文件系统

注释:-c参数用于以参数的形式设置要执行的命令;-x参数是专家模式

如下,用quota命令设置ahei用户对/root目录下的具体容量配额
硬盘限额:硬盘使用量的软限制和硬限制分别为3MB和6MB
数量限额:创建文件数量的软限制和硬限制分别为3个和6个

[root@centos ~]# xfs_quota -x -c 'limit bsoft=3m bhard=6m isoft=3 ihard=6 ahei' /boot
[root@centos ~]# xfs_quota -x -c report /boot
User quota on /boot (/dev/sda1)   Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 95084 0 0 00 [--------]
tom 0 3072 6144 00 [--------]

切换到该用户下,分别创建一个5M 8M的文件来测试

[root@centos ~]# su - tom
##创建5M的文件
[tom@centos ~]$ dd if=/dev/zero of=/boot/ahei bs=5M count=1
1+0 records in
1+0 records out
5242880 bytes (5.2 MB) copied, 0.123966 s, 42.3 MB/s
##创建8M的文件
[tom@centos ~]$ dd if=/dev/zero of=/boot/ahei bs=8M count=1
####可以发现被限制了,无法创建成功

> dd: error writing ‘/boot/ahei’: Disk quota exceeded

1+0 records in
0+0 records out
6291456 bytes (6.3 MB) copied, 0.0201593 s, 312 MB/s
2.2.2 edquota命令

edquota命令用于编辑用户的quota配额限制,格式为“edquota [参数] [用户] ”

在为用户设置了quota磁盘容量配额限制后,可以使用edquota命令按需修改限额的数值

参数

描述

-u

表示要针对哪个用户进行设置

-g

表示要针对哪个用户组进行设置|

下面把我们上边添加的用户ahei的硬盘使用量的硬限额从5MB提升到8MB:

[root@centos ~]# edquota -u tom
Disk quotas for user tom (uid 1001):
 Filesystem blocks soft hard inodes soft hard
 /dev/sda1 6144 3072 8192 1 3 6
 ##切换用户
[root@centos ~]# su - tom
Last login: Mon Sep 7 16:43:12 CST 2017 on pts/0
##创建8M的文件
[tom@centos ~]$ dd if=/dev/zero of=/boot/tom bs=8M count=1
1+0 records in
1+0 records out
8388608 bytes (8.4 MB) copied, 0.0268044 s, 313 MB/s
##创建10M
[tom@centos ~]$ dd if=/dev/zero of=/boot/tom bs=10M count=1
dd: error writing ‘/boot/tom’: Disk quota exceeded
1+0 records in
0+0 records out
8388608 bytes (8.4 MB) copied, 0.167529 s, 50.1 MB/s

2.3 软硬方式链接

硬链接(hard link):可以将它理解为一个“指向原始文件inode的指针”,系统不为它分配独立的inode和文件。所以,硬链接文件与原始文件其实是同一个文件,只是名字不同。我们每添加一个硬链接,该文件的inode连接数就会增加1;而且只有当该文件的inode连接数为0时,才算彻底将它删除。换言之,由于硬链接实际上是指向原文件inode的指针,因此即便原始文件被删除,依然可以通过硬链接文件来访问。需要注意的是,由于技术的局限性,我们不能跨分区对目录文件进行链接。

软链接(也称为符号链接[symbolic link]):仅仅包含所链接文件的路径名,因此能链接目录文件,也可以跨越文件系统进行链接。但是,当原始文件被删除后,链接文件也将失效,从这一点上来说与Windows系统中的“快捷方式”具有一样的性质。

In命令

n命令用于创建链接文件,格式为“ln [选项] 目标,参数如下表

参数

作用

-s

创建“符号链接”(如果不带-s参数,则默认创建硬链接)

-f

-i

覆盖前先询问

-v

显示创建链接的过程

演示效果:创建一个一个文件,创建软连接,测试当删除原文件后,是否可以读取新建的链接文件

[root@cetnos ~]# echo "hhhhhhhhhhhhh" > readme.txt
[root@cetnos ~]# ln -s readme.txt readit.txt
[root@cetnos ~]# cat readme.txt 
hhhhhhhhhhhhh
[root@cetnos ~]# cat readit.txt 
hhhhhhhhhhhhh
[root@cetnos ~]# ls -l readme.txt 
#####可以看到原始文件的硬盘链接数量为  1

> -rw-r--r-- 1 root root 26 Jan 11 00:08 readme.txt

[root@cetnos ~]# rm -f readme.txt 
[root@cetnos ~]# cat readit.txt 
cat: readit.txt: No such file or directory

如下所示,创建一个硬链接

针对原始文件的硬盘存储位置创建了一个指针,新创建的这个硬链接就不再依赖于原始文件的名称等信息,也不会因为原始文件的删除而导致无法读取

[root@centos ~]# echo "999999999" > readme.txt
[root@centos ~]# ln readme.txt readit.txt
[root@centos ~]# cat readme.txt 
999999999
[root@centos ~]# cat readit.txt 
999999999
[root@centos ~]# ls -l readme.txt 
-rw-r--r-- 2 root root 26 Jan 11 00:13 readme.txt
[root@centos ~]# rm -f readme.txt 
[root@centos ~]# cat readit.txt 
999999999

以上就是自学存储结构与磁盘划分所了解到的,如有纰漏请指出