分区管理工具-fdisk命令实战案例

时间:2024-03-13 21:15:18

          分区管理工具-fdisk命令实战案例

                              作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

   fdisk工具擅长管理MBR分区。接下来我们就简单介绍一下该工具的使用。

 

一.分区表概述

  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12347792.html

 

二.fdisk非交互式命令案例

1>.查看fidsk的帮助信息

[root@yinzhengjie.com ~]# fdisk -h
Usage:
 fdisk [options] <disk>    change partition table
 fdisk [options] -l <disk> list partition table(s)
 fdisk -s <partition>      give partition size(s) in blocks

Options:
 -b <size>             sector size (512, 1024, 2048 or 4096)
 -c[=<mode>]           compatible mode: \'dos\' or \'nondos\' (default)
 -h                    print this help text
 -u[=<unit>]           display units: \'cylinders\' or \'sectors\' (default)
 -v                    print program version
 -C <number>           specify the number of cylinders
 -H <number>           specify the number of heads
 -S <number>           specify the number of sectors per track

[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk -h
[root@yinzhengjie.com ~]# man fdisk           #使用man手册查看fdisk查看的信息更全面。

2>.查看所有磁盘的分区表信息

[root@yinzhengjie.com ~]# lsblk 
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0    2T  0 disk 
├─sda1                   8:1    0    2M  0 part 
├─sda2                   8:2    0    1G  0 part /boot
└─sda3                   8:3    0    2T  0 part 
  ├─centos-root        253:0    0  500G  0 lvm  /
  └─centos-yinzhengjie 253:1    0  1.5T  0 lvm  /yinzhengjie
sdb                      8:16   0  600G  0 disk 
sdc                      8:32   0  400G  0 disk 
sdd                      8:48   0  800G  0 disk 
sde                      8:64   0 1000G  0 disk 
sr0                     11:0    1 1024M  0 rom  
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# lsblk
[root@yinzhengjie.com ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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: gpt
Disk identifier: 60B0BC61-D7A6-4522-972F-E8B13E38A9C1


#         Start          End    Size  Type            Name
 1         2048         6143      2M  BIOS boot       
 2         6144      2103295      1G  Microsoft basic 
 3      2103296   4294723583      2T  Linux LVM       

Disk /dev/sdd: 859.0 GB, 858993459200 bytes, 1677721600 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 /dev/sde: 1073.7 GB, 1073741824000 bytes, 2097152000 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 /dev/sdc: 429.5 GB, 429496729600 bytes, 838860800 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 /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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 /dev/mapper/centos-root: 536.9 GB, 536870912000 bytes, 1048576000 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 /dev/mapper/centos-yinzhengjie: 1660.9 GB, 1660944384000 bytes, 3244032000 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

[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk -l
[root@yinzhengjie.com ~]# cat /proc/partitions 
major minor  #blocks  name

  11        0    1048575 sr0
   8        0 2147483648 sda
   8        1       2048 sda1
   8        2    1048576 sda2
   8        3 2146310144 sda3
   8       48  838860800 sdd
   8       64 1048576000 sde
   8       32  419430400 sdc
   8       16  629145600 sdb
   8       17  104857600 sdb1
   8       18  209715200 sdb2
   8       19          1 sdb3
   8       21  104857600 sdb5
   8       22   83886080 sdb6
 253        0  524288000 dm-0
 253        1 1622016000 dm-1
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# cat /proc/partitions
[root@yinzhengjie.com ~]# ll /dev/sd*
brw-rw---- 1 root disk 8,  0 May  8 19:31 /dev/sda
brw-rw---- 1 root disk 8,  1 May  8 19:31 /dev/sda1
brw-rw---- 1 root disk 8,  2 May  8 19:31 /dev/sda2
brw-rw---- 1 root disk 8,  3 May  8 19:31 /dev/sda3
brw-rw---- 1 root disk 8, 16 May  8 20:37 /dev/sdb
brw-rw---- 1 root disk 8, 17 May  8 20:37 /dev/sdb1
brw-rw---- 1 root disk 8, 18 May  8 20:37 /dev/sdb2
brw-rw---- 1 root disk 8, 19 May  8 20:37 /dev/sdb3
brw-rw---- 1 root disk 8, 21 May  8 20:37 /dev/sdb5
brw-rw---- 1 root disk 8, 22 May  8 20:37 /dev/sdb6
brw-rw---- 1 root disk 8, 32 May  8 16:33 /dev/sdc
brw-rw---- 1 root disk 8, 48 May  8 16:33 /dev/sdd
brw-rw---- 1 root disk 8, 64 May  8 16:33 /dev/sde
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# ll /dev/sd*

3>.查看单个磁盘的分区信息

[root@yinzhengjie.com ~]# lsblk /dev/sda
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0    2T  0 disk 
├─sda1                   8:1    0    2M  0 part 
├─sda2                   8:2    0    1G  0 part /boot
└─sda3                   8:3    0    2T  0 part 
  ├─centos-root        253:0    0  500G  0 lvm  /
  └─centos-yinzhengjie 253:1    0  1.5T  0 lvm  /yinzhengjie
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# lsblk /dev/sda
[root@yinzhengjie.com ~]# fdisk -l /dev/sda
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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: gpt
Disk identifier: 60B0BC61-D7A6-4522-972F-E8B13E38A9C1


#         Start          End    Size  Type            Name
 1         2048         6143      2M  BIOS boot       
 2         6144      2103295      1G  Microsoft basic 
 3      2103296   4294723583      2T  Linux LVM       
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk -l /dev/sda

 

三.fdisk交互式命令

[root@yinzhengjie.com ~]# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x134e9c3c.

Command (m for help): 

1>.查看帮助信息

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

2>.创建新的分区

Command (m for help): n       
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p                       #我们可以不输入p,因为默认就是p选项,即主分区
Partition number (1-4, default 1):               #注意哈,此处如果不输入编号默认分配编号"1"
First sector (2048-1258291199, default 2048):         #这里的扇区默认是2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1258291199, default 1258291199): +100G      #注意哈,如果不加单位的话默认是以扇区为单位开始分配空间,因此通常分配时我们按G来分配哟~
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help):  
Command (m for help): p  

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System      #从左到右的每个字段分别为设备名,起始扇区,结束扇区,块数量,分区的系统ID,分区的系统ID对应的类型
/dev/sdb1            2048   209717247   104857600   83  Linux

Command (m for help): 
Command (m for help): n    
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (209717248-1258291199, default 209717248): 
Using default value 209717248
Last sector, +sectors or +size{K,M,G} (209717248-1258291199, default 1258291199): +200G
Partition 2 of type Linux and of size 200 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux

Command (m for help): 
Command (m for help): 
Command (m for help): n                            #分配第二个主分区
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e
Partition number (3,4, default 3): 
First sector (629147648-1258291199, default 629147648): 
Using default value 629147648
Last sector, +sectors or +size{K,M,G} (629147648-1258291199, default 1258291199): +256G
Partition 3 of type Extended and of size 256 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended

Command (m for help): 
Command (m for help): n                            #分配扩展分区
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended

Command (m for help): 
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)            #当我们创建好扩展分区后,再次创建分区时会提示咱们是否创建逻辑分区,设备数字编号默认从5开始。
Select (default p): l
Adding logical partition 5
First sector (629149696-1166018559, default 629149696): 
Using default value 629149696
Last sector, +sectors or +size{K,M,G} (629149696-1166018559, default 1166018559): +50G
Partition 5 of type Linux and of size 50 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux

Command (m for help): 
Command (m for help): n                            #分配第一个逻辑分区
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux

Command (m for help): 
Command (m for help): n     
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (734009344-1166018559, default 734009344): 
Using default value 734009344
Last sector, +sectors or +size{K,M,G} (734009344-1166018559, default 1166018559): +100G
Partition 6 of type Linux and of size 100 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux
/dev/sdb6       734009344   943724543   104857600   83  Linux

Command (m for help): 
Command (m for help): n                            #分配第二个逻辑分区
Command (m for help): p       

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux
/dev/sdb6       734009344   943724543   104857600   83  Linux

Command (m for help): 
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 7
First sector (943726592-1166018559, default 943726592): 
Using default value 943726592
Last sector, +sectors or +size{K,M,G} (943726592-1166018559, default 1166018559): +80G
Partition 7 of type Linux and of size 80 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux
/dev/sdb6       734009344   943724543   104857600   83  Linux
/dev/sdb7       943726592  1111498751    83886080   83  Linux

Command (m for help): 
Command (m for help): n                            #分配第三个逻辑分区

3>.删除分区

Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648   734005247    52428800   83  Linux

Command (m for help): 
Command (m for help): d          #删除分区
Partition number (1-3, default 3):    #我们上面可以看到有3个分区信息,默认删除最后一个分区,我们也可以输入分区数的编号 
Partition 3 is deleted

Command (m for help): 
Command (m for help): p          #删除分区后再次查看分区信息

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux

Command (m for help): 
Command (m for help): 
Command (m for help): d          #删除分区

4>.退出交互式界面

[root@yinzhengjie.com ~]# 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): 
Command (m for help): q

[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
Command (m for help): q                    #直接退出并不保存任何修改
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       734009344   943724543   104857600   83  Linux
/dev/sdb6       943726592  1111498751    83886080   83  Linux

Command (m for help): 
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@yinzhengjie.com ~]# 
Command (m for help): w                    #将内存中的分区怒信息同步到磁盘并退出
[root@yinzhengjie.com ~]# lsblk 
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0    2T  0 disk 
├─sda1                   8:1    0    2M  0 part 
├─sda2                   8:2    0    1G  0 part /boot
└─sda3                   8:3    0    2T  0 part 
  ├─centos-root        253:0    0  500G  0 lvm  /
  └─centos-yinzhengjie 253:1    0  1.5T  0 lvm  /yinzhengjie
sdb                      8:16   0  600G  0 disk 
├─sdb1                   8:17   0  100G  0 part 
├─sdb2                   8:18   0  200G  0 part 
├─sdb3                   8:19   0    1K  0 part 
├─sdb5                   8:21   0  100G  0 part 
└─sdb6                   8:22   0   80G  0 part 
sdc                      8:32   0  400G  0 disk 
sdd                      8:48   0  800G  0 disk 
sde                      8:64   0 1000G  0 disk 
sr0                     11:0    1 1024M  0 rom  
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# lsblk
[root@yinzhengjie.com ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       734009344   943724543   104857600   83  Linux
/dev/sdb6       943726592  1111498751    83886080   83  Linux
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk -l /dev/sdb

5>.修改分区的系统ID

Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       734009344   943724543   104857600   83  Linux
/dev/sdb6       943726592  1111498751    83886080   83  Linux

Command (m for help):  
Command (m for help): t
Partition number (1-3,5,6, default 6): 
Hex code (type L to list all codes): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext\'d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e
Changed type of partition \'Linux\' to \'Linux LVM\'

Command (m for help): 
Command (m for help): p  

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       734009344   943724543   104857600   83  Linux
/dev/sdb6       943726592  1111498751    83886080   8e  Linux LVM

Command (m for help): 
Command (m for help): t                    #修改分区的系统ID

 

四.分区的注意事项

1>.设备分区数字编号不稳定

  生产环境中建议大家不要使用设备编号去挂载文件,因为当我们删除一个分区后,可能会导致灾难性的事情发生。

  当我们删除一个分区后,删除的分区后面的所有分区数字编号减1,这意味着你之前在"/etc/fstab"文件中挂载的所有分区可能都会出现问题。

  博主推荐大家使用UUID去唯一标识设备名称,这也是红帽公司在开源的Linux发行版如此设计的。
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       629149696   734007295    52428800   83  Linux
/dev/sdb6       734009344   943724543   104857600   83  Linux
/dev/sdb7       943726592  1111498751    83886080   83  Linux

Command (m for help):  
Command (m for help): d 
Partition number (1-3,5-7, default 7): 5        #我这里故意选择第五个逻辑分区进行删除。
Partition 5 is deleted

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 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: 0x7bc495c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   629147647   209715200   83  Linux
/dev/sdb3       629147648  1166018559   268435456    5  Extended
/dev/sdb5       734009344   943724543   104857600   83  Linux        #我们发现之前数字编号5的分区的确删除了,但是其后所有分区数字均会减1
/dev/sdb6       943726592  1111498751    83886080   83  Linux

Command (m for help): 
Command (m for help): 
Command (m for help): d                #实操案例戳这里

2>.fdisk命令查看的是磁盘上的数据

  当我们为一块操作系统正在使用的磁盘进行分区时,此时可能会导致磁盘上的分区表和内存中的分区表的数据不一致的情况。这是我们需要手动将磁盘中分区表的数据同步到内存中。

  在CentOS 7.x系列,使用partprobe命令就可以搞定这这件事。具体案例可参考我下面的笔记。
  在CentOS
6.x系列,使用partprobe命令可能不好好使,推荐大家使用part命令,"partx -a /dev/sda"(适用于增加分区),"partx -d --nr 6 /dev/sda "(适用于删除分区所对应的编号)。
[root@yinzhengjie.com ~]# fdisk /dev/sda
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): p

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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: gpt
Disk identifier: 60B0BC61-D7A6-4522-972F-E8B13E38A9C1


#         Start          End    Size  Type            Name
 1         2048         6143      2M  BIOS boot       
 2         6144      2103295      1G  Microsoft basic 
 3      2103296   4294723583      2T  Linux LVM       

Command (m for help): 
Command (m for help): n
Partition number (4-128, default 4): 
First sector (34-4294967262, default 4294723584): 
Last sector, +sectors or +size{K,M,G,T,P} (4294723584-4294967262, default 4294967262): +1M
Created partition 4


Command (m for help): w  
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk /dev/sda                 #实操案例戳这里
[root@yinzhengjie.com ~]# fdisk -l /dev/sda
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 2199.0 GB, 2199023255552 bytes, 4294967296 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: gpt
Disk identifier: 60B0BC61-D7A6-4522-972F-E8B13E38A9C1


#         Start          End    Size  Type            Name
 1         2048         6143      2M  BIOS boot       
 2         6144      2103295      1G  Microsoft basic 
 3      2103296   4294723583      2T  Linux LVM       
 4   4294723584   4294725631      1M  Linux filesyste 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# fdisk -l /dev/sda              #fdisk命令查看的是磁盘上的数据。
[root@yinzhengjie.com ~]# ll /dev/sda*
brw-rw---- 1 root disk 8, 0 May  8 20:47 /dev/sda
brw-rw---- 1 root disk 8, 1 May  8 20:47 /dev/sda1
brw-rw---- 1 root disk 8, 2 May  8 20:47 /dev/sda2
brw-rw---- 1 root disk 8, 3 May  8 20:47 /dev/sda3
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# ll /dev/sda*                 #查看的是内存中的数据
[root@yinzhengjie.com ~]# cat /proc/partitions  | grep sda
   8        0 2147483648 sda
   8        1       2048 sda1
   8        2    1048576 sda2
   8        3 2146310144 sda3
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# cat /proc/partitions | grep sda      #查看的是内存中的数据
[root@yinzhengjie.com ~]# lsblk /dev/sda
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0    2T  0 disk 
├─sda1                   8:1    0    2M  0 part 
├─sda2                   8:2    0    1G  0 part /boot
└─sda3                   8:3    0    2T  0 part 
  ├─centos-root        253:0    0  500G  0 lvm  /
  └─centos-yinzhengjie 253:1    0  1.5T  0 lvm  /yinzhengjie
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# lsblk /dev/sda                #依旧查看的是内存中的数据
[root@yinzhengjie.com ~]# partprobe /dev/sda              #该命令可以某块将磁盘中的数据同步到内存中,如果不指定磁盘,默认会将所有的磁盘设备的分区表重新刷新到内存。
[root@yinzhengjie.com ~]# lsblk /dev/sda
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0    2T  0 disk 
├─sda1                   8:1    0    2M  0 part 
├─sda2                   8:2    0    1G  0 part /boot
├─sda3                   8:3    0    2T  0 part 
│ ├─centos-root        253:0    0  500G  0 lvm  /
│ └─centos-yinzhengjie 253:1    0  1.5T  0 lvm  /yinzhengjie
└─sda4                   8:4    0    1M  0 part 
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# lsblk /dev/sda                #再次查看发现内存中有最新的分区信息啦
[root@yinzhengjie.com ~]# cat /proc/partitions  | grep sda
   8        0 2147483648 sda
   8        1       2048 sda1
   8        2    1048576 sda2
   8        3 2146310144 sda3
   8        4       1024 sda4
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# cat /proc/partitions | grep sda
[root@yinzhengjie.com ~]# ll /dev/sda* 
brw-rw---- 1 root disk 8, 0 May  8 20:55 /dev/sda
brw-rw---- 1 root disk 8, 1 May  8 20:55 /dev/sda1
brw-rw---- 1 root disk 8, 2 May  8 20:55 /dev/sda2
brw-rw---- 1 root disk 8, 3 May  8 20:55 /dev/sda3
brw-rw---- 1 root disk 8, 4 May  8 20:55 /dev/sda4
[root@yinzhengjie.com ~]# 
[root@yinzhengjie.com ~]# ll /dev/sda*

 

五.博主推荐阅读

  gdisk工具使用案例:
    https://www.cnblogs.com/yinzhengjie/p/12359770.html

  parted工具使用案例:
    https://www.cnblogs.com/yinzhengjie/p/12355151.html