磁盘和文件系统
磁盘管理及分区
fdisk -l 查看系统中的磁盘设备
未查找到系统新接入的磁盘
扫描重新加入的磁盘
ls /sys/class/scsi_host ##查询HBA总线编号###
echo "- - -" > /sys/class/scsi_host/host0/scan $$ echo "- - -" > /sys/class/scsi_host/host1/scan $$ echo "- - -" > /sys/class/scsi_host/host2/scan
### dmesg | grep -i "sd" ####检测新硬盘是否被识别#
磁盘管理及分区
列出磁盘设备信息:显示系统中所有磁盘设备的名称,如/dev/sda
、/dev/sdb
等.
展示磁盘容量:提供每个磁盘的总容量大小,以字节为单位,也会换算成更易读的单位如 GB、TB 等。
显示分区表类型:说明磁盘使用的分区表类型,常见的有 MBR(Master Boot Record)和 GPT(GUID Partition Table)。
列出分区详细信息:包括每个分区的设备名称、起始位置、结束位置、分区大小、分区类型等。
关键字信息:
Device:分区的设备文件名称。 Boot:是否是引导分区。若是引导分区,则有“*”标识。 Start :该分区在硬盘中的起始位置(柱面数)。 End:该分区在硬盘中的结束位置(柱面数) Blocks:分区的大小,以Blocks(块)为单位,默认的块大小为1024字节。 ID:分区对应的系统ID。83 表示 Linux操作系统中的XFS 分区,8e表示 LVM 逻辑卷。
system:分区类型
fdish /dev/sdb sd(二十六英文选择)
登录到root用户 [chen@localhost ~]$ su - root Password: Last login: Mon Mar 10 08:40:07 CST 2025 on pts/0 Last failed login: Mon Mar 10 15:36:18 CST 2025 on pts/0 There was 1 failed login attempt since the last successful login.
此为日志,进入磁盘分区工具
[root@localhost ~]# 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): n Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): p Partition number (3,4, default 3): 1 Value out of range. Partition number (3,4, default 3): 4 First sector (12584960-41943039, default 12584960): Using default value 12584960 Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039): +5^H1^H^H^H Unsupported suffix'.1 Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte) 2^N: K (KibiByte), M (MebiByte), G (GibiByte) Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039): +1G Partition 4 of type Linux and of size 1 GiB is set
使用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: 0xc21ab43a
Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 12584959 1048576 5 Extended /dev/sdb4 12584960 14682111 1048576 83 Linux /dev/sdb5 10489856 12584959 1047552 83 Linux
请求帮助
Command (m for help): [: unknown command 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 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)
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: 0xc21ab43a
Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 12584959 1048576 5 Extended /dev/sdb4 12584960 14682111 1048576 83 Linux /dev/sdb5 10489856 12584959 1047552 83 Linux
Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): e
Command (m for help): d Partition number (1,2,5, default 5): 5 Partition 5 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: 0xc21ab43a
Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 12584959 1048576 5 Extended
更改分区类型
Command (m for help): t Partition number (1,2, default 2): 2
查询Linux中分区类型详解: 可自行查询,因为系统排列的问题有点杂乱无章
Hex code (type L to list all codes): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Hex code (type L to list all codes): 82
You cannot change a partition into an extended one or vice versa. Delete it first.
Type of partition 2 is unchanged: Extended
查看分区情况
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: 0xc21ab43a
Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 12584959 1048576 5 Extended
退出
使用w和q可以退出,w保存退出,q不保存
Command (m for help): w The partition table has been altered!
Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# 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): q
[root@localhost ~]#
挂载
mkswap命令将/dev/sdb1改为xfs linux 83
(同样可设置为其他文件系统)
[root@localhost ~]# mkfs -t xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=65472 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=261888, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
mkswap命令将/dev/sdb1改为交换分区(虚拟内存)
此前以做过交换分区定义
[root@localhost ~]# mkswap /dev/sdb1 mkswap: /dev/sdb1: warning: wiping old swap signature. Setting up swapspace version 1, size = 5242876 KiB no label, UUID=8180a20c-f078-44be-ab31-c0fea2b61ad0
临时挂载
光盘/dev/sdb5挂载到/etc/mnt,
[root@localhost /]# mount /dev/sdb5 /etc/mnt mount: /dev/sdb5 is already mounted on /etc/mnt
卸载文件系统
[root@localhost /]# umount /etc/mnt 绝对目录 [root@localhost /]# umount /dev/sdb5 选定光盘对象
查看磁盘使用情况
[root@localhost /]# df -hT
Filesystem Type Size
Used Avail Use%
Mounted on devtmpfs devtmpfs 3.8G 0 3.8G
0% /dev tmpfs tmpfs 3.9G 0 3.9G 0%
/dev/shm tmpfs tmpfs 3.9G 13M 3.8G 1%
/run tmpfs tmpfs 3.9G 0 3.9G 0%
/sys/fs/cgroup /dev/sda3 xfs 18G 5.7G 13G 32%
/dev/sda1 xfs 297M 163M 134M 55%
/boot tmpfs tmpfs 781M 12K 781M 1%
/run/user/42 tmpfs tmpfs 781M 0 781M 0%
/run/user/1
配置永久挂载
编辑/etc/fstab文件。在文件末尾添加一行: /dev/sdb1 /mnt/data ext4 defaults 0 0
各字段含义如下:
第一个字段:磁盘分区设备名,这里是/dev/sdb1。
第二个字段:挂载点,即之前创建的/mnt/data。
第三个字段:文件系统类型,这里是ext4。
第四个字段:挂载选项,defaults表示使用默认选项。
第五个字段:用于dump工具备份,0表示不备份。
第六个字段:用于fsck工具检查磁盘的顺序,0表示不检查。
使用以下命令编辑/etc/fstab文件: vim /etc/fstab
验证永久挂载 使用mount -a
命令重新挂载/etc/fstab文件中所有的文件系统, mount -a