创建分区
本节将介绍如何对一个硬盘进行分区以及删除分区。
假设我们有一个硬盘/dev/sdb
,它还没有任何的分区,如下:
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 1 223.4G 0 disk
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 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
接下来,我们对硬盘/dev/sdb
进行分区,目标是分成一个主分区,扩展分区中分两个逻辑分区。首先,执行命令fdisk /dev/sdb
,然后会提示输入交互命令(完整的交互命令见本文附录),输入p
表示打印分区情况,n
表示新建分区,我们输入n
来新建分区。然后会提示选择创建的分区类型,p
表示主分区,e
表示扩展分区,我们输入p
来创建一个主分区。然后会继续提示该分区的编号,由于还没有分区存在,所以编号可以选择1
至4
,我们直接回车选择默认值1
。然后会继续提示输入起始扇区的索引值,由于1
到2047
的扇区已经被系统使用不可分配,我们直接回车选择默认值从2048
开始。接着,它会提示输入结束扇区的索引值或者大小,我们输入+50G
然后回车,此时一个50G大小的主分区就创建好了。接着,我们在命令提示符中输入w
表示把刚才的操作从内存写入到磁盘分区表中。此次命令结束。
[root@localhost ~]# 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 0x93de47a2.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-468559871, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-468559871, default 468559871): +50G
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
此时,虽然分区表已经被修改了,但是我们还看不到刚才的分区效果,我们还得执行一条命令partprobe
,然后我们就可以看到刚才的分区了。如下
[root@localhost ~]# partprobe
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 837G 0 disk
└─sda1 8:1 1 50G 0 part /
sdb 8:16 1 223.4G 0 disk
└─sdb1 8:17 1 50G 0 part
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 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: 0x93de47a2
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104859647 52428800 83 Linux
接着,我们把硬盘/dev/sdb
上剩余的空间创建一个扩展分区。首先,执行命令fdisk /dev/sdb
,接着在命令提示中输入n
,然后输入e
选择扩展分区,接着直接回车使用默认的分区号2
,然后直接回车起始扇区使用默认的值,然后再直接回车结束扇区也使用默认的值,此时剩余的空间就都划分到了扩展分区中。最后,输入w
把更改从内存写入到磁盘分区表中。
[root@localhost ~]# 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): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2):
First sector (104859648-468559871, default 104859648):
Using default value 104859648
Last sector, +sectors or +size{K,M,G} (104859648-468559871, default 468559871):
Using default value 468559871
Partition 2 of type Extended and of size 173.4 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
接着,我们执行partprobe
命令,然后再来查看分区情况,如下。我们发现,在lsblk
命令中看到的/sdb2
的大小为1K(虽然不知道为什么是1K,但没关系)。同时发现,/dev/sdb2
的System
为Extended
,可以看出,它是一个扩展分区。
[root@localhost ~]# partprobe
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 837G 0 disk
└─sda1 8:1 1 50G 0 part /
sdb 8:16 1 223.4G 0 disk
├─sdb1 8:17 1 50G 0 part
└─sdb2 8:18 1 1K 0 part
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 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: 0x93de47a2
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104859647 52428800 83 Linux
/dev/sdb2 104859648 468559871 181850112 5 Extended
接着,我们在/dev/sdb2
这个扩展分区中创建两个逻辑分区。首先输入命令fdisk /dev/sdb
,接着输入n
,此时由于已经建立了扩展分区,那么再新建分区的话就只能是主分区或者是逻辑分区,所以提示变成了p
与l
(l
表示逻辑分区)。我们输入l
,逻辑分区的编号是从5开始的,不需要我们选择。接着我们直接回车起始扇区使用默认值(即从扩展分区的起始扇区开始),然后输入+80G
,最后输入w
。
[root@localhost ~]# 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): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (104861696-468559871, default 104861696):
Using default value 104861696
Last sector, +sectors or +size{K,M,G} (104861696-468559871, default 468559871): +80G
Partition 5 of type Linux and of size 80 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
此时我们先不执行partprobe
命令,步骤与上面类似,如下
[root@localhost ~]# 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): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (272635904-468559871, default 272635904):
Using default value 272635904
Last sector, +sectors or +size{K,M,G} (272635904-468559871, default 468559871):
Using default value 468559871
Partition 6 of type Linux and of size 93.4 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
两个逻辑分区都创建好了以后,我们执行partprobe
命令然后查看分区情况,如下。这个时候,我们基本可以知道如何从lsblk
命令与fdisk
命令的输出判断某个分区是哪种类型的分区了。在lsblk
的输出中,编号小于等于4(sdb1到sdb4)的为主分区或扩展分区,而其中大小为1K的为扩展分区,其余的为主分区;编号大于等于5(sdb5、sdb6)的为逻辑分区。在fdisk
的输出中,同样,编号小于等于4的为主分区或扩展分区,而标志为Extended
的为扩展分区,标志为Linux
的为主分区;编号大于等于5的为逻辑分区。
[root@localhost ~]# partprobe
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 837G 0 disk
└─sda1 8:1 1 50G 0 part /
sdb 8:16 1 223.4G 0 disk
├─sdb1 8:17 1 50G 0 part
├─sdb2 8:18 1 1K 0 part
├─sdb5 8:21 1 80G 0 part
└─sdb6 8:22 1 93.4G 0 part
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 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: 0x93de47a2
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104859647 52428800 83 Linux
/dev/sdb2 104859648 468559871 181850112 5 Extended
/dev/sdb5 104861696 272633855 83886080 83 Linux
/dev/sdb6 272635904 468559871 97961984 83 Linux
那么到此,我们的分区任务就已经完成了。
删除分区
最好不要直接删除已经挂载的分区,如果分区已经挂载,请先卸载再删除,以免出现难以预料的问题。
接着上面的内容,现在我们要删除分区/dev/sdb6
。首先,执行命令fdisk /dev/sdb
,然后输入d
,然后输入6
,最后输入w
,删除操作完成。
[root@localhost ~]# 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): d
Partition number (1,2,5,6, default 6): 6
Partition 6 is deleted
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
接着,我们执行命令partprobe
,然后查看硬盘/dev/sdb
的分区情况,如下,分区删除成功
[root@localhost ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 1 223.4G 0 disk
├─sdb1 8:17 1 50G 0 part
├─sdb2 8:18 1 1K 0 part
└─sdb5 8:21 1 80G 0 part
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 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: 0x93de47a2
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104859647 52428800 83 Linux
/dev/sdb2 104859648 468559871 181850112 5 Extended
/dev/sdb5 104861696 272633855 83886080 83 Linux
附录1:fdisk交互命令(部分)
命令
说明
a
设置可引导标记
d
删除一个分区
l
分区类型
n
新建分区
p
显示分区列表
q
不保存退出
t
改变一个分区的类型
w
保存退出
附录2:分区类型
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
Reference
Last updated
Was this helpful?