磁盘相关知识:

1、硬盘有数个盘片,每个盘片的两个面各自有一个磁头

2、盘片被划分为多个扇区

3、同一盘片不同半径的同心圆为磁道

4、不同盘片相同半径构成的圆柱面为柱面

 

fdisk命令:

1、查看已经识别的磁盘设备:

[root@localhost ~]# fdisk -l /dev/[hs]d[a-z]

Disk /dev/sda: 128.8 GB, 128849018880 bytes    
255 heads, 63 sectors/track, 15665 cylinders      
Units = cylinders of 16065 * 512 = 8225280 bytes      
Sector size (logical/physical): 512 bytes / 512 bytes      
I/O size (minimum/optimal): 512 bytes / 512 bytes      
Disk identifier: 0x0004fe34

   Device Boot      Start         End      Blocks   Id  System    
/dev/sda1   *           1          64      512000   83  Linux      
Partition 1 does not end on cylinder boundary.      
/dev/sda2              64       15666   125316096   8e  Linux LVM

 

前面一段表示这个磁盘/dev/sda有120GB,有255个磁头,每个磁道有63个扇区,一共有15665个柱面,每一个物理和逻辑扇区为512字节。

下面这7个字段分别表示:

(1)、Device表示磁盘分区

(2)、Boot表示可引导操作系统,*表示有一个可引导操作系统

(3)、Start:表示这个分区从第几个柱面开始

(4)、End:表示这个分区从第几个柱面结束

(5)、Blocks表示一共包含多少个块

(6)、Id为分区类型标识,用来标识为那种操作系统的分区类型

(7)、System分区表示所属的操作系统类型和Id相对应。

2、管理分区

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to    
         switch off the mode (command 'c') and change display units to      
         sectors (command 'u').

Command (m for help):

 

这里用fdisk 后面对应哪个磁盘设备来创建文件分区,这里输入m可以查看fdisk的子命令:

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)

 

下面来看下fdisk常用的子命令:

p: 显示现有分区表;    
n: 创建新分区      
d: 删除现有分区      
t: 修改分区ID      
l: 查看支持哪些分区ID      
w: 保存退出      
q: 不保存退出      
m: 显示帮助信息

 

用子命令p查看现有的分区表

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes    
255 heads, 63 sectors/track, 2610 cylinders      
Units = cylinders of 16065 * 512 = 8225280 bytes      
Sector size (logical/physical): 512 bytes / 512 bytes      
I/O size (minimum/optimal): 512 bytes / 512 bytes      
Disk identifier: 0x58996cef

   Device Boot      Start         End      Blocks   Id  System    
/dev/sdb1               1         654     5253223+  83  Linux      
/dev/sdb2             655        2610    15711570    5  Extended      
/dev/sdb5             655         916     2104483+  83  Linux      
/dev/sdb6             917         918       16033+  82  Linux swap / Solaris

这里可以看到/dev/sdb现在有4个分区,可以看出/dev/sdb1为主分区,/dev/sdb2为扩展分区,/dev/sdb5和/dev/sdb6为逻辑分区。

用子命令n来创建新分区:

Command (m for help): n    
Command action      
   l   logical (5 or over)      
   p   primary partition (1-4)

 

这里可以选“l”为创建逻辑分区,选“P”为创建主分区

在这里我选择“l”:

Command (m for help): n    
Command action      
   l   logical (5 or over)      
   p   primary partition (1-4)      
l      
First cylinder (919-2610, default 919):      
Using default value 919      
Last cylinder, +cylinders or +size{K,M,G} (919-2610, default 2610): +2G

 

在这里First cylinder表示这个分区从哪个柱面开始,Last cylinder表示为从哪个柱面结束,当然我这里直接打“+2G”表示我直接给这个分区2G的大小。

创建好分区后我们用“P”子命令看下有没有新创建一个2G的分区:

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes    
255 heads, 63 sectors/track, 2610 cylinders      
Units = cylinders of 16065 * 512 = 8225280 bytes      
Sector size (logical/physical): 512 bytes / 512 bytes      
I/O size (minimum/optimal): 512 bytes / 512 bytes      
Disk identifier: 0x58996cef

   Device Boot      Start         End      Blocks   Id  System    
/dev/sdb1               1         654     5253223+  83  Linux      
/dev/sdb2             655        2610    15711570    5  Extended      
/dev/sdb5             655         916     2104483+  83  Linux      
/dev/sdb6             917         918       16033+  82  Linux swap / Solaris      
/dev/sdb7             919        1180     2104483+  83  Linux

这里我们看到了已经新建了一个分区/dev/sdb7,创建后我们Linux内核还不一定会识别我们新创建的分区,我们要看内核是否已经识别到新建的分区可以用cat命令查看/proc/partitions文件

[root@localhost ~]# cat /proc/partitions      
major minor  #blocks  name

   8        0  125829120 sda    
   8        1     512000 sda1      
   8        2  125316096 sda2      
   8       16   20971520 sdb      
   8       17    5253223 sdb1      
   8       18          1 sdb2      
   8       21    2104483 sdb5      
   8       22      16033 sdb6      
253        0   52428800 dm-0      
253        1     983040 dm-1      
253        2   71901184 dm-2

 

我们这里看到内核还没识别到/dev/sdb7设备,怎么办呢?我们可以使用以下两个命令来通知内核来识别我们新建分区的设备:

1、partx –a /dev/sdb

2、kpartx –af /dev/sdb