传统的磁盘管理
先对一个硬盘进行分区,然后再将该分区进行文件系统的格式化,最后挂载
缺点:不能对磁盘空间进行动态的管理,分区后大小就固定了,空间不足时,不能进行扩充。
# mkfs.ext4 /dev/sdb
# mkfs -t ext4 /dev/sdb
/dev/sdb is entire device, not just one partition!
/dev/sdb是整个设备,而不仅仅是一个分区!
磁盘,划分的最小单位是扇区;扇区的单位是字节,一个扇区是512字节
对硬盘进行分区,其实是划分逻辑边界,从哪个扇区到哪个扇区是一个分区。
主分区,主分区最多四个,逻辑分区可以有多个。
MBR 分区表类型,它的分区只有两种情况:4个主分区 或者 3个主分区 + 1个扩展分区
示例:对sdb进行分区
# fdisk /dev/sdb
Command (m for help): 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(SGI)分区表
l list known partition types |显示分区类型
m print this menu | 打印帮助菜单
n add a new partition | 添加新的分区
o create a new empty DOS partition table |创建新的空DOS分区表
p print the partition table | 显示分区表
q quit without saving changes | 不保存,退出
s create a new empty Sun disklabel |创建新的空Sun磁盘标签
t change a partition's system id |改变分区类型
t 更改分区的系统id;调整系统标识符(system ID)为8e(LVM的标识符)
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): 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): 回车,默认主分区编号为1
分区类型
p是主分区,当前磁盘0个主分区,0个扩展分区,4个空闲
e是扩展分区
选择,创建主分区还是扩展分区?默认p,主分区
分区编号1-4,默认1
First sector (2048-41943039, default 2048): 默认,直接回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2097152 或者 +1G
Partition 1 of type Linux and of size 1 GiB is set
起始扇区
结束扇区,有三种方式,结束扇区的扇区编号,+扇区数量,+size
打印显示刚设置的分区
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: 0x8e14f6d0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
改变分区类型,设置为 Linux LVM
Command (m for help): t
Selected partition 1
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): 8e
Changed type of partition 'Linux' to 'Linux LVM'
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: 0x8e14f6d0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
保存退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@conda ~]#
接下来,依次划分出 sdb2 sdb3 主分区
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: 0x8e14f6d0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 6293503 2097152 8e Linux LVM
/dev/sdb3 6293504 12584959 3145728 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@conda ~]#
第四块分区,划分为 扩展分区
[root@conda ~]# 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 (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
Selected partition 4
First sector (12584960-41943039, default 12584960): 默认,直接回车
Using default value 12584960
Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039): +10G
Partition 4 of type Extended and of size 10 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: 0x8e14f6d0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 6293503 2097152 8e Linux LVM
/dev/sdb3 6293504 12584959 3145728 8e Linux LVM
/dev/sdb4 12584960 33556479 10485760 5 Extended
Command (m for help):
继续添加分区,这个时候,添加的就是 逻辑分区了
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (12587008-33556479, default 12587008): 默认,直接回车
Using default value 12587008
Last sector, +sectors or +size{K,M,G} (12587008-33556479, default 33556479): +2G
Partition 5 of type Linux and of size 2 GiB is set
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (16783360-33556479, default 16783360): 默认,直接回车
Using default value 16783360
Last sector, +sectors or +size{K,M,G} (16783360-33556479, default 33556479): +2G
Partition 6 of type Linux and of size 2 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: 0x8e14f6d0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 6293503 2097152 8e Linux LVM
/dev/sdb3 6293504 12584959 3145728 8e Linux LVM
/dev/sdb4 12584960 33556479 10485760 5 Extended
/dev/sdb5 12587008 16781311 2097152 83 Linux
/dev/sdb6 16783360 20977663 2097152 83 Linux
Command (m for help):
逻辑分区是没有数量限制的,只要你扩展分区的空间足够,逻辑分区就可以一直添加下去
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@conda ~]# lsblk
[root@conda ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 120G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 119G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 65.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 2G 0 part
├─sdb3 8:19 0 3G 0 part
├─sdb4 8:20 0 1K 0 part
├─sdb5 8:21 0 2G 0 part
└─sdb6 8:22 0 2G 0 part
sdc 8:32 0 20G 0 disk
sr0 11:0 1 918M 0 rom
nvme0n1 259:0 0 20G 0 disk /data
[root@conda ~]#
格式化分区
格式化磁盘(为磁盘写入文件系统),设置分区sdb1文件类型为ext4
mkfs.ext4 /dev/sdb1
注意: 要格式化成ext4,使用命令 mkfs.ext4, 要格式化成 xfs系统,则使用 mkfs.xfs
挂载
mount /dev/sdb1 /app
/etc/fstab 文件详解
系统开机时会主动读取/etc/fstab这个文件中的内容,根据文件里面的配置挂载磁盘。
[root@conda ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 23 16:37:14 2023
#
# 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
UUID=f593706a-0708-4ad1-a34c-ed4cfd475891 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
# Device Mount point filesystem parameters dump fsck
第一列 分区名
磁盘设备文件或者该设备的Label或者UUID
[root@conda ~]# blkid /dev/sda1
/dev/sda1: UUID="f593706a-0708-4ad1-a34c-ed4cfd475891" TYPE="xfs"
[root@conda ~]# blkid /dev/sda2
/dev/sda2: UUID="dpgVEa-dZvj-RunM-wRa8-yjw8-ACrN-IbkWgx" TYPE="LVM2_member"
第二列 挂载点
挂载点必须是已经存在的目录。
第三列 文件系统类型
第四列 文件系统的参数
Async/sync 设置是否为同步方式运行,默认为async
auto/noauto 当执行 mount -a 的命令时,此文件系统是否被主动挂载。默认为auto
rw/ro 以读写模式挂载/以只读模式挂载
exec/noexec 限制此文件系统内是否能够进行"执行"的操作
user/nouser 任何用户都可以挂载/只有超级用户可以挂载
suid/nosuid 是否允许SUID的存在
Usrquota 启动文件系统支持磁盘配额模式
Grpquota 启动文件系统对群组磁盘配额模式的支持
defaults 同时具有rw,suid,dev,exec,auto,nouser,async等默认参数的设置
第五列 dump备份设置
0 忽略备份操作;
1 允许dump备份程序备份;
第六列 fsck磁盘检查设置,是否检验扇区
开机的过程中,系统默认会以fsck检验我们系统是否为完整(clean)。
0 永远不检查;
1 最早检验(一般根目录会选择),根目录分区一般设置为1
2 1级别检验完成之后进行检验
其它分区从2开始,数字越小越先检查,如果两个分区的数字相同,则同时检查。
当修改完此文件并保存后,重启服务器生效。
问题处理
partx:用于操作磁盘分区表,特别是用于读取和操作已存在的分区表。
它通常用于重新扫描磁盘分区表以便 Linux 操作系统可以识别并使用新的分区。
-a, --add 添加指定的分区,或读取磁盘并添加所有分区。
-d, --delete 删除指定的分区或所有分区。
-u, --update 更新指定的分区。
-l, --list 列出分区。请注意,所有数字都以 512 字节扇区为单位。
此输出格式已弃用,建议使用 --show。
让新分区生效
让分区生效有两种方法:一种是重启,一种是获取新的分区表。获取新分区表使用的命令是partx
[root@conda ~]# partx -a /dev/sdb
partprobe 是一个命令行工具,通常用于在 Linux 系统中通知操作系统内核重新读取分区表。
这在你使用 fdisk 或类似工具修改了磁盘分区后尤其有用,因为在某些情况下,操作系统不会立即识别这些更改。使用 partprobe 可以避免重启系统来更新分区信息。
partprobe 命令将会使系统尝试重新读取所有分区
[root@conda ~]# partprobe /dev/sda 让系统只针对特定的设备重新读取分区表