Fdisk给磁盘分区

一般fdisk用来管理linux的磁盘,进行分区,格式化等操作

fdisk简述

fdisk 常用于查看磁盘使用情况和磁盘分区

命令选项

1
2
3
4
5
6
7
8
 -b <大小>             扇区大小(512、1024、2048或4096)
 -c[=<模式>]           兼容模式:“dos”或“nondos”(默认)
 -h                    打印此帮助文本
 -u[=<单位>]           显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
 -v                    打印程序版本
 -C <数字>             指定柱面数
 -H <数字>             指定磁头数
 -S <数字>             指定每个磁道的扇区数

磁盘分区操作

给磁盘分区

选择要操作的盘

1
[root@k8s-77-166 ~]# fdisk /dev/sdb

输入m列出可以执行的命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 翻译后如下
命令动作
    a 切换可引导标志
    b 编辑 bsd 磁盘标签
    c 切换 dos 兼容性标志
    d 删除一个分区
    g 创建一个新的空 GPT 分区表
    G 创建一个 IRIX (SGI) 分区表
    l 列出已知的分区类型
    m 打印这个菜单
    n 添加一个新分区
    o 创建一个新的空 DOS 分区表
    p 打印分区表
    q 退出而不保存更改
    s 创建一个新的空 Sun 磁盘标签
    t 更改分区的系统 ID
    u 更改显示/输入单位
    v 验证分区表
    w 将表写入磁盘并退出
    x 额外功能(仅限专家)

输入p列出磁盘目前的分区情况

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xac03980f

   Device Boot      Start         End      Blocks   Id  System

输入n建立新的磁盘分区,这里建立两个主磁盘分区

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Command (m for help): n #创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p #选择p创建主分区 如果创建扩展分区的话选择e
Partition number (1-4, default 1): 1 #第一个主分区
First sector (2048-209715199, default 2048):  #起始处,直接回车就行
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +20G  #分区大小为K,M,G。制作分区的大小,这里选择第一块分区大小为20G
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p #输入p查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux

Command (m for help): n #接着创建第二个住分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p #这里也是选择p
Partition number (2-4, default 2): 2 #第二个分区
First sector (41945088-209715199, default 41945088): #默认回车
Using default value 41945088
Last sector, +sectors or +size{K,M,G} (41945088-209715199, default 209715199): #直接回车 将剩余空间全部给这个分区
Using default value 209715199
Partition 2 of type Linux and of size 80 GiB is set

Command (m for help): t #修改分区的类型,这里选择
Partition number (1,2, default 2): 2 #选择分区
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 #输入lvm类型的代码
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p #查看分区情况  可以看到分区2已经为lvm类型

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088   209715199    83885056   8e  Linux LVM

Command (m for help): w #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

删除分区

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@k8s-77-166 ~]# 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): p #查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088   209715199    83885056   8e  Linux LVM

Command (m for help): d #删除分区
Partition number (1,2, default 2): 2 #选择要删除的分区
Partition 2 is deleted

Command (m for help): p #查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux

Command (m for help): w #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建扩展分区

创建扩展分区,并在扩展分区上创建两个逻辑分区,将一个逻辑分区设置为lvm类型

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
[root@k8s-77-166 ~]# 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): p #查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux

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 #默认2分区
First sector (41945088-209715199, default 41945088): #起始位置 默认
Using default value 41945088
Last sector, +sectors or +size{K,M,G} (41945088-209715199, default 209715199): +30G #选择扩展分大小为30G
Partition 2 of type Extended and of size 30 GiB is set

Command (m for help): p #查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088   104859647    31457280    5  Extended

Command (m for help): n #创建分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l #选择l创建逻辑分区
Adding logical partition 5 # 注意:主分区和扩展分区的磁盘号位1-4,也就是说最多有4个主分区或者扩展分区,逻辑分区开始的磁盘号为5
First sector (41947136-104859647, default 41947136): #起始位置 默认回车
Using default value 41947136 
Last sector, +sectors or +size{K,M,G} (41947136-104859647, default 104859647): +10G #选择逻辑分区大小为10G
Partition 5 of type Linux and of size 10 GiB is set

Command (m for help): n #创建分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l #选择l创建逻辑分区
Adding logical partition 6
First sector (62920704-104859647, default 62920704):#起始位置 默认回车
Using default value 62920704
Last sector, +sectors or +size{K,M,G} (62920704-104859647, default 104859647): #直接回车 将剩余空间全部给这个分区
Using default value 104859647
Partition 6 of type Linux and of size 20 GiB is set

Command (m for help): p #查看分区情况

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088   104859647    31457280    5  Extended
/dev/sdb5        41947136    62918655    10485760   83  Linux
/dev/sdb6        62920704   104859647    20969472   83  Linux

Command (m for help): t #修改分区的类型,这里选择
Partition number (1,2,5,6, default 6): 6 #选择逻辑分区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  #选择lvm的代码
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p #查看分区

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x708c5a80

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088   104859647    31457280    5  Extended
/dev/sdb5        41947136    62918655    10485760   83  Linux
/dev/sdb6        62920704   104859647    20969472   8e  Linux LVM

Command (m for help): w #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

格式化分区

建立好分区之后,还需要对分区进行格式化才能在系统中使用磁盘

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 不同文件系统文件类型的格式化命令也不相同,一下列出常用的几种
mkfs.ext2 /dev/sdb1
mkfs.ext3 /dev/sdb1
mkfs.ext4 /dev/sdb1
mkfs.xfs  /dev/sdb1
mkfs.btrfs /dev/sdb1


### 调整 Linux 文件系统大小
#resize2fs 是用于 ext2、ext3 和 ext4 文件系统的命令。
#它允许您调整文件系统的大小(即扩大或缩小文件系统),以匹配所分配的磁盘空间大小。
resize2fs /dev/sda1

#xfs_growfs 是用于 xfs 文件系统的命令。它允许您扩大或缩小文件系统,以匹配所分配的磁盘空间大小。
#在使用该命令之前,您必须先扩展磁盘分区。
xfs_growfs /

参考链接

https://wangchujiang.com/linux-command/c/fdisk.html

https://www.cnblogs.com/jiechn/p/4494958.html

https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_039_fdisk_gdisk_parted.html

Buy me a coffee
支付宝
微信
0%