Linux fdisk命令操作磁盘(添加、删除、转换分区等)

创建分区
1->查看原始分区
sudo fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x000a424d

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 39845887 19921920 83 Linux
/dev/sda2 39847934 41940991 1046529 5 Extended
/dev/sda5 39847936 41940991 1046528 82 Linux swap / Solaris

Disk /dev/sdb: 31.0 GB, 30953963520 bytes
64 heads, 32 sectors/track, 29520 cylinders, total 60456960 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 identifier: 0x000ababa

Device Boot Start End Blocks Id System

2->创建Partition
sudo fdisk /dev/sdb

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 删除硬盘分割区属性
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)

->n
Partition type:
p primary (0 primary, 0 extended, 4 free) (主分区)
e extended (扩展分区)
->Select (default p): p
->Partition number (1-4, default 1): 1
->First sector (2048-60456959, default 2048): (分区起始位置,一般直接回车)
Using default value 2048
->Last sector, +sectors or +size{K,M,G} (2048-60456959, default 60456959): +5G (分区大小)

->Command (m for help): w (保存设置)
The partition table has been altered!

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

3->format partition
sudo mkfs.vfat /dev/sdb1
mkfs.vfat 3.0.12 (29 Oct 2011)


4->分区结果
sudo fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x000a424d

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 39845887 19921920 83 Linux
/dev/sda2 39847934 41940991 1046529 5 Extended
/dev/sda5 39847936 41940991 1046528 82 Linux swap / Solaris

Disk /dev/sdb: 31.0 GB, 30953963520 bytes
64 heads, 32 sectors/track, 29520 cylinders, total 60456960 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 identifier: 0x000ababa

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux

No partition is defined yet! 解决办法

no partition is defined yet翻译过来意思是:没有定义分区!白话文的意思是:我们执行命令删除操作的时候,它在这个磁盘上没有找到这个分区,因此删除不了分区;也就是没有定义分区;

 可以使用fdisk -l查看磁盘信息

以上面分区举例进行删除分区操作:

1、sudo fdisk /dev/sdb

2、d

3、w

执行sudo fdisk /dev/sdb1就会提示No partition is defined yet!,而执行sudo fdisk /dev/sdb则不会,原因就是fdisk 是挂载磁盘,不是挂载分区;/dev/sdb这才是磁盘,而/dev/sdb1这是主分区,分区号是1;所以会失败;

 

查看分区个数

fdisk -l /dev/mmcblk0 | grep "^/dev" | wc -l

原文地址:https://www.cnblogs.com/Malphite/p/9138746.html