2023年11月25日发(作者:)

linux下使⽤fdisk进⾏磁盘分区详解

⽬录

.前⾔

当服务器插⼊⼀块硬盘,如果我们想要使⽤该硬盘,需要先使⽤磁盘分区管理⼯具进⾏磁盘分区,然后格式化分区,把分区挂载到⽬录

上,才可以正式使⽤该硬盘存储⽂件。磁盘分区管理⼯具有很多,本⽂主要介绍fdiskgdisk,parted,并进⾏⽐较。

.关于磁盘分区的结构

1.磁盘的分区结构有两种,⼀种是MBR分区,⼀种是GPT分区,下⾯对两种结构的特点进⾏⼀⼀说明。

(Master Boot Record)

是传统的分区机制,应⽤于绝⼤多数使⽤BIOS引导的PC设备(苹果使⽤EFI的⽅式),很多Server服务器即⽀持BIOS也⽀持EFI的引导⽅式;

MBR⽀持32bit64bit系统;

MBR分区分为主分区(⼀块硬盘最多只能创建4个主分区)、扩展分区(⼀个扩展分区会占⽤⼀个主分区的位置)、逻辑分区(逻辑分区是基于扩展分区创建出来的,先

有扩展分区,然后在扩展分区的基础上再创建逻辑分区;也就是说我们要使⽤逻辑分区,必须先要创建扩展分区,扩展分区的空间是不能被直接使⽤的,我们必须在扩展分

区的基础上去建⽴逻辑分区,才能够被使⽤)。在Linux上使⽤扩展分区和逻辑分区最多可以创建15个分区;

MBR只⽀持不超过2TB的硬盘。

(GUID Partition Table)

是⼀个较新的分区机制,解决了MBR的很多缺点;

⽀持超过2TB的磁盘;

向后兼容MBR

使⽤GPT分区的前提是底层硬件必须⽀持UEFIIntel提出的取代BIOS的新⼀代的引导系统)才能使⽤,也就是底层硬件必须使⽤UEFI

GPT分区只⽀持64位操作系统;

GPT分区列表⽀持最⼤128PB(1PB=1024TB),可以定义128个分区,没有主分区,扩展分区和逻辑分区的概念,所有分区都能格式化。

4.判断分区是GPT还是MBR的⽅法如下:

[root@node5 ~]# fdisk -l

Disk /dev/sda: 37.6 GB, 37580963840 bytes, 73400320 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: 0x000b606a

#Disk label type: dos这⾏信息说明该分区是MBR分区

#如果显⽰Disk label type: gpt则说明该分区是GPT分区

#如下命令也可以查看

[root@node5 ~]# gdisk -l /dev/sda

GPT fdisk (gdisk) version 0.8.10

Partition table scan:

MBR: MBR only

BSD: not present

APM: not present

GPT: not present

#还可以使⽤如下⽅法

[root@node5 ~]# parted -l

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sda: 37.6GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos

Disk Flags:

Number Start End Size Type File system Flags

1 1049kB 1075MB 1074MB primary xfs boot

2 1075MB 21.5GB 20.4GB primary lvm

#显⽰Partition Table: msdos,则是MBR分区

#显⽰Partition Table: gpt,则是GPT分区

.fdisk命令详解

进⼊fdisk命令窗⼝之后,输⼊m,可以查看所有的fdisk功能。

[root@node5 ~]# 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): m

Command action

a toggle a bootable flag # 切换可启动标志

b edit bsd disklabel # 编辑磁盘标记

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 # 打印菜单

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 # 改变⼀个分区的类型

u change display/entry units # 改变显⽰的单位

v verify the partition table # 验证分区表

w write table to disk and exit # 写分区表并退出

x extra functionality (experts only) # ⾼级功能

.使⽤fdisk进⾏磁盘分区

4.1 磁盘分区规划

本次磁盘分区的规划如下:创建⼀个主分区,⼀个扩展分区,其中扩展分区包含两个逻辑分区。

4.2 fdisk进⾏磁盘分区

使⽤fdisk/dev/sdb硬盘进⾏分区。

#使⽤lsblk查看可以知道,sdb这块磁盘⼤⼩为1G

[root@node5 ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 35G 0 disk

├─sda1 8:1 0 1G 0 part /boot

└─sda2 8:2 0 19G 0 part

├─centos-root 253:0 0 17G 0 lvm /

└─centos-swap 253:1 0 2G 0 lvm [SWAP]

sdb 8:16 0 1G 0 disk

└─sdb1 8:17 0 200M 0 part

#/dev/sdb这块磁盘进⾏分区

[root@node5 ~]# 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): 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): p #打印分区表

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x56e11754

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

Command (m for help): n #新建分区

Partition type:

p primary (1 primary, 0 extended, 3 free)

e extended

Select (default p): e #选择创建扩展分区

Partition number (2-4, default 2): #扩展分区编号,在此我们直接回车,默认为/dev/sdb2

First sector (104448-2097151, default 104448): #默认回车,从当前扇区开始

Using default value 104448

Last sector, +sectors or +size{K,M,G} (104448-2097151, default 2097151): +500M #分配扩展分区⼤⼩,在此为500M

Partition 2 of type Extended and of size 500 MiB is set

Command (m for help): n

Partition type:

p primary (1 primary, 1 extended, 2 free)

l logical (numbered from 5)

Select (default p): l #新建逻辑分区

Adding logical partition 5 #默认逻辑分区编号为5

First sector (106496-1128447, default 106496): #逻辑分区起始位置

Using default value 106496

Last sector, +sectors or +size{K,M,G} (106496-1128447, default 1128447): +200M #分配逻辑分区⼤⼩,在此为200M

Partition 5 of type Linux and of size 200 MiB is set

Command (m for help): n

Partition type:

p primary (1 primary, 1 extended, 2 free)

l logical (numbered from 5)

Select (default p): l #新建第⼆个逻辑分区

Adding logical partition 6

First sector (518144-1128447, default 518144):

Using default value 518144

Last sector, +sectors or +size{K,M,G} (518144-1128447, default 1128447): #直接回车,默认分配剩余空间

Using default value 1128447

Partition 6 of type Linux and of size 298 MiB is set

Command (m for help): p

Superblock backups stored on blocks:

8193, 24577, 40961, 57345, 73729

Allocating group tables: done

Writing inode tables: done

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

4.4 创建挂载点/挂载⽬录

[root@node5 ~]# mkdir /sdb1

[root@node5 ~]# mkdir /sdb5

4.5 挂载分区

[root@node5 ~]# mount /dev/sdb1 /sdb1

[root@node5 ~]# mount /dev/sdb5 /sdb5

[root@node5 ~]# df -hT

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/centos-root xfs 17G 11G 6.1G 65% /

devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev

tmpfs tmpfs 3.9G 8.0K 3.9G 1% /dev/shm

tmpfs tmpfs 3.9G 8.8M 3.9G 1% /run

tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup

/dev/sda1 xfs 1014M 125M 890M 13% /boot

tmpfs tmpfs 785M 0 785M 0% /run/user/0

/dev/sdb1 xfs 47M 2.7M 45M 6% /sdb1

/dev/sdb5 ext4 190M 1.6M 175M 1% /sdb5

4.6 设置开机⾃动挂载分区

磁盘分区使⽤mount⼿动挂载之后,还需要把挂载信息写⼊/etc/fstab⽂件中,不然重启之后,需要重新挂载。系统开机时会主动读

/etc/fstab这个⽂件中的内容,根据⽂件⾥⾯的配置挂载磁盘。这样我们只需要将磁盘的挂载信息写⼊这个⽂件中我们就不需要每次开

机启动之后⼿动进⾏挂载了。

[root@node5 ~]# echo "/dev/sdb1 /sdb1 xfs defaults 0 0 " >> /etc/fstab

[root@node5 ~]# echo "/dev/sdb5 /sdb5 ext4 defaults 0 0 " >> /etc/fstab

#注释,/etc/fstab⽂件详解

#⽰例为: /dev/sda3 /home ext4 defaults,noatime 0 2

/etc/fstab⽂件主要包括6段,依次是:

要挂载的分区或存储设备

挂载的⽬录位置

挂载分区的⽂件系统类型,⽐如:ext3ext4xfsswap

挂载使⽤的参数有哪些。举例如下:

auto - 在启动时或键⼊了 mount -a 命令时⾃动挂载。

noauto - 只在你的命令下被挂载。

exec - 允许执⾏此分区的⼆进制⽂件。

noexec - 不允许执⾏此⽂件系统上的⼆进制⽂件。

ro - 以只读模式挂载⽂件系统。

rw - 以读写模式挂载⽂件系统。

user - 允许任意⽤户挂载此⽂件系统,若⽆显⽰定义,隐含启⽤ noexec, nosuid, nodev 参数。

users - 允许所有 users 组中的⽤户挂载⽂件系统.

nouser - 只能被 root 挂载。

owner - 允许设备所有者挂载.

sync - I/O 同步进⾏。

async - I/O 异步进⾏。

dev - 解析⽂件系统上的块特殊设备。

nodev - 不解析⽂件系统上的块特殊设备。

suid - 允许 suid 操作和设定 sgid 位。这⼀参数通常⽤于⼀些特殊任务,使⼀般⽤户运⾏程序时临时提升权限。

nosuid - 禁⽌ suid 操作和设定 sgid 位。

noatime - 不更新⽂件系统上 inode 访问记录,可以提升性能。

nodiratime - 不更新⽂件系统上的⽬录 inode 访问记录,可以提升性能(参见 atime 参数)

relatime - 实时更新 inode access 记录。只有在记录中的访问时间早于当前访问才会被更新。(与 noatime 相似,但不会打断如 mutt 或其它程序探测⽂件在上次访问后是否被修改的进程。),可以提升性能。

flush - vfat 的选项,更频繁的刷新数据,复制对话框或进度条在全部数据都写⼊后才消失。

defaults - 使⽤⽂件系统的默认挂载参数,例如 ext4 的默认参数为:rw, suid, dev, exec, auto, nouser, async.

dump ⼯具通过它决定何时作备份. dump 会检查其内容,并⽤数字来决定是否对这个⽂件系统进⾏备份。 允许的数字是 0 1 0 表⽰忽略, 1 则进⾏备份。⼤部分的⽤户是没有安装 dump ,对他们⽽⾔ 应设为 0

fsck 读取 的数值来决定需要检查的⽂件系统的检查顺序。允许的数字是0, 1, 2 根⽬录应当获得最⾼的优先权 1, 其它所有需要被检查的设备设置为 2. 0 表⽰设备不会被 fsck 所检查。

4.7 查看磁盘分区效果

[root@node5 ~]# df -hT /sdb1

Filesystem Type Size Used Avail Use% Mounted on

/dev/sdb1 xfs 47M 2.7M 45M 6% /sdb1

[root@node5 ~]# df -hT /sdb5

Filesystem Type Size Used Avail Use% Mounted on

/dev/sdb5 ext4 190M 1.6M 175M 1% /sdb5