一、实验需求

1.添加四块硬盘,建立RAID5环境(md5),三主一备。 2.将md5使用LVM做成VG01组,在该VG中新建了两个LV。 3.将这两个LV格式化为ext4\xfs,开机自动挂载到系统mnt1,mnt2目录下 4.开启磁盘配额功能,用来进行用户与组分配额的实验 5.在系统中添加用户tom,lisa并加入caiwu组中 6.对组和用户同时设置磁盘配额,分别切换至永华目录下写文件,验证生效优先关系。

二、实验步骤

** 1、打开虚拟机VMware添加四块硬盘**

** 2、查看添加的硬盘是否生效**

[root@localhost ~]# fdisk -l \\查看添加的磁盘是否生效

磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x000c1bf5

设备 Boot Start End Blocks Id System /dev/sda1 * 2048 9766911 4882432 83 Linux /dev/sda2 9766912 39063551 14648320 83 Linux /dev/sda3 39063552 48828415 4882432 82 Linux swap / Solaris /dev/sda4 48828416 83886079 17528832 5 Extended /dev/sda5 48830464 58595327 4882432 83 Linux /dev/sda6 58597376 83886079 12644352 83 Linux

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/sdc:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/sdd:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/sde:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节

3、对磁盘进行分区

[root@localhost ~]# fdisk /dev/sdb 欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。

Device does not contain a recognized partition table 使用磁盘标识符 0xf20d7e28 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助):w The partition table has been altered!

Calling ioctl() to re-read partition table. 正在同步磁盘。

[root@localhost ~]# fdisk /dev/sdc 欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。

Device does not contain a recognized partition table 使用磁盘标识符 0x3c5be687 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助): 命令(输入 m 获取帮助): 命令(输入 m 获取帮助):w The partition table has been altered!

Calling ioctl() to re-read partition table. 正在同步磁盘。

[root@localhost ~]# fdisk /dev/sdd 欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。

Device does not contain a recognized partition table 使用磁盘标识符 0x2bbb35fc 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助):w The partition table has been altered!

Calling ioctl() to re-read partition table. 正在同步磁盘。

[root@localhost ~]# fdisk /dev/sde 欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。

Device does not contain a recognized partition table 使用磁盘标识符 0xa0797fd4 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助): 命令(输入 m 获取帮助):w The partition table has been altered!

Calling ioctl() to re-read partition table. 正在同步磁盘。

4、建立RAID5环境

[root@localhost ~]# mdadm -Cv /dev/md5 -l5 -n3 -x1 /dev/sd[b-e] \\三主一备 mdadm: layout defaults to left-symmetric mdadm: layout defaults to left-symmetric mdadm: chunk size defaults to 512K mdadm: partition table exists on /dev/sdb mdadm: partition table exists on /dev/sdb but will be lost or meaningless after creating array mdadm: partition table exists on /dev/sdc mdadm: partition table exists on /dev/sdc but will be lost or meaningless after creating array mdadm: partition table exists on /dev/sdd mdadm: partition table exists on /dev/sdd but will be lost or meaningless after creating array mdadm: partition table exists on /dev/sde mdadm: partition table exists on /dev/sde but will be lost or meaningless after creating array mdadm: size set to 20955136K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started. [root@localhost ~]# cat /proc/mdstat \\查看md5的状态 Personalities : [raid6] [raid5] [raid4] md5 : active raid5 sdd[4] sde3 sdc[1] sdb[0] 41910272 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [UU_] [=========>...........] recovery = 49.0% (10276096/20955136) finish=0.8min speed=200197K/sec

unused devices: <none> [root@localhost ~]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] md5 : active raid5 sdd[4] sde3 sdc[1] sdb[0] 41910272 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>

[root@localhost ~]# mdadm -vDs /dev/md5 \\查看md5的详细信息 ARRAY /dev/md5 level=raid5 num-devices=3 metadata=1.2 spares=1 name=localhost.localdomain:5 UUID=86676ad1:377ccb12:fb0520b0:74147ede devices=/dev/sdb,/dev/sdc,/dev/sdd,/dev/sde

5、对md5做LVM

[root@localhost ~]# pvcreate /dev/md5 \\创建物理卷 Physical volume "/dev/md5" successfully created. [root@localhost ~]# vgcreate VG01 /dev/md5 \\创建卷组 Volume group "VG01" successfully created [root@localhost ~]# lvcreate -L 15GB -n LV01 VG01 \\创建逻辑卷LV01并且分配空间 Logical volume "LV01" created. [root@localhost ~]# lvcreate -L 15GB -n LV02 VG01 \\创建逻辑卷LV02 Logical volume "LV02" created. [root@localhost ~]# mkfs.ext4 /dev/VG01/LV01 \\将LV01格式化成ext4 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 983040 inodes, 3932160 blocks 196608 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=2151677952 120 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成

[root@localhost ~]# mkfs.xfs /dev/VG01/LV02 \\将LV02格式化成xfs格式 meta-data=/dev/VG01/LV02 isize=512 agcount=16, agsize=245632 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=3930112, imaxpct=25 = sunit=128 swidth=256 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=8 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0

6、挂载

[root@localhost ~]# mkdir /mnt1 /mnt2
[root@localhost ~]# vi /etc/fstab  \\配置挂载
# /etc/fstab
# Created by anaconda on Wed Sep 12 10:01:17 2018
#
# 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
#

UUID=15361c2d-d2c9-4e8d-bc16-be2805b35791 / xfs defaults 0 0 UUID=24fd8eb1-045e-4e3d-8829-3d5229c1b64e /boot xfs defaults 0 0 UUID=357ff8f8-ec6f-404a-acbf-4fd28722c058 /home xfs defaults 0 0 UUID=26dd9d73-2195-4863-a981-a6ea1967d70a /var xfs defaults 0 0 UUID=f4b2e122-3d0a-48cf-9fd3-235fa8eeb1aa swap swap defaults 0 0 /dev/cdrom /mnt iso9660 ro 0 0 /dev/VG01/LV01 /mnt1 ext4 defaults,usrquota,grpquota 0 0 \将逻辑卷挂载并且指定磁盘配额 /dev/VG01/LV02 /mnt2 xfs defaults,usrquota,grpquota 0 0

[root@localhost ~]# mount -a  \\重启挂载点
[root@localhost ~]# df -h	\\查看挂载是否成功

文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 14G 882M 14G 7% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sr0 4.3G 4.3G 0 100% /mnt /dev/sda6 13G 33M 13G 1% /home /dev/sda5 4.7G 119M 4.6G 3% /var /dev/sda1 4.7G 119M 4.6G 3% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/VG01-LV01 15G 41M 14G 1% /mnt1 /dev/mapper/VG01-LV02 15G 33M 15G 1% /mnt2

7、开启磁盘配额功能

[root@localhost ~]# quotacheck -cvug /dev/VG01/LV01 \\ext4在开启之前要 quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown. quotacheck: Scanning /dev/mapper/VG01-LV01 [/mnt1] done quotacheck: Cannot stat old user quota file /mnt1/aquota.user: 没有那个文件或目录. Usage will not be subtracted. quotacheck: Cannot stat old group quota file /mnt1/aquota.group: 没有那个文件或目录. Usage will not be subtracted. quotacheck: Cannot stat old user quota file /mnt1/aquota.user: 没有那个文件或目录. Usage will not be subtracted. quotacheck: Cannot stat old group quota file /mnt1/aquota.group: 没有那个文件或目录. Usage will not be subtracted. quotacheck: Checked 2 directories and 0 files quotacheck: Old file not found. quotacheck: Old file not found.

[root@localhost ~]# quotaon /mnt1  \\开启磁盘配额
[root@localhost ~]# quotaon -p /dev/VG01/LV01	  \\检查磁盘配额是否开启

group quota on /mnt1 (/dev/mapper/VG01-LV01) is on user quota on /mnt1 (/dev/mapper/VG01-LV01) is on [root@localhost ~]# quotaon /mnt2 \xfs可直接开启 [root@localhost ~]# quotaon -p /dev/VG01/LV02 \检查是否开启成功 group quota on /mnt2 (/dev/mapper/VG01-LV02) is on user quota on /mnt2 (/dev/mapper/VG01-LV02) is on

8、为用户开启磁盘配额

[root@localhost ~]# groupadd caiwu	\\添加caiwu组
[root@localhost ~]# useradd tom -g caiwu	 \\新建tom用户并加入到caiwu组中
[root@localhost ~]# passwd tom 

更改用户 tom 的密码 。 新的 密码: 无效的密码: 密码少于 8 个字符 重新输入新的 密码: passwd:所有的身份验证令×××已经成功更新。

[root@localhost ~]# useradd lisa -g caiwu	\\新建lisa用户加入到caiwu组
[root@localhost ~]# passwd lisa

更改用户 lisa 的密码 。 新的 密码: 无效的密码: 密码少于 8 个字符 重新输入新的 密码: passwd:所有的身份验证令×××已经成功更新。 [root@localhost ~]# edquota -u tom \\为用户tom开启磁盘配额 Disk quotas for user tom (uid 1000): Filesystem blocks soft hard inodes soft hard /dev/mapper/VG01-LV01 2048 1024 2048 3 1 2 /dev/mapper/VG01-LV02 0 0 0 0 0 0 [root@localhost ~]# edquota -u tom \为用户lisa开启磁盘配额 Disk quotas for user lisa (uid 1001): Filesystem blocks soft hard inodes soft hard /dev/mapper/VG01-LV01 4096 1024 4096 4 1 4 /dev/mapper/VG01-LV02 0 0 0 0 0 0

9、验证开启磁盘配额是否生效

[root@localhost /]# chmod 777 /mnt1   \\改变文件夹的权限
[root@localhost /]#su tom		\\切换到tom用户
[tom@localhost mnt]$ cd /mnt1
[tom@localhost mnt1]$ dd if=/dev/zero of=tom1.txt bs=1M count=1	  \\添加一个1MB的文件

记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00334678 秒,313 MB/秒 [tom@localhost mnt1]$ dd if=/dev/zero of=tom2.txt bs=1M count=1 \添加第二个文件 dm-0: warning, user file quota exceeded. dm-0: warning, user block quota exceeded. 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.0029935 秒,350 MB/秒

[tom@localhost mnt1]$ dd if=/dev/zero of=tom3.txt bs=1M count=1	   \\添加第三个文件失败
dm-0: write failed, user block limit reached.

dd: 写入"tom3.txt" 出错: 超出磁盘限额 记录了1+0 的读入 记录了0+0 的写出 0字节(0 B)已复制,0.00107794 秒,0.0 kB/秒 [tom@localhost mnt1]$ su lisa \切换到lisa用户 密码:

[lisa@localhost mnt1]$ cd /mnt1
[lisa@localhost mnt1]$ dd if=/dev/zero of=lisa1.txt bs=1M count=1	\\添加一个1MB的文件

记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00408002 秒,257 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa2.txt bs=1M count=1 \\添加第二个文件 dm-0: warning, user file quota exceeded. dm-0: warning, user block quota exceeded. 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00293226 秒,358 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa3.txt bs=1M count=1 \\添加第三个文件 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.0035511 秒,295 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa4.txt bs=1M count=1 \\添加第四个文件 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00359935 秒,291 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa5.txt bs=1M count=1 \\添加第五个文件失败 dm-0: write failed, user file limit reached. dd: 打开"lisa5.txt" 失败: 超出磁盘限额

10、为组配置配磁盘配额并且验证生效的优先关系

通过lisa用户验证(lisa可以存4个大小为1M的文件)

[root@localhost /]# edquota -g caiwu \\为财务组配置磁盘配额 Disk quotas for group caiwu (gid 1000): Filesystem blocks soft hard inodes soft hard /dev/mapper/VG01-LV01 6144 1024 2048 7 1 2 /dev/mapper/VG01-LV02 0 0 0 0 0 0

[root@localhost /]# su lisa	\\切换到lisa
[lisa@localhost /]$ cd /mnt1
[lisa@localhost mnt1]$ ls

aquota.group aquota.user lisa1.txt lisa2.txt lisa3.txt lisa4.txt lost+found tom1.txt tom2.txt tom3.txt [lisa@localhost mnt1]$ rm -rf * \\将LV01挂载目录下添加的文件删除 rm: 无法删除"aquota.group": 不允许的操作 rm: 无法删除"aquota.user": 不允许的操作 [lisa@localhost mnt1]$ ls aquota.group aquota.user [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa1.txt bs=1M count=1 \\添加第一个1M文件 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00305979 秒,343 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa2.txt bs=1M count=1 \添加第二个文件 dm-0: warning, user file quota exceeded. dm-0: warning, group file quota exceeded. dm-0: warning, user block quota exceeded. dm-0: warning, group block quota exceeded. 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00278399 秒,377 MB/秒 [lisa@localhost mnt1]$ dd if=/dev/zero of=lisa3.txt bs=1M count=1 \添加三个文件失败 dm-0: write failed, group file limit reached. dd: 打开"lisa3.txt" 失败: 超出磁盘限额

结论:

通过为组配置磁盘配额,发现原来lisa配置的空间为4MB,文件数量为4的磁盘配额失效,最终以组的配额空间2MB,数量为2为准,说明组的生效关系优先于用户。