執行個體解說fdisk使用方法 for Linux

來源:互聯網
上載者:User

fdisk
是一款功能強大的分區工具,也是目前在Unix類作業系統中最流行的工具之一;分區工具老字型大小;本篇只介紹其最簡單的分區操作功能;

一、fdisk
的介紹

fdisk - Partition table manipulator for Linux
,譯成中文的意思是磁碟分割表操作工具;本人譯的不太好,也沒有看中文文檔;其實就是分區工具; fdsik
能劃分磁碟成為若干個區,同時也能為每個分區指定分區的檔案系統,比如linux 、fat32、 linux 、linux swap 、fat16
以及其實類Unix類作業系統的檔案系統等;當然我們用fdisk
對磁碟操作分區時,並不是一個終點,我們還要對分區進行格式化所需要的檔案系統;這樣一個分區才能使用;這和DOS中的fdisk 是類似的;

二、合理規劃您的硬碟分區

在操作分區之前,我們要明白硬碟分區一點理論,比如硬碟容量和分區大小的計算;對一個硬碟如何規劃分區等,請參考
《合理規劃您的硬碟分區》

三、fdisk -l
查看硬碟及分區資訊

通過《合理規劃您的硬碟分區》
,我們知道主要磁碟分割(包括擴充分區)的總個數不能超過四個;也不能把擴充分區包圍在主要磁碟分割之間;根據這個原則,我們劃分硬碟分區就比較容易的多;也能為以後減少不必要的麻煩;


1、通過fdisk -l 查看機器所掛硬碟個數及分區情況

[root@localhost beinan]# fdisk -l

Disk /dev/hda: 80.0 GB,
80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units =
cylinders of 16065 * 512 = 8225280 bytes

   Device Boot     
Start         End      Blocks   Id  System
/dev/hda1   *           1        
765     6144831    7  HPFS/NTFS
/dev/hda2             766        2805   
16386300    c  W95 FAT32 (LBA)
/dev/hda3            2806        9729   
55617030    5  Extended
/dev/hda5            2806        3825     8193118+ 
83  Linux
/dev/hda6            3826        5100    10241406   83 
Linux
/dev/hda7            5101        5198      787153+  82  Linux swap /
Solaris
/dev/hda8            5199        6657    11719386   83 
Linux
/dev/hda9            6658        7751     8787523+  83 
Linux
/dev/hda10           7752        9729    15888253+  83 
Linux

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63
sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536
bytes

   Device Boot      Start         End      Blocks   Id 
System
/dev/sda1               1          25      201568+   c  W95 FAT32
(LBA)
/dev/sda2              26         125      806400    5 
Extended
/dev/sda5              26          50      201568+  83 
Linux
/dev/sda6              51          76      200781   83 
Linux

通過上面的資訊,我們知道此機器中掛載兩個硬碟(或移動硬碟),其中一個是hda
另一個是sda ;如果我們想查看單個硬碟情況,可以通過 fdisk -l /dev/hda1 或者fdisk -l /dev/sda1 來操作;以fdisk
-l 輸出的硬碟標識為準;其中 hda有三個主要磁碟分割(包括擴充分區),分別是主要磁碟分割 hda1 hda2 和hda3(擴充分區) ;邏輯分區是
hda5到hda10;
其中 sda 有兩個主要磁碟分割(包括擴充分區),分別是 hda1 和hda2 (擴充分區);邏輯分區是 sda5 hda6
;硬碟總容量=主要磁碟分割(包括擴充分區)總容量
擴充分區容量=邏輯分區總容量通過上面的例子,我們可以得知
hda=hda1+hda2+hda3,其中hda3=hda5+hda6+hda7+hda8+hda9+hda10 ...
...

2、關於fdisk -l 一些數值的說明

Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track,
9729 cylinders
Units = cylinders of 16065 * 512 = 8225280
bytes

這個硬碟是80G的,有255個磁面;63個扇區;9729個磁柱;每個
cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(約為)=8.225280M(約為);

分區序列    引導    開始      終止    容量      分區類型ID    分區類型
Device        
Boot      Start         End      Blocks            Id             
System
/dev/hda1     *           1         765        6144831            
7              HPFS/NTFS
/dev/hda2              766        2805    
16386300             c              W95 FAT32 (LBA)
/dev/hda3            
2806        9729    55617030             5             
Extended
/dev/hda5             2806        3825     8193118+        
83               Linux
/dev/hda6             3826        5100   
10241406           83               Linux
/dev/hda7             5101       
5198      787153+          82               Linux swap /
Solaris
/dev/hda8             5199        6657    11719386          
83               Linux
/dev/hda9             6658        7751    
8787523+         83               Linux
/dev/hda10           7752       
9729    15888253+        83              
Linux

說明: 硬碟分區的表示:在Linux 是通過hd*x 或 sd*x
表示的,其中 * 表示的是a、b、c ... ... x表示的數字 1、2、3 ... ...
hd大多是IDE硬碟;sd大多是SCSI或移動儲存;
引導(Boot):表示引導分區,在上面的例子中 hda1 是引導分區;
Start
(開始):表示的一個分區從X cylinder(磁柱)開始;
End (結束):表示一個分區到 Y
cylinder(磁柱)結束;
id和System 表示的是一個意思,id看起來不太直觀,我們要在fdisk 一個分區時,通過指定id來確認分區類型;比如
7表示的就NTFS 分區;這個在fdisk
中要通過t功能來指定。下面的部份會提到;
Blocks(容量):這是我翻譯的,其實不準確,表示的意思的確是容量的意思,其單位是K;一個分區容量的值是由下面的公式而來的;
Blocks = (相應分區End數值 - 相應分區Start數值)x 單位cylinder(磁柱)的容量 所以我們算一下 hda1的 Blocks 的大小 :
hda1 Blocks=(765-1)x8225.280=6284113.92 K = 6284.113.92M
註:換算單位以硬碟廠家提供的10進位算起,如果以作業系統二進位來算,這個分區容量應該更少一些,得出的這個值和我們通過 fdisk -l 看到的
/dev/hda1的值是大體相當的,因為換算方法不一樣,所以也不可能儘可能的精確;再加上分區時的一點損失之類,有時或大或小是存在的;我們查看分區大小或者檔案的時候,還是用十進位來計算比較直觀;推算辦法是
byte 向前推小數點三位就是K ,K單位的值向前推小數點三位就是M,M向前推小數點三位就是G... ... 一般也差不了多少;這麼算就行;

3、估算一個存放裝置是否被完全劃分

我們估算一個硬碟是否完全被劃分,我們只要看 fdisk
-l 輸出的內容中的 cylinders(柱體) 上一個分區的End 和 下一個分區的Start是不是一個連續的數字,另外要看一下每個硬碟裝置的fdisk -l
的開頭部份,看一下他的 cylinders(柱體)的值;比如hda裝置,我們看到的是 9729 cylinders ;我們通過
hda的分區表可以看到上一個分區的End的值+1 就是下一個分區的Start 的值;比如 hda2的Start的值是 hda1 的End 的值+1,這證明
hda1 和hda2 中間沒有空白分區,是連續的,以此類推;在 hda10,我們看到 End 的值是9729 ,而在fdisk -l頭部資訊中也有9729
cylinders,證明這個硬碟已經完全劃分;

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track,
125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

  
Device Boot      Start         End      Blocks   Id 
System
/dev/sda1               1          25      201568+   c  W95 FAT32
(LBA)
/dev/sda2              26         125      806400    5 
Extended
/dev/sda5              26          50      201568+  83 
Linux
/dev/sda6              51          76      200781   83 
Linux

我們再看看 sda 移動儲是不是被完全劃分了;sda有
125個cylinders
(柱體),有一個主要磁碟分割和一個擴充分區構成;在擴充分區中,我們看到End的值為125,而這個移動硬碟的cylinder也是125,這能說明這個硬碟不可能再添加任何主要磁碟分割了;根據我們上面所說的
sda1 sda2 sda5 sda6 之間未有任何未劃分空間,但sda6 的cylinders (柱體)的End值卻是 76 ,而
sda總的cylinders (柱體)有125個,由此看來sda 在 sda6後面有未劃分地區;至於sda
有多少未劃分空間,我們算一下就知道了;擴充分區總容量是 806400 K ,大約是 806.400M左右,而邏輯分區 sda5 和sda6 的大小加起來是
400M左右,所以還仍有400M左右未劃分空間,並且只能劃分為鏈邏輯分區;

四、fdisk 對硬碟及分區的操作,進入fdisk
對硬碟操作階段

我們可以對硬碟進行分區操作,前提是您把fdisk -l 弄明白了;通過fdisk -l
,我們能找出機器中所有硬碟個數及裝置名稱;比如上面的例子,我們會看到兩個裝置一個是/dev/hda ,另一個是/dev/sda ;

fdisk
操作硬碟的命令格式如下:

[root@localhost beinan]# fdisk    裝置

比如我們通過 fdisk -l 得知 /dev/hda 或者
/dev/sda裝置;我們如果想再添加或者刪除一些分區,可以用

[root@localhost beinan]# fdisk     /dev/hda

[root@localhost
beinan]# fdisk  /dev/sda


在以後的例子中,我們要以 /dev/sda裝置為例,來講解如何用fdisk 來操作添加、刪除分區等動作;

1、fdisk
的說明

當我們通過 fdisk 裝置,進入相應裝置的操作時,會發現有如下的提示;以 fdisk /dev/sda
裝置為例,以下同;

[root@localhost beinan]# fdisk /dev/sda
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 
註:l是列出分區類型,以供我們設定相應分區的類型;
   m   print this menu  註:m 是列出協助資訊;
   n   add
a new partition 註:添加一個分區;
   o   create a new empty DOS partition table

   p   print the partition table 註:p列出分區表;
   q   quit without saving
changes 註:不儲存退出;
   s   create a new empty Sun disklabel   
   t   change
a partition's system id  註:t 改變分區類型;
   u   change display/entry units 

   v   verify the partition table
   w   write table to disk and exit 
註:把分區表寫入硬碟並退出;
   x   extra functionality (experts only) 
註:擴充應用,專家功能;

其實我們常用的只有注有中文的,其它的功能我們不常用(呵,主要是我不會用,否則早會賣弄一下了);x擴充功能,也不是常用的;一般的情況下只要懂得
d l m p q t w
就行了;下面以執行個體操作來詳述,沒有例子沒有辦法就,新手也看不懂;

2、列出當前操作硬碟的分區情況,用p;

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944
bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of
16128 * 512 = 8257536 bytes

   Device Boot      Start         End     
Blocks   Id  System
/dev/sda1               1          25      201568+   c 
W95 FAT32 (LBA)
/dev/sda2              26         125      806400    5 
Extended
/dev/sda5              26          50      201568+  83 
Linux
/dev/sda6              51          76      200781   83 
Linux

3、通過fdisk的d指令來刪除一個分區

Command (m for help): p    註:列出分區情況;

Disk /dev/sda: 1035 MB,
1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units =
cylinders of 16128 * 512 = 8257536 bytes

   Device Boot     
Start         End      Blocks   Id  System
/dev/sda1               1         
25      201568+   c  W95 FAT32 (LBA)
/dev/sda2              26        
125      806400    5  Extended

/dev/sda5              26          50     
201568+  83  Linux
/dev/sda6              51          76      200781   83 
Linux

Command (m for help): d 註:執行刪除分區指定;
Partition number (1-6): 6 
註:我想刪除 sda6 ,就在這裡輸入 6 ;

Command (m for help): p
註:再查看一下硬碟分區情況,看是否刪除了?

Disk /dev/sda: 1035 MB, 1035730944 bytes
256
heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 =
8257536 bytes

   Device Boot      Start         End      Blocks   Id 
System
/dev/sda1               1          25      201568+   c  W95 FAT32
(LBA)
/dev/sda2              26         125      806400    5 
Extended
/dev/sda5              26          50      201568+  83 
Linux

Command (m for
help):

警告:刪除分區時要小心,請看好分區的序號,如果您刪除了擴充分區,擴充分區之下的邏輯分區都會刪除;所以操作時一定要小心;如果知道自己操作錯了,請不要驚慌,用q不儲存退出;切記切記!!!!在分區操作錯了之時,千萬不要輸入w儲存退出!!!

4、通過fdisk的n指令增加一個分區

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944
bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of
16128 * 512 = 8257536 bytes

   Device Boot      Start         End     
Blocks   Id  System
/dev/sda1               1          25      201568+   c 
W95 FAT32 (LBA)
/dev/sda2              26         125      806400    5 
Extended
/dev/sda5              26          50      201568+  83 
Linux

Command (m for help): n  註:增加一個分區;
Command action
   l  
logical (5 or over)  註:增加邏輯分區,分區編號要大於5;為什麼要大於5,因為已經有sda5了;
   p   primary
partition (1-4) 註:增加一個主要磁碟分割;編號從 1-4 ;但sda1 和sda2都被佔用,所以只能從3開始;
p
Partition
number (1-4): 3
No free sectors available 
註:失敗中,為什麼失敗?

註:我試圖增加一個主要磁碟分割,看來是失敗了,為什麼失敗?因為我們看到主要磁碟分割+擴充分區把整個磁碟都用光了,看擴充分區的End的值,再看一下
p輸出資訊中有125 cylinders;最好還是看前面部份;那裡有提到;所以我們只能增加邏輯分區了;

Command (m for help): n
Command action
   l   logical (5 or
over)
   p   primary partition (1-4)
l   註:在這裡輸入l,就進入劃分邏輯分區階段了;
First
cylinder (51-125, default 51):   註:這個就是分區的Start
值;這裡最好直接按斷行符號,如果您輸入了一個非預設的數字,會造成空間浪費;
Using default value 51
Last cylinder
or +size or +sizeM or +sizeK (51-125, default 125): +200M  註:這個是定義分區大小的,+200M
就是大小為200M ;當然您也可以根據p提示的單位cylinder的大小來算,然後來指定
End的數值。回頭看看是怎麼算的;還是用+200M這個辦法來添加,這樣能直觀一點。如果您想添加一個10G左右大小的分區,請輸入 +10000M

Command (m for
help):

5、通過fdisk的t指令指定分區類型

Command (m for help): t  註:通過t來指定分區類型;
Partition number (1-6): 6
註:要改變哪個分區類型呢?我指定了6,其實也就是sda6
Hex code (type L to list codes):L 
註:在這裡輸入L,就可以查看分區類型的id了;
Hex code (type L to list codes): b  註:如果我想讓這個分區是 W95
FAT32 類型的,通過L查看得知 b是表示的是,所以輸入了b;
Changed system type of partition 6 to b (W95
FAT32)  註:系統資訊,改變成功;是否是改變了,請用p查看;

Command (m for help): p

Disk
/dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125
cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

   Device
Boot      Start         End      Blocks   Id  System
/dev/sda1              
1          25      201568+   c  W95 FAT32 (LBA)
/dev/sda2             
26         125      806400    5  Extended
/dev/sda5              26         
50      201568+  83  Linux
/dev/sda6              51          75     
201568+   b  W95 FAT32

6、fdisk
的退出,用q或者 w

其中 q是 不儲存退出,w是儲存退出;

Command (m for help): w

Command (m for help):
q

7、一個添加分區的例子

本例中我們會添加兩個200M的主要磁碟分割,其它為擴充分區,在擴充分區中我們添加兩個200M大小的邏輯分區;

Command (m for help): p 註:列出分區表;

Disk /dev/sda: 1035 MB, 1035730944
bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of
16128 * 512 = 8257536 bytes

   Device Boot      Start         End     
Blocks   Id  System

Command (m for help): n 註:添加分區;
Command
action
   e   extended
   p   primary partition (1-4)

註:添加主要磁碟分割;
Partition number (1-4): 1   註:添加主要磁碟分割1;
First cylinder (1-125,
default 1):   註:直接斷行符號,主要磁碟分割1的起始位置;預設為1,預設就好;
Using default value 1
Last
cylinder or +size or +sizeM or +sizeK (1-125, default 125): +200M  
註:指定分區大小,用+200M來指定大小為200M

Command (m for help): n  註:添加新分區;
Command
action
   e   extended
   p   primary partition (1-4)

註:添加主要磁碟分割
Partition number (1-4): 2  註:添加主要磁碟分割2;
First cylinder (26-125,
default 26):
Using default value 26
Last cylinder or +size or +sizeM or
+sizeK (26-125, default 125): +200M 註:指定分區大小,用+200M來指定大小為200M

Command (m
for help): n
Command action
   e   extended
   p   primary partition
(1-4)
e  註:添加擴充分區;
Partition number (1-4): 3  註:指定為3
,因為主要磁碟分割已經分了兩個了,這個也算主要磁碟分割,從3開始;
First cylinder (51-125, default 51): 
註:直接斷行符號;
Using default value 51
Last cylinder or +size or +sizeM or +sizeK
(51-125, default 125):   註:直接斷行符號,把其餘的所有空間都給擴充分區;
Using default value
125

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944
bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of
16128 * 512 = 8257536 bytes

   Device Boot      Start         End     
Blocks   Id  System
/dev/sda1               1          25      201568+  83 
Linux
/dev/sda2              26          50      201600   83 
Linux
/dev/sda3              51         125      604800    5 
Extended

Command (m for help): n
Command action
   l   logical (5
or over)
   p   primary partition (1-4)
l  註:添加邏輯分區;
First cylinder
(51-125, default 51):
Using default value 51
Last cylinder or +size or
+sizeM or +sizeK (51-125, default 125): +200M 
註:添加一個大小為200M大小的分區;

Command (m for help): n
Command action
   l  
logical (5 or over)
   p   primary partition (1-4)
l  註:添加一個邏輯分區;
First
cylinder (76-125, default 76):
Using default value 76
Last cylinder or
+size or +sizeM or +sizeK (76-125, default 125): +200M
註:添加一個大小為200M大小的分區;

Command (m for help): p  列出分區表;

Disk /dev/sda:
1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units
= cylinders of 16128 * 512 = 8257536 bytes

   Device Boot     
Start         End      Blocks   Id  System
/dev/sda1               1         
25      201568+  83  Linux
/dev/sda2              26          50     
201600   83  Linux
/dev/sda3              51         125      604800    5 
Extended
/dev/sda5              51          75      201568+  83 
Linux
/dev/sda6              76         100      201568+  83 
Linux

然後我們根據前面所說通過t指令來改變分區類型;最後不要忘記w儲存退出;

五、對分區進行格式化,以及載入;

先提示一下;用 mkfs.bfs mkfs.ext2
mkfs.jfs mkfs.msdos mkfs.vfatmkfs.cramfs mkfs.ext3 mkfs.minix mkfs.reiserfs
mkfs.xfs 等命令來格式化分區,比如我想格式化 sda6為ext3檔案系統,則輸入;

[root@localhost beinan]# mkfs.ext3
/dev/sda6

如果我想載入
sda6到目前系統來存取檔案,應該有mount 命令,但首先您得建一個掛載目錄;比如 /mnt/sda6 ;

[root@localhost beinan]# mkdir /mnt/sda6
[root@localhost beinan]# mount
/dev/sda6 /mnt/sda6
[root@localhost beinan]# df -lh
Filesystem           
容量  已用 可用 已用% 掛載點
/dev/hda8              11G  8.4G  2.0G  81%
/
/dev/shm              236M     0  236M   0%
/dev/shm
/dev/hda10             16G  6.9G  8.3G  46%
/mnt/hda10
/dev/sda6             191M  5.6M  176M   4%
/mnt/sda6

這樣我們就能進入 /mnt/sda6目錄,然後存取檔案了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.