一、MBR 和 GPT 区别
1.1 MBR
MBR分区表(即主引导记录)大家都很熟悉,是过去我们使用windows时常用的
所支持的最大卷:2T,而且对分区有限制:最多4个主分区或3个主分区加一个扩展分区
1.2 GPT
GPT(即GUID分区表)。是源自EFI标准的一种较新的磁盘分区表结构的标准,是未来磁盘分区的主要形式。与MBR分区方式相比,具有如下优点。
突破MBR 4个主分区限制,每个磁盘最多支持128个分区。支持大于2T的分区,最大卷可达18EB。
对于 GPT 的分区,建议使用 parted 工具进行分区,fdisk 在 GPT 这块不是很好。
二、parted 命令介绍
2.1 命令行模式
# 该模式可以直接在命令行下对磁盘进行分区操作,比较适合编程应用
parted [option] device [command]
# 示例
parted /dev/sdb print # 显示 /dev/sdb 分区
2.2 交互模式
# 该模式可以直接在命令行下对磁盘进行分区操作,比较适合编程应用。
parted [option] device [command]
# 示例
parted /dev/sdb # 进入交互模式
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
2.3 常用选项
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
help 打印帮助信息
mklabel 创建分区表, 即是使用msdos(MBR)还是使用gpt,或者是其他方式分区表
mkpart 创建新分区
格式:mkpart PART-TYPE START END
2.4 PART-TYPE类型主要有primary(主分区), extended(扩展分区), logical(逻辑区). 扩展分区和逻辑分区只对msdos
rm 删除一个分区
resizepart 调整指定的分区的大小
quit 退出该交互界面
三、示例
3.1 fdisk 对磁盘进行分区
# 首先对于容量较大的磁盘使用 fdisk 工具会得到如下提示
Welcome to fdisk (util-linux 2.27.1).
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.
The size of this disk is 5.5 TiB (6001175126016 bytes). DOS partition table format can not be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
Created a new DOS disklabel with disk identifier 0x30e8c209.
Command (m for help):
3.2 更改分区表
(parted) mklabel gpt
(parted) print
Model: SEAGATE ST6000NM0034 (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
3.3 创建分区
(parted) mkpart primary 0% 100%
(parted) p
Model: SEAGATE ST6000NM0034 (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 6001GB 6001GB primary
3.4 此时即可格式化文件系统了
mkfs.xfs /dev/sd
四、排错
4.1 分区没有对齐
(parted) mkpart primary 0 6TB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel?
解决方式一
(parted) mkpart primary 0% 100%
(parted) p
Model: SEAGATE ST6000NM0034 (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 6001GB 6001GB primary
解决方式二
(parted) mkpart primary 0TB -1
(parted) p
Model: SEAGATE ST6000NM0034 (scsi)
Disk /dev/sdb: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 6001GB 6001GB primary
fdisk
进行硬盘分区,一种格式化方式,用fdisk可以对硬盘进行主分区、扩展分区。在创建扩展分区后,可以创建逻辑分区。
fdisk -l
查看系统所有硬盘及分区
[root@xxx /gogo]# :fdisk -l
Disk /dev/sda: 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: 0x000d2163
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
fdisk -l 指定分区
查看指定分区的信息
[root@xxx /gogo]# :fdisk -l /dev/sdb6
Disk /dev/sdb6: 5365 MB, 5365563392 bytes, 10479616 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
fdisk 磁盘名称(/dev/sda) #创建或修改磁盘的分区结构
参数:
p:打印分区表
n:新建一个新分区
d:删除一个分区
m:输出菜单
q:退出不保存
w:把分区写进分区表,保存并退出
创建主分区
[root@xxx /gogo]# :fdisk /dev/sdc
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 0xc2957253.
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): 1
First sector (2048-41943039, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
You have new mail in /var/spool/mail/root
注:若报错Device or resource busy. The kernel still uses the old table.The new table will be used at the next reboot。可以执行partprobe解决或者重启。
创建扩展分区
[root@xxx /gogo]# :fdisk /dev/sdc
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, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2): 2
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G
Partition 2 of type Extended 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.
创建逻辑分区
[root@xxx /gogo]# :fdisk /dev/sdc
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): l
Adding logical partition 5
First sector (20975616-31459327, default 20975616):
Using default value 20975616
Last sector, +sectors or +size{K,M,G} (20975616-31459327, default 31459327):
Using default value 31459327
Partition 5 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.
查看一下分区信息
partprobe
格式:partprobe 磁盘名 #将磁盘的最新分区状况发送给内核
1
警告提示:若提示Device or resource busy. The kernel still uses the old table.The new table will be used at the next reboot。也可不必重启,从而解决设备占用的问题。
mkfs
格式:mkfs 选项 分区名称
-t 文件系统,指定文件系统类型ext3、ext4、xfs。若不指定-t,默认是ext3
mkfs命令非常简单易用,不过不能调整分区的默认参数(比如块大小是4096)
1
2
3
一些文件类型
[root@xxx /gogo]# :mkfs
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
指定sdc2分区为ext4文件格式等...
[root@xxx /gogo]# :mkfs.ext4 /dev/sdc2
[root@xxx /gogo]# :mkfs.xfs /dev/sdc5
扩展一个命令
mke2fs 设置文件类型,block大小以及inode数
eg:**mke2fs -t ext4 –b4096 –i4096 /dev/sdb1**
1
格式:mke2fs 选项 分区名称
-t 文件系统:指定格式化成哪种文件系统类型ext3、ext4,暂不支持xfs
-b 字节:指定格式化时,每个block 的大小
-i 字节:指定多少字节分配一个inode号
使用-i 指定分区的inode数量时,必须同时指定block的大小
配置磁盘信息步骤
先分区,赋予文件格式
建立挂载点
mkdir –p /disk/movie
1
挂载
mount /dev/sdb1 /disk/movie
1
查看
mount #查看已挂载的所有设备
df -h #查看分区占用百分比
1
2
自动挂载–/etc/fstab
第一列:设备文件名(UUID)
查看UUID的方式:
dumpe2fs -h 分区名称
ls –l /dev/disk/by-uuid/
或者命令blkid
第二列:挂载点
第三列:文件系统类型
第四列:挂载特殊选项
第五列:是否备份: 0 不备份 1 每天备份 #备份
第六列:是否检查磁盘状态: 0 不检查 1 启动时检查 2 启动后检查
————————————————
版权声明:本文为CSDN博主「老徐是个铁憨憨」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46669463/article/details/105642873
3、挂载分区
[root@localhost]# mount -t ext4 /dev/sdb1 /bk
[root@localhost]# mount -t ext4 /dev/sdb2 /mail
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 4.1T 194M 3.9T 1% /bk
/dev/sdb2 ext4 6.8T 179M 6.4T 1% /mail
4、修改/etc/fstab,使其开机自动挂载
/dev/sdb1 /bk ext4 defaults 1 2
/dev/sdb2 /mail ext4 defaults 1 2