磁盘分区以及配额

一、磁盘创建以及分区

1、打开虚拟机设置,点击添加→选择硬盘→下一步→选择磁盘类型(NVME类型只有在关机情况下才可以添加)→创建虚拟磁盘→设置磁盘容量,并选择将虚拟磁盘存储为单个文件→指定磁盘文件→完成

2、创建磁盘分区,首先配置磁盘文件

fdisk /dev/nvme0n1  ---由于磁盘类型是nvme所以磁盘文件是/dev下的nvme01

3、进入文件配置后,输入help可以查看命令

image.png

4、开始分区

Command (m for help): n   ---创建新的分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)   ---创建主分区
   l   logical (numbered from 5)   ---创建逻辑分区,只有创建额外分区后才会出现,在创建之前是输入e创建额外分区
Select (default p): p
Partition number (1,4, default 1): 1  ---分区编号,输入1代表新分区号为1
First sector (2048-10485759, default 2048):   ---起始扇区,直接回车代表直接从当前扇区开始创建
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-616447, default 616447): +200M  ---分区大小
Partition 1 of type Linux and of size 200 MiB is set
Command (m for help): p  ---查看分区情况

Disk /dev/nvme0n1: 5368 MB, 5368709120 bytes, 10485760 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: 0x7135cfb9

        Device Boot      Start         End      Blocks   Id  System
/dev/nvme0n1p1            2048      411647      204800   83  Linux  ---创建的分区
/dev/nvme0n1p2          616448     1640447      512000   83  Linux
/dev/nvme0n1p3         1640448    10485759     4422656    5  Extended
/dev/nvme0n1p5         1642496     3076095      716800    b  W95 FAT32
/dev/nvme0n1p6         3078144     7272447     2097152   82  Linux swap / Solaris

Command (m for help): w  ---输入w保存并退出

5、识别分区

[root@wanghaihan ~]# partprobe /dev/nvme0n1  ---重读更新分区信息,将分区信息通报内核

6、创建文件系统

[root@wanghaihan ~]# mkfs.ext4 /dev/nvme0n1p1  ---在/dev/nvme0n1p1下创建类型为ext4的文件系统
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@wanghaihan ~]# cat /proc/partitions  ---查看被系统已经识别的分区
major minor  #blocks  name

   8        0   31457280 sda
   8        1    1048576 sda1
   8        2   30407680 sda2
  11        0    4444160 sr0
 253        0   28303360 dm-0
 253        1    2097152 dm-1
 259        0    5242880 nvme0n1
 259        1     204800 nvme0n1p1
 259        2     512000 nvme0n1p2
 259        3          0 nvme0n1p3
 259        4     716800 nvme0n1p5
 259        5    2097152 nvme0n1p6

7、进入挂载目录,配置开机自动挂载

[root@wanghaihan ~]# blkid
/dev/sda1: UUID="2a64bdae-b004-46ff-a94f-5859f9b80f8c" TYPE="xfs" 
/dev/sda2: UUID="2XwJ40-qMhr-oGfp-IHJF-vHmk-zFFe-2GU8n7" TYPE="LVM2_member" 
/dev/sr0: UUID="2020-02-25-11-40-31-00" LABEL="RHEL-7.8 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/rhel-root: UUID="5f02bac3-c040-4041-8f4c-722ec5672f36" TYPE="xfs" 
/dev/mapper/rhel-swap: UUID="03ee9e10-2937-4042-becb-b20c151ce925" TYPE="swap" 
/dev/nvme0n1p1: UUID="0a7c5329-ba32-4563-99fa-8ce70693a09e" TYPE="ext4" 
/dev/nvme0n1p2: LABEL="p1" UUID="840dca89-c3d2-419f-8c72-787b1e5f2483" TYPE="xfs" 
/dev/nvme0n1p5: LABEL="WINFS" UUID="643B-3070" TYPE="vfat" 
/dev/nvme0n1p6: UUID="a7924369-65cc-41ea-a50c-8b2a15fd4160" TYPE="swap" 

image.png

8、重启挂载

[root@wanghaihan ~]# mount -a  ---重启挂载

9、查看挂载情况

[root@wanghaihan ~]# df -Th | grep -v tmp  ---查看已存在的挂载信息,并排除tmp文件
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs        27G   15G   13G  54% /
/dev/sr0              iso9660   4.3G  4.3G     0 100% /mnt/cdrom
/dev/sda1             xfs      1014M  239M  776M  24% /boot
/dev/nvme0n1p1        ext4      190M  1.6M  175M   1% /mnt/xfs1  ---挂载成功
/dev/nvme0n1p2        xfs       497M   26M  472M   6% /mnt/xfs2
/dev/nvme0n1p5        vfat      699M  4.0K  699M   1% /mnt/win

二、磁盘配额(ext4文件系统)

1、修改文件系统文件/etc/fstab,配置磁盘配额,限制用户和组。

image.png
随后查看挂载信息

[root@wanghaihan ~]# mount | grep nvme0n1p1    ---查看nvme0n1p1的挂载信息
/dev/nvme0n1p1 on /mnt/xfs1 type ext4 (rw,relatime,seclabel,data=ordered)   ---此时还没有更新配额的相关配置
[root@wanghaihan ~]# mount -o remount /mnt/xfs1   ---重新挂载
[root@wanghaihan ~]# mount | grep nvme0n1p1
/dev/nvme0n1p1 on /mnt/xfs1 type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)  ---配额的配置成功更新

2、创建配额文件

[root@wanghaihan ~]# useradd quota  ---创建新用户用来验证
[root@wanghaihan ~]# ls -la /mnt/xfs1  ---查看配额的挂载目录的详细信息
total 13
drwxr-xr-x. 3 root root  1024 Apr 12 11:06 .
drwxr-xr-x. 7 root root    65 Apr 11 16:13 ..
drwx------. 2 root root 12288 Apr 12 11:06 lost+found
[root@wanghaihan ~]# quotacheck -augcv  --- -a 扫描/etc/mtab中所有启⽤配额的分区
                                            -u 扫描磁盘计算⽤户所占⽤的⽂件数,⽣成aquota.user⽂件
                                            -g 扫描磁盘计算组所占⽤的⽂件数,⽣成aquota.group⽂件
                                            -c 创建配额⽂件aquota.user和aquota.group
                                            -v 显示扫描过程
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/nvme0n1p1 [/mnt/xfs1] done
quotacheck: Cannot stat old user quota file /mnt/xfs1/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/xfs1/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /mnt/xfs1/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/xfs1/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 3 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
[root@wanghaihan ~]# ls -la /mnt/xfs1  ---再次查看,发现创建配额文件
total 27
drwxr-xr-x. 3 root root  1024 Apr 12 16:15 .
drwxr-xr-x. 7 root root    65 Apr 11 16:13 ..
-rw-------. 1 root root  6144 Apr 12 16:15 aquota.group
-rw-------. 1 root root  6144 Apr 12 16:15 aquota.user
drwx------. 2 root root 12288 Apr 12 11:06 lost+found

3、配置配额文件

[root@wanghaihan ~]# edquota -u quota 

image.png

[root@wanghaihan ~]# quotaon -a

4、测试环节

[root@wanghaihan ~]# ls -ld /mnt/xfs1
drwxr-xr-x. 3 root root 1024 Apr 12 16:15 /mnt/xfs1  ---发现other无读写权限,因此为other增加权限
[root@wanghaihan ~]# chmod 777 /mnt/xfs1
[root@wanghaihan ~]# ls -ld /mnt/xfs1
drwxrwxrwx. 3 root root 1024 Apr 12 16:15 /mnt/xfs1
[root@wanghaihan ~]# su quota  ---切换为普通账户
[quota@wanghaihan root]$ cd /mnt/xfs1  
[quota@wanghaihan xfs1]$ dd if=/dev/zero of=test1 bs=1M count=30  ---创建一个30个字符特定大小为1M的文件
30+0 records in
30+0 records out
31457280 bytes (31 MB) copied, 0.0224057 s, 1.4 GB/s
[quota@wanghaihan xfs1]$ dd if=/dev/zero of=test2 bs=1M count=30
30+0 records in
30+0 records out
31457280 bytes (31 MB) copied, 0.048802 s, 645 MB/s
[quota@wanghaihan xfs1]$ dd if=/dev/zero of=test3 bs=1M count=30
nvme0n1p1: warning, user block quota exceeded.  ---到达软限制,出现警告
30+0 records in
30+0 records out
31457280 bytes (31 MB) copied, 0.0503002 s, 625 MB/s
[quota@wanghaihan xfs1]$ dd if=/dev/zero of=test4 bs=1M count=30  ---在第四次时,创建的文件超过了硬限制100M,多余的文件创建失败
nvme0n1p1: write failed, user block limit reached.
dd: error writing ‘test4’: Disk quota exceeded
10+0 records in
9+0 records out
10481664 bytes (10 MB) copied, 0.00678926 s, 1.5 GB/s
[quota@wanghaihan xfs1]$ ls -lh  ---30+30+30+10正好达到100M限制,限制对root无效。
total 101M
-rw-------. 1 root  root  7.0K Apr 12 16:15 aquota.group
-rw-------. 1 root  root  7.0K Apr 12 16:31 aquota.user
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test1
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test2
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test3
-rw-rw-r--. 1 quota quota  10M Apr 12 16:43 test4
[quota@wanghaihan xfs1]$ rm -rf test4  ---删除一个文件,否则由于空间限制无法创建新文件验证数量限制
[quota@wanghaihan xfs1]$ touch file{1..10}  ---创建10个空文件
nvme0n1p1: warning, user file quota exceeded.
nvme0n1p1: write failed, user file limit reached.
touch: cannot touch ‘file8’: Disk quota exceeded  ---第8到第10个文件创建失败
touch: cannot touch ‘file9’: Disk quota exceeded
touch: cannot touch ‘file10’: Disk quota exceeded
[quota@wanghaihan xfs1]$ ls -lh  
total 91M
-rw-------. 1 root  root  7.0K Apr 12 16:15 aquota.group
-rw-------. 1 root  root  7.0K Apr 12 16:31 aquota.user
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file1
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file2
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file3
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file4
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file5
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file6
-rw-rw-r--. 1 quota quota    0 Apr 12 16:54 file7
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test1
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test2
-rw-rw-r--. 1 quota quota  30M Apr 12 16:43 test3
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值