Linux 文件系统基本管理 硬盘分区管理学习笔记
(GPT表头记录),记录了分区表自身的位置和大小,同时也记录了前面提到备份用的GPT分区所在位置(最后34个LBA),还放置了分区表的校验码(CRC32),校验码的作用是让操作系统判断GPT的正确与否,倘若发现错误则可以从备份的GPT中恢复正常运行。,记录分区信息,每个LBA可以提供4组的分区记录,默认情况下可以有4×32=128组分区记录。MBR 记录用4个字节(1byte=8bit)存储分区的
Linux 文件系统基本管理
案例:构建基于 ISO 文件的 yum 仓库
client 想安装图形化,但仓库来源于Internet,希望使用本地仓库以提高速度。
现要求在server服务器上部署nginx服务发布dvd光盘中的仓库。
操作思路:
server上部署并配置nginx服务
server上挂载 ISO 文件到nginx服务网站目录
client创建指向server上web站点仓库
测试
部署web服务器
# 安装软件
[root@server ~ 16:58:23]# yum install -y nginx
# 设置允许查看目录中清单
[root@server ~ 17:00:28]# vim /etc/nginx/nginx.conf
......
http {
# 添加以下参数
autoindex on;
......
[root@server ~ 17:01:15]# systemctl enable nginx.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 准备测试文件
[root@server ~ 17:01:29]# mv /usr/share/nginx/html/index.html{,.ori}
[root@server ~ 17:01:37]# echo welcome to Nginx website. > /usr/share/nginx/html/index.html
# 客户端测试
[root@server ~ 17:01:50]# curl http://10.1.8.10
welcome to Nginx website.
挂载光盘到web服务器发布目录
确保光盘关联正确的iso并连接到虚拟机
[root@server ~ 17:02:40]# mkdir /usr/share/nginx/html/dvd
[root@server ~ 17:05:51]# mount /dev/sr0 /usr/share/nginx/html/dvd
mount: /dev/sr0 写保护,将以只读方式挂载
客户端测试
[root@client ~ 17:06:45]# mkdir /etc/yum.repos.d/old
[root@client ~ 17:07:46]# mv /etc/yum.repos.d/*repo /etc/yum.repos.d/old
[root@client ~ 17:07:55]# vim /etc/yum.repos.d/dvd.repo
[dvd]
name= dvd from local server
baseurl=http://10.1.8.10/dvd
gpgcheck=0
[root@client ~ 17:08:23]# yum clean all
[root@client ~ 17:08:28]# yum grouplist
[root@client ~ 17:08:28]# yum grouplist
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Determining fastest mirrors
dvd | 3.6 kB 00:00
(1/2): dvd/group_gz | 153 kB 00:00
(2/2): dvd/primary_db | 3.3 MB 00:00
可用的环境分组:
最小安装
基础设施服务器
计算节点
文件及打印服务器
....
[root@client ~ 17:09:10]# yum groupinstall -y "Server with GUI"
# 启动图形化
[root@client ~ 17:10:02]# init 5
# 切换到字符
[root@client ~ 17:10:54]# init 3
查找系统中文件
locate
locate 命令根据文件名及其路径,在 mlocate 数据库中查找文件,并返回结果。数据库中存放文件和文件路径信息。
常规用户查找时,返回的结果仅包含用户有读取权限的目录树中匹配项。
查找文件前,需要root用户手动执行updatedb命令更新mlocate数据库。
[root@server ~ 17:05:58]# yum install -y mlocate
[root@server ~ 17:16:35]# updatedb
locate命令语法:
locate [OPTION]... [PATTERN]...
常用选项:
-b, --basename
-i, --ignore-case
-c, --count
-r, --regexp
示例:
[root@server ~ 17:17:02]# yum install -y httpd
# 安装后,未跟新数据库,所以检索不到
[root@server ~ 17:18:13]# locate httpd.conf
# 更新后查找
[root@server ~ 17:18:39]# updatedb
[root@server ~ 17:19:10]# locate httpd.conf
/etc/httpd/conf/httpd.conf
/usr/lib/tmpfiles.d/httpd.conf
# -b选项 查找
[root@server ~ 17:19:13]# locate -b httpd
/etc/httpd
/etc/httpd/conf/httpd.conf
/etc/logrotate.d/httpd
/etc/sysconfig/httpd
/usr/lib/systemd/system/httpd.service
/usr/lib/tmpfiles.d/httpd.conf
/usr/lib64/httpd
/usr/libexec/initscripts/legacy-actions/httpd
/usr/sbin/httpd
/usr/share/httpd
......
# 扩展
[root@server ~ 17:19:49]# basename /usr/share/doc
doc
[root@server ~ 17:20:17]# dirname /usr/share/doc
/usr/share
# -i选项 忽略大小写
[root@server ~ 17:20:23]# locate PASSWD
[root@server ~ 17:21:03]# locate -i PASSWD
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/htpasswd
/usr/bin/passwd
.....
# -c选项 返回找到的数量
[root@server ~ 17:21:14]# locate -b -c PASSWD
0
# -r选项 正则表达式匹配查找
[root@server ~ 17:22:12]# locate -r 'http.*conf'
/etc/httpd/conf
/etc/httpd/conf.d
/etc/httpd/conf.modules.d
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/httpd/conf.d/README
/etc/httpd/conf.d/autoindex.conf
......
find
find 命令在本地文件系统中执行实时查找文件。使用find命令的用户对文件夹必须有读取和执行权限。
语法:
find [path] [expression] [action]
path,是查询路径,如果没有指定文件夹,将会查找当前目录及子目录。
expression,是查询条件表达式。
action,是找到文件后采取的动作。
根据文件 name 查找
[root@server ~ 17:22:43]# touch /etc/PASSWD
[root@server ~ 17:25:20]# find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@server ~ 17:25:25]# find /etc/ -name '*passwd*'
/etc/passwd-
/etc/passwd
/etc/pam.d/passwd
/etc/security/opasswd
[root@server ~ 17:25:38]# find /etc/ -iname passwd
/etc/passwd
/etc/pam.d/passwd
/etc/PASSWD
根据文件 type 查找
-type 根据文件类型查找,支持文件类型:
b,block (buffered) special。
c,character (unbuffered) special。
d,directory。
p,named pipe (FIFO)。
f,regular file。
l,symbolic link。
s,socket。
-type 参数值:
b 块设备文件(硬盘、分区 /dev/sda1)
c 字符设备
d 目录
p 命名管道
f 普通文件(最常用)
l 软链接(快捷方式)
s socket 通信文件
示例:
# 1. 只找 目录
find /etc -type d
# 2. 只找 普通文件
find /root -type f
# 3. 只找 软链接(快捷方式)
find /usr -type l
# 4. 只找 块设备(硬盘/分区)
find /dev -type b
# 5. 组合:找 /home 下所有 .log 普通文件
find /home -type f -name "*.log"
[root@server ~ 17:26:31]# mkdir /etc/yum.repos.d/passwd
[root@server ~ 17:28:35]# find /etc/ -name 'passwd' -type d
/etc/yum.repos.d/passwd
[root@server ~ 17:28:40]# rmdir /etc/yum.repos.d/passwd
根据文件 owner 查找
-user、-uid,属于特定用户。
[root@server ~ 17:28:45]# id liang
uid=1000(liang) gid=1000(liang) 组=1000(liang)
根据文件 perm 查找
# 准备文件
[root@server ~ 17:31:25]# mkdir lab;cd lab
[root@server lab 17:32:25]# touch file-{1..3}
[root@server lab 17:32:30]# ls -l file*
-rw-r--r-- 1 root root 0 5月 20 17:32 file-1
-rw-r--r-- 1 root root 0 5月 20 17:32 file-2
-rw-r--r-- 1 root root 0 5月 20 17:32 file-3
-perm mode,查找文件权限为mode的文件。
# 查找文件权限为764的文件 [root@centos7 lab]# find -perm 764 | xargs ls -l -rwxrw-r--. 1 root root 0 Dec 24 20:28 ./file-1 ```
-perm -mode,例如:mode为764,则ugo必须同时满足的最小权限:user至少为7、group至少为6、other为4。
```bash [root@centos7 lab]# chmod 777 file-1 [root@centos7 lab]# chmod 764 file-2 [root@centos7 lab]# chmod 760 file-3 [root@centos7 lab]# find . -perm -764 | xargs ls -l -rwxrwxrwx. 1 root root 0 Dec 24 20:28 ./file-1 -rwxrw-r--. 1 root root 0 Dec 24 20:28 ./file-2
# 0作为通配符,表示不匹配对应权限位。 # 查找系统中具有suid权限的文件 [root@centos7 lab]# find / -perm -4000
# 查找系统中同时具有特殊权限的文件:suid和sgid [root@centos7 lab]# find / -perm -6000 2>/dev/null |xargs ls -l -rwsr-sr-x. 1 abrt abrt 15344 Oct 2 2020 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache ```
-perm /mode,例如mode为764,则ugo只要有一个满足即可:user至少为7、group至少为6、other至少为4。
```bash [root@centos7 lab]# chmod a=- file-1 [root@centos7 lab]# chmod uo=-,g=rwx file-2 [root@centos7 lab]# chmod ug=-,o=r file-3
[root@centos7 lab]# find -name 'file-*' -perm /764| xargs ls -l ----rwx---. 1 root root 0 Dec 24 20:28 ./file-2 -------r--. 1 root root 0 Dec 24 20:28 ./file-3
# 查找系统中具有特殊权限的文件:suid、sgid、sticky [root@centos7 lab]# find / -perm /7000
# 查找系统中具有特殊权限的文件:suid或者sgid [root@centos7 lab]# find / -perm /6000 ```
-readable -writable -executable,文件具有可读、可写、可执行。
根据文件 size 查找
单位 c(字节)、k(KiB)、M(MiB)、G(GiB)
# 大小等于10M
[root@server ~ 18:16:15]# find -size 10M
# 大小大于10G
[root@server ~ 18:16:21]# find -size +10G
# 大小小于10k
[root@server ~ 18:16:27]# find -size -10k
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
....
# 大小等于1M
[root@server ~ 18:16:33]# find -size 1M
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
......
# 注意:size会取整为1个单位,所以find -size 1M结果包含小于1M的文件。
# 可以使用1024k取代1M
[root@server ~ 18:17:55]# find -size 1024k
根据文件 time 查找
-amin, -cmin, -mmin 单位1分钟。
-atime, -ctime, -mtime 单位24小时。
-newer file,比file新的文件。
# 10分钟前(正好10分钟)
[root@server ~ 18:20:02]# find -amin 10
# 10分钟以前(大于10分钟)
[root@server ~ 18:29:58]# find -amin +10
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.pki
./.pki/nssdb
./.viminfo
./lab
./lab/file-1
./lab/file-2
./lab/file-3
# 10分钟以内(小于10分钟)
[root@server ~ 18:30:04]# find -amin -10
根据文件硬链接数和 inum 查找
# 硬链接数等于、大于、小于3的文件
[root@server ~ 18:30:09]# find -links 3
./.pki
[root@server ~ 18:33:58]# find -links +3
.
[root@server ~ 18:34:04]# find -links -3
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.pki/nssdb
./.viminfo
./lab
./lab/file-1
./lab/file-2
./lab/file-3
# inode为67160130的文件
[root@server ~ 18:34:10]# ls -i /etc/fstab
67160130 /etc/fstab
[root@server ~ 18:34:36]# find / -inum 67160130| xargs ls -i
67160130 /etc/fstab
多条件表达式
逻辑与: expr1 -a expr2 或者 expr1 expr2
bash [root@centos7 lab]# find . -name ‘file-*’ -perm /764
逻辑或:expr1 -o expr2
# 例如 [root@centos7 ~]# find / -perm /7000 # 等效与 [root@centos7 ~]# find / -perm -4000 -o -perm -2000 -o -perm -1000 ```
逻辑非:! expr
bash [root@centos7 ~]# find / ! -size -200M 2>/dev/null /proc/kcore /proc/59432/task/59432/fd/5 /proc/59432/task/59432/fdinfo/5 /proc/59432/fd/6 /proc/59432/fdinfo/6 [root@centos7 ~]# ls -lh /proc/kcore -r--------. 1 root root 128T Dec 24 22:00 /proc/kcore
action
-delete,查出找到的文件。
```bash [root@centos7 ~]# find / -name PASSWD /etc/PASSWD
[root@centos7 ~]# find / -name PASSWD -delete [root@centos7 ~]# find / -name PASSWD ```
-ls,相当于 ls -dils 查看找到的文件。
```bash [root@centos7 ~]# find /etc/ -name passwd /etc/pam.d/passwd /etc/passwd
[root@centos7 ~]# find /etc/ -name passwd -ls 67584593 4 -rw-r--r-- 1 root root 188 Apr 1 2020 /etc/pam.d/passwd 69030856 4 -rw-r--r-- 1 root root 2315 Dec 24 19:59 /etc/passwd ```
-exec command \;,找到文件后,执行相应的command。
bash [root@centos7 ~]# find /etc/ -name passwd -exec echo haha \; haha haha
-exec command {} \;,找到文件后,可以在命令中对文件操作。
bash [root@centos7 ~]# find / -inum 67160130 /etc/fstab # 将inode为67160130的所有文件复制到当前目录 [root@centos7 ~]# mkdir findfiles [root@centos7 ~]# find / -inum 67160130 -exec cp -a {} ./findfiles \; [root@centos7 ~]# ls findfiles/fstab fstab
Linux 硬盘分区管理
硬盘为什么要分区?
将操作系统文件与用户文件分隔开,避免应用数据太多撑满操作系统盘。
限制应用或用户的可用空间。
如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上其他分区。
用于创建交换分区。
限制磁盘空间使用,以提高诊断工具和备份镜像的性能。
便于定制文件系统,例如有的文件系统存放大量小文件,有的文件系统存放大量大文件。
MBR 分区方案
自 1982年以来,主启动记录(MBR)分区方案指定了在运行 BIOS 固件的系统上如何对磁盘进行分区
该方案支持最多4个主分区。
在 Linux系统上,管理员可以使用扩展分区和逻辑分区来创建最多 15个分区。
逻辑分区是可以格式化(format),扩展分区是不可以格式化。
MBR 记录用4个字节(1byte=8bit)存储分区的总扇区数,最大能表示2的32次方的扇区个数,按每扇区512字节计算,每个分区最大不能超过 2 TiB。
类比为:笔记本只有4个usb接口,如果不够用,则将第四个usb接口外接扩展坞,由扩展坞提供多个usb接口。
通常,我们将磁盘第一个扇区称为主引导扇区,位于硬盘的柱面0、磁头0、扇区1的位置,这一扇区包含MBR引导代码,承担系统启动职能。它不属于磁盘上任何分区,因而分区空间内的格式化命令不能清除主引导记录的任何信息。
主引导扇区由三个部分组成:

引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。
磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。
结束标志(占2个字节),其值为AA55(十六进制)。
fdisk 工具
fdisk 工具可用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况进行划分分区。
fdisk 命令语法
[root@server ~ 18:34:42]# fdisk -h
用法:
fdisk [选项] <磁盘> 更改分区表
fdisk [选项] -l <磁盘> 列出分区表
fdisk -s <分区> 给出分区大小(块数)
选项:
-b <大小> 扇区大小(512、1024、2048或4096)
-c[=<模式>] 兼容模式:“dos”或“nondos”(默认)
-h 打印此帮助文本
-u[=<单位>] 显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
-v 打印程序版本
-C <数字> 指定柱面数
-H <数字> 指定磁头数
-S <数字> 指定每个磁道的扇区数
除了使用-l选项查看分区表,其他选项暂时都不用。
查看分区
fdisk工具大部分操作通过交互式完成,出了查看分区表。
DOS disklabel 指的硬盘管理方式是MBR。
我们使用上一章准备的一块硬盘/dev/sdb
# 方法1:
[root@server ~ 18:44:48]# fdisk -l /dev/sdb
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
# 方法2:
[root@server ~ 18:44:54]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xf2aae81f 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xf2aae81f
设备 Boot Start End Blocks Id System
命令(输入 m 获取帮助):q
创建分区
[root@server ~ 18:50:30]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x984af3dd 创建新的 DOS 磁盘标签。
# 输入n,创建一个新分区
命令(输入 m 获取帮助):n
# 选择分区类型
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
# 直接回车,选择默认分区类型:primary
Select (default p): `回车`
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
# 设置分区结束位置,输入+2G,也就是起始位置之后2
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x984af3dd
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
# 输入w,保存更改并退出
# 输入q,不保存更改并退出
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
# 再次验证分区表变化
[root@server ~ 18:52:38]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 2G 0 part
注意:如果此时分区表未生成,执行以下命令,通知kernel重新生成分区表。有时候重启系统才会生成最新分区表。
[root@centos7 ~]# partprobe
删除分区
[root@server ~ 18:52:51]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
# 输入p,打印分区表
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x984af3dd
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
# 输入d,删除分区,因为只有1个分区,所以自动删除了第一个分区
命令(输入 m 获取帮助):d
已选择分区 1
分区 1 已删除
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x984af3dd
设备 Boot Start End Blocks Id System
命令(输入 m 获取帮助):q
如果硬盘空间比较大,需要的文件系统数量超过4个,那么就需要借助扩展分区创建逻辑分区了。
如下示例,创建第4个分区的时候,类型选择扩展分区;创建第5个分区的时候,类型选择逻辑分区。
[root@server ~ 18:58:27]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
# 再次创建一个容量为3G的分区
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
分区号 (2-4,默认 2):
起始 扇区 (4196352-41943039,默认为 4196352):
Last 扇区, +扇区 or +size{K,M,G} (6291456-41943039,默认为 41943039):+3G
分区 2 已设置为 Linux 类型,大小设为 3 GiB
# 查看分区表
[root@server ~ 19:02:23]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 3G 0 part
└─sdb3 8:19 0 1023M 0 part
创建分区演示完成,只保留1个分区,多余的删除。
[root@server ~ 19:02:30]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):d
分区号 (1-3,默认 3):
分区 3 已删除
命令(输入 m 获取帮助):d
分区号 (1,2,默认 2):
分区 2 已删除
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
# 查看分区表
[root@server ~ 19:03:47]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 2G 0 part
GPT 分区方案
GPT是运行**统一可扩展固件接口(UEFI)**固件系统上硬盘分区表的标准。
MBR分区方案只能管理最大2TiB分区和磁盘。全局唯一标识分区表(GPT,GUID Partition Table)使用8个字节(1byte=8bit)存储分区的总扇区数,可支持最多18 ZiB(=264512 Byte),即18亿*太字节的分区和磁盘。
MBR分区方案支持最多15个分区。GPT分区方案最多可提供128个分区。
GPT 提供分区表信息的冗余。
GPT分区表结构如下:
LBA0(保护性MBR),分为两个部分:
第一部分是与MBR中446字节相似的区块,用于存放第一阶段的启动引导程序。
第二部分是与MBR分区表记录区对应,该区域存储一个特殊标识符0xEE,用于表示该磁盘为GPT格式。若磁盘管理程序(比较老的软件)无法识别该磁盘,不能修改这个分区信息,进一步保护磁盘。
LBA1(GPT表头记录),记录了分区表自身的位置和大小,同时也记录了前面提到备份用的GPT分区所在位置(最后34个LBA),还放置了分区表的校验码(CRC32),校验码的作用是让操作系统判断GPT的正确与否,倘若发现错误则可以从备份的GPT中恢复正常运行。
LBA2-33,记录分区信息,每个LBA可以提供4组的分区记录,默认情况下可以有4×32=128组分区记录。因为每个LBA都有512字节,所以每组分区记录所占128字节,除去每组记录需要的标识符和相关记录信息外,GPT在每组记录中提供了64位记载分区的扇区总数。
虽然GPT最大支持128个分区,但是实际使用过程中分区超过第120个会出现无法格式化使用的情况。
gdisk 工具
gdisk工具用于管理采用GPT分区方案的磁盘分区,主要用于管理磁盘容量超过2T的磁盘。
gdisk命令语法
gdisk [ -l ] device
查看分区表
[root@server ~ 19:07:43]# yum install -y gdisk
# 这里显示识别到了MBR分区方案
[root@server ~ 19:08:47]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory.
***************************************************************
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 8C7A4B38-0B96-4DFF-ACCE-F6C8CDF46A9F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number Start (sector) End (sector) Size Code Name
转换分区表方案
gdisk工具用于管理gpt分区,所以我们需要将磁盘的分区管理方案由MBR转换成GPT。
[root@server ~ 19:08:58]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
# 输入?,查看帮助信息。gdisk管理命令跟fdisk很相似
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
# 输入o,将磁盘的分区管理方案由MBR转换成GPT
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y
# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3A5CD49-784B-4317-8EDE-B0ED1FE600C8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number Start (sector) End (sector) Size Code Name
# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
创建分区
[root@server ~ 19:21:26]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
# 这里识别到了GPT分区方案
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
# 输入n,创建新分区
Command (? for help): n
Partition number (1-128, default 1):
# 设置分区起始位置
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:
# 设置分区结束位置
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'
# 设置分区类型,输入L查看所有分区类型
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM
a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap
a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID
a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap
a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+
af01 Apple RAID af02 Apple RAID offline af03 Apple label
af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot
bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT
eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit
Press the <Enter> key to see more codes:
ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition
fb00 VMWare VMFS fb01 VMWare reserved fc00 VMWare kcore crash p
fd00 Linux RAID
# 输入回车,选择默认分区类型
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3A5CD49-784B-4317-8EDE-B0ED1FE600C8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4196351 2.0 GiB 8300 Linux filesystem
更改分区名称
# 输入c,修改分区ID为1的分区名
Command (? for help): c
Using 1
# 输入新名称
Enter name: data01
# 查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3A5CD49-784B-4317-8EDE-B0ED1FE600C8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4196351 2.0 GiB 8300 data01
查看分区详细信息
Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: BFDEA53C-E965-4EB3-AE00-7C7D4B776603
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'
删除分区
# 输入d,删除分区ID为1的分区
Command (? for help): d
Using 1
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3A5CD49-784B-4317-8EDE-B0ED1FE600C8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number Start (sector) End (sector) Size Code Name
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
wipefs 工具
作用:查看和清除磁盘管理信息。
注意:数据无价,操作需谨慎,最好提前备份。
# 查看磁盘管理信息
[root@server ~ 19:31:08]# wipefs /dev/sdb
offset type
----------------------------------------------------------------
0x200 gpt [partition table]
# 清除未挂载磁盘的分区表
[root@server ~ 19:32:28]# wipefs -a /dev/sdb
/dev/sdb:8 个字节已擦除,位置偏移为 0x00000200 (gpt):45 46 49 20 50 41 52 54
/dev/sdb:8 个字节已擦除,位置偏移为 0x4fffffe00 (gpt):45 46 49 20 50 41 52 54
/dev/sdb:2 个字节已擦除,位置偏移为 0x000001fe (PMBR):55 aa
/dev/sdb: calling ioclt to re-read partition table: 成功
# 禁止使用 -f 强制清除分区表
[root@server ~ 19:32:34]# wipefs -fa /dev/sdb
parted 工具
parted 工具既可以管理采用 MBR 分区方案的磁盘,又可以管理采用GPT分区方案的磁盘。
parted 命令同时支持交互式操作和非交互式操作(编写脚本)。
我们先来看看交互式操作。
操作流程:
查看分区表。如果是未初始化硬盘,创建分区。
设置单位(MiB)
创建分区
调整分区大小
调整分区类型
删除分区
查看分区表
[root@server ~ 19:32:34]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
# 查看帮助,输入help
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt)
alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available
devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition
NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright
information of GNU Parted
# 查看分区表,输入 print
(parted) mklabel msdos
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
设置单位
parted工具默认单位是MB(103K),设置为MiB(210KiB)
(parted) unit MiB
管理 MBR 磁盘
设置磁盘分区管理方案
# 输入mklabel或mktable设置磁盘分区管理方案
# 设置分区方案为msdos,也就是MBR,输入mklabel msdos
(parted) mklabel msdos
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
# 确认更改,输入y
是/Yes/否/No? y
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
# 分区表已改成msdos
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
==注意:==parted命令所做更改立刻生效。
创建分区
# 创建分区,输入 mkpart
(parted) mkpart
# 设置分区类型,输入 primary
分区类型? primary/主分区/extended/扩展分区? primary
# 设置分区文件系统类型,输入 xfs,实际不生效,格式化文件系统仍需手动操作
文件系统类型? [ext2]? xfs
# 设置分区起始位置,输入 1
起始点? 1
# 设置分区结束位置,输入 2049
结束点? 2049
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 1049kB 2049MB 2048MB primary
扩展分区
# 1 代表分区号,4097代表分区结束位置
(parted) resizepart 1 4097
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 1049kB 4097MB 4096MB primary
删除分区
(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
免交互操作
# 设置磁盘分区管理方案
[root@server ~ 19:48:48]# parted /dev/sdb mklabel msdos
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
是/Yes/否/No? y
信息: You may need to update /etc/fstab.
# 查看分区
[root@server ~ 19:49:01]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
# 创建分区
[root@server ~ 19:49:27]# parted /dev/sdb unit MiB mkpart primary 1 2049
信息: You may need to update /etc/fstab.
[root@server ~ 19:49:38]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 1.00MiB 2049MiB 2048MiB primary
# 扩展分区
[root@server ~ 19:49:49]# parted /dev/sdb unit MiB resizepart 1 5121
信息: You may need to update /etc/fstab.
[root@server ~ 19:52:29]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 1.00MiB 5121MiB 5120MiB primary
# 删除分区
[root@server ~ 19:52:36]# parted /dev/sdb rm 1
信息: You may need to update /etc/fstab.
[root@server ~ 19:53:05]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
管理 GPT 磁盘
管理GPT磁盘基本与管理MBR磁盘一致,除了创建分区。
设置磁盘分区管理方案
# 设置分区方案为gpt,输入mklabel gpt
[root@server ~ 19:54:14]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
是/Yes/否/No? y
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
# 分区表已改成gpt
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
创建分区
(parted) unit MiB
(parted) mkpart
# 设置分区名
分区名称? []? data01
文件系统类型? [ext2]? xfs
起始点? 1
结束点? 2049
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 2049MiB 2048MiB data01
扩展分区
(parted) resizepart 1 5121
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 5121MiB 5120MiB data01
删除分区
(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
免交互操作
# 设置磁盘分区管理方案
[root@server ~ 19:58:58]# parted /dev/sdb mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
是/Yes/否/No? y
信息: You may need to update /etc/fstab.
# 查看分区
[root@server ~ 19:59:10]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
# 创建分区
[root@server ~ 19:59:37]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.
[root@server ~ 20:00:21]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 2049MiB 2048MiB data01
# 扩展分区
[root@server ~ 20:00:29]# parted /dev/sdb unit MiB resizepart 1 5121
信息: You may need to update /etc/fstab.
[root@server ~ 20:01:00]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 5121MiB 5120MiB data01
# 删除分区
[root@server ~ 20:01:05]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1.00MiB 5121MiB 5120MiB data01
文件系统持久化挂载
环境准备
利用 parted 创建一个分区,并格式化为xfs文件系统。
[root@server ~ 20:01:36]# parted /dev/sdb mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
是/Yes/否/No? y
信息: You may need to update /etc/fstab.
[root@server ~ 20:03:00]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.
[root@server ~ 20:03:12]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 如果格式化时候,提示存在文件系统,则需要-f选项强制格式化
[root@server ~ 20:03:22]# 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@server ~ 20:03:37]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
持久化挂载
当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /etc/fstab文件中添加一个条目。
/etc/fstab 是以空格分隔的文件,每行具有六个字段。
第一个字段指定设备。可以使用UUID或device来指定设备。
第二个字段是目录挂载点。通过它可以访问目录结构中的块设备。挂载点必须存在;如果不存在,请使用mkdir命令进行创建。
第三个字段包含文件系统类型,如xfs或ext4 。
第四个字段是挂载选项,以逗号分隔的。 defaults是一组常用选项。详细信息参考mount(8) 。
第五个字段指定dump命令是否备份设备。
第六个字段指定fsck顺序字段,决定了在系统启动吋是否应运行fsck命令,以验证文件系统是否干净。 该字段中的值指示了 fsck的运行顺序。 对于XFS文件系统, 请将该字段设为0 ,因为XFS并不使用fsck来检查自己的文件系统状态。 对于ext4 文件系统,如果是根文件系统, 请将该字段设 为 1 ; 如果是其他ext4 文件系统, 则将该字段设为2。 这样, fsck就会先处理根文件系统,然后同步检查不同磁盘上的文件系统,并按顺序检查同一磁盘上的文件系统。
示例:
[root@server ~ 20:03:43]# blkid /dev/sdb1
/dev/sdb1: UUID="37cb4456-5374-420b-921b-5f5d6c8b12d3" TYPE="xfs" PARTLABEL="data01" PARTUUID="6d1b4734-8878-4a92-aa6c-c3ce88c39fca"
[root@server ~ 20:06:06]# vim /etc/fstab
# 最后一行增加一个条目
UUID="37cb4456-5374-420b-921b-5f5d6c8b12d3" TYPE="xfs" PARTLABEL="data01"
# 使用如下命令立刻挂载
[root@server ~ 20:11:19]# mount /data01
mount: /dev/sdb1 已经挂载或 /data01 忙
/dev/sdb1 已经挂载到 /data01 上
# 验证
[root@server ~ 20:11:26]# df -h /data01
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 33M 2.0G 2% /data01
# 重启系统验证
[root@server ~ 20:11:38]# reboot
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(server) at 20:12:36.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
Connecting to 10.1.8.10:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed May 20 18:44:48 2026 from 10.1.8.1
[root@server ~ 20:12:55]# df -h /data01
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.6G 49G 4% /
取消持久化挂载
删除/etc/fstab中对应条目即可。
综合案例:文件系统空间不足
情况1:大量的大文件占用空间
准备环境
[root@server ~ 20:13:03]# parted /dev/sdb mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
是/Yes/否/No? y
信息: You may need to update /etc/fstab.
[root@server ~ 20:14:04]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.
[root@server ~ 20:14:11]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server ~ 20:14:18]# mkdir /myapp-1
[root@server ~ 20:14:24]# mount /dev/sdb1 /myapp-1
[root@server ~ 20:14:31]# cp -r /etc/ /myapp-1/
# 创建一个大文件
[root@server ~ 20:14:37]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: 写入"/myapp-1/etc/bigfile" 出错: 设备上没有空间
记录了1973+0 的读入
记录了1972+0 的写出
2067988480字节(2.1 GB)已复制,5.84044 秒,354 MB/秒
原因:大文件占用大量空间。
解决方法:找到文件后删除。
[root@server ~ 20:15:17]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 2.0G 616K 100% /myapp-1
# 方法1:find 查找
[root@server ~ 20:16:15]# find /myapp-1/ -size +100M
/myapp-1/etc/bigfile
# 方法2:du 查找
[root@server ~ 20:16:22]# du -s /myapp-1/* |sort -n |tail -2
2052056 /myapp-1/etc
[root@server ~ 20:16:34]# du -s /myapp-1/etc/* |sort -n |tail -2
19452 /myapp-1/etc/selinux
2019520 /myapp-1/etc/bigfile
[root@server ~ 20:16:40]# du -s /myapp-1/etc/bigfile/* |sort -n |tail -2
du: 无法访问"/myapp-1/etc/bigfile/*": 不是目录
# 删除大文件
[root@server ~ 20:16:46]# rm -f /myapp-1/etc/bigfile
[root@server ~ 20:18:19]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 66M 2.0G 4% /myapp-1
找到文件后,删除即可。
情况2:删除文件后,空间没有释放
准备环境
[root@server ~ 20:18:24]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
dd: 写入"/myapp-1/etc/bigfile" 出错: 设备上没有空间
记录了1973+0 的读入
记录了1972+0 的写出
2067857408字节(2.1 GB)已复制,0.672109 秒,3.1 GB/秒
[root@server ~ 20:19:13]# tail -f /myapp-1/etc/bigfile &
[1] 1261
# 删除文件后,空间没有释放
[root@server ~ 20:19:21]# rm -f /myapp-1/etc/bigfile
[root@server ~ 20:19:44]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 2.0G 744K 100% /myapp-1
tail: 无法监视"/myapp-1/etc/bigfile": 没有那个文件或目录
#原因:被删除的文件,仍然有程序在使用。
#解决方法:找到像一个的程序并,终止程序。
[root@server ~ 20:22:22]# lsof |grep delete |grep /myapp-1
[root@server ~ 20:22:33]# kill 1261
[root@server ~ 20:22:49]# df -h /myapp-1/
文件系统 容量 已用 可用 已用% 挂载点
/dev/sdb1 2.0G 66M 2.0G 4% /myapp-1
建议:清理大文件,先使用重定向清空文件内容,再删除文件。
情况3:大量的小文件占用空间
准备环境
[root@server ~ 20:23:08]# parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073
信息: You may need to update /etc/fstab.
[root@server ~ 20:24:38]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@server ~ 20:24:44]# mkdir /myapp-2
[root@server ~ 20:24:54]# df -i /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/mapper/centos-root 26214400 32299 26182101 1% /
root@server ~ 20:25:00]# touch /myapp-2/file-{00001..65530}
[root@server ~ 20:25:23]# df -h /myapp-2/
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.7G 49G 4% /
原因:文件系统中inode使用完了。
解决方法:删除大量的小文件或者将这些小文件备份到其他地方。
[root@server ~ 20:25:43]# df -hi /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/mapper/centos-root 25M 96K 25M 1% /
[root@server ~ 20:28:41]# rm -f /myapp-2/file-0*
[root@server ~ 20:29:00]# cat /myapp-2/file-{00001..10000} > file-00001
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐
所有评论(0)