|NO.Z.00042|——————————|^^ 操作 ^^|——|Linux&磁盘管理.V01|——|常用操作|磁盘分区|Prited分区|Swap分区|
一、文件系统管理
### --- 磁盘
~~~ 硬盘的转速:7200转/分钟
~~~ SATA接口:620兆/秒
~~~ 每个扇区的大小事固定的,为 512Byte。扇区也是磁盘的最小存贮单位。
~~~ 硬盘的大小是使用“磁头数×柱面数×扇区数×每个扇区的大小
### --- 超级块:
~~~ super block:记录整个文件系统的信息,包括block与inode的总量
### --- 数据块:
~~~ date block:用来实际保存数据的;block的大小1kB,2KB,4KB。
### --- 标签:inode:
~~~ 用来记录文件的权限所有者及所属组,文件大小等。每个文件需要占用一个inode
### --- 常用命令
### --- df :查看分区情况
~~~ -a 显示特殊文件系统,这些文件系统几乎都是保存在内存中的。如/proc,因为是挂载在内存中,所以占用量都是 0
~~~ -h 单位不再只用 KB,而是换算成习惯单位
~~~ -T 多出了文件系统类型一列
### --- du:统计目录或者文件大小
~~~ ll只会统计这个文件的block的大小;不会统计block内子文件的文件名占用空间,不会统计block内子文件的攻击大小
~~~ du 与 df 的区别:du 是用于统计文件大小的,统计的文件大小是准确的;df 是用于统计空间大小的,统计的剩余空是准确的
### --- 手工释放占用空间
[root@server11 ~]# lsof |grep deleted // 风险极高;找到文件后删除对应的文件释放
~~~ fsck文件系统修复命令 // 系统开机时会自动执行。
[root@server11 ~]# fsck -y /dev/sda1
### --- 查看磁盘状态信息
[root@server21 ~]# dumpe2fs /dev/sda1
### --- 只看超级块信息
[root@server21 ~]# dumpe2fs -h /dev/sda1
### --- 查看文件的详细信息
[root@server21 ~]# stat yanqi
### --- 判断文件类型
[root@server21 ~]# file yanqi
[root@server21 ~]# file yanqimulu/
二、磁盘分区
### --- 添加一块新的硬盘:
[root@server21 ~]# lsblk
sdb 8:16 0 100G 0 disk // sdb是一块全新的硬盘,没有做任何分区
### --- 为sdb2分区
~~~ 报错:Device or resource busy:需要重启后会生效,说明分区表正忙。但是不会影响分区操作
[root@server21 ~]# fdisk /dev/sdb
~~~ 分主分区
Command (m for help):m // m表示获取帮助
Command (m for help): p // 打印分区信息
Command (m for help): l // l可以查看到分区号是哪一个;默认不写就是标准分区,83ID号
Command (m for help): n // n新建分区
Command action
e extended // 扩展分区
p primary partition (1-4) // 主分区 1个主分区,在主分区下新建扩展分区
p // 新建主分区
Partition number (1-4): 1 // 选择主分区号,建议不要跳,若是跳过前面的会不可以使用,最好从1开始
First cylinder (1-13054, default 1): // 选择其实柱面;一圈柱面大概80多兆;默认是从第1个柱面开始分
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +20G // 允许可以写成字节数,柱面大小自己可以定义;最小分区单位是81个兆;加字节单位才是按大小来算的,若是不加柱面是按柱面来算
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux // 第一个新建完成
### --- 分扩展分区;把剩余所有空间给扩展分区
e
Partition number (1-4): 2
First cylinder (2613-13054, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-13054, default 13054):
Using default value 13054
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux
/dev/sdb2 2613 13054 83875365 5 Extended
### --- 逻辑分区
Command (m for help): n
Command action
l logical (5 or over) // 创建完扩展分区,就会显示l逻辑分区
p primary partition (1-4)
~~~ 省略了选择磁盘ID号的过程
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (2613-13054, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-13054, default 13054): +2G
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux
/dev/sdb2 2613 13054 83875365 5 Extended
/dev/sdb5 2613 2874 2104483+ 83 Linux
Command (m for help): w // 保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
### --- 查看分区
[root@server21 ~]# lsblk
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 20G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
[root@server21 ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux // 可以格式化
/dev/sdb2 2613 13054 83875365 5 Extended // 不可以格式化;因为它是扩展分区
/dev/sdb5 2613 2874 2104483+ 83 Linux // 可以格式化
### --- 格式化
[root@server21 ~]# mkfs -t ext4 /dev/sdb1 // mkfs不可以调整数据库大小
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@server21 ~]# mkfs -t ext4 /dev/sdb5
~~~ mke2fs // 可以调整数据块大小
[root@server21 ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux
/dev/sdb2 2613 13054 83875365 5 Extended
/dev/sdb5 2613 2874 2104483+ 83 Linux
### --- 挂载
~~~ 分区分号之后,理论上分区大小是不可以调整的
[root@server21 ~]# mkdir /disk1
[root@server21 ~]# mkdir /disk5
[root@server21 ~]# mount /dev/sdb1 /disk1
[root@server21 ~]# mount /dev/sdb1 /disk5
[root@server21 ~]# mount
/dev/sdb1 on /disk1 type ext4 (rw)
/dev/sdb1 on /disk5 type ext4 (rw)
[root@server21 ~]# df -h
/dev/sdb1 20G 44M 19G 1% /disk1
/dev/sdb1 20G 44M 19G 1% /disk5
### --- 添加自动挂载
~~~ 自动挂载,只允许把固定设备添加自动挂载;挂载可以为磁盘分区号或者UUID
~~~ 查询磁盘的UUID
[root@server21 ~]# vim /etc/fstab
[root@server21 ~]# ll /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Feb 4 10:14 11baea76-8f6e-4a32-acd5-3a269ca6b319 -> ../../sdb5
lrwxrwxrwx 1 root root 10 Feb 4 10:14 c0f09e0d-9507-4083-bfd7-4ae872021028 -> ../../sdb1
[root@server21 ~]# vim /etc/fstab
UUID=c0f09e0d-9507-4083-bfd7-4ae872021028 /disk1 ext4 defaults 0 0
UUID=11baea76-8f6e-4a32-acd5-3a269ca6b319 /disk5 ext4 defaults 0 0
OR
/dev/sdb1 /disk1 ext4 defaults 0 0
/dev/sdb5 /disk5 ext4 defaults 0 0
~~~ 设备文件名:挂载点:文件系统:挂载选项:1是否可以被备份,0不备份,1每天备份,2不定期备份:2是否检测磁盘fsck,0不检测,1启动时检测2启动后检测;
~~~ #只有/分区在启动时会去检测,其他磁盘不需要添加检测
~~~ 重启验证
[root@server21 ~]# mount
/dev/sdb1 on /disk1 type ext4 (rw)
/dev/sdb5 on /disk5 type ext4 (rw)
### --- 模拟配置文件写错,重启报错
~~~ /etc/fstab修复
~~~ /etc/fstab/文件修复
[root@server21 ~]# cat /etc/fstab
/dev/sdb1 /disk1 ext4 defaults 0 0
/dev/sdb /disk5 ext4 defaults 0 0
~~~ 实验验证只有设备名写错的时候会报错,其他参数时不会发生报错信息的。
[root@localhost ~]# mount -a
mount: /dev/sdb already mounted or /disk5 busy
### --- 重启系统
~~~ 报错信息
[root@server21 ~]# reboot
Give root password for maintenance
(or type Control-D to continue): // 输入root密码 #ctrl+D会重启
### --- 解决方案
~~~ 现在就可以编写配置文件保存退出了。
[root@localhost ~]# vim /etc/fstab // 修改配置文件即可;但是文件是只读权限,需要重新挂载,把根分区挂载即可
[root@localhost ~]# mount -o remount,rw /
三、parted分区
### --- 清除之前的自动挂载操作
### --- 修改成GPT分区
[root@server21 ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help // 使用help执行帮助
(parted) print // 打印
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos // 分区类型
Number Start End Size Type File system Flags
1 32.3kB 21.5GB 21.5GB primary ext4
2 21.5GB 107GB 85.9GB extended
5 21.5GB 23.6GB 2155MB logical ext4
Warning: Partition(s) on /dev/sdb are being used.
Ignore/Cancel? Ignore // 忽略或者放弃;
Warning: 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? yes // yes再次确认
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sdb (Device or resource busy). As a result, it may not reflect all of your changes until after reboot. #分区很忙,需要下次重启后生效
(parted) print // 查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt // 分区有了
Number Start End Size File system Name Flags // 之前旧的分区没有了。
(parted) quit
[root@server21 ~]# reboot // 重启
### --- 建立分区
[root@server21 ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
~~~ ### --- 调整分区大小
(parted) mkpart // 创建分区,后面不要参数,全部靠交互指令
Partition name? []? disk1 // 分区名称,自己定义
File system type? [ext2]? // 文件系统类型,直接回车
Start? 1MB // 起始点,分区从1MB开始
End? 20GB // 结束点,分区到20GB结束 #分区完成
(parted) print
Number Start End Size File system Name Flags // 查看新建的分区
1 1049kB 20.0GB 20.0GB disk1 // 此刻是没有文件系统的,下面进行格式化分区创建文件系统
~~~ ### --- 建立分文件系统
(parted) mkfs // 创建分区
Yes/No? yes // 警告格式化数据会丢失,
Partition number? 1 // 分区编号,1一个分区
File system type? [ext2]? // 指定文件系统类型,写别的也没有用直接回车
(parted) print
Number Start End Size File system Name Flags
1 1049kB 20.0GB 20.0GB ext2 disk1 // 文件系统显示出来
(parted) quit // 退出
Information: You may need to update /etc/fstab.
~~~ 查看新建的分区
[root@server21 ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sdb1 1 13055 104857599+ ee GPT
### --- 格式为ext4文件系统
[root@server21 ~]# mkfs -t ext4 /dev/sdb1
### --- 挂载
[root@server21 ~]# mount /dev/sdb1 /disk1/
[root@server21 ~]# mount
/dev/sdb1 on /disk1 type ext4 (rw)
### --- 设置开机自动启动
[root@server21 ~]# vim /etc/fstab
/dev/sdb1 /disk1 ext4 defaults 0 0
### --- 调整分区大小:现在磁盘是20GB,改为50GB
### --- 删除分区
~~~ 自动挂载需要卸载,卸载挂载。切记
[root@server21 ~]# umount /dev/sdb1
[root@server21 ~]# parted /dev/sdb1
(parted) print
Disk /dev/sdb1: 20.0GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Number Start End Size File system Flags
1 0.00B 20.0GB 20.0GB ext4
四、手动添加swap分区
### --- 手动添加swap分区
### --- 查看内存命令
~~~ buffers:缓冲:是用来加速硬盘的写入速度
~~~ cached:缓存:是用来加速硬盘的读取速度
[root@server21 ~]# free -h
total used free shared buffers cached
Mem: 1.8G 154M 1.7G 248K 5.9M 42M
-/+ buffers/cache: 106M 1.7G
Swap: 4.0G 0B 4.0G
### --- 新建一个swap分区5GB
[root@server11 ~]# fdisk /dev/sdb
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-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +5G
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
~~~ 默认使用的是linux的标准的分区号:83
Command (m for help): l
82 Linux swap / // swap的ID号是82
### --- 手动swap分区的ID号
Command (m for help): t // t修改分区号
Selected partition 1
Hex code (type L to list all codes): 82
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris
~~~ 分区号改为82了;swap的分区
Command (m for help): w // 保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
### --- 格式化mkswap
[root@server11 ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=f2bbbc8d-5e3b-4cd4-9376-ff8a82d5679e
~~~ 查看新建的,还没有加载过来,swap还是之前的大小
[root@server11 ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 183M 1.1G 17M 527M 1.4G
Swap: 4.0G 0B 4.0G
### --- 临时加载到swap分区中,
[root@server11 ~]# swapon /dev/sdb1
[root@server11 ~]# free -h // swap分区为9GB,说明加载成功
total used free shared buff/cache available
Mem: 1.8G 186M 1.1G 17M 527M 1.4G
Swap: 9.0G 0B 9.0G
### --- 永久生效加载swap分区
~~~ 或者以UUID的方式写入自动挂载文件
[root@server11 ~]# vim /etc/fstab
/dev/sdb1 swap swap defaults 0 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
cdv005-basic
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」