本文主要是介绍Linux入门真经-030GPT分区与parted,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、GPT分区表结构
GPT类型的分区表中,用于记录分区信息的区块称为LBA。GPT一共有34个LBA,除了LBA0和LBA1有特殊作用之外,LBA2-LBA33都可以用于记录分区表,每一个LBA可以记录4个分区,所以,理论上GPT支持4*32=128个主分区(也支持扩展分区机制,但是基本都用不着了)。GPT还在磁盘的尾部扇区对LBA做了整体的备份,结构图如下(图片摘自维基百科):
此外,GPT不再有MBR只能识别2T硬盘容量的限制,理论上能识别18EB(1EB=1024PB=1,048,576TB)的磁盘,且单个分区最大可以支持到1840T。
以上介绍了解即可,接下来介绍如何进行GPT格式的磁盘分区。
一般来说,要进行GPT格式的分区,通常使用parted或者gdisk。gdisk不是最简安装的内置命令,本节介绍parted命令。
2、parted分区工具
parted像fdisk一样,提供了一个交互式接口。而且,parted还支持非交互式管理磁盘(更便于我们在shell编程中应用了)。
交互式管理磁盘:
进入交互式环境:parted DEVICE
举例(我们将上节创建的分区删除并重新创建分区):
[root@localhost /]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to viewa list of commands.
(parted)
help可以跳出指引菜单
(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
rmNUMBER 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
setNUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG onpartition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU
Parted
(parted)
查看当前硬盘分区信息:
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 10.7GB 10.7GB primary
2 10.7GB 11.8GB 1074MB primary
3 11.8GB 12.9GB 1074MB primary
4 12.9GB 21.5GB 8589MB extended
5 12.9GB 18.3GB 5369MB logical
(parted)
我们可以看到它显示得很清楚,Partition Table:msdos表示该硬盘基于MBR格式进行的分区
删除分区:rm 编号
(parted) rm 1
(parted) rm 2
(parted) rm 3
(parted) rm 4
(parted) rm 5
Error: Partition doesn't exist. 注:扩展分区被删了,逻辑分区自然不复存在
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
(parted)
创建分区:
使用help [子命令]可以看子命令的相关说明,比如
(parted) help mklabel
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
LABEL-TYPEis one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop
(parted)
其中,msdos指的是MBR。
首先指明分区表格式为gpt:
(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on/dev/sdb will be destroyed and all data on this disk will be lost.
Do you want to continue?
Yes/No? yes
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
然后新建分区,先看看help:
(parted) help mkpart
mkpart PART-TYPE [FS-TYPE] START END make a partition
PART-TYPEis one of: primary, logical, extended
FS-TYPE is one of: btrfs, nilfs2, ext4, ext3, ext2, fat32, fat16, hfsx,hfs+, hfs, jfs,
swsusp, linux-swap(v1), linux-swap(v0), ntfs, reiserfs, hp-ufs, sun-ufs,xfs, apfs2, apfs1,
asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7,affs6, affs5, affs4,
affs3, affs2, affs1, affs0, linux-swap, linux-swap(new), linux-swap(old)
START and END are disk locations, such as 4GB or 10%. Negative values count from the end of
the disk. For example, -1sspecifies exactly the last sector.
'mkpart' makes a partition without creating a new file system on thepartition. FS-TYPE may
be specified to set an appropriate partition ID.
可以看出,新建分区的格式为:
mkpart PART-TYPE [FS-TYPE] START END
mkpart 主/扩展/逻辑分区 [文件系统格式] 开始位置 结束位置
其中文件系统我们还没介绍,这个是可选项可先不填。
关于始末位置可能有些朋友比较迷惑,不知道从哪里开始。其实你可以尽量填小些,parted随后会告诉你合适的数值,如下:
(parted) mkpart primary 512B 10G
Warning: You requested a partition from512B to 10.0GB (sectors 1..19531250).
The closest location we can manage is17.4kB to 10.0GB (sectors 34..19531250).
Is this still acceptable to you?
Yes/No?No
还记得不,GPT要保留34个lable嘛,键入No之后重新分区
建议前面稍微比提示留大一点点,给分区表让足路,反正也不差一两MB的大小;而且你指定得过小过精确反而会影响parted自动对齐的机制。
(parted) mkpart primary 1 10G
(parted) mkpart primary 10G 15G
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB primary
2 10.0GB 15.0GB 5000MB primary
(parted)
parted是即时保存的,直切quit退出即可
(parted) quit
Information: You may need to update/etc/fstab.
[root@localhost /]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2
[root@localhost /]#
非交互式
parted [options] [device [command[options...]...]]
其实就是把之前交互式模式里的指令一行写完。
显示硬盘分区信息
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB primary
2 10.0GB 15.0GB 5000MB primary
删除现有分区
[root@localhost /]# parted /dev/sdb rm 1
Information: You may need to update/etc/fstab.
[root@localhost /]# parted /dev/sdb rm 2
Information: You may need to update/etc/fstab.
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
[root@localhost /]#
新增分区
[root@localhost /]# parted /dev/sdb mkpartprimary 1 10G
Information: You may need to update/etc/fstab.
[root@localhost /]# parted /dev/sdb mkpartprimary 10G 15G
Information: You may need to update/etc/fstab.
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB primary
2 10.0GB 15.0GB 5000MB primary
[root@localhost /]#
好的。到目前为止,你应该掌握了如何在linux系统中对硬盘进行分区了。然而分区只是第一步,我们还要在分区之上建立文件系统才能开始存取数据。下一节就开始为大家介绍Linux文件系统。
关注本公众号获取每日更新
这篇关于Linux入门真经-030GPT分区与parted的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!