硬盘分区管理以及文件系统管理

张开发
2026/4/15 5:47:57 15 分钟阅读

分享文章

硬盘分区管理以及文件系统管理
一、硬盘分区管理大容量的硬盘分区使用C盘系统盘D盘办公E盘娱乐。类似于买了一个房子100平方隔断主卧、次卧1、次卧2、厨房、卫生间。1.1识别硬盘设备接口类型设备命名示例说明SATA/SAS/USB/SCSI/dev/sda、/dev/sdb...物理机常用的磁盘设备命名virtio-blk虚拟机/dev/vda、/dev/vdb...KVM 虚拟机常用磁盘命名NVMe SSD/dev/nvme0n1、/dev/nvme1n1...高性能 NVMe 固态硬盘命名SD/MMC/eMMC/dev/mmcblk0、/dev/mmcblk1...嵌入式设备常用存储命名虚拟机添加2个硬盘sata 接口20GNVMe接口40G关机添加#查看块block设备 [rootcentos7 ~ 09:45:52]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 199G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 2G 0 lvm [SWAP] └─centos-home 253:2 0 147G 0 lvm /home sdb 8:16 0 20G 0 disk sr0 11:0 1 4.4G 0 rom nvme0n1 259:0 0 40G 0 disk设备类型块设备b大部分情况下都是存储设备例如光盘、硬盘、U盘。字符设备c一般是输入和输出设备例如终端设备tty1、vty、pts/0等1.2硬盘分区方案直接管理主板上外接设备的系统称之为固件。例如调整设备启动顺序光盘作为第一启动项用来安装系统。1.2.1MBR自1982年起主启动记录MBR分区方案成为运行BIOS 固件系统的磁盘分区标准。主分区数量限制最多支持4个主分区。扩展分区与逻辑分区Linux 系统中可通过“主分区扩展分区”的方式突破主分区限制扩展分区仅作为逻辑分区的“容器”不可格式化逻辑分区可格式化使用整体最多可创建15个分区。容量限制MBR 用4个字节存储分区总扇区数按单扇区512字节计算单个分区最大容量不超过2 TiB。1.2.2GPT全局唯一标识分区表GPT是运行统一可扩展固件接口UEFI系统的磁盘分区标准。容量支持使用8个字节存储分区总扇区数单分区/磁盘最大支持9.44 ZB2⁶⁴ × 512 B完全满足大磁盘需求。实际空间受文件系统限制Linux XFS支持最大 8 EB。分区数量默认支持128个分区无需扩展分区/逻辑分区实际使用中建议不超过120个避免格式化异常。数据冗余GPT 分区表包含主表和备份表且自带 CRC32 校验码可自动检测并恢复损坏的分区表。1.2.3fdisk工具fdisk 工具用于管理 MBR 分区方案的硬盘新版本的fdisk也可以用来管理GPT。查看硬盘信息/dev device设备专门放硬盘分区U 盘光驱键盘鼠标显示器网卡部分所有硬件Linux 都把它变成一个文件放在 /dev 里。# 查看所有块设备进一步过滤出sd相关设备 # 此时存在sda和sdb两块硬盘 [rootcentos7 ~ 10:31:27]# fdisk -l | grep sd Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 419430399 208665600 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors # 查看特定硬盘信息 #可见sda分区成sda1和sda2两个区 [rootcentos7 ~ 10:33:40]# fdisk -l /dev/sda Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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 #MBR分区格式如果是GPT分区格式则会显示gpt Disk identifier: 0x000d8dd5 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 419430399 208665600 8e Linux LVM # 可见sdb 硬盘没有分区 [rootcentos7 ~ 10:35:44]# fdisk -l /dev/sdb 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帮助信息# 交互方式管理sdb硬盘 [rootcentos7 ~ 10:38:11]# 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x6b051453. # m 查看帮助 Command (m for help): m Command action 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 partitions system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)查看分区表# p 查看分区表 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: 0x6b051453 Device Boot Start End Blocks Id System # sdb硬盘是空盘没有分区创建分区# n 新建分区 Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, sectors or size{K,M,G} (2048-41943039, default 41943039): 5G Partition 1 of type Linux and of size 5 GiB is set # 再次查看多了一个分区 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: 0x6b051453 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux保存分区表# w 保存修改 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. #再次查看块设备 [rootcentos7 ~ 10:41:07]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 5G 0 part删除分区[rootcentos7 ~ 11:35:55]# 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. # d 删除分区 Command (m for help): d Selected partition 1 Partition 1 is deleted 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: 0x6b051453 Device Boot Start End Blocks Id System # 对应的分区被删除 # 保存退出 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [rootcentos7 ~ 11:36:33]# ldisk /dev/sdb -bash: ldisk: command not found1.2.4gdisk工具gdisk 工具用于管理 GPT 分区方案的硬盘。#安装工具 [rootcentos7 ~ 10:41:22]# yum install gdisk -y查看硬盘信息[rootcentos7 ~ 11:34:56]# 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): E893B005-7A66-45F2-AC18-8A4D73022299 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帮助信息[rootcentos7 ~ 11:37:43]# 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 dont want to convert your MBR partitions to GPT format! *************************************************************** Command (? for help): ? b back up GPT data to a file c change a partitions 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 partitions type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu查看分区表Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 6F6D8426-7ABD-48E2-BF20-41EA4E5388F1 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): 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}: 10G Current type is Linux filesystem Hex code or GUID (L to show codes, Enter 8300): Changed type of partition to Linux filesystem Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 6F6D8426-7ABD-48E2-BF20-41EA4E5388F1 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 20971453 sectors (10.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 20973567 10.0 GiB 8300 Linux filesystem保存分区表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. #再次查看 [rootcentos7 ~ 11:40:38]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 10G 0 part删除分区[rootcentos7 ~ 11:41:00]# gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 6F6D8426-7ABD-48E2-BF20-41EA4E5388F1 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 20971453 sectors (10.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 20973567 10.0 GiB 8300 Linux filesystem 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): 6F6D8426-7ABD-48E2-BF20-41EA4E5388F1 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. #再次查看 [rootcentos7 ~ 11:41:51]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk二、文件系统基本管理# 提前准备好分区/dev/sdb1、/dev/sdb2 [rootcentos7 ~ 11:51:46]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 5G 0 part └─sdb2 8:18 0 10G 0 part2.1文件系统格式化格式化清空数据建文件系统这是挂载前必须做的一步。硬盘分区不能直接使用需要格式化成相应的文件系统ext4、xfs才能存储文件。将sdb1格式化为ext4#mkfs make filesystem创建文件系统格式化 #ext4是Linux最常用的文件系统格式 #/dev/sdb1 是要格式化的分区 [rootcentos7 ~ 11:51:50]# mkfs.ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label OS type: Linux Block size4096 (log2) Fragment size4096 (log2) Stride0 blocks, Stripe width0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block0 Maximum filesystem blocks1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done将sdb2格式化为xfs[rootcentos7 ~ 14:43:08]# mkfs.xfs /dev/sdb2 meta-data/dev/sdb2 isize512 agcount4, agsize655360 blks sectsz512 attr2, projid32bit1 crc1 finobt0, sparse0 data bsize4096 blocks2621440, imaxpct25 sunit0 swidth0 blks naming version 2 bsize4096 ascii-ci0 ftype1 log internal log bsize4096 blocks2560, version2 sectsz512 sunit0 blks, lazy-count1 realtime none extsz4096 blocks0, rtextents0对比项ext4xfs适合场景小文件多、通用场景大文件、高并发、数据库、大数据最大文件大小16TB8EB极大基本用不满最大分区大小1EB8EB删除文件慢一点非常快格式化速度中等极快CentOS 7 默认不是是2.2文件系统挂载硬盘挂载之后才能使用类似于windows中分配盘符C盘。挂载把一个“硬盘分区”和一个“文件夹”绑在一起分区/dev/sdb1 真正存数据的地方仓库文件夹/mnt/web 你访问的入口门2.2.1示例一挂载/dec/sdb1给web应用使用# 准备挂载点目录 [rootcentos7 ~ 14:43:15]# mkdir /mnt/web # 挂载/dev/sdb1到/mnt/web后续写入分区sdb1的数据通过路径/mnt/web引用。 [rootcentos7 ~ 14:43:22]# mount /dev/sdb1 /mnt/web # 查看设备和挂载点关系 [rootcentos7 ~ 14:43:46]# df -h /mnt/web Filesystem Size Used Avail Use% Mounted on /dev/sdb1 4.8G 20M 4.6G 1% /mnt/web # 写入测试 [rootcentos7 ~ 14:43:54]# echo Hello World /mnt/web/web.txt [rootcentos7 ~ 14:44:02]# cat /mnt/web/web.txt Hello World # 此时文件/mnt/web/web.txt存储到/dev/sdb1中。2.2.2示例二挂载/dev/sdb2给db应用使用[rootcentos7 ~ 14:44:08]# mkdir /mnt/db [rootcentos7 ~ 14:44:15]# mount /dev/sdb2 /mnt/db [rootcentos7 ~ 14:44:20]# df -h /mnt/db Filesystem Size Used Avail Use% Mounted on /dev/sdb2 10G 33M 10G 1% /mnt/db [rootcentos7 ~ 14:44:25]# echo Hello mysql /mnt/db/db.txt [rootcentos7 ~ 14:44:30]# cat /mnt/db/db.txt Hello mysql2.2.3实践部署web服务器web 服务器提供 web 页面分享提供一些动态网站例如购物。部署服务流程安装相关软件包准备相关材料配置服务启用并启动服务设置防火墙客户端访问测试# 1. 安装相关软件包 [rootcentos7 ~ 11:36:51]# yum install nginx -y # 2. 准备相关材料 # 使用/mnt/web 作为网站的根目录 [rootcentos7 ~ 13:55:04]# echo hello world from nginx /mnt/web/index.html # 3. 配置服务 [rootcentos7 ~ 13:55:06]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.hgq.cloud.conf [rootcentos7 ~ 14:00:05]# vim /etc/nginx/conf.d/vhost-www.hgq.cloud.conf server { server_name www.xxx.cloud; root /mnt/web; # 如果希望网站支持显示清单 autoindex on; # 设置字符集为utf-8防止乱码 charset utf-8; # 指定web网页主页文件 index welcome.html; } # 4. 启用并启动服务 [rootcentos7 ~ 14:08:04]# system enable nginx --now # 5. 设置防火墙 [rootcentos7 ~ 13:59:18]# systemctl stop firewalld # 6. 客户端访问测试 [rootcentos7 ~ 13:59:32]# vim /etc/hosts #添加域名解析 10.1.8.10 www.xxx.cloud www [rootcentos7 ~ 14:31:23]# curl http://www.xxx.cloud hello world from nginx2.3文件系统卸载文件系统不使用的时候可以卸载取消目录和设备之间的映射关系[rootcentos7 ~ 14:42:38]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 5G 0 part /mnt/web └─sdb2 8:18 0 10G 0 part /mnt/db # 1.通过挂载点卸载 [rootcentos7 ~ 14:45:08]# umount /mnt/db [rootcentos7 ~ 14:45:16]# lsblk /dev/sdb2 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb2 8:18 0 10G 0 part # 2.通过设备名称卸载 #查看设备id [rootcentos7 ~ 14:45:31]# blkid /dev/sdb2 /dev/sdb2: UUID16b28f2d-2a32-4aa9-814e-98a482701fac TYPExfs PARTUUIDb9379dba-bc87-4a19-baed-c045529908ce #使用UUID挂载 [rootcentos7 ~ 14:46:06]# mount UUID16b28f2d-2a32-4aa9-814e-98a482701fac /mnt/db [rootcentos7 ~ 14:48:41]# lsblk /dev/sdb2 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb2 8:18 0 10G 0 part /mnt/db #卸载 [rootcentos7 ~ 14:48:54]# umount /dev/sdb2 [rootcentos7 ~ 14:49:00]# lsblk /dev/sdb2 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb2 8:18 0 10G 0 part2.4文件系统卸载失败模拟占用[rootcentos7 ~ 15:01:30]# tail -f /mnt/web/index.html [1] 2224卸载文件系统报错:target is busy[rootcentos7 ~ 15:03:47]# umount /mnt/web umount: /mnt/web: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))找到使用设备的进程[rootcentos7 ~ 15:04:03]# yum install -y lsof # 参数是挂载点 [rootcentos7 ~ 15:04:18]# lsof /mnt/web/ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME tail 2224 root 3r REG 8,17 23 13 /mnt/web/index.html # 优雅的退出进程 [rootcentos7 ~ 15:04:25]# kill -15 2224 #再次卸载就不报错了 [rootcentos7 ~ 15:04:49]# umount /mnt/web [1] Terminated tail -f /mnt/web/index.html2.5持久化挂载计算机重启后文件系统自动挂载。/etc/fstab存储了系统自动挂载设备清单。[rootcentos7 ~ 15:37:59]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Fri Apr 3 14:11:21 2026 # # Accessible filesystems, by reference, are maintained under /dev/disk # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID800c4bdd-14d2-4e60-b90e-00719e3a0376 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0设置/dev/sdb1 自动挂载到 /mnt/web[rootcentos7 ~ 16:06:47]# cp /etc/fstab /etc/fstab-0408 [rootcentos7 ~ 16:07:16]# vim /etc/fstab在/etc/fstab最后添加/dev/sdb1 /mnt/web ext4 defaults 0 0条目参数说明/dev/sdb1挂载的设备/mnt/web设备挂载点ext4设备的文件系统defaults挂载选项用于控制文件系统属性例如defaults改为ro则文件系统只能读readonly0是否备份现在已弃用可用之为0或者10文件系统监测顺序值越小越优先检查。基本不用重启测试[rootcentos7 ~ 17:11:05]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 5G 0 part /mnt/web └─sdb2 8:18 0 10G 0 part如果/etc/fstab中具备相应条目则挂载文件系统的时候只需要写一个名称[rootcentos7 ~ 17:11:19]# umount /mnt/web [rootcentos7 ~ 17:12:20]# mount /mnt/web [rootcentos7 ~ 17:12:27]# df -h /mnt/web/ Filesystem Size Used Avail Use% Mounted on /dev/sdb1 4.8G 20M 4.6G 1% /mnt/web

更多文章