linux下磁盘分区(fdisk,gdisk,parted)三种分区工具比较

方式一:fdisk 

1.1 fdisk命令的基础

[root@centos7 mnt]$ fdisk -l
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 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): 

1.2 fdisk实战

第一步:连接终端,输入fdisk -l 命令查看硬盘信息

图1

第二步:对第二块硬盘分区,输入fdisk /dev/vdb (图1的第二块硬盘名称)

 

 第三步:格式化和挂载分区

方式二:gdisk的使用

2.1 gdisk的命令基础

[root@centos7 mnt]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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

2.2 gdisk实战

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

  • GPT分区
  • /dev/sdb1分区类型为Linux LVM,大小为30G
  • /dev/sdb2分区类型为swap,大小为20G
  • /dev/sdb3分区类型为Linux, 大小为10G
  • /dev/sdb4分区类型为linux,大小为10G
  • 保留40G留作后用
[root@centos7 mnt]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 04F07BAC-C3A6-44BC-8D27-90A198C1C3B2
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 209715133 sectors (100.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-209715166, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: +30G
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 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 1  
bf08 Solaris Reserved 2    bf09 Solaris Reserved 3    bf0a Solaris Reserved 4  
bf0b Solaris Reserved 5    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 = 8300): 8e00
Changed type of partition to ‘Linux LVM‘

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-209715166, default = 62916608) or {+-}size{KMGTP}:  
Last sector (62916608-209715166, default = 209715166) or {+-}size{KMGTP}: +20G
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to ‘Linux swap‘

Command (? for help): n
Partition number (3-128, default 3): 
First sector (34-209715166, default = 104859648) or {+-}size{KMGTP}: 
Last sector (104859648-209715166, default = 209715166) or {+-}size{KMGTP}: +10G
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to ‘Linux filesystem‘

Command (? for help): n
Partition number (4-128, default 4): 
First sector (34-209715166, default = 125831168) or {+-}size{KMGTP}: 
Last sector (125831168-209715166, default = 209715166) or {+-}size{KMGTP}: +10G
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to ‘Linux filesystem‘

Command (? for help): p
Disk /dev/sdb: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 04F07BAC-C3A6-44BC-8D27-90A198C1C3B2
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 62914493 sectors (30.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        62916607   30.0 GiB    8E00  Linux LVM
   2        62916608       104859647   20.0 GiB    8200  Linux swap
   3       104859648       125831167   10.0 GiB    8300  Linux filesystem
   4       125831168       146802687   10.0 GiB    8300  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.

方式三、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 1 30 set 1 lvm on
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary 30 50 set 2 swap on
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary 50 60
[root@centos7 mnt]$ parted -s /dev/sdb unit GB mkpart primary 60 70
[root@centos7 mnt]$ parted -s /dev/sdb print

4:三种分区的比较

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

参考地址:http://www.mamicode.com/info-detail-2110019.html

参考地址:https://www.cnblogs.com/300js/articles/7235063.html

posted on 2018-12-07 13:35  腾飞的鹰  阅读(3732)  评论(0编辑  收藏  举报

导航