Linux磁盤分區及其常用命令


一、磁盤分區命名方式

              在Linux中,每一個硬件設備都映射到一個系統的文件,包括硬盤、光驅等IDE或SCSI設備。Linux把各種IDE設備分配了一個由hd前綴組成的文件。而各種SCSI設備,則被分配了一個由sd前綴組成的文件,編號方法為拉丁字母表順序。例如,第一個IDE設備(如IDE硬盤或IDE光驅),Linux定義為hda;第二個IDE設備定義為hdb;下面依次類推。SCSI設備就是sda、sdb、sdc等。(USB磁盤通常會被識別誒SCSI設備,因此其設備名可能是sda)。

            在Linux中規定,每一個磁盤設備最多能有4個主分區(其中包括擴展分區)。任何一個擴展分區都要占用一個主分區號碼。在一個硬盤中,主分區和擴展分區一共最多是4個。編號順序為阿拉伯數字順序。

            需要注意的是,主分區按1234編號,擴展分區中的邏輯分區,編號直接從5開始,無論是否有2號或3號主分區。對於第一個IDE硬盤的第一主分區,則編號為hda1,而第二個IDE硬盤的第一個邏輯分區編號應為hdb5。

             常見的Linux磁盤命名的規則維hdXY(或sdXY),其中,X為小寫拉丁字母,Y為阿拉伯數字。個別系統可能命名有差異。

二、常用磁盤管理命令。

        1、掛載磁盤分區——mount

                  要使用磁盤分區,就需要掛載該分區。掛載時需要指定需要掛載的設備和掛載目錄(該目錄也成為掛載d點)

                  常用的命令格式如下

                        mount -t type device dir

                            選項 -t 的參數type為文件系統格式(ext4,vfat,ntfs等; )

                                     device為設備名稱(如:"/dev/hda1" "/dev/sdb1")

                                     dir為掛載目錄,成功掛載后,就可以通過訪問該目錄以訪問該分區內的文件(如:"/mnt/windows_c" "/mnt/cdrom")只要是未被使用的空目錄都可用於掛載分區                   

             -V:顯示程序版本;
             -l:顯示已加載的文件系統列表;
             -h:顯示幫助信息並退出;
             -v:冗長模式,輸出指令執行的詳細信息;
             -n:加載沒有寫入文件“/etc/mtab”中的文件系統;
             -r:將文件系統加載為只讀模式;
             -a:加載文件“/etc/fstab”中描述的所有文件系統。

 

2、卸載磁盤分區——umount

          要移除磁盤,首先需要卸載該分區。常用命令格式如下

                 umount [device|dir]

              卸載時只需要一個參數,可以是設備名稱,也可以是掛載點

               

          -a:卸除/etc/mtab中記錄的所有文件系統;
          -h:顯示幫助;
          -n:卸除時不要將信息存入/etc/mtab文件中;
          -r:若無法成功卸除,則嘗試以只讀的方式重新掛入文件系統;
          -t<文件系統類型>:僅卸除選項中所指定的文件系統;
          -v:執行時顯示詳細的信息;
          -V:顯示版本信息。

3、查看磁盤分區信息

          1)、查看磁盤的掛載情況——mount                查看磁盤的掛載情況方法:還接輸入不帶參數的mount命令;

           2)、查看磁盤的分區情況——fdisk                 查看磁盤的分區情況使用命令“fdisk -l”;

          3)、查看磁盤的使用情況——df                        查看磁盤的使用情況,直接使用df;

三,磁盤分區

       1, linux磁盤分區主要分為基本分區(primary partion)和擴充分區(extension partion)兩種,基本分區和擴充分區的數目之和不能大於四個。且基本分區可以馬上被使用但不能再分區。擴充分區必須再進行分區后才能使用,也就是說它必須還要進行二次分區。在 Linux 中,每一個硬件設備都映射到一個系統的文件,對於硬盤、光驅等 IDE 或 SCSI 設備也不例外。Linux把各種 IDE 設備分配了一個由 hd 前綴組成的文件;而對於各種 SCSI 設備,則分配了一個由 sd 前綴組成的文件。

    2,磁盤分區工具fdisk命令

首先選擇要進行操作的磁盤:

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

輸入m列出可以執行的命令:

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)

輸入p列出磁盤目前的分區情況:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           1        8001   8e  Linux LVM
/dev/sdb2               2          26      200812+  83  Linux

輸入d然后選擇分區,刪除現有分區:

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

查看分區情況,確認分區已經刪除:

Command (m for help): print

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

輸入n建立新的磁盤分區,首先建立兩個主磁盤分區:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p    //建立主分區
Partition number (1-4): 1  //分區號
First cylinder (1-391, default 1):  //分區起始位置
Using default value 1
last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100  //分區結束位置,單位為扇區

Command (m for help): n  //再建立一個分區
Command action
   e   extended
   p   primary partition (1-4)
p 
Partition number (1-4): 2  //分區號為2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M  //分區結束位置,單位為M

確認分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux

再建立一個邏輯分區:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e  //選擇擴展分區
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391

確認擴展分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended

在擴展分區上建立兩個邏輯分區:

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l //選擇邏輯分區
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M    

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391

確認邏輯分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended
/dev/sdb5             126         175      401593+  83  Linux
/dev/sdb6             176         391     1734988+  83  Linux

Command (m for help):

從上面的結果我們可以看到,在硬盤sdb我們建立了2個主分區(sdb1,sdb2),1個擴展分區(sdb3),2個邏輯分區(sdb5,sdb6)

注意:主分區和擴展分區的磁盤號位1-4,也就是說最多有4個主分區或者擴展分區,邏輯分區開始的磁盤號為5,因此在這個實驗中試沒有sdb4的。

最后對分區操作進行保存:

Command (m for help): w
The partition table has been altered!

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

建立好分區之后我們還需要對分區進行格式化才能在系統中使用磁盤。

四,磁盤分區的格式化

1,格式化:指將分區格式化成不同的文件系統。 那什么是文件系統呢? 文件系統:指操作系統用於明確存儲設備或分區上的文件的方法和數據結構:即在存儲設備上組織文件的方法。

2,格式化磁盤的主要命令是mkfs,其常用的命令格式為:mkfs -t  type device [block_size]

      選項 t 的參數type為文件系統格式(如ext4,vfat,ntfs等),參數device為設備名稱(如“/dev/hda1”),參數block_size為block大小,為可選項;

      格式化交換分區的命令略有不同,不是mkfs,而是mkswap。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM