20260408 硬盘分区管理
一、硬盘分区管理大容量的硬盘分区使用C盘系统盘D盘办公E盘娱乐。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 …1.2 虚拟机添加2个硬盘sata 接口20GNVMe接口40G关机添加)# 查看块 block设备清单[rootcentos7 ~10:11:58]# lsblkNAME 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 sr011:014.4G0rom nvme0n1259:0040G0disk1.3 设备类型块设备b大部分情况下都是存储设备例如光盘、硬盘、U盘。字符设备c一般是输入和输出设备例如终端设备tty1、vty、pts/0等二、硬盘分区方案直接管理主板上外接设备的系统称之为固件。例如调整设备启动顺序光盘作为第一启动项用来安装系统。2.1 fdisk 工具fdisk 工具用于管理 MBR 分区方案的硬盘新版本的fdisk也可以用来管理GPT。2.1.1 查看硬盘信息# 查看所有块设备进一步过滤出sd相关设备[rootcentos7 ~10:13:40]# 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:32:00]# 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: 0x000a58c5 Device Boot Start End Blocks Id System /dev/sda1 *20482099199104857683Linux /dev/sda220992004194303992086656008e Linux LVM# sdb 硬盘没有分区[rootcentos7 ~10:32:13]# 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 /512bytes2.1.2 帮助信息# 交互方式管理sdb硬盘[rootcentos7 ~10:33:53]# 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 0xdfdac101.# m 查看帮助Command(mforhelp):mCommand 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 systemid# 修改分区类型IDu change display/entryunits# 切换显示单位vverify the partition table# 校验分区表wwritetable to disk andexit# 保存并退出x extra functionality(experts only)# 扩展功能仅专家模式2.1.3 查看分区表# p 查看分区表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: 0xf914efe5 Device Boot Start End Blocks Id System# sdb硬盘是空盘没有分区2.1.4 创建分区# n 新建分区Command(mforhelp): n Partition type: p primary(0primary,0extended,4free)e extended Select(default p):回车# 主分区类型Using default response p Partition number(1-4, default1):回车# 主分区类型First sector(2048-41943039, default2048):回车# 主分区类型Using default value2048Last sector, sectors or size{K,M,G}(2048-41943039, default41943039): 5G# 分区的结束为主Partition1oftypeLinux and of size5GiB isset# 再次查看多了一个分区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: 0xf914efe5 Device Boot Start End Blocks Id System /dev/sdb1204810487807524288083Linux2.1.5 保存分区表# w 保存修改Command(mforhelp): w The partition table has been altered!Calling ioctl()to re-read partition table. Syncing disks.# 再次查看块设备[rootcentos7 ~10:33:55]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk └─sdb18:1705G0part2.1.6 删除分区[rootcentos7 ~10:34:05]# fdisk /dev/sdbWelcome tofdisk(util-linux2.23.2). Changes will remaininmemory only,untilyou decide towritethem. Be careful before using thewritecommand.# d 删除分区Command(mforhelp):dSelected partition1Partition1is deleted Command(mforhelp):pDisk /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: 0xdfdac101 Device Boot Start End Blocks Id System# 对应的分区被删除# 保存退出Command(mforhelp):wThe partition table has been altered!Calling ioctl()to re-read partition table. Syncing disks.计算机存储单位。GB1000MBGiB1024MiB2.2 gdisk 工具gdisk 工具用于管理 GPT 分区方案的硬盘。2.2.1 安装工具# 安装gdisk工具[rootcentos7 ~10:34:04]# yum install -y gdisk2.2.2 查看硬盘信息[rootcentos7 ~11:08:26]# gdisk -l /dev/sdbGPTfdisk(gdisk)version0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR;using GPT. Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): AE4AA03E-2C20-46B9-BE2F-2F7379AD3CEE 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 Name2.2.3 帮助信息[rootcentos7 ~11:05:49]# 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 partitionstypecode# 修改分区类型码vverify disk# 校验磁盘wwritetable to disk andexit# 保存并退出x extra functionality(experts only)# 扩展功能专家模式? print this menu# 打印帮助菜单2.2.4 查看分区表Command(?forhelp): p Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): 7EAD059D-101E-4C4F-A263-B30CF8B6FD42 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 Name2.2.5 创建分区Command(?forhelp): n Partition number(1-128, default1): First sector(34-41943006, default2048)or{-}size{KMGTP}: Last sector(2048-41943006, default41943006)or{-}size{KMGTP}: 10G CurrenttypeisLinux filesystemHex code or GUID(L to show codes, Enter8300):回车# 分区也是有类型例如lvm表明分区的用途Changedtypeof partition toLinux filesystemCommand(?forhelp): p Disk /dev/sdb:41943040sectors,20.0GiB Logical sector size:512bytes Disk identifier(GUID): 7EAD059D-101E-4C4F-A263-B30CF8B6FD42 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 filesystem2.2.6 保存分区表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.# 再次查看[rootcentos7 ~11:07:04]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk └─sdb18:17010G0part2.2.7 删除分区[rootcentos7 ~ 08:53:59]# 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): 7EAD059D-101E-4C4F-A263-B30CF8B6FD42 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): 7EAD059D-101E-4C4F-A263-B30CF8B6FD42 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.# 再次查看[rootcentos7 ~ 08:53:39]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk三、文件系统管理# 提前准备好分区/dev/sdb1、/dev/sdb2[rootcentos7 ~11:37:26]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part └─sdb28:18010G0part3.1 文件系统格式化硬盘分区不能直接使用需要格式化成相应的文件系统ext4、xfs才能存储文件。# 将sdb1格式化为ext4[rootcentos7 ~11:37:31]# 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# 将sdb2格式化为xfs[rootcentos7 ~11:40:41]# mkfs.xfs /dev/sdb2meta-data/dev/sdb2isize512agcount4,agsize655360blkssectsz512attr2,projid32bit1crc1finobt0,sparse0databsize4096blocks2621440,imaxpct25sunit0swidth0blks namingversion2bsize4096ascii-ci0ftype1loginternal logbsize4096blocks2560,version2sectsz512sunit0blks, lazy-count1realtimenoneextsz4096blocks0,rtextents03.2 文件系统挂载硬盘挂载之后才能使用类似于windows中分配盘符C盘。# 准备挂载点目录[rootcentos7 ~11:41:43]# mkdir /mnt/web# 挂载/dev/sdb1到/mnt/web后续写入分区sdb1的数据通过路径/mnt/web引用。[rootcentos7 ~11:48:41]# mount /dev/sdb1 /mnt/web[rootcentos7 ~11:48:50]# df -h /mnt/webFilesystem Size Used Avail Use% Mounted on /dev/sdb14.8G 20M4.6G1% /mnt/web# 写入测试[rootcentos7 ~11:49:04]# echo Hellow World /mnt/web/test.txt# 此时文件/mnt/web/test.txt存储到sdb1中。[rootcentos7 ~11:49:28]# mkdir /mnt/db[rootcentos7 ~11:49:39]# mount /dev/sdb2 /mnt/db[rootcentos7 ~11:49:55]# df -h /mnt/dbFilesystem Size Used Avail Use% Mounted on /dev/sdb2 10G 33M 10G1% /mnt/db[rootcentos7 ~11:50:05]# echo Hello mysql /mnt/db/db.txt[rootcentos7 ~11:50:31]# cat /mnt/db/db.txtHello mysql四、部署web服务器web 服务器提供web页面分享提供一些动态网站例如购物。部署服务流程安装相关软件包准备相关材料配置服务启用并启动服务设置防火墙客户端访问测试# 1. 下载相关的软件包[rootcentos7 ~13:41:38]# yum install -y nginx# 2. 准备相关的材料# 使用/mnt/web 作为网站的根目录[rootcentos7 ~13:42:17]# echo Hello World From Nginx /mnt/web/index.html# 3. 配置服务[rootcentos7 ~13:51:10]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.jiang.cloud.conf[rootcentos7 ~13:51:54]# vim /etc/nginx/conf.d/vhost-www.jiang.cloud.conf[rootcentos7 ~13:53:20]# cat /etc/nginx/conf.d/vhost-www.jiang.cloud.confserver{server_name www.jiang.cloud;root /mnt/web;}# 4. 启用并启动服务[rootcentos7 ~13:53:28]# systemctl enable nginx --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.# 5. 设置防火墙[rootcentos7 ~13:53:48]# systemctl stop firewalld# 6. 客户端访问测试[rootcentos7 ~13:55:31]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6# 增加域名解析10.1.8.10 www.jiang.cloud www# Nginx访问[rootcentos7 ~13:55:10]# curl http://www.jiang.cloud/Hello World From Nginx# 如果是windows访问则修改C:\Windows\System32\drivers\etc\hosts也添加相应记录打开路径的文件 然后 增加一条10.1.8.10 www.jiang.cloud www 然后网页中搜 http://www.jiang.cloud4.1 文件系统卸载4.1.1 文件系统不使用的时候可以卸载取消目录和设备之间的映射关系。[rootcentos7 ~14:11:03]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part /mnt/web └─sdb28:18010G0part /mnt/db# 卸载挂载点[rootcentos7 ~14:40:09]# umount /mnt/db[rootcentos7 ~14:40:30]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part# 换一种方式挂载和卸载[rootcentos7 ~14:40:37]# blkid /dev/sdb2/dev/sdb2:UUID09867aa9-a49d-4b9b-9799-24908bc5c86aTYPExfsPARTLABELLinux filesystemPARTUUID63ebb374-b079-4e67-8f59-2da423fc4c7a# 可以使用多种方式表达设备# 使用 UUID 挂载[rootcentos7 ~14:41:38]# mount UUID09867aa9-a49d-4b9b-9799-24908bc5c86a /mnt/db[rootcentos7 ~14:41:44]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part /mnt/db# 通过设备名称卸载[rootcentos7 ~14:41:51]# umount /dev/sdb2[rootcentos7 ~14:42:05]# lsblk /dev/sdb2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb28:18010G0part4.2 文件系统卸载失败4.2.1. 模拟占用[rootcentos7 ~14:56:49]# tail -f /mnt/web/index.html [5]24514.2.2. 卸载文件系统报错: target is busy.[rootcentos7 ~14:57:29]# 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))4.2.3. 找到使用设备的进程[rootcentos7 ~14:58:01]# yum install -y lsof# 参数是挂载点[rootcentos7 ~14:58:20]# lsof /mnt/web/COMMAND PIDUSERFD TYPE DEVICE SIZE/OFF NODE NAMEtail2451root 3r REG8,172313/mnt/web/index.html# tail 读取文件内容# 优雅退出进程或者使用kill关闭进程[rootcentos7 ~14:58:48]# kill -15 2451[rootcentos7 ~14:59:08]#[5]Terminatedtail-f/mnt/web/index.html[rootcentos7 ~14:59:09]# umount /mnt/web4.3持久化挂载4.3.1 计算机重启后文件系统自动挂载。/etc/fstab存储了系统自动挂载设备清单[rootcentos7 ~15:15:05]# cat /etc/fstab## /etc/fstab# Created by anaconda on Fri Apr 3 14:10:34 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 defaults00UUID1906c7bf-f40a-49f9-bbd8-b9c65572a8ea /boot xfs defaults00/dev/mapper/centos-home /home xfs defaults00/dev/mapper/centos-swap swap swap defaults004.3.2 设置/dev/sdb1 自动挂载到 /mnt/webrootcentos7 ~16:05:30]# cp /etc/fstab /etc/fstab-0408[rootcentos7 ~16:06:00]# vim /etc/fstab# 最后添加/dev/sdb1 /mnt/web ext4 defaults00条目参数说明/dev/sdb1挂载的设备/mnt/web设备挂载点ext4设备的文件系统defaults挂载选项用于控制文件系统属性例如defaults改为ro则文件系统只能读readonly0是否备份现在已弃用可用之为0或者10文件系统监测顺序值越小越优先检查。基本不用4.3.3 重启测试。[rootcentos7 ~16:08:08]# lsblk /dev/sdbNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb8:16020G0disk ├─sdb18:1705G0part /mnt/web └─sdb28:18010G0part4.3.4 如果/etc/fstab中具备相应条目则挂载文件系统的时候只需要写一个名称。[rootcentos7 ~16:08:15]# umount /mnt/web[rootcentos7 ~16:08:25]# mount /mnt/web[rootcentos7 ~16:08:36]# df -h /mnt/webFilesystem Size Used Avail Use% Mounted on /dev/sdb14.8G 20M4.6G1% /mnt/web