Storage

proc filesystem

procfs 是一个特殊的文件系统,包含一个伪文件系统(启动时动态生成的文件系统),用于通过内核访问进程信息。这个文件系统通常被挂载到 /proc 目录。由于 proc 不是一个真正的文件系统,它也就不占用存储空间,只是占用有限的内存。

下面以 bash 进程为例查看 /proc/PID/ 下的信息。

/proc/PID/exe #

指向原始的可执行文件

$ ls -lh /proc/3553026/exe 
lrwxrwxrwx 1 root root 0  4月 10 22:09 /proc/3553026/exe -> /usr/bin/bash

/proc/PID/fd #

是一个目录, 包含此进程打开的所有文件描述符 (file descriptors)

$ ls -lhv /proc/3553026/fd
total 0
lrwx------ 1 root root 64  4月 10 22:06 0 -> /dev/pts/14
lrwx------ 1 root root 64  4月 10 22:06 1 -> /dev/pts/14
lrwx------ 1 root root 64  4月 10 22:06 2 -> /dev/pts/14
lrwx------ 1 root root 64  4月 10 22:06 255 -> /dev/pts/14

TODO #

参考 #

Linux 磁盘分区

https://www.baeldung.com/linux/partitioning-disks https://en.wikipedia.org/wiki/Master_boot_record https://www.cnblogs.com/god-of-death/p/18221794

概览 #

通常安装 Linux 系统的第一步就是磁盘分区。在我们创建任何文件前需要先存在文件系统。

分区被用来将原始存储空间分割成大块,可以用来隔离文件系统故障。

磁盘类型 (Disk Types) #

分区表格式 MBR 和 GPT #

MBR (Master Boot Record) 和 GPT (GUID Partition Table) 是最广泛使用的分区表,相较于 GPT,MBR 是一个老的标准并且有一些限制。

BIOS 和 UEFI #

分区工具 #

fdiskparted

TODO

Linux 文件系统之 inode

TODO

https://www.ruanyifeng.com/blog/2011/12/inode.html

inode 是什么 #

inode 是 Linux 文件系统中的一个重要概念,它是一个文件的元数据,记录了文件的权限、类型、大小、创建时间、修改时间等信息。

inode 的结构 #

inode 的结构如下:

struct inode {
    umode_t i_mode;       // 文件类型和权限
    uid_t i_uid;         // 文件所有者
    gid_t i_gid;         // 文件所属组
    loff_t i_size;       // 文件大小
    struct timespec i_atime; // 文件访问时间
    struct timespec i_ctime; // 文件创建时间
    struct timespec i_mtime; // 文件修改时间
}

使用场景 #

通过 inode 查找并删除文件 #

# ls -li
total 32
   459 -rw-r--r-- 1 root root 20070 Mar 13 11:27 ''$'\033\033'
526984 drwxr-xr-x 5 root root  4096 Jan  1 17:47  charts
   784 drwxr-xr-x 2 root root  4096 Jan 19 20:42  pods
 49203 drwx------ 3 root root  4096 Feb 10  2023  snap
# find . -inum 459
./??
# find . -inum 459 -delete
# ls -li
total 12
526984 drwxr-xr-x 5 root root 4096 Jan  1 17:47 charts
   784 drwxr-xr-x 2 root root 4096 Jan 19 20:42 pods
 49203 drwx------ 3 root root 4096 Feb 10  2023 snap

参考 #

删除分区并扩容另一个分区和根文件系统

现在要将 /dev/sda3 分区删掉并扩容到 /dev/sda2, 并且在不重启服务器的情况下扩容根文件系统(跟文件系统 / 挂载在 /dev/sda2 上, 并且 filesystem 是 ext4)

磁盘初始分区和挂载情况 #

➜  ~ lsblk /dev/sda
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  512M  0 part /boot/efi
├─sda2   8:2    0 98.5G  0 part /
└─sda3   8:3    0  976M  0 part 

➜  ~ fdisk -l /dev/sda
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: BlockVolume     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disklabel type: gpt
Disk identifier: 40BED670-8B91-4520-9785-DB1F1035C039

Device         Start       End   Sectors  Size Type
/dev/sda1       2048   1050623   1048576  512M EFI System
/dev/sda2    1050624 207714303 206663680 98.5G Linux filesystem
/dev/sda3  207714304 209713151   1998848  976M Linux swap

➜  ~ df -hT /dev/sda2
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda2      ext4   97G   28G   64G  31% /

删除分区 /dev/sda3 #

➜  ~ fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
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-3, default 3): 3

Partition 3 has been deleted.

Command (m for help): p
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: BlockVolume     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disklabel type: gpt
Disk identifier: 40BED670-8B91-4520-9785-DB1F1035C039

Device       Start       End   Sectors  Size Type
/dev/sda1     2048   1050623   1048576  512M EFI System
/dev/sda2  1050624 207714303 206663680 98.5G Linux filesystem

Command (m for help): w # 保存退出
The partition table has been altered.
Syncing disks.

扩容分区 /dev/sda2 和 根文件系统 #

使用 fdisk 扩容 /dev/sda2, 前提是 /dev/sda2 后面没有其他分区了,可以这样扩容(先删除不退出并重建分区, 分区 Start 不变, End 增大)

...