1 2种分区结构简介

MBR分区

  • 硬盘主引导记录MBR由4个部分组成
  • 主引导程序(偏移地址0000H--0088H),它负责从活动分区中装载,并运行系统引导程序。
  • 出错信息数据区,偏移地址0089H--00E1H为出错信息,00E2H--01BDH全为0字节。
  • 分区表(DPT,Disk Partition Table)含4个分区项,偏移地
  • 址01BEH--01FDH,每个分区表项长16个字节,共64字节为分区项1、分区项2、分区项3、分区项4
  • 结束标志字,偏移地址01FE--01FF的2个字节值为结束标志55AA

GPT分区

  • GPT:GUID(Globals Unique Identifiers) partitiontable 支持128个分区,使用64位,支持8Z(512Byte/block )64Z ( 4096Byte/block)
  • 使用128位UUID(Universally Unique Identifier) 表示磁盘和分区 GPT分区表自动备份在头和尾两份,并有CRC校验位
  • UEFI (统一扩展固件接口)硬件支持GPT,使操作系统启动

2 fdisk的使用

2.1 fdisk命令的基础

[root@centos7 mnt]$ dd if=/dev/fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.). 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 0xc0e6470d. 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) # 高级功能 Command (m for help):

2.2 fdisk实战

例子: 使用fdisk工具给/dev/sdb(100G)分区满足下面几个要求

  • 3个主分区一个扩展分区
  • /dev/sdb1分区类型为Linux LVM,大小为30G
  • /dev/sdb2分区类型为swap,大小为20G
  • /dev/sdb3分区类型为Linux, 大小为10G
  • /dev/sdb5分区类型为linux,大小为10G
  • 保留40G留作后用
[root@centos7 mnt]$ fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.). 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 0x2fd8d812. Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (-, default ):
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ): +30G
Partition of type Linux and of size GiB is set Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (-, default ):
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ): +20G
Partition of type Linux and of size GiB is set Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (,, default ):
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ): +10G
Partition of type Linux and of size GiB is set Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default e): e
Selected partition
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ):
Using default value
Partition of type Extended and of size GiB is set Command (m for help): n
All primary partitions are in use
Adding logical partition
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ): +10G
Partition of type Linux and of size GiB is set Command (m for help): p Disk /dev/sdb: 107.4 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x2fd8d812 Device Boot Start End Blocks Id System
/dev/sdb1 Linux
/dev/sdb2 Linux
/dev/sdb3 Linux
/dev/sdb4 Extended
/dev/sdb5 Linux Command (m for help): t
Partition number (-, default ):
Hex code (type L to list all codes): L Empty NEC DOS Minix / old Lin bf Solaris
FAT12 Hidden NTFS Win Linux swap / So c1 DRDOS/sec (FAT-
XENIX root Plan Linux c4 DRDOS/sec (FAT-
XENIX usr 3c PartitionMagic OS/ hidden C: c6 DRDOS/sec (FAT-
FAT16 <32M Venix Linux extended c7 Syrinx
Extended PPC PReP Boot NTFS volume set da Non-FS data
FAT16 SFS NTFS volume set db CP/M / CTOS / .
HPFS/NTFS/exFAT 4d QNX4.x Linux plaintext de Dell Utility
AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
AIX bootable 4f QNX4.x 3rd part Amoeba e1 DOS access
a OS/ Boot Manag OnTrack DM Amoeba BBT e3 DOS R/O
b W95 FAT32 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
OPUS EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
Hidden FAT12 Golden Bow a8 Darwin UFS f1 SpeedStor
Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
Hidden FAT16 < SpeedStor ab Darwin boot f2 DOS secondary
Hidden FAT16 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
Hidden HPFS/NTF Novell Netware b7 BSDI fs fc VMware VMKCORE
AST SmartSleep Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): t
Partition number (-, default ):
Hex code (type L to list all codes):
Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p Disk /dev/sdb: 107.4 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x2fd8d812 Device Boot Start End Blocks Id System
/dev/sdb1 8e Linux LVM
/dev/sdb2 Linux swap / Solaris
/dev/sdb3 Linux
/dev/sdb4 Extended
/dev/sdb5 Linux
Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.

3 gdisk的使用

3.1 gdisk的命令基础

[root@centos7 mnt]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8. Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present ***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
*************************************************************** Command (? for help): ?
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) # 创建一个空的GUID分区表
p print the partition table # 打印分区表
q quit without saving changes # 退出不保存
r recovery and transformation options (experts only) # 专家模式
s sort partitions # 排序分区
t change a partition's type code # 改变分区类型
v verify disk # 验证磁盘
w write table to disk and exit # 写磁盘并退出
x extra functionality (experts only) # 额外功能(专家模式)
? print this menu # 打印菜单 Command (? for help):

3.2gdisk实战

例子: 使用gdisk工具给/dev/sdb(100G)分区满足下面几个要求

  • GPT分区
  • /dev/sdb1分区类型为Linux LVM,大小为30G
  • /dev/sdb2分区类型为swap,大小为20G
  • /dev/sdb3分区类型为Linux, 大小为10G
  • /dev/sdb4分区类型为linux,大小为10G
  • 保留40G留作后用

上面我们对/dev/sdb已经分区了, 使用如下命令清空

[root@centos7 mnt]$ dd if=/dev/zero of=/dev/sdb bs= count=512                      #清空分区的前512字节,分区表被清空,小心操作
[root@centos7 mnt]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8. Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p
Disk /dev/sdb: sectors, 100.0 GiB
Logical sector size: bytes
Disk identifier (GUID): 04F07BAC-C3A6-44BC-8D27-90A198C1C3B2
Partition table holds up to entries
First usable sector is , last usable sector is
Partitions will be aligned on -sector boundaries
Total free space is sectors (100.0 GiB) Number Start (sector) End (sector) Size Code Name Command (? for help): n
Partition number (-, default ):
First sector (-, default = ) or {+-}size{KMGTP}:
Last sector (-, default = ) or {+-}size{KMGTP}: +30G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = ): L
Microsoft basic data 0c01 Microsoft reserved Windows RE
Windows LDM data Windows LDM metadata IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
Linux swap Linux filesystem Linux reserved
8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot
a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS
a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot
a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS
a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap
a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated
a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot
af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline
af03 Apple label af04 AppleTV recovery af05 Apple Core Storage
be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z
bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var
bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved
bf08 Solaris Reserved bf09 Solaris Reserved bf0a Solaris Reserved
bf0b Solaris Reserved c001 HP-UX data c002 HP-UX service
ed00 Sony system partitio ef00 EFI System ef01 MBR partition scheme
ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved
fc00 VMWare kcore crash p fd00 Linux RAID
Hex code or GUID (L to show codes, Enter = ): 8e00
Changed type of partition to 'Linux LVM' Command (? for help): n
Partition number (-, default ):
First sector (-, default = ) or {+-}size{KMGTP}:
Last sector (-, default = ) or {+-}size{KMGTP}: +20G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = ):
Changed type of partition to 'Linux swap' Command (? for help): n
Partition number (-, default ):
First sector (-, default = ) or {+-}size{KMGTP}:
Last sector (-, default = ) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = ):
Changed type of partition to 'Linux filesystem' Command (? for help): n
Partition number (-, default ):
First sector (-, default = ) or {+-}size{KMGTP}:
Last sector (-, default = ) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = ):
Changed type of partition to 'Linux filesystem' Command (? for help): p
Disk /dev/sdb: sectors, 100.0 GiB
Logical sector size: bytes
Disk identifier (GUID): 04F07BAC-C3A6-44BC-8D27-90A198C1C3B2
Partition table holds up to entries
First usable sector is , last usable sector is
Partitions will be aligned on -sector boundaries
Total free space is sectors (30.0 GiB) Number Start (sector) End (sector) Size Code Name
30.0 GiB 8E00 Linux LVM
20.0 GiB Linux swap
10.0 GiB Linux filesystem
10.0 GiB Linux filesystem Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!! Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

3 parted使用

3.1 parted命令基础

[root@centos7 mnt]$ parted --help
Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run in
interactive mode. OPTIONs:
-h, --help displays this help message
-l, --list lists partition layout on all block devices
-m, --machine displays machine parseable output
-s, --script never prompts for user intervention
-v, --version displays the version
-a, --align=[none|cyl|min|opt] alignment for new partitions COMMANDs:
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 (partitionM # 设置分区类型 详细使用man获取
table)
mkpart PART-TYPE [FS-TYPE] START END make a partition # 创建一个分区 start,end为M,详细信息使用man获取
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
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 Report bugs to bug-parted@gnu.org

3.2 parted实战

  • GPT分区
  • /dev/sdb1分区类型为Linux LVM,大小为30G
  • /dev/sdb2分区类型为swap,大小为20G
  • /dev/sdb3分区类型为Linux, 大小为10G
  • /dev/sdb4分区类型为linux,大小为10G
  • 保留40G留作后用
[root@centos7 mnt]$ parted -s /dev/sdb mklabel gpt
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary set lvm on
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary set swap on
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary
[root@centos7 mnt]$ parted -s /dev/sdb print

4 三种分区的比较

  • fdisk只能用于MBR分区,gdisk,parted可以用于GPT分区。
  • fdisk大多数运维工作人员已经习惯这个交互模式。
  • parted命令在创建删除分区使用命令比较方便,但是功能不是太完善,没有备份还原命令。
  • gdisk在分区上命令和fdisk风格一样, 使用方便,学习难度低且功能强大,推荐使用。

linux下(fdisk,gdisk,parted)三种分区工具比较的更多相关文章

  1. linux下安装mysql的三种方法:rpm包安装、yum安装、源码包安装

    1 安装MySQL数据库服务器安装方法一://查询系统自带的数据库rpm -qa | grep -i mysql //卸载查询到的所有mysqlrpm -e --nodeps mysql-libs-5 ...

  2. centos Linux下磁盘管理 parted,df ,du,fdisk,partprobe,mkfs.ext4,mount,/etc/fstab,fsck,e2fsck,mk2efs,tmpfs ,nr_inodes, LVM,传统方式扩容文件系统 第七节课

    centos Linux下磁盘管理   parted,df ,du,fdisk,partprobe,mkfs.ext4,mount,/etc/fstab,fsck,e2fsck,mk2efs,tmpf ...

  3. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  4. Linux就这个范儿 第18章 这里也是鼓乐笙箫 Linux读写内存数据的三种方式

    Linux就这个范儿 第18章  这里也是鼓乐笙箫  Linux读写内存数据的三种方式 P703 Linux读写内存数据的三种方式 1.read  ,write方式会在用户空间和内核空间不断拷贝数据, ...

  5. linux下软件安装的几种方式

    linux下软件安装的几种方式(主要有源码安装, rpm安装, yum安装). 一:源码安装 几乎所有的开源软件都支持在Linux下运行,而这些软件一般都以源码形式发放,只需要Linux安装了gcc. ...

  6. Ubuntu下安装程序的三种方法(转)

    引言 在Ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种.下面针对每一种方法各举例来说明. 一.apt-get方法 ...

  7. 【转】Linux下软件安装的几种方式

    转自Linux下软件安装的几种方式 Linux 系统的/usr目录 Linux 软件安装到哪里合适,目录详解 Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统 ...

  8. Linux 下操作GPIO(两种方法,驱动和mmap)(转载)

    目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...

  9. Linux下进程通信的八种方法

    Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...

随机推荐

  1. VMware Workstation安装Red hat7.0联网问题总结

    1.在red hat7当中iconfig命令是被取消了的,开发者用ip addr命令取代了ifconfig命令. 当然也是可以用ifconfig命令的 (前提是你安装的linux是可以联网的): 1) ...

  2. Django网站制作根目录,巧用404,可访问根目录任意网页

    原文链接:http://www.bianbingdang.com/article_detail/106.html 在制作网页过程中,网站需要格式各样的验证.比如百度站长.搜狗联盟的校验网站.不止如此, ...

  3. System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。

    System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.micro ...

  4. ubuntu/deepin 下 Sha 哈 dow 哈 socks 全局配置

    1. 安装编译环境 Ubuntu在默认情况下没有提供C/C++的编译环境,因此需要手动安装.如果单独安装gcc以及g++比较麻烦,为了方便,我们直接安装Ubuntu提供的build-essential ...

  5. 初入linux系统

    作为微软的老铁粉了,看到微软进军linux这么久了,是时候该跟上脚本了,不然该落后了,脚步是如此之快,着实让我吃了一惊,说干就干, 绝不是开玩笑的,谁也阻止不了.net开源,跨平台的脚步了.以前别人说 ...

  6. this全解js

    转(掘金) this关键字是JavaScript中最复杂的机制之一,是一个特别的关键字,被自动定义在所有函数的作用域中,但是相信很多JavaScript开发者并不是非常清楚它究竟指向的是什么.听说你很 ...

  7. DRC错误解决办法

    一.WARNING(ORCAP-1589): Net has two or more aliases - possible short? 错误原因:一个网络有两个网络标号,可能造成短路! 问题本质:原 ...

  8. mvc根据绝对路径下载文件

    首先页面需要一个a标签直接指向下载文件的Action并传值:图片地址,以及图片名称(记住要带后缀名的). 然后是Action里面的代码. SiteHelper.DownloadFile(fileUrl ...

  9. typescript之初学习

    ts的优势: 支持ES6规范 强大的IDE支持(类型检测.语法提示.重构等) Angular的开发语言 ts的新特性: 一.字符串 1.多行字符串,使用反引号`` 2.字符串模板,使用表达式传入变量和 ...

  10. Ubuntu添加中文输入法

    1.点击左上角Dash home 2.输入keyboard input methods 3.点击keyboard input methods,选择input methods,打钩. 4.点击下三角,选 ...