Linux存储结构与磁盘划分

文件存储结构
在Linux系统中,目录、字符设备、块设备、套接字、打印机等都被抽象成了文件,即“Linux系统中一切都是文件”。既然平时我们打交道的都是文件,那么又应该如何找到它们呢?在Windows操作系统中,想要找到一个文件,我们要依次进入该文件所在的磁盘分区(假设这里是D盘),然后在进入该分区下的具体目录,最终找到这个文件。但是在Linux系统中并不存在C/D/E/F等盘符,Linux系统中的一切文件都是从“根(/)”目录开始的,并按照文件系统层次化标准(FHS)采用树形结构来存放文件,以及定义了常见目录的用途。另外,Linux系统中的文件和目录名称是严格区分大小写的。例如,root、rOOt、Root、rooT均代表不同的目录,并且文件名称中不得包含斜杠(/)。Linux系统中的文件存储结构如图所示。
在这里插入图片描述FHS定义了用户应该把什么类型的文件文件存放到什么位置,但用户不一定要遵守,这里只是定义而非gun定。

在这里插入图片描述
由于现在的IDE设备已经很少见了,所以一般的硬盘设备都会是以“/dev/sd”开头的。而一台主机上可以有多块硬盘,因此系统采用a~p来代表16块不同的硬盘(默认从a开始分配,如果超过26个硬盘,那么前26个命名sd[a-z],第27-52个命名为sd[aa-az],第53-78个命名为sd[ba-bz],依此类推)而且硬盘的分区编号也很有讲究:

 1. 主分区或扩展分区的编号从1开始,到4结束;
 2. 逻辑分区从编号5开始。

注意两点

 1. 存储设备的名称从a开始按序命名,是由系统内核的识别顺序决定的,与设备在第几个插槽无关。
 2. 分区编号并不代表分区个数,分区的数字编码不一定是强制顺延下来的,也有可能是手工指定的。

硬盘命名规则
在这里插入图片描述
正是因为计算机有了硬盘设备,我们才可以在玩游戏的过程中或游戏通关之后随时存档,而不用每次重头开始。硬盘设备是由大量的扇区组成的,每个扇区的容量为512字节。其中第一个扇区最重要,它里面保存着主引导记录与分区表信息。就第一个扇区来讲,主引导记录需要占用446字节,分区表为64字节,结束符占用2字节;其中分区表中每记录一个分区信息就需要16字节,这样一来最多只有4个分区信息可以写到第一个扇区中,这4个分区就是4个主分区。第一个扇区中的数据信息如图所示。在这里插入图片描述
文件系统与数据资料

常见的文件系统

Ext3:是一款日志文件系统,能够在系统异常宕机时避免文件系统资料丢失,并能自动修复数据的不一致与错误。然而,当硬盘容量较大时,所需的修复时间也会很长,而且也不能百分之百地保证资料不会丢失。它会把整个磁盘的每个写入动作的细节都预先记录下来,以便在发生异常宕机后能回溯追踪到被中断的部分,然后尝试进行修复。

Ext4:Ext3的改进版本,作为RHEL 6系统中的默认文件管理系统,它支持的存储容量高达1EB(1EB=1,073,741,824GB),且能够有无限多的子目录。另外,Ext4文件系统能够批量分配block块,从而极大地提高了读写效率。

XFS:是一种高性能的日志文件系统,而且是RHEL 7中默认的文件管理系统,它的优势在发生意外宕机后尤其明显,即可以快速地恢复可能被破坏的文件,而且强大的日志功能只用花费极低的计算和存储性能。并且它最大可支持的存储容量为18EB,这几乎满足了所有需求。

1.inode存放文件的权限与属性记录,每个文件占用一个独立的inode表格,该表格的大小默认为128字节,里面记录着如下信息:

1. 该文件的访问权限(read、write、execute);
2.该文件的所有者与所属组(owner、group);
3.该文件的大小(size);
4.该文件的创建或内容修改时间(ctime);
5.该文件的最后一次访问时间(atime);
6.该文件的修改时间(mtime);
7.文件的特殊权限(SUID、SGID、SBIT);
8.该文件的真实数据地址(point)。

2.block存放文件的实际内容(大小可以为1KB、2KB、4KB)。

文件很小(1KB),但依然会占用一个block,因此会潜在地浪费3KB。
文件很大(5KB),那么会占用两个block(5KB-4KB后剩下的1KB也要占用一个block)。

挂载硬件设备

1.分区

fdisk命令:用于管理磁盘分区。格式:fdisk [磁盘名称]。它提供了集添加、删除、转换分区等功能于一身的“一站式分区服务”。这是一条交互式命令。

常用参数:

	参数	作用
 	m	查看全部可用的参数
	n	添加新的分区
	d	删除某个分区信息
	l	列出所有可用的分区类型
	t	改变某个分区的类型
	p	查看分区表信息
	w	保存并退出
	q	不保存直接退出

2.格式化

mkfs命令:用于格式化操作。格式:mkfs.文件类型 磁盘设备文件。

3.挂载与卸载

①挂载操作就是把硬件设备与目录进行关联的动作
②卸载操作就是取消硬件设备与目录关联的动作,卸载文件系统就意味不再使用硬件的设备资源

挂载 mount 设备 目录

-a:挂载所有在/etc/fstab中定义的文件系统
-t:指定文件系统的类型

卸载 umount 设备或者目录

用mount命令挂载,系统重启后就会失效。要在/etc/fstab文件追加挂载信息才能永久挂载。在/etc/fstab文件中,挂载信息格式:“设备文件 挂载目录 格式类型 权限选项 是否备份 是否自检”。

挂载信息格式各字段含义:

字段	   意义
设备文件	   一般为设备的路径+设备名称,也可以写唯一识别码(UUID,Universally Unique Identifier)
挂载目录	   指定要挂载到的目录,需在挂载前创建好
格式类型	   指定文件系统的格式,比如Ext3、Ext4、XFS、SWAP、iso9660(此为光盘设备)等
权限选项	   若设置为defaults,则默认权限为:rw, suid, dev, exec, auto, nouser, async
是否备份	   若为1则开机后使用dump进行磁盘备份,为0则不备份
是否自检	   若为1则开机后自动进行磁盘自检,为0则不自检

df命令:用于查看挂载状态和硬盘使用量信息。常用参数为-h。格式:df -h。
du命令:用于查看文件数据占用量。

du -sh /*        #查看在Linux系统根目录下所有一级目录分别占用的空间大小
du -sh /newFS    #查看/newFS下的内容占了多少容量
[root@zzy Desktop]# ls -l /dev/sd*
brw-rw----. 1 root disk 8,  0 Oct 19  2020 /dev/sda
brw-rw----. 1 root disk 8,  1 Oct 19  2020 /dev/sda1
brw-rw----. 1 root disk 8,  2 Oct 19  2020 /dev/sda2
brw-rw----. 1 root disk 8, 16 Oct 19  2020 /dev/sdb
[root@zzy Desktop]# 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.


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: 0x53def796

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@zzy Desktop]# file /dev/sdb1
/dev/sdb1: block special
[root@zzy Desktop]# mkfs
Usage:
 mkfs [options] [-t <type>] [fs-options] <device> [<size>]

Options:
 -t, --type=<type>  filesystem type; when unspecified, ext2 is used
     fs-options     parameters for the real filesystem builder
     <device>       path to the device to be used
     <size>         number of blocks to be used on the device
 -V, --verbose      explain what is being done;
                      specifying -V more than once will cause a dry-run
 -V, --version      display version information and exit;
                      -V as --version must be the only option
 -h, --help         display this help text and exit

For more information see mkfs(8).
[root@zzy Desktop]# mkfs
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat    
[root@zzy Desktop]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@zzy Desktop]# mount /dev/sdb1 /zzy
[root@zzy Desktop]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/rhel_zzy-root   18G  2.9G   15G  17% /
devtmpfs                   985M     0  985M   0% /dev
tmpfs                      994M  140K  994M   1% /dev/shm
tmpfs                      994M  8.9M  986M   1% /run
tmpfs                      994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                  497M  119M  379M  24% /boot
/dev/sdb1                  2.0G   33M  2.0G   2% /zzy
[root@zzy Desktop]# vim /etc/fstab


在这里插入图片描述

^(* ̄(oo) ̄)^ 

 1. 在 file /dev/sdb1 下出现block说明是正确的,如若不对连续输入两遍partprobe
 2. 第二个mkfs后连续按两下Tab键
 3. 挂载之后必须在vim编辑器里面永久挂载,否者重启虚拟机后就没了

添加交换分区
1.分区

fdisk /dev/sdb
Command (m for help): n                                                            #添加分区
Select (default p): e                                                              #先创建扩展分区,才能创建逻辑分区
Partition number (2-4, default 2):                                                 #直接回车
First sector (4196352-20971519, default 4196352):                                  #回车
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +6G    #为扩展分区分配6G空间
Command (m for help): n                                                            #添加分区
Select (default p): l                                                              #创建逻辑分区
First sector (4198400-16779263, default 4198400):                                  #回车
Last sector, +sectors or +size{K,M,G} (4198400-16779263, default 16779263): +5G    #为逻辑分区分配5G空间,空间最多不能超过扩展分区的大小
Command (m for help): w                                                            #保存并退出

2.格式化
mkswap命令:swap分区的专用格式化命令。
3.挂载
swapon命令:因为swap分区没有挂载点,所以需要用挂载swap分区的专用命令来对swap分区进行挂在操作。

swapoff命令:卸载swap分区设备。

[root@zzy Desktop]# free -h
             total       used       free     shared    buffers     cached
Mem:          1.9G       794M       1.2G       9.9M       988K       239M
-/+ buffers/cache:       554M       1.4G
Swap:         2.0G         0B       2.0G
[root@zzy Desktop]# 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.


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: 0x53def796

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     8390655     4194304   83  Linux
/dev/sdb2         8390656    16779263     4194304   83  Linux

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): ^Z
[1]+  Stopped                 fdisk /dev/sdb
[root@zzy Desktop]# 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.


Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

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@zzy Desktop]# 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.


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: 0x53def796

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     8390655     4194304   83  Linux

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@zzy Desktop]# clear
[3;J
[root@zzy Desktop]# free -h 
             total       used       free     shared    buffers     cached
Mem:          1.9G       781M       1.2G       9.8M       1.3M       244M
-/+ buffers/cache:       535M       1.4G
Swap:         2.0G         0B       2.0G
[root@zzy Desktop]# 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.


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: 0x53def796

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     8390655     4194304   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (8390656-41943039, default 8390656): 
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-41943039, default 41943039): +4G
Partition 2 of type Linux and of size 4 GiB is set

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@zzy Desktop]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=28dbb311-ad19-40e7-9814-e51004a7d18c
[root@zzy Desktop]# swapon /dev/sdb2
[root@zzy Desktop]# vim /etc/fstab
[root@zzy Desktop]# free -h 
             total       used       free     shared    buffers     cached
Mem:          1.9G       811M       1.1G       9.8M       1.3M       250M
-/+ buffers/cache:       560M       1.4G
Swap:         6.0G         0B       6.0G

挂载之后必须在vim编辑器里面永久挂载,不然重启后就没了

磁盘容量配额
Linux系统的设计初衷就是让许多人一起使用并执行各自的任务,从而成为多用户、多任务的操作系统。但是,硬件资源是固定且有限的,如果某些用户不断地在Linux系统上创建文件或者存放电影,硬盘空间总有一天会被占满。针对这种情况,root管理员就需要使用磁盘容量配额服务来限制某位用户或某个用户组针对特定文件夹可以使用的最大硬盘空间或最大文件个数,一旦达到这个最大值就不再允许继续使用。可以使用quota命令进行磁盘容量配额管理,从而限制用户的硬盘可用容量或所能创建的最大文件个数。quota命令还有软限制和硬限制的功能。

软限制:当达到软限制时会提示用户,但仍允许用户在限定的额度内继续使用。
硬限制:当达到硬限制时会提示用户,且强制终止用户的操作

RHEL 7系统中已经安装了quota磁盘容量配额服务程序包,但存储设备却默认没有开启对quota的支持,此时需要手动编辑配置文件,让RHEL 7系统中的/boot目录能够支持quota磁盘配额技术。

1.编辑/etc/fstab文件,添加参数让存储设备支持磁盘配额技术。早期文件系统添加的参数为usrquota参数,而xfs文件系统使用的是uquota或usrquota。
2.重启系统,使用mount命令查看目录是否已经支持quota磁盘配额技术。
3.设置quota磁盘配额。

[root@zzy Desktop]# mount | grep /boot
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@zzy Desktop]# vim /etc/fstab
[root@zzy Desktop]# reboot
[root@zzy Desktop]# mount | grep /boot
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)
[root@zzy Desktop]# xfs_quota -x -c "limit bsoft=3M bhard=6M isoft=3 ihard=6 zzy" /boot
[root@zzy Desktop]# xfs_quota -x -c report /boot
User quota on /boot (/dev/sda1)
                               Blocks                     
User ID          Used       Soft       Hard    Warn/Grace     
---------- -------------------------------------------------- 
root            95348          0          0     00 [--------]
zzy                 0       3072       6144     00 [--------]
[root@zzy zzy]# chmod -Rf 777 /boot
[root@zzy zzy]# su - zzy
Last login: Tue Oct 20 07:10:06 EDT 2020 on pts/0
[zzy@zzy ~]$ cd ~
[zzy@zzy ~]$ dd if=/dev/zero of=/boot/zzy bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.000558576 s, 1.9 GB/s
[zzy@zzy ~]$ cd /boot
[zzy@zzy boot]$ touch a b c d e f 
touch: cannot touch ‘f’: Disk quota exceeded
[zzy@zzy boot]$ xfs_quota -x -c report /boot
User quota on /boot (/dev/sda1)
                               Blocks                     
User ID          Used       Soft       Hard    Warn/Grace     
---------- -------------------------------------------------- 
zzy              1024       3072       6144     00 [--------]

这里是引用

软硬方式链接
硬链接(hard link):
软链接(也称为符号链接[symbolic link]):
ln命令:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值