linux 磁盤分區,格式化,掛載

    在操作系統中,磁盤管理屬於設備管理的範疇,一塊硬盤安裝到主機之後稱爲裸設備,若要能夠linux系統中使用必須對其進行如下步驟: 分區 --> 格式化 --> 掛載   

 

1. linux 系統中設備類型  

 一切接文件  所以硬盤也是一個文件 

  塊設備文件 (b)

    通常稱爲  隨機設備  。所謂 塊設備 是指對其信息的存取以 塊 爲單位,如通常的光盤  硬盤 軟盤 磁帶等 , 塊 長取512 1024 或者 4096 字節 。塊設備 可以直接通過塊設備特別文件來訪問,爲了提高數據的傳輸效率 , 塊設備 驅動程序內部採用快緩衝技術  。

    字符設備文件(c) 

       通常稱爲 線性設備 。 所謂 字符設備 是指在I/O傳輸中以字符爲單位進行傳輸的設備,如鍵盤 打印機等 不過需要的是以字符爲單位不一定意味着以字節爲單位,因爲有的編碼規則規定 一個字符佔16bit 

合2個字節。

2. 設備文件存放的/dev目錄 : 

/dev/hd[a-t]IDE 設備
/dev/sd[a-z]SCSI 設備 
/dev/fd[0-7]標準軟驅
/dev/md[0-31]軟raid設備
/dev/loop[0-7]本地迴環設備
/dev/ram[0-15]內存
/dev/null相當於回收站
/dev/zero 無限零資源 冒泡機
/dev/tty[0-63]虛擬終端
/dev/ttys[0-3]串口
/dev/lp[0-3]
並口
/dev/console 控制檯

/dev/cdrom -> /dev/hdc


/dev/modem -> /dev/ttys[0-9]


/dev/pilot -> /dev/ttys[0-9]


        注意 : 控制檯必須在本地 而終端可以在遠端 系統只有一個控制檯 叫console 其他都是終端 。 

3. linux 中硬盤類型和文件名     

    IDE硬盤和文件名

        IDE接口的硬盤命名方式爲:/dev/hdxy  

        hd :表示IDE的接口類型  

        x   :表示硬盤的序號   x 的值可以是 a b c d  

     y   :表示分區號     

        如第三塊IDE硬盤的第三個分區系統文件名爲 : /dev/sdc3 

    SATA或者SCSI硬盤和文件名 

        硬盤命名方式爲: /dev/sdxy 

        以下同上  

        說明:linux對SCSI硬盤最大支持15個分區  

                  所有使用usb接口的移動存儲設備 移動硬盤 優盤 usb光驅 等 一律使用/dev/sdxy的設備文件  

                  光驅設備文件一般都是默認爲 /dev/cdrom  

                  一個硬盤最多可以有四個主分區 ,擴展分區只能有一個 。 

       

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        2576    20480000   83  Linux
/dev/sda3            2576        3596     8192000   83  Linux
/dev/sda4            3596        5222    13065216    5  Extended       #擴展分區 
/dev/sda5            3596        3851     2048000   82  Linux swap / Solaris

  MBR : 

       主引導分區是硬盤的第一物理分區 ,它位於磁盤的0磁頭0柱面1扇區且有倆部分組成  主引導記錄MBR+硬盤分區表DPT

       主引導扇區 = MBR + DPT + MagicNumber   


4. linux 磁盤分區 

命令名稱: fdisk

命令所在路徑 : /sbin/fdisk 

執行權限:root 

功能描述:磁盤分區 可以最大劃分15個分區

語法 : fdisk [options] [DEVICE] 

fdisk -l  

-l : 查看當前磁盤分區的詳細情況  

[root@bu2 ~]# fdisk -l                 #查看磁盤分區的情況 不帶參數顯示的是當前主機上所有的磁盤分區情況                      

Disk /dev/sda: 42.9 GB, 42949672960 bytes               #前主機只有一塊硬盤 ,容量是42.9G
255 heads, 63 sectors/track, 5221 cylinders             #255個磁頭 每個磁道63個扇區 共5221個柱面
Units = cylinders of 16065 * 512 = 8225280 bytes           
Sector size (logical/physical): 512 bytes / 512 bytes        #每個扇區512字節                         
I/O size (minimum/optimal): 512 bytes / 512 bytes            #I/O大小 
Disk identifier: 0x000dc4af

   Device Boot      Start         End      Blocks   Id  System     
/dev/sda1   *           1          26      204800   83  Linux            #第一個分區 ,按柱面劃分 ,從第一個柱面開始到26柱面結束 

Partition 1 does not end on cylinder boundary.           #此處的警告說明該分區沒有包含整個柱面 (一個柱面一定要是屬於一個分區) 

/dev/sda2              26        2576    20480000   83  Linux    #第二個分區 ,分區類型爲linux分區
/dev/sda3            2576        3596     8192000   83  Linux
/dev/sda4            3596        5222    13065216    5  Extended
/dev/sda5            3596        3851     2048000   82  Linux swap / Solaris    #邏輯分區 ,分區類型爲 "交換分區"

fdisk交互式命令  

[root@bu2 ~]# fdisk /dev/sda         #fdisk的參數爲設備名稱而非分區名

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): 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                        #各分區類型所對應的 system ID 
   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                    #修改分區類型的 system ID   
   u   change display/entry units                     
   v   verify the partition table                        
   w   write table to disk and exit                      #保存退出
   x   extra functionality (experts only)
下面用這個新硬盤來創建分區 


[root@bu ~]# fdisk /dev/sdb       #新添加的一塊硬盤,還沒有被分區 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x23f33c7d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): 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: 0x23f33c7d

   Device Boot      Start         End      Blocks   Id  System

創建主分區  

[root@bu ~]# fdisk /dev/sdb 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9bfc918e.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): n        #新建一個分區
Command action    
   e   extended                 #擴展分區              
   p   primary partition (1-4)  #主分區    
 p                               
Partition number (1-4): 1     #分區序號
First cylinder (1-2610, default 1):         
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G     #要劃分的分區大小前面要帶+符號

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: 0x9bfc918e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

創建擴展分區和邏輯分區 並保存退出   

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 1       #按1報錯 因爲1已經有了 
Partition 1 is already defined.  Delete it before re-adding it.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4):        #默認不選不可以
Value out of range.
Partition number (1-4): 4      #最大可以選擇4 ,不可以超過4 
First cylinder (133-2610, default 133): 
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): 
Using default value 2610

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: 0x9bfc918e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133        2610    19904535    5  Extended

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

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

[root@bu ~]# 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): n

Command action

   l   logical (5 or over)            #邏輯分區

   p   primary partition (1-4)            

l

First cylinder (133-2610, default 133): 

Using default value 133

Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1G


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: 0x9bfc918e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         132     1060258+  83  Linux

/dev/sdb4             133        2610    19904535    5  Extended     #擴展分區 (不可以直接拿來使用,如果想使用需創建爲邏輯分區)

/dev/sdb5             133         264     1060258+  83  Linux        #邏輯分區從 5 開始算起

說明: 

a   分區創建好了之後,用fdisk -l命令可以查看分區情況,但需要注意的是:雖然fidsk命令列出了所有的分區情況,不代表新分區就可以被操作系統識別了,文件系統是內核的功能(即軟件能識別新分區但內核並不能識別),可以用"cat /proc/partitions"命令查看,若要內核能識別需要把分區變化情況同步到內核。

b   要特別注意:分區及格式化操作會永久地破壞目前存在於該分區上的任何數據,操作時需要特別小心(操作前對原有數據做備份)!


系統內核重新讀取硬盤分區表


[root@bu2 ~]# fdisk -l 

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x000dc4af

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        2576    20480000   83  Linux
/dev/sda3            2576        3596     8192000   83  Linux
/dev/sda4            3596        5222    13065216    5  Extended
/dev/sda5            3596        3851     2048000   82  Linux swap / Solaris

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: 0x1d7d6d5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133        2610    19904535    5  Extended
/dev/sdb5             133         264     1060258+  83  Linux
[root@bu2 ~]# cat /proc/partitions   #查看內核說別的分區信息
major minor  #blocks  name   #所有分區都能被內核識別到,這樣纔可以經行格式化分區操作,如果不能被內核識別到,則我們可以內核重新讀取DPT

   8        0   41943040 sda
   8        1     204800 sda1
   8        2   20480000 sda2
   8        3    8192000 sda3
   8        4          1 sda4
   8        5    2048000 sda5
   8       16   20971520 sdb
   8       17    1060258 sdb1
   8       18          1 sdb2
   8       21    1060258 sdb5

讓內核重新讀取分區表 

rhel5 : partprobe  [device]  

rhel5 能執行成功,rhel6上可能不行 ,需要重新啓動系統  

[root@bu2 ~]# partprobe /dev/sda    # 提示需要重啓系統所有分區才能被內核識別
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (設備或資源忙).  
As a result, it may not reflect all of your changes until after reboot
[root@bu2 ~]# partx -a /dev/sda       #不帶分區選項表示讀取指定硬盤的所有分區
BLKPG: Device or resource busy        #顯示設備繁忙無法添加分區
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@bu2 ~]# partx -a /dev/sda1 /dev/sda    #手動添加成功 
[root@bu2 ~]#


5. linux 創建文件系統 

只有內核識別到硬盤分區信息 ,才能夠進行格式化操作,格式化的過程就是文件系統創建的過程。 

linux 系統支持的文件系統類型 

[root@bu2 ~]# ls /lib/modules/2.6.32-358.el6.x86_64/kernel/fs/
autofs4     configfs  exportfs  fat      jbd    mbcache.ko  nls       xfs
btrfs       cramfs    ext2      fscache  jbd2   nfs         squashfs
cachefiles  dlm       ext3      fuse     jffs2  nfs_common  ubifs
cifs        ecryptfs  ext4      gfs2     lockd  nfsd        udf


文件系統的簡單介紹 

1)minix 

   是Minix操作系統使用的文件系統,是Linux最先使用的文件系統。它有許多缺點:分區只能小於64MB,只支持短文件名,單一的時間戳,等等。在軟盤和動態磁盤領域仍然有用。

(2)ext 
   是對Minix的擴展。已完全被ext2取代,內核最終將不會支持它。

(3)ext2 
   是Linux使用的,性能很好的文件系統,用於固定文件系統和可活動文件系統。它是作爲ext文件系統的擴展而設計的。ext2在Linux所支持的文件系統中,提供最好的性能(在速度和CPU使用方面),簡短的說,ext2是Linux的主要文件系統。

(4)ext3 
   是對ext2增加日誌功能後的擴展。是RHEL5中默認使用的文件系統類型。它向前、向後兼容ext2,意爲ext2不用丟失數據和格式化就可以轉換爲ext3,ext3也可以轉換爲ext2 而不用丟失數據(只要重新安裝該分區就行了),強烈推薦使用這種文件系統。

(5)ext4

   爲第4代擴展文件系統,是RHEL6中默認使用的文件系統類型,屬於典型的日誌型文件系統。其特點是保持有磁盤存取記錄的日誌數據,便於恢復,性能和穩定性更加出色。

(6)ReiserFS/JFS

   仍然在完善的帶日誌文件系統,特別適合小文件的文件系統

(7)ISO9660文件系統

   該文件系統中光盤所使用的標準文件系統,Linux對該文件系統也有很好的支持,不僅能讀取光盤和光盤ISO映像文件,而且還支持在Linux環境中刻錄光盤。

(8)NFS文件系統 
   NFS即網絡文件系統,用於在UNIX系統間通過網絡進行文件共享,用戶可將網絡中NFS服務器提供的共享目錄掛載到本地的文件目錄中,從而實現操作和訪問NFS文件系統中的內容。

(9)swap文件系統

   swap文件系統用於Linux的交換分區。在Linux中,使用整個交換分區來提供虛擬內存,其分區大小一般應是系統物理內存的2倍,在安裝Linux操作系統時,就應創建交換分區, 它是Linux正常運行所必需的,其類型必須是swap,交換分區由操作系統自行管理。

    


RHEL5   EXT3RHEL6 EXT4
格式化命令

命令名稱:mkfs


命令所在路徑: /sbin/mkfs 


執行權限: root 


功能描述:創建問價系統 


語法: mkfs [-t fstype] /dev/磁盤分區名


命令使用方法: 


mkfs -t ext4=mkfs.ext4 =mke2fs -t ext4 


mkfs -t ext3=mkfs.ext3 =mke2fs -t ext3 =mke2fs -j 


mkfs -t ext2=mkfs.ext2 =mke2fs= mke2fs -t ext2 

命令名稱: mke2fs


命令所在路徑: /sbin/mke2fs 


執行權限: root


功能描述:創建ext2/ext3/ext4 文件系統


語法: mke2fs -t {ext2|ext3|ext4}


常用選項: 


-b {1024|2048|4096} 指定塊大小 

 

-L label 設定卷標


-m 預留給管理使用的塊的個數 


-E 設定文件系統的擴展屬性 


配置文件 : 


/etc/mke2fs.conf 用於設定默認特性,及

文件系統的特有的特性


[root@bu2 ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=4096 (log=2)      #塊大小爲4096
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user      #默認分區的5%的空間預留給管理使用的塊
第一個數據塊=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376
正在寫入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@bu2 ~]# mke2fs -b 2048  /dev/sdb1 
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=2048 (log=1)                    #可以看到塊大小變成了2048B了
分塊大小=2048 (log=1)               
Stride=0 blocks, Stripe width=0 blocks
66528 inodes, 530128 blocks
26506 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=537919488
33 block groups
16384 blocks per group, 16384 fragments per group
2016 inodes per group
Superblock backups stored on blocks: 
        16384, 49152, 81920, 114688, 147456, 409600, 442368
正在寫入inode表: 完成                            
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


6. 查看調整分區系統參數 


tune2fs
e2label
blkid

命令名稱: tune2fs


命令所在路徑: /sbin/tune2fs 


執行權限: root


功能描述: 調整文件系統參數


語法: tune2fs [options] device 


常用選項: 


-l:顯示文件系統超級模塊信息


-L LABEL:重新設定卷標


-m :調整預留給管理塊的所佔總體空      間的比例

-r : 調整預留給管理使用的塊個數


-o : 設定掛載默認選項


-O :設定文件系統默認特性 


-E :調整文件系統的擴展屬性 

命令名稱: e2label


命令所在路徑:/sbin/e2label 


執行權限: root


功能描述: 顯示或者設定卷標 


語法: e2label device [Volume_label]

命令名稱:blkid


命令所在路徑:/sbin/blkid


執行權限: root


功能描述:顯示設備的uuid 文件系統

及卷標


語法:blkid device 



uuid : 用來表示全局唯一ID號 

       用在多分區中識別某個分區


[root@bu2 ~]# tune2fs -l /dev/sda1                         #查看sda1分區超級塊信息
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /boot
Filesystem UUID:          e445d0b8-1f2b-48ba-949b-8960d9f96b3a           #uuid 
Filesystem magic number:  0xEF53                                    #文件系統魔數
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash       
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              51200
Block count:              204800                #總共塊個數
Reserved block count:     10240
Free blocks:              169185
Free inodes:              51162
First block:              1
Block size:               1024                  #塊大小 
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2048
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Tue Apr 22 04:45:21 2014          #文件系統創建時間
Last mount time:          Mon May 26 23:53:32 2014
Last write time:          Mon May 26 23:53:32 2014
Mount count:              9
Maximum mount count:      -1
Last checked:             Tue Apr 22 04:45:21 2014
Check interval:           0 (<none>)
Lifetime writes:          34 MB
Reserved blocks uid:      0 (user root)                     #預留的用戶是root 
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:               128
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      8efe5e2e-ce59-4693-8ff2-13b6382cf019
Journal backup:           inode blocks

7.設定卷標 查看卷標 uuid 和文件系統類型 

[root@bu2 ~]# e2label /dev/sda1 bdstravel         #設定卷標爲bdstravel 

[root@bu2 ~]# blkid /dev/sda1            #卷標 uuid 文件系統類型

/dev/sda1: LABEL="bdstravel" UUID="e445d0b8-1f2b-48ba-949b-8960d9f96b3a" TYPE="ext4"


還需要掛載纔可以正常使用剛纔所創建的分區 .


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