Linux磁盘分区以及文件系统基本管理-0408

张开发
2026/4/9 16:32:55 15 分钟阅读

分享文章

Linux磁盘分区以及文件系统基本管理-0408
1.硬盘分区管理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设备清单[wangjiecentos7 ~ 09:45:31]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda8:00200G0disk ├─sda18:101G0part /boot └─sda28:20199G0part ├─centos-root253:0050G0lvm / ├─centos-swap253:102G0lvm[SWAP]└─centos-home253:20147G0lvm /home sdb8:16020G0disk# 新增的sata硬盘sr011:014.4G0rom nvme0n1259:0040G0disk# 新增的NVMe硬盘设备类型块设备b大部分情况下都是存储设备例如光盘、硬盘、U盘。字符设备c一般是输入和输出设备例如终端设备tty1、vty、pts/0等##1.2 硬盘分区方案直接管理主板上外接设备的系统称之为固件。例如调整设备启动顺序光盘作为第一启动项用来安装系统。1.1.1MBR自1982年起主启动记录MBR分区方案成为运行BIOS 固件系统的磁盘分区标准。主分区数量限制最多支持4个主分区。扩展分区与逻辑分区Linux 系统中可通过“主分区扩展分区”的方式突破主分区限制扩展分区仅作为逻辑分区的“容器”不可格式化逻辑分区可格式化使用整体最多可创建15个分区。容量限制MBR 用4个字节存储分区总扇区数按单扇区512字节计算单个分区最大容量不超过2 TiB。1.1.2GPT全局唯一标识分区表GPT是运行统一可扩展固件接口UEFI系统的磁盘分区标准。容量支持使用8个字节存储分区总扇区数单分区/磁盘最大支持18 ZiB18亿 TiB完全满足大磁盘需求。分区数量默认支持128个分区无需扩展分区/逻辑分区实际使用中建议不超过120个避免格式化异常。数据冗余GPT 分区表包含主表和备份表且自带 CRC32 校验码可自动检测并恢复损坏的分区表。1.2fdisk 管理 MBRfdisk 管理 MBR新版本的fdisk也可以用来管理GPT。1.2.1查看硬盘信息# 查看所有块设备进一步过滤出sd相关设备[rootcentos7 ~10:31:44]# fdisk -l |grep sdDisk /dev/sda:214.7GB,214748364800bytes,419430400sectors /dev/sda1 *20482099199104857683Linux /dev/sda220992004194303992086656008e Linux LVM Disk /dev/sdb:21.5GB,21474836480bytes,41943040sectors# 查看特定硬盘信息[rootcentos7 ~10:31:56]# fdisk -l /dev/sdaDisk /dev/sda:214.7GB,214748364800bytes,419430400sectors Unitssectors of1*512512bytes Sector size(logical/physical):512bytes /512bytes I/O size(minimum/optimal):512bytes /512bytes Disk label type: dos Disk identifier: 0x000b88c4 Device Boot Start End Blocks Id System /dev/sda1 *20482099199104857683Linux /dev/sda220992004194303992086656008e Linux LVM# sdb 硬盘没有分区[rootcentos7 ~10:32:12]# fdisk -l /dev/sdbDisk /dev/sdb:21.5GB,21474836480bytes,41943040sectors Unitssectors of1*512512bytes Sector size(logical/physical):512bytes /512bytes I/O size(minimum/optimal):512bytes /512bytes1.2.2创建分区# 交互方式管理sdb硬盘[rootcentos7 ~10:32:36]# fdisk /dev/sdbWelcome tofdisk(util-linux2.23.2). Changes will remaininmemory only,untilyou decide towritethem. Be careful before using thewritecommand. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x7e3d0bea.# m 查看帮助Command(mforhelp): m Command action a toggle a bootable flag# 切换分区启动标记b edit bsd disklabel# 编辑BSD磁盘标签c toggle the dos compatibility flag# 切换DOS兼容标记d delete a partition# 删除分区g create a new empty GPT partition table# 创建GPT分区表G create an IRIX(SGI)partition table# 创建IRIX分区表l list known partition types# 列出已知分区类型m print this menu# 打印帮助菜单nadda new partition# 创建新分区o create a new empty DOS partition table# 创建空MBR分区表p print the partition table# 打印分区表q quit without saving changes# 退出不保存s create a new empty Sun disklabel# 创建Sun磁盘标签t change a partitions system id # 修改分区类型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: 0x7e3d0bea 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: 0x7e3d0bea 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:34:03]# 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 ~ 10:34:12]# 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. Command (m for help): d # d 删除分区 Selected partition1Partition1is deleted Command(mforhelp): p# 查看分区对应分区被删除Disk /dev/sdb:21.5GB,21474836480bytes,41943040sectors Unitssectors of1*512512bytes Sector size(logical/physical):512bytes /512bytes I/O size(minimum/optimal):512bytes /512bytes Disk label type: dos Disk identifier: 0x7e3d0bea Device Boot Start End Blocks Id System Command(mforhelp): w The partition table has been altered!Calling ioctl()to re-read partition table. Syncing disks.1.3gdisk 管理 GPT1.3.1安装gdisk# 安装工具[rootcentos7 ~10:46:27]# yum install -y gdisk# 查看硬盘信息[rootcentos7 ~10:48:12]# gdisk -l /dev/sdbGPTfdisk(gdisk)version0.8.10 Partition table scan: MBR: MBR only# MBR 管理方案BSD: not present APM: not present GPT: not present *************************************************************** Found invalid GPT and valid MBR;converting MBR to GPTformatinmemory. *************************************************************** Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): 658B9223-2908-43C7-9C16-2EAF3A7A9178 Partition table holds up to128entries First usable sector is34, last usable sector is41943006Partitions will be aligned on2048-sector boundaries Totalfreespace is41942973sectors(20.0GiB)Number Start(sector)End(sector)Size Code Name1.3.2新增分区[rootcentos7 ~10:49:36]# gdisk /dev/sdbGPTfdisk(gdisk)version0.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 GPTformatinmemory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE!Exit by typingqifyou dont want to convert your MBR partitions to GPT format! *************************************************************** Command (? for help): ? b back up GPT data to a file # 备份GPT数据到文件 c change a partitions name# 修改分区名称d delete a partition# 删除分区i show detailed information on a partition# 查看分区详细信息l list known partition types# 列出已知分区类型nadda new partition# 创建新分区o create a new empty GUID partition table(GPT)# 创建空GPT分区表p print the partition table# 打印分区表q quit without saving changes# 退出不保存r recovery and transformation options(experts only)# 恢复/转换选项专家模式ssortpartitions# 排序分区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): 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): 回车 # 分区也是有类型例如lvm表明分区的用途 Changed type of partition to Linux filesystem Command(?forhelp): p Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): B98B6448-57EE-49C2-8875-42AC2523AAF3 Partition table holds up to128entries First usable sector is34, last usable sector is41943006Partitions will be aligned on2048-sector boundaries Totalfreespace is20971453sectors(10.0GiB)Number Start(sector)End(sector)Size Code Name120482097356710.0GiB8300Linux filesystem# 保存分区表Command(?forhelp): w Final checks complete. About towriteGPT 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.1.3.3删除分区[rootcentos7 ~10:53:44]# gdisk /dev/sdbGPTfdisk(gdisk)version0.8.10 Partition table scan: MBR: protective# 保护式MBR属于GPT的一部分BSD: not present APM: not present GPT: present# GPT 格式Found valid GPT with protective MBR;using GPT. Command(?forhelp): p Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): B98B6448-57EE-49C2-8875-42AC2523AAF3 Partition table holds up to128entries First usable sector is34, last usable sector is41943006Partitions will be aligned on2048-sector boundaries Totalfreespace is20971453sectors(10.0GiB)Number Start(sector)End(sector)Size Code Name120482097356710.0GiB8300Linux filesystem# d 删除Command(?forhelp): d Using1# 再次确认Command(?forhelp): p Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): B98B6448-57EE-49C2-8875-42AC2523AAF3 Partition table holds up to128entries First usable sector is34, last usable sector is41943006Partitions will be aligned on2048-sector boundaries Totalfreespace is41942973sectors(20.0GiB)Number Start(sector)End(sector)Size Code Name# 保存退出Command(?forhelp): w Final checks complete. About towriteGPT 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.2.文件系统基本管理2.1文件系统格式化[rootcentos7 ~11:35:56]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part └─sdb28:18010G0part硬盘分区不能直接使用需要格式化成相应的文件系统ext4、xfs才能存储文件。[rootcentos7 ~11:36:17]# mkfs.ext4 /dev/sdb1mke2fs1.42.9(28-Dec-2013)FilesystemlabelOS type: Linux Blocksize4096(log2)Fragmentsize4096(log2)Stride0blocks, Stripewidth0blocks327680inodes,1310720blocks65536blocks(5.00%)reservedforthe super user First datablock0Maximum filesystemblocks134217728040blockgroups32768blocks per group,32768fragments per group8192inodes per group Superblock backups stored on blocks:32768,98304,163840,229376,294912,819200,884736Allocating group tables:doneWriting inode tables:doneCreating journal(32768blocks):doneWriting superblocks and filesystem accounting information:done[rootcentos7 ~11:49:32]# mkfs.xfs /dev/sdb2meta-data/dev/sdb2isize512agcount4,agsize655360blkssectsz512attr2,projid32bit1crc1finobt0,sparse0databsize4096blocks2621440,imaxpct25sunit0swidth0blks namingversion2bsize4096ascii-ci0ftype1loginternal logbsize4096blocks2560,version2sectsz512sunit0blks, lazy-count1realtimenoneextsz4096blocks0,rtextents02.2文件系统挂载硬盘挂载之后才能使用类似于windows中分配盘符C盘。2.2.1示例1挂载/dev/sdb1给web应用使用# 准备挂载点目录[rootcentos7 ~11:49:51]# mkdir /mnt/web# 挂载/dev/sdb1到/mnt/web后续写入分区sdb1的数据通过路径/mnt/web引用。[rootcentos7 ~11:50:19]# mount /dev/sdb1 /mnt/web# 查看设备和挂载点关系[rootcentos7 ~11:50:41]# df -h /mnt/webFilesystem Size Used Avail Use% Mounted on /dev/sdb14.8G 20M4.6G1% /mnt/web# 写入测试[rootcentos7 ~11:50:59]# echo Hello World /mnt/web/test.txt[rootcentos7 ~11:51:04]# cat /mnt/web/web.txtHello World# 此时文件/mnt/web/web.txt存储到/dev/sdb1中。2.2.2示例2挂载/dev/sdb2给db应用使用[rootcentos7 ~11:51:35]# mkdir /mnt/db[rootcentos7 ~11:51:45]# mount /dev/sdb2 /mnt/db[rootcentos7 ~11:52:03]# df -h /mnt/dbFilesystem Size Used Avail Use% Mounted on /dev/sdb2 10G 33M 10G1% /mnt/db[rootcentos7 ~11:52:21]# echo Hello MySQL /mnt/db/db.txt[rootcentos7 ~11:52:59]# cat /mnt/db/db.txtHello MySQL2.3部署 web 服务器web 服务器提供web页面分享提供一些动态网站例如购物。部署服务流程安装相关软件包准备相关材料配置服务启用并启动服务设置防火墙客户端访问测试# 1. 安装相关软件包[rootcentos7 ~13:41:54]# yum install -y nginx# 2. 准备相关材料# 使用/mnt/web 作为网站的根目录[rootcentos7 ~14:04:11]# echo Hello World From Nginx /mnt/web/index.html# 3. 配置服务[rootcentos7 ~13:42:22]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.wj.cloud.conf[rootcentos7 ~13:52:54]# vim /etc/nginx/conf.d/vhost-www.wj.cloud.confserver{server_name www.laoma.cloud;root /mnt/web;}# 4. 启用并启动服务[rootcentos7 ~14:02:16]# systemctl enable nginx --now# 5. 设置防火墙[rootcentos7 ~14:02:41]# systemctl stop firewalld# 6. 客户端访问测试# 增加域名解析[rootcentos7 ~14:02:55]# vim /etc/hosts10.1.8.10 www.wj.cloud www# 验证[rootcentos7 ~14:05:10]# curl http://www.wj.cloud/Hello World From Nginx补充# 如果是windows访问则修改C:\Windows\System32\drivers\etc\hosts也添加相应记录2.3文件系统卸载文件系统不使用的时候可以卸载取消目录和设备之间的映射关系。[rootcentos7 ~14:39:39]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part /mnt/web └─sdb28:18010G0part /mnt/db# 卸载挂载点[rootcentos7 ~14:40:08]# umount /mnt/db[rootcentos7 ~14:40:22]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part换一种方式挂载和卸载# 查看设备id[rootcentos7 ~14:40:32]# blkid /dev/sdb2/dev/sdb2:UUIDb1280461-852c-441c-b5eb-4b4b799ba301TYPExfsPARTUUID125326cb-d176-4b50-866f-f6d2c296d282# 可以使用以下任意一种方式表达设备- /dev/sdb2 -UUIDb1280461-852c-441c-b5eb-4b4b799ba301-PARTLABELLinux filesystem-PARTUUID695ac164-8e69-408f-bdfb-6b444bc49e3e# 使用 UUID 挂载[rootcentos7 ~14:40:58]# mount UUIDb1280461-852c-441c-b5eb-4b4b799ba301 /mnt/db[rootcentos7 ~14:41:48]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part /mnt/db# 通过设备名称卸载[rootcentos7 ~14:41:59]# umount /dev/sdb2[rootcentos7 ~14:42:15]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part2.3.1文件系统卸载失败# 模拟占用[rootcentos7 ~14:57:00]# tail -f /mnt/web/index.html [1]2440[rootcentos7 ~14:57:27]# Hello World From Nginx# 卸载文件系统报错: target is busy.[rootcentos7 ~14:57:46]# umount /mnt/webumount: /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 ~14:57:57]# yum install -y lsof# 参数是挂载点[rootcentos7 ~14:58:12]# lsof /mnt/web/COMMAND PIDUSERFD TYPE DEVICE SIZE/OFF NODE NAMEtail2440root 3r REG8,172313/mnt/web/index.html# tail 读取文件内容# 优雅退出进程或者使用kill关闭进程[rootcentos7 ~14:58:25]# kill -15 2440[rootcentos7 ~14:58:42]# umount /mnt/web[1] Terminatedtail-f/mnt/web/index.html2.3.2持久化挂载计算机重启后文件系统自动挂载。/etc/fstab存储了系统自动挂载设备清单。[rootcentos7 ~15:38:42]# cat /etc/fstab## /etc/fstab# Created by anaconda on Fri Apr 3 14:10:30 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 defaults00UUID9c4aa7a0-31f9-480e-900b-737dd1bad5de /boot xfs defaults00/dev/mapper/centos-home /home xfs defaults00/dev/mapper/centos-swap swap swap defaults00[rootcentos7 ~16:05:12]# cp /etc/fstab /etc/fstab-0408[rootcentos7 ~16:05:48]# vim /etc/fstab# 最后添加/dev/sdb1 /mnt/web ext4 defaults00条目参数说明/dev/sdb1挂载的设备/mnt/web设备挂载点ext4设备的文件系统defaults挂载选项用于控制文件系统属性例如defaults改为ro则文件系统只能读readonly0是否备份现在已弃用可用之为0或者10文件系统监测顺序值越小越优先检查。基本不用重启测试[wangjiecentos7 ~16:45:02]$ lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part /mnt/web └─sdb28:18010G0part /mnt/db如果/etc/fstab中具备相应条目则挂载文件系统的时候只需要写一个名称。[rootcentos7 ~16:45:13]# umount /mnt/web[rootcentos7 ~16:46:21]# mount /mnt/webmount: /dev/sdb1 is already mounted or /mnt/web busy /dev/sdb1 is already mounted on /mnt/web[rootcentos7 ~16:46:30]# df -h /mnt/webFilesystem Size Used Avail Use% Mounted on /dev/sdb14.8G 20M4.6G1% /mnt/web补充vmware nat 映射映射web端口浏览器访问 http://物理主机IP/例如 http://192.168.42.216:80

更多文章