Linux文件系統之新建分區(fdisk命令)

磁盤相關知識:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章