歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux操作系統學習筆記管理磁盤和文件系統(2)

Linux操作系統學習筆記管理磁盤和文件系統(2)

日期:2017/2/25 10:32:41   编辑:Linux教程

規劃硬盤中的分區——fdisk

  fdisk [磁盤設備]

  使用此命令可進入交互操縱,您可以在下面的交互操縱裡面胡作非為,但是切記離開時必須按q命令,不能按w

#找出系統中所在根目錄所在磁盤,並查閱該硬盤內的相關信息
[root@localhost ~]# df / <==重點找出磁盤文件名而已 文件系統 1K-塊 已用 可用 已用% 掛載點 /dev/hda2 9920624 2968604 6439952 32% / [root@localhost ~]# fdisk /dev/hda ==>進入交互操縱模式,注意這裡不要寫具體的數字 The number of cylinders for this disk is set to 2610. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): m <==等待輸入,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 ==>不存儲,直接離開fdisk程序 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)

常見操作有:創建主分區、創建擴展分區(將硬盤的所有剩余空間作為擴展分區)、創建邏輯分區、改變分區的系統代碼、保存分區表信息、用partprobe探測硬盤分區變化

下面舉出幾例做測試,離開時不保存:

 Command (m for help): p  ==>查看磁盤分區表,和終端命令模式下的fdisk -l 作用相同

Disk /dev/hda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1288    10241437+  83  Linux
/dev/hda3            1289        1925     5116702+  83  Linux
/dev/hda4            1926        2610     5502262+   5  Extended
/dev/hda5            1926        1989      514048+  82  Linux swap / Solaris

Command (m for help): d  
Partition number (1-5): 4   ==>刪除第4個分區

Command (m for help): d    ==>刪除第3個分區
Partition number (1-4): 3

Command (m for help): p  ==>再次顯示時觀察下面只顯示了前兩個分區,第5個沒顯示

Disk /dev/hda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1288    10241437+  83  Linux

Command (m for help): n  ==>創建一個分區
Command action
   e   extended
   p   primary partition (1-4)
p  ==>選擇創建的分區類型為主分區
Partition number (1-4): 4 ==>選擇4號做主分區
First cylinder (1289-2610, default 1289): 
Using default value 1289   ==>起始柱面若直接按enter鍵就是使用默認值
Last cylinder or +size or +sizeM or +sizeK (1289-2610, default 2610): +512M

Command (m for help): p  ==>再次查看分區表,發現多了一個剛從建立的分區4

Disk /dev/hda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1288    10241437+  83  Linux
/dev/hda4            1289        1351      506047+  83  Linux

Command (m for help): n  ==>再建一個分區
Command action
   e   extended
   p   primary partition (1-4)
e   ==>建立擴展分區
Selected partition 3  ==>這個是系統主動選擇的分區
First cylinder (1352-2610, default 1352): 
Using default value 1352
Last cylinder or +size or +sizeM or +sizeK (1352-2610, default 2610): 
Using default value 2610

Command (m for help): p

Disk /dev/hda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1288    10241437+  83  Linux
/dev/hda3            1352        2610    10112917+   5  Extended  ==>看,剛新建的擴展分區
/dev/hda4            1289        1351      506047+  83  Linux

Partition table entries are not in disk order

Command (m for help): n  ==>再多建幾個分區玩玩,反正後面不保存就行了,,呵呵
First cylinder (1352-2610, default 1352): 
Using default value 1352
Last cylinder or +size or +sizeM or +sizeK (1352-2610, default 2610): +2084
Value out of range.
Last cylinder or +size or +sizeM or +sizeK (1352-2610, default 2610): +2084M

Command (m for help): n   ==>有沒有發現,最後建立2個分區的時候根本沒有詢問我們要建立是主分區還是擴展分區,為啥?因為分區已經超過4了,您別無選擇,只能是擴展分區了。。。
First cylinder (1606-2610, default 1606): 
Using default value 1606
Last cylinder or +size or +sizeM or +sizeK (1606-2610, default 2610): 
Using default value 2610

Command (m for help): p

Disk /dev/hda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1288    10241437+  83  Linux
/dev/hda3            1352        2610    10112917+   5  Extended
/dev/hda4            1289        1351      506047+  83  Linux
/dev/hda5            1352        1605     2040223+  83  Linux  ==>這是後面又新建的2個分區
/dev/hda6            1606        2610     8072631   83  Linux

Partition table entries are not in disk order

Command (m for help): q  ==>這裡一定要按q啊,因為我剛才僅僅是演示操作,還破壞性的刪除了2個分區,你要是真要分區,那就要按w保存了。

[root@localhost ~]# 

	

  磁盤分好區後,要更新下內核,否則您使用fdisk -l 可能看不到分區後的變化,只有重啟才可以。但是使用partprobe強制讓內核重新找一次分區表就行了。

[root@localhost ~]# partprobe [/dev/hda ]   <==強制讓內核重新找一次分區表,則可以立刻看出分區的變化

注意:普通用戶沒有權限進行硬盤分區,只有root用戶可以,但最好是在單用戶模式下比較安全,此外,在進行分區的時候,若該硬盤某個分區正在使用當中,則很有可能系統內核會無法加載硬盤的分區表,解決的方法就是將該使用中的分區給卸載,然後再重新分區一次,重新寫入分區表。

Copyright © Linux教程網 All Rights Reserved