Administrator
发布于 2023-07-24 / 65 阅读
0
0

File System & Mount

文件系统和挂载点

df命令

refer to : https://www.man7.org/linux/man-pages/man1/df.1.html

NAME
df - report file system space usage

SYNOPSIS
df [OPTION]… [FILE]…

DESCRIPTION
This manual page documents the GNU version of df. df displays
the amount of space available on the file system containing each
file name argument. If no file name is given, the space
available on all currently mounted file systems is shown. Space
is shown in 1K blocks by default, unless the environment variable
POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

If an argument is the absolute file name of a device node
containing a mounted file system, df shows the space available on
that file system rather than on the file system containing the
device node. This version of df cannot show the space available
on unmounted file systems, because on most kinds of systems doing
so requires very nonportable intimate knowledge of file system
structures.

OPTIONS
Show information about the file system on which each FILE
resides, or all file systems by default.

Mandatory arguments to long options are mandatory for short
options too.

    -h, --human-readable
          print sizes in powers of 1024 (e.g., 1023M)

    -T, --print-type
          print file system type 
[root@datastorage home]# df -hT

Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs   12G     0   12G   0% /dev
tmpfs               tmpfs      12G     0   12G   0% /dev/shm
tmpfs               tmpfs      12G   17M   12G   1% /run
tmpfs               tmpfs      12G     0   12G   0% /sys/fs/cgroup
/dev/mapper/rl-root xfs        70G   12G   59G  17% /
/dev/nvme0n1p1      xfs      1014M  191M  824M  19% /boot
/dev/mapper/rl-home xfs       168G  1.2G  166G   1% /home
tmpfs               tmpfs     2.4G     0  2.4G   0% /run/user/0

理论知识

那就让我们看一下,从一个裸硬盘到安装好Linux系统这个过程文件系统的变化。首先你有一个存储设备(比如一个硬盘、U盘、分区、逻辑卷等),这个存储设备在没有文件系统之前,它就是这样:

img

一个存储就可以简单的看成一个有很多块单元组成的一个大块设备。比如一个500G的硬盘你可以脑补成一个102410241024的魔方,每个魔方块是一个扇区大小,可以存512个字节。然后存储设备提供给你的基本功能就是,你可以以块为单位指定读或写某一个或一段块。这就是存储的基本样子,就是由块排列而成的一个大块。这上面显然没有什么文件、目录之类的概念,有的只是块以及0和1。

要想引出文件和目录这样的概念,就需要文件系统登场了。文件系统将块设备的每一个块组织起来,一些作为管理单元,一些作为数据单元,这才引入了日常我们看到的文件、文件夹、树形目录结构等东西。

如果你并非专业人员,可以不用了解那么细,知道文件系统引入了文件、目录以及目录结构等这些东西。于是上面的块设备在有了文件系统(mkfs)后就变成了这样:

img

刚刚做好的文件系统除了一些管理数据(我们称为元数据)以外,在目录树结构上只有一个根,我们也叫它[root inode](https://www.zhihu.com/search?q=root inode&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra={“sourceType”%3A"answer"%2C"sourceId"%3A2318166297}),这是一个on-disk的实体。

我们知道,外存中的数据是不能和CPU直接交互的,我们需要将外存中的数据加载到内存中来,于是Linux内核就在内存中先准备了一个初始的空的根目录(当然实际过程要复杂的多),然后将上述存储设备的文件系统挂载到这个空的根目录上,这样就把on-disk的结构和内存关联了起来,发生在根目录及其下面的操作都将实际读写在上面的存储设备上。就像这样(圆圈代表内存中的结构):

img

下面为了构建一个基本的Linux系统,安装程序会构建一些基本的目录和文件,比如/bin,/etc,/usr,/home等目录及其下面的文件,然后就差不多变成了这样:

img

到这里就已经有一个可以运行的Linux系统的雏形了。

那么比如你还有一个分区或硬盘或逻辑卷什么的,你觉得/home目录下你会用很多空间,你想让/home中的数据单独放在一个额外的更大的存储上。这块存储的初始状态也是和上面图1一样,你需要在先这块新的存储上做文件系统,让它变成图2的样子。但是这次你不是要把它挂载到全局目录树的根上了,你是想让/home目录和它关联,所以你需要把它挂载到/home目录上,像这样:

image-20230724185346501

新的粉色的存储设备上的文件系统就挂载在了/home目录,全局目录树/home就和粉色文件系统的根关联和起来(具体怎么关联的参考我上面给出的链接中的其它文章),也可以说粉色文件系统的被装载/挂载到了/home上,而这时/home就叫一个挂载点。

这之后对/home下的任何读写操作都将经过粉色设备上的文件系统发生在粉色的存储设备上。比如你创建一些用户,并在其下面做了一些操作:

img

这样看着就比较像我们装好的一个Linux系统的样子了。

上面灰色设备就相当于你问题图中的/dev/vda1,而其上面的文件系统就相当于/dev/vda1上的文件系统。

粉色设备就相当于你问题图中的/dev/mapper/vghome-lvhome,而其上的文件系统就相当于vghome-lvhome上的文件系统。

如果你卸载粉色的文件系统,对/home的读写操作将转而发生在灰色的文件系统所在的存储设备上。如果你喜欢,你可以继续嵌套的挂载,比如再将一个文件系统挂载到/home/Download上。对于全局路径结构来说,它们是一个连贯的结构,但是对于具体的文件系统来说,它们可能是由一个或多个文件系统组合而成

调整挂载点的大小

refer to : https://blog.csdn.net/qq_37612755/article/details/103809782

[root@datastorage home]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs   12G     0   12G   0% /dev
tmpfs               tmpfs      12G     0   12G   0% /dev/shm
tmpfs               tmpfs      12G   17M   12G   1% /run
tmpfs               tmpfs      12G     0   12G   0% /sys/fs/cgroup
/dev/mapper/rl-root xfs        70G   12G   59G  17% /
/dev/nvme0n1p1      xfs      1014M  191M  824M  19% /boot
/dev/mapper/rl-home xfs       168G  1.2G  166G   1% /home
tmpfs               tmpfs     2.4G     0  2.4G   0% /run/user/0

如上,我们可以看到/home 这个挂载点的大小是168G,而/ 这个挂载点的大小只有70G。

现在,我们希望能够扩大/ 这个挂载点的大小到210G,所以/挂载点需要扩容140G,而/home这个挂载点需要缩小140G


在调整之前,我们先看这个//home挂载点的文件系统,都是xfs。
xfs文件系统的调整命令是xfs_growfs(只支持增大)

lvextend -L 120G /dev/mapper/rl-root //增大120G
xfs_growfs /dev/mapper/rl-root //执行调整

如果文件系统是ext2/ext3/ext4,那么调整命令是resize2fs(增大和减小都支持)

lvextend -L 120G /dev/mapper/centos-home //增大120G
lvreduce -L -8G /dev/mapper/centos-home //减小8G
resize2fs /dev/mapper/centos-home //执行调整


具体的执行步骤如下:

卸载/home

cd /
umount /home

查看分区,如果没有/home,代表卸载成功

df -hT

将 /home 分区减小140G(根据自己实际情况设定大小) :

lvreduce -L -140G /dev/mapper/rl-home

因为 xfs文件系统不能执行分区减小的调整!所以这里我们要执行格式化操作,

mkfs.xfs /dev/mapper/rl-home -f

重新挂载 /home 分区:

mount /dev/mapper/rl-home /home/

验证:

df -hT

查看剩余空间

[root@datastorage /]# vgdisplay
  --- Volume group ---
  VG Name               rl
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <249.00 GiB
  PE Size               4.00 MiB
  Total PE              63743
  Alloc PE / Size       27903 / <109.00 GiB
  Free  PE / Size       35840 / 140.00 GiB
  VG UUID               Pj1jLN-MH52-cjVC-waLC-TpvI-AJ5J-h5srOC

将上面空余的 140G 分到 / 分区下

lvextend -L +140G /dev/mapper/rl-root
xfs_growfs /dev/mapper/rl-root

验证:

df -hT

评论