Linux 磁盘分区、格式化和挂载

一、磁盘分区表分类

Linux中想使用磁盘的步骤和Windows一样

加硬盘->分区->格式化->挂载

#通过命令行方式对磁盘进行分区(两种方式,第一种就是MBR,第二种就是GPT)

如果想采用MBR(Master Boot Record)的方式进行分区就使用fdisk命令

MBR特征:

  • 较旧的分区表格式,仅支持最多4个主分区(或者3个主分区加一个扩展分区)。
  • 支持的最大磁盘容量为2TB。
  • 常用于较旧的BIOS启动系统。
  • MBR的结构较简单,但不如GPT可靠。

如果想采用GPT(GUID Partition Table)的方式进行分区就使用gdisk命令

GPT特征:

  • 现代的分区表格式,支持超过2TB的磁盘容量。
  • 支持最多128个主分区。
  • 具有更强的冗余功能,可以存储多个备份的分区表。
  • 必须使用UEFI(Unified Extensible Firmware Interface)启动。

1、手动创建分区(效率太低)

2、自动创建分区(有一定的操作性)

# 查看当前所有磁盘的分区情况

[server root ~] # fdisk -l

# 查看当前模块硬盘的分区情况

[server root ~] # fdisk -l /dev/sdb 

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors

一个sectors等于512B

[server root ~] # fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000e1369

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624     9439231     4194304   82  Linux swap / Solaris
/dev/sda3         9439232    83886079    37223424   83  Linux

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

[server root ~] # 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

[server root ~] # 

二、fdisk命令创建分区

对于服务器来说,磁盘从使用目的来说分为三种,第一种是用来存放操作系统的,此类硬盘一个只存放操作系统以及配置文件,剩下的任何数据都不应该主动存放在此硬盘中。第二种磁盘是用来存放本地大文件,一般这种磁盘叫做本地存储盘。第三种磁盘是用来存放本地大文件,而是使用了网络存储(SAN->storage area network),一般这种磁盘叫做网络存储

1、交互式的命令

[server root ~] # fdisk /dev/sdb

[server root ~] # 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 0x0f1f0bcc.

Command (m for help): q(quit 不保存的)

[server root ~] # 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 0x57ae1208.

Command (m for help): w(write 保存)
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[server root ~] # 


2、分区主分区

对/dev/sdb磁盘进行分区,如果分了四个主分区后,还有剩余空间,那就没法进行额外分区了,如下

[server root ~] # 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 0x05cc5d51.

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 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): 

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 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): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[server root ~] # 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): 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: 0x05cc5d51

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    12584959     1048576   83  Linux
/dev/sdb3        12584960    14682111     1048576   83  Linux
/dev/sdb4        14682112    16779263     1048576   83  Linux

Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.

# 如果要创建四个以上的分区,则必须替换首先使用扩展分区的主分区。
Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.

3、创建扩展分区,然后继续创建主分区

# 需要先删除一个主分区,然后创建扩展分区,默认剩余空间都给扩展分区
[server root ~] # 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
Partition number (1-4, default 4): 4
Partition 4 is deleted

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 (14682112-41943039, default 14682112): 
Using default value 14682112
Last sector, +sectors or +size{K,M,G} (14682112-41943039, default 41943039): 
Using default value 41943039
Partition 4 of type Extended and of size 13 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: 0x05cc5d51

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    12584959     1048576   83  Linux
/dev/sdb3        12584960    14682111     1048576   83  Linux
/dev/sdb4        14682112    41943039    13630464    5  Extended


# 创建扩展分区之后,就可以在创建主分区了
[server root ~] # 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
All primary partitions are in use
Adding logical partition 5
First sector (14684160-41943039, default 14684160): 
Using default value 14684160
Last sector, +sectors or +size{K,M,G} (14684160-41943039, default 41943039): +5G
Partition 5 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: 0x05cc5d51

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    12584959     1048576   83  Linux
/dev/sdb3        12584960    14682111     1048576   83  Linux
/dev/sdb4        14682112    41943039    13630464    5  Extended
/dev/sdb5        14684160    25169919     5242880   83  Linux



4、自动创建分区

自动分区采用了重定向的方法

fdisk /dev/sdb < test

或者

fdisk /dev/sdb < test &>> /dev/null

区别:显示创建过程与不显示创建过程

# 先将需要分区大小,分区类型是主分区还是扩展分区,直接写到一个文本中,如下(注意一定要空行)
[server root ~] # vim test
n
p


+1G

n
e



w

q

# 重定向分区                                                    
[server root ~] # fdisk /dev/sdb < test
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): Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default 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): Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): Command (m for help): Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): Partition number (2-4, default 2): First sector (2099200-41943039, default 2099200): Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): Using default value 41943039
Partition 2 of type Extended and of size 19 GiB is set

Command (m for help): The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[server root ~] # 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
Disk label type: dos
Disk identifier: 0x05cc5d51

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200    41943039    19921920    5  Extended

注意:在 Linux 中,分区大于 2TB 的硬盘时,通常需要使用 GPT(GUID Partition Table) 分区表,因为 MBR(Master Boot Record) 分区表只支持最多 2TB 的磁盘容量。GPT 分区表则没有这个限制,能够支持最大 9.4ZB 的磁盘容量

需要安装gdisk

apt-get install gdisk                 # Debian/Ubuntu 系统

yum install gdisk                      # CentOS/RHEL 系统

gdisk 相关参数

b back up GPT data to a file                       #备份当前的 GPT 数据到一个文件

c change a partition's 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)                #创建一个新的空的 GPT 分区表

p print the partition table                               # 打印当前分区表的内容,显示磁盘的分区信息

q quit without saving changes                       #退出 gdisk

r recovery and transformation options (experts only)            #恢复和转换选项。

s sort partitions                                              #对分区进行排序。

t change a partition's type code                     #更改某个分区的类型

v verify disk                                                    #验证磁盘的 GPT 分区表

w write table to disk and exit                          #将更改后的分区表写入磁盘并退出

x extra functionality (experts only)                  #扩展功能,适用于高级用户,提供一些特殊的操作,如转换分区表格式等

三、格式化分区

1、Linux常见文件系统格式有:ext2、ext3、ext4、xfs、btrfs和btrfs

#ext4的文件系统限制是,单个文件大小不能超过1T

#xfs的文件系统每个文件系统量最大支持8eb,单个文件可以支持16tb

2、mkfs命令格式化分区

# mkfs.ext4格式化分区/dev/sdb1
[server root ~] # mkf
mkfifo       mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs     
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.minix   
[server root ~] # mkfs.ext
mkfs.ext2  mkfs.ext3  mkfs.ext4  
[server root ~] # mkfs.ext4 /dev/sdb1 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
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 

# mkfs.xfs格式化分区/dev/sdb5
[server root ~] # mkfs.xfs /dev/sdb5 
meta-data=/dev/sdb5              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[server root ~] # 


# mkfs.ext2格式化分区/dev/sdb6
[server root ~] # mkfs.ext2 /dev/sdb6 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 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                            
Writing superblocks and filesystem accounting information: done 

[server root ~] # 



四、挂载Linux分区(文件系统)

1、临时挂载

mount 命令格式

mount -t 文件系统类型 文件系统所在的分区路径        文件系统的挂载点路径

mount -t fstypes        分区路径        挂载点路径

# 还未挂载前
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.5M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  372M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0

# 创建挂载目录
[server root ~] # mkdir /mount-point{1..3}
[server root ~] # 
[server root ~] # ls /mount-point* -d
/mount-point1  /mount-point2  /mount-point3
[server root ~] # 

# 将三个分区进行挂载,挂载到/mount-point(1,2,3)目录下
[server root ~] # mount /dev/sdb1 /mount-point1
[server root ~] # mount /dev/sdb5 /mount-point2
[server root ~] # mount /dev/sdb6 /mount-point3
[server root ~] # 
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.5M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  372M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1
/dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2
/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3
[server root ~] # 

2、 永久挂载分区

mount命令只是临时挂载,系统重启之后就没了

如果想文件系统永久挂载,就需要把挂载信息写入到/etc/fstab。

/etc/fstab这个文件非常重要,一旦这个文件删除了,或者里边的某些行错误删除了,甚至写法错误都会导致系统无法正常启动。

# 系统重启后,挂载的分区没了
[server root ~] # reboot
Connection closing...Socket close.
Connection closed by foreign host.

Disconnected from remote host(centos) at 14:15:58.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 
Reconnecting in 30 seconds. Press any key to exit local shell.
..............................

Connecting to 192.168.18.134:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Fri Nov 22 09:21:41 2024 from 192.168.18.1
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.5M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  372M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
[server root ~] # 

编辑/etc/fstab之前,最好先备份

# 备份文件
[server root ~] # cp /etc/fstab /etc/fstab.bak
[server root ~] # ls /etc/fstab
fstab      fstab.bak  

[server root ~] # cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Jan 16 23:35:15 2024
#
# 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
#
UUID=7f5b46d3-e0da-430a-97f2-2e6a19f9e4ff /                       xfs     defaults        0 0
UUID=ebaf11a7-f84f-4429-8df8-0ee0111e76e5 /boot                   xfs     defaults        0 0
UUID=48826076-975a-4d59-bf1a-5b1004e96f29 swap                    swap    defaults        0 0
# 第一列是文件系统所在的分区路径
#第二列是文件系统的挂载点
#第三列是文件系统的类型
#第四列是挂载参数
#第五列和第六列分别写0,只有在一些特殊的文件系统,后两列才需要改成非0


/dev/sdb1       /mount-point1   ext4    defaults        0 0
/dev/sdb5       /mount-point2   xfs     defaults        0 0
/dev/sdb6       /mount-point3   ext2    defaults        0 0

当fstab文件编辑完成后,需使用mount -a命令自动挂载生效。

注意:如果某个文件系统已经挂载了。且还在fstab里边被记录,那么mount -a不会将该文件卸载,再重新挂载,mount -a会自动忽略改文件系统

[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.5M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  372M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0

# mount -a命令自动挂载生效
[server root ~] # mount -a
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.5M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  372M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1
/dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2
/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3
[server root ~] # 

重启之后挂载正常

[server root ~] # reboot

Channel(Socket) closed from remote host(centos) at 14:35:29.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 
WARNING! The remote SSH server rejected X11 forwarding request.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Fri Nov 22 14:36:10 2024 from 192.168.18.1
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.6M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2
/dev/sda1      xfs       509M  138M  372M  28% /boot
/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3
/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
[server root ~] # 

上一篇:每日刷题(有效括号序列,滑动窗口最大值,最小的K个数,寻找第K大)


下一篇:Easyexcel(6-单元格合并)-注解