【linux基礎】1三、文件系統管理(上)

目錄node

mknod,[h|s]d[a-z][0-9]
linux

Track,sectors,cylinder,MBR(msdos),GPT,編程

fdisk,partedwindows

元數據區(inode table),數據區緩存

VFS,超級塊bash

硬連接與軟連接服務器


1、linux磁盤管理網絡

     linux一切皆文件,linux對設備的操做都是經過文件接口來實現分佈式

一、設備類型ide

塊設備(block):隨機訪問,數據交換單位爲「塊」

字符(character):線性訪問,數據交換單位爲字符

設備文件:FHS(LSB) 文件系統層級標準

/dev/        #設備相關文件的目錄

       設備文件關聯設備驅動程序

設備號:

       major:主設備號  區分設備類別,用於標明所需驅動程序

       minor:次設備號  區分同類設備不一樣個體

[root@Node3 dev]# ls -l sd*
brw-rw---- 1 root disk 8, 0 12月  1 14:18 sda        #主設備和次設備號用逗號隔開
brw-rw---- 1 root disk 8, 1 12月  1 14:18 sda1
brw-rw---- 1 root disk 8, 2 12月  1 14:18 sda2
brw-rw---- 1 root disk 8, 3 12月  1 14:18 sda3

手動建立設備文件:

mknod

mknod [OPTION]... NAME TYPE [MAJOR MINOR]                #本身建立的沒意義

         -m:指定其權限

         TYPE:設備類型c,b,s

[root@Node3 src]# mknod testdev c 100 0
[root@Node3 src]# ls -l testdev
crw-r--r-- 1 root root 100, 0 12月  5 08:42 testdev
[root@Node3 src]# mknod -m 755 testdev01 c 100 1
[root@Node3 src]# ls -l testdev01
crwxr-xr-x 1 root root 100, 1 12月  5 08:42 testdev01

二、設備文件的文件名(ICANN)           #國際互聯網地址分配

磁盤設備文件:

      IDE接口(ATA):並行,133MB/s

              兩個IDE控制器:每一個控制器可經過線纜介入兩塊磁盤,一主一從

              在linux上表示爲/dev/hd[a-z][N],N表示分區                                       

      SCSI接口:並行,640MB/s  工業級別  #smail computer system interface 小型計算機系統接口

      SATA接口:串行,6Gpbs

      USB接口:串行,5Gpbs

      SAS接口:串行,

             在linux系統上的設備文件都表現爲了:/dev/sd[a-z][N]


三、硬盤的內部結構

磁道:Track

扇區:Sectors

柱面:Cylinder

分區:經過柱面來劃分,

分區標識方式:

     主分區或擴展分區:1-4

     邏輯分區:5,6,7,8..

注意:對linux而言,每一個磁盤分區都是獨立的可自我管理,可訪問磁盤設備,每一個磁盤分區都能建立獨立的文件系統

硬盤的內部結構和工做原理,詳見硬盤內部硬件結構和工做原理詳解

MBR:master boot record 主引導扇區  

       它是存在於硬盤的0柱面,0磁頭,1扇區裏,佔512字節的空間(扇區編號從0開始而不是1)。

   它由三個部分組成,主引導程序(bootloader)硬盤分區表DPT(Disk Partition table)和硬盤有效標誌(55AA)。在總共512字節的主引導扇區裏主引導程序(boot loader)佔446個字節,第二部分是Partition table區(分區表),即DPT,佔64個字節,硬盤中分區有多少以及每一分區的大小都記在其中。第三部分是magic number(標識當前MBR信息是否有效的標記),佔2個字節,固定爲55AA(有效)

       主引導扇區MBR是不屬於任何一個操做系統,也不能用操做系統提供的磁盤操做命令來讀取它,但能夠經過命令來修改和重寫,例如 fdisk/mbr;扇區中的主引導程序用來引導操做系統,告訴主機硬件從那裏加載操做系統引導文件;

      扇區中的分區表DPT部分只有64字節,因爲每一個分區信息須要16個字節,因此對於採用MBR型分區結構的硬盤(其磁盤卷標類型爲MS-DOS),最多隻能識別4個主要分區。因此對於一個採用此種分區結構的硬盤來講,想要獲得4個以上的主要分區是不可能的。這裏就須要引出擴展分區了。擴展分區也是主分區(Primary partition)的一種,但它與主分區的不一樣在於理論上能夠劃分爲無數個邏輯分區,每個邏輯分區都有一個和MBR結構相似的擴展引導記錄(EBR)

         在MBR分區表中最多4個主分區或者3個主分區+1個擴展分區,也就是說擴展分區只能有一個而且不能保存數據也不能格式化,只能再細劃分爲邏輯分區纔可使用
        在Linux系統中,硬盤分區命名爲sda1-sda4或者hda1-hda4(其中a表示硬盤編號多是a、b、c等等)。在MBR硬盤中,分區號1-4是主分區(或者擴展分區),邏輯分區號只能從5開始。

   傳統的硬盤採用MBR分區格式,使用LBA尋址,這種尋址機制是32位的,所以最大可以支持2^32232次方)個扇區,而每一個扇區的數據量是512字節,因而2^32 x 512,就得出了最大支持容量,這個數值按硬盤廠商的計算方式(1000進位制)來講,大約就是2.199TB.3TB硬盤明顯已經超越了這個數值,因而多出來的那部分容量就會沒法尋址,因而也就沒法使用。因此在MBR分區表中,一個分區最大的容量爲2T且每一個分區的起始柱面必須在這個disk的前2T內。你有一個3T的硬盤,根據要求你至少要把它劃分爲2個分區,且最後一個分區的起始扇區要位於硬盤的前2T空間內。若是硬盤太大則必須改用GPT。詳見MBR GPT FDISK PARTED 詳解


2、磁盤分區

一、fdisk命令

        fdisk指令是Linux下通用的磁盤分區工具,它能夠操縱硬盤分區表,完成對硬盤分區進行管理的各類操做。

fdisk [options] <disk>   change partition table

fdisk [options] -l <disk>  list partition table(s)  

 1)查看分區

[root@Node3 ~]# 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: 0x00037190

   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        2418    18902016   83  Linux
/dev/sda3            2418        2673     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: 0x00000000

[root@Node3 ~]# fdisk -l /dev/sda1     #查看單獨的分區  

Disk /dev/sda1: 524 MB, 524288000 bytes
255 heads, 63 sectors/track, 63 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: 0x00000000

 2)管理分區 

      fdisk提供了一個交互式接口來管理分區,它有許多子命令,分別用於不一樣的管理功能 ,全部的操做均在內存中完成。  

經常使用命令:

      m:查看子命令的幫助信息

      p:顯示現有分區表

      n:建立新分區

      d:刪除現有分區

      t:修改分區ID

      l:查看支持哪些分區ID

      w:保存退出 

      q:不保存退出  

使用一個新的硬盤分區:

[root@Node3 ~]# 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 0x9b1b2b22.
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): 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)

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

   Device Boot      Start         End      Blocks   Id  System

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): +10G

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (1307-2610, default 1307): 
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-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: 0x9b1b2b22

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l    #此時這裏只能選l了,選p的話會由於沒有空間而報錯
First cylinder (1307-2610, default 1307): 
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +5G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1961-2610, default 1961): 
Using default value 1961
Last cylinder, +cylinders or +size{K,M,G} (1961-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: 0x9b1b2b22

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended
/dev/sdb5            1307        1960     5253223+  83  Linux
/dev/sdb6            1961        2610     5221093+  83  Linux

Command (m for help): w      #保存並推出
The partition table has been altered!            #分區表已更改

Calling ioctl() to re-read partition table.      #調用ioctl()重讀分區表
Syncing disks.                                       #同步磁盤 
[root@Node3 ~]# 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: 0x00037190

   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        2418    18902016   83  Linux
/dev/sda3            2418        2673     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: 0x9b1b2b22

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended
/dev/sdb5            1307        1960     5253223+  83  Linux
/dev/sdb6            1961        2610     5221093+  83  Linux

使用已使用的/dev/sda硬盤的空餘空間建立新分區:

[root@Node3 ~]# fdisk /dev/sda

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
Selected partition 4
First cylinder (2673-5221, default 2673): 
Using default value 2673
Last cylinder, +cylinders or +size{K,M,G} (2673-5221, default 5221): +1G

Command (m for help): p

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

   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        2418    18902016   83  Linux
/dev/sda3            2418        2673     2048000   82  Linux swap / Solaris
/dev/sda4            2673        2804     1060090   83  Linux

Command (m for help): w
The partition table has been altered!   #分區表已更改

Calling ioctl() to re-read partition table.   #調用ioctl()重讀分區表

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.     #重讀分區表失敗

注意:

         這裏須要注意若是磁盤沒有任何分區,沒有使用。內核能自動從新讀取分區表識別分區,並同步到磁盤。若是是已有分區的磁盤則會有警告信息提示,不能識別,不能進行下一步格式化,要重啓後才能夠。(以前一直都是在虛擬機上新加硬盤操做的,沒注意到這個問題)

[root@Node3 ~]# 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: 0x00037190

   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        2418    18902016   83  Linux
/dev/sda3            2418        2673     2048000   82  Linux swap / Solaris
/dev/sda4            2673        2804     1060090   83  Linux
#fdisk -l 命令能看到然並卵,格式化時就找不到

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended
/dev/sdb5            1307        1960     5253223+  83  Linux
/dev/sdb6            1961        2610     5221093+  83  Linux

查看內核是否已經識別新建分區:  cat /proc/partitions

[root@Node3 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   41943040 sda
   8        1     512000 sda1
   8        2   18902016 sda2
   8        3    2048000 sda3
   8       16   20971520 sdb
   8       17   10490413 sdb1
   8       18          1 sdb2
   8       21    5253223 sdb5
   8       22    5221093 sdb6

通知內核強制從新讀取分區表:

CentOS 5: partprobe /dev/Device               

CentOS 6: partx -a /dev/sd[a-z] 或 kpartx -af /dev/sd[a-z]     

注意:成功讀取分區可能須要命令重複執行2次或以上


       若是使用fdisk對大於2T的磁盤分區,雖然能夠分區,但僅識別2T的分區,因此分區大小超過2T的話,就使用Parted工具來實現對GPT磁盤進行分區操做

注意:是分區不能識別超過2T的分區,不是2T的硬盤,因此fdisk將超過2T的硬盤分區多個小於2T的分區是沒有問題的

[root@Node3 ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x2a4c75c9.
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: The size of this disk is 4.3 TB (4294967296000 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


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/sdc: 4295.0 GB, 4294967296000 bytes
255 heads, 63 sectors/track, 522166 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: 0x2a4c75c9

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-522166, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): +3000G            
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): +2500G  
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): +2000G

Command (m for help): p

Disk /dev/sdc: 4295.0 GB, 4294967296000 bytes
255 heads, 63 sectors/track, 522166 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: 0x2a4c75c9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1      261084  2097157198+  83  Linux

Command (m for help): q


二、parted

       parted是一個能夠分區並進行分區調整的工具,他能夠建立,破壞,移動,複製,調整ext2 linux-swap fat fat32 reiserfs類型的分區,能夠建立,調整,移動Macintosh的HFS分區,檢測jfs,ntfs,ufs,xfs分區。

Parted 命令分爲兩種模式:命令行模式和交互模式。

命令行模式:parted [option] device [command] ,

       該模式能夠直接在命令行下對磁盤進行分區操做,比較適合編程應用。

交互模式:parted [option] device 相似於使用fdisk /dev/xxx

       MBR:MBR分區表(即主引導記錄)你們都很熟悉。所支持的最大卷:2T,並且對分區有限制:最多4個主分區或3個主分區加一個擴展分區

       GPT: GPT(即GUID分區表)。是源自EFI標準的一種較新的磁盤分區表結構的標準,是將來磁盤分區的主要形式。與MBR分區方式相比,具備以下優勢。突破MBR 4個主分區限制,每一個磁盤最多支持128個分區。支持大於2T的分區,最大卷可達18EB。

    使用方法:parted [options] [device [command [options...]...]]

 【選項說明】

  -l  顯示全部塊設備上的分區

選    項

功    能

-h

顯示幫助信息

-i

交互式模式

-s

腳本模式,不提示用戶

-v

顯示版本號

【參數說明】

參數

功    能

設備

指定要分區的硬盤所對應的設備文件(沒有指定則使用第一個塊設備)

命令

要執行的parted命令。忽略此參數時,

parted指令進入本身的提示符。
支持的內部命令以下:

check(對指定分區執行簡單的檢查);

cp(將原設備上的原分區的文件複製到當前設備的目標分區);

help(顯示命令幫助);

mkfs(在分區上建立指定的文件系統);

mklabel(建立新的磁盤標籤(分區表));

mkpart(建立分區);

mkpartfs(建立分區及分區上的文件系統);

move(移動分區);

name(設置分區的名稱);

print(顯示分區列表);

quit(退出parted);

resize(調整分區大小);

rm(刪除指定分區);

select(選擇要操做的硬盤);

set(改變分區的狀態標誌)

查看分區:

[root@Node3 ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     標誌
 1      1049kB  525MB   524MB   primary  ext4            啓動
 2      525MB   19.9GB  19.4GB  primary  ext4
 3      19.9GB  22.0GB  2097MB  primary  linux-swap(v1)
 4      22.0GB  23.1GB  1086MB  primary


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  標誌
 1      32.3kB  10.7GB  10.7GB  primary
 2      10.7GB  21.5GB  10.7GB  extended
 5      10.7GB  16.1GB  5379MB  logical
 6      16.1GB  21.5GB  5346MB  logical


錯誤: /dev/sdc: unrecognised disk label   #沒法識別的磁盤標籤                                 

[root@Node3 ~]#

管理分區:

[root@Node3 ~]# parted /dev/sdc
GNU Parted 2.1
使用 /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  check NUMBER                             do a simple check on the file system
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   copy file system to another partition
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkfs NUMBER FS-TYPE                      make a FS-TYPE file system on partition NUMBER
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
  move NUMBER START END                    move partition NUMBER
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices,
        free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resize NUMBER START END                  resize partition NUMBER and its file system
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright
        information of GNU Parted
(parted) print                                                            
錯誤: /dev/sdc: unrecognised disk label                                   
(parted) mklabel gpt     #建立一個分區表,就是硬盤分區(分區表)格式                                                 
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  標誌

(parted) mkpart        #建立一個分區                                    
分區名稱?  []?                                                           
文件系統類型?  [ext2]? ext3                                              
起始點?                                                                  
起始點? 0                                                                
結束點? +3T          #不用加+號了                                                         
錯誤: Invalid number.                                                              
(parted) mkpart
分區名稱?  []? gp1
文件系統類型?  [ext2]? ext3                                              
起始點? 0                                                                
結束點? 3000G                                                            
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放棄/Cancel?                                                  
忽略/Ignore/放棄/Cancel? ignore      #忽略警告                                       
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  標誌
 1      17.4kB  3000GB  3000GB               gp1
                                            
(parted) mkpart                                                           
分區名稱?  []?                                                           
文件系統類型?  [ext2]? ext3                                              
起始點? 3000G                                                            
結束點? 4000G                                                            
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  標誌
 1      17.4kB  3000GB  3000GB               gp1
 2      3000GB  4000GB  1000GB

(parted) mkpart                                                           
分區名稱?  []?                                                           
文件系統類型?  [ext2]?                                                   
起始點? 4000G                                                            
結束點? 4001G                                                            
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  標誌
 1      17.4kB  3000GB  3000GB               gp1
 2      3000GB  4000GB  1000GB
 3      4000GB  4001GB  1000MB

(parted) rm                                                               
分區編號? 3                                                              
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  標誌
 1      17.4kB  3000GB  3000GB               gp1
 2      3000GB  4000GB  1000GB

(parted) q                                                                
信息: You may need to update /etc/fstab.                                  

[root@Node3 ~]#

       因爲parted內建的mkfs還不夠完善,因此完成之後咱們可使用quit命令退出parted並使用 系統的mkfs命令對分區進行格式化了。

查看系統上的磁盤分區:

[root@Node3 ~]# 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: 0x00037190

   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        2418    18902016   83  Linux
/dev/sda3            2418        2673     2048000   82  Linux swap / Solaris
/dev/sda4            2673        2804     1060090   83  Linux

WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.
#能夠看到fdisk不支持GPT分區

Disk /dev/sdc: 4295.0 GB, 4294967296000 bytes
255 heads, 63 sectors/track, 522166 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1      267350  2147483647+  ee  GPT         #顯示不出來

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended
/dev/sdb5            1307        1960     5253223+  83  Linux
/dev/sdb6            1961        2610     5221093+  83  Linux

用parted查看:

[root@Node3 ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     標誌
 1      1049kB  525MB   524MB   primary  ext4            啓動
 2      525MB   19.9GB  19.4GB  primary  ext4
 3      19.9GB  22.0GB  2097MB  primary  linux-swap(v1)
 4      22.0GB  23.1GB  1086MB  primary


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  標誌
 1      32.3kB  10.7GB  10.7GB  primary
 2      10.7GB  21.5GB  10.7GB  extended
 5      10.7GB  16.1GB  5379MB  logical
 6      16.1GB  21.5GB  5346MB  logical


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  標誌
 1      17.4kB  3000GB  3000GB               gp1
 2      3000GB  4000GB  1000GB


[root@Node3 ~]#


3、文件系統

      磁盤分區完成後,接下來的步驟就是格式化,格式化就是在分區上建立文件系統   

一、 文件系統

元數據

      文件跟內容沒有關係的屬性類信息,可使用stat命令查看,屬主屬組、大小,文件名,時間戳等

元數據區(inode table):

      存放元數據,每一個索引項(條目)稱一個Inodeinode記錄了文件的元數據(不包括文件名)和文件保存在哪些磁盤塊上

根在內核中:其須要被關聯至根文件系統,即rootfs    #系統啓動時就關聯了,不然啓動不了系統

目錄會保存它一級子目錄、文件的文件名和指向的indoe信息(編號)

例: /var/logs/messages  訪問一個文件的過程

      根的inode-->根目錄對應的磁盤塊-->var對應的inode編號-->查inode table,找到編號對應的Inode-->var目錄的磁盤塊-->logs對應的Inode編號-->查inode table,找到編號對應的inode-->找到Logs目錄的磁盤塊-->找到messages文件名對應的INODE編號-->查inode table,找到編號對應的inode-->找到messages磁盤塊

dentry:內核會緩存文件的查找路徑


二、VFS

Virtual File System   虛擬文件系統,把不一樣的文件系統虛擬成一個接口。

Linux文件系統:ext2,ext3,ext4,reiserfs,xfs,btrfs,

    光驅:iso9600

    網絡文件系統:nfs,cifs

    集羣文件系統:gfs2,ocfs2

    分佈式文件系統:ceph

    windows文件系統:ntfs,vfat

    僞文件系統:tmpfs,hugepagefs,proc,sysfs

    Unix的文件系統:FFS,UFS,JFS

    交換文件系統:swap

日誌型文件系統:加速文件檢測並修復的過程,ext3,ext4,xfs

非日誌型文件系統:ext2


三、塊組(這個好像是ext系列文件系統纔有的

      block group,爲了方便管理,把塊分紅塊組,每一個塊組的磁盤塊個數都同樣,每一個塊組都有本身的數據區和元數據區。

超級塊:記錄了整個分區的塊組信息,在其它的塊組中有該組超級塊的備份


四、inode位圖,塊位圖:快捷查找inode和塊是否已經被使用


4、連接文件

連接文件:訪問同一個文件數據不一樣路徑

一、硬連接(實際連接)      

       多個文件路徑指向了同一個Inode

建立方法:

       cp  -l  SRC  DEST

        ln  SRC  DEST

特性:

      一、目錄不支持硬連接

      二、硬連接不能跨文件系統(分區)

      三、硬連接文件與原文件是指向同一個Inode,建立硬連接文件會增長Inode的引用計數,


二、軟連接(符號連接)

       連接文件是一個徹底獨立的新文件(新的inode),但其指向了原文件的訪問路徑

建立方法:

      ln -s SRC DEST

特性:

   1、符號連接文件與原文件是兩個獨立文件

        二、目錄能夠建立符號連接

        三、能夠跨文件系統

        四、刪除連接不影響原文件,但刪除原文件,符號指向的文件路徑不復存在,所以,此時連接文件將變成失效的連接,

        五、其大小並不是真正原文件大小,而是指向的原文件的文件路徑字符串所包含的字節數


1、inode是什麼?

理解inode,要從文件儲存提及。

文件儲存在硬盤上,硬盤的最小存儲單位叫作"扇區"(Sector)。每一個扇區儲存512字節(至關於0.5KB)。

 

操做系統讀取硬盤的時候,不會一個個扇區地讀取,這樣效率過低,而是一次性連續讀取多個扇區,即一次性讀取一個"塊"(block)。這種由多個扇區組成的"塊",是文件存取的最小單位。"塊"的大小,最多見的是4KB,即連續八個sector組成一個 block。


文件數據都儲存在"塊"中,那麼很顯然,咱們還必須找到一個地方儲存文件的元信息,好比文件的建立者、文件的建立日期、文件的大小等等。這種儲存文件元信息的區域就叫作inode,中文譯名爲"索引節點"。


2、inode的內容

inode包含文件的元信息,具體來講有如下內容:

  * 文件的字節數

  * 文件擁有者的User ID

  * 文件的Group ID

  * 文件的讀、寫、執行權限

  * 文件的時間戳,共有三個:ctime指inode上一次變更的時間,mtime指文件內容上一次變更的時間,atime指文件上一次打開的時間。

  * 連接數,即有多少文件名指向這個inode

  * 文件數據block的位置

 

能夠用stat命令,查看某個文件的inode信息:

stat example.txt


總之,除了文件名之外的全部文件信息,都存在inode之中。至於爲何沒有文件名,下文會有詳細解釋。

 

3、inode的大小

inode也會消耗硬盤空間,因此分區格式化的時候,分區自動將硬盤分紅兩個區域。一個是數據區,存放文件數據;另外一個是inode區(inode table),存放inode所包含的信息。

每一個inode節點的大小,通常是128字節或256字節。inode節點的總數,在格式化時就給定,通常是每1KB或每2KB就設置一個inode。假定在一塊1GB的硬盤中,每一個inode節點的大小爲128字節,每1KB就設置一個inode,那麼inode table的大小就會達到128MB,佔整塊硬盤的12.8%。

 

查看每一個硬盤分區的inode總數和已經使用的數量,可使用df命令。

df -i

查看每一個inode節點的大小,能夠用以下命令:

sudo dumpe2fs -h /dev/hda | grep "Inode size"

因爲每一個文件都必須有一個inode,所以有可能發生inode已經用光,可是硬盤還未存滿的狀況。這時,就沒法在硬盤上建立新文件。

 

4、inode號碼

每一個inode都有一個號碼,操做系統用inode號碼來識別不一樣的文件。

 

這裏值得重複一遍,Unix/Linux系統內部不使用文件名,而使用inode號碼來識別文件。對於系統來講,文件名只是inode號碼便於識別的別稱或者綽號。表面上,用戶經過文件名,打開文件。實際上,系統內部這個過程分紅三步:首先,系統找到這個文件名對應的inode號碼;其次,經過inode號碼,獲取inode信息;最後,根據inode信息,找到文件數據所在的block,讀出數據。

 

使用ls -i命令,能夠看到文件名對應的inode號碼:

ls -i example.txt

 

5、目錄文件

Unix/Linux系統中,目錄(directory)也是一種文件。打開目錄,實際上就是打開目錄文件。

目錄文件的結構很是簡單,就是一系列目錄項(dirent)的列表。每一個目錄項,由兩部分組成:所包含文件的文件名,以及該文件名對應的inode號碼。


ls命令只列出目錄文件中的全部文件名:

ls /etc

ls -i命令列出整個目錄文件,即文件名和inode號碼:

ls -i /etc

若是要查看文件的詳細信息,就必須根據inode號碼,訪問inode節點,讀取信息。ls -l命令列出文件的詳細信息。

ls -l /etc

 

6、硬連接

    通常狀況下,文件名和inode號碼是"一一對應"關係,每一個inode號碼對應一個文件名。可是,Unix/Linux系統容許,多個文件名指向同一個inode號碼。這意味着,能夠用不一樣的文件名訪問一樣的內容;對文件內容進行修改,會影響到全部文件名;可是,刪除一個文件名,不影響另外一個文件名的訪問。這種狀況就被稱爲"硬連接"(hard link)。

 

ln命令能夠建立硬連接:

ln 源文件 目標文件

   運行上面這條命令之後,源文件與目標文件的inode號碼相同,都指向同一個inode。inode信息中有一項叫作"連接數",記錄指向該inode的文件名總數,這時就會增長1。反過來,刪除一個文件名,就會使得inode節點中的"連接數"減1。當這個值減到0,代表沒有文件名指向這個inode,系統就會回收這個inode號碼,以及其所對應block區域。

 

這裏順便說一下目錄文件的"連接數"。建立目錄時,默認會生成兩個目錄項:"."和".."。前者的inode號碼就是當前目錄的inode號碼,等同於當前目錄的"硬連接";後者的inode號碼就是當前目錄的父目錄的inode號碼,等同於父目錄的"硬連接"。因此,任何一個目錄的"硬連接"總數,老是等於2加上它的子目錄總數(含隱藏目錄),這裏的2是父目錄對其的「硬連接」和當前目錄下的".硬連接「。

 

7、軟連接

  除了硬連接之外,還有一種特殊狀況。文件A和文件B的inode號碼雖然不同,可是文件A的內容是文件B的路徑。讀取文件A時,系統會自動將訪問者導向文件B。所以,不管打開哪個文件,最終讀取的都是文件B。這時,文件A就稱爲文件B的"軟連接"(soft link)或者"符號連接(symbolic link)。

 

這意味着,文件A依賴於文件B而存在,若是刪除了文件B,打開文件A就會報錯:"No such file or directory"。這是軟連接與硬連接最大的不一樣:文件A指向文件B的文件名,而不是文件B的inode號碼,文件B的inode"連接數"不會所以發生變化。

 

ln -s命令能夠建立軟連接。

ln -s 源文文件或目錄 目標文件或目錄

 

8、inode的特殊做用

因爲inode號碼與文件名分離,這種機制致使了一些Unix/Linux系統特有的現象。

  1. 有時,文件名包含特殊字符,沒法正常刪除。這時,直接刪除inode節點,就能起到刪除文件的做用。

  2. 移動文件或重命名文件,只是改變文件名,不影響inode號碼。

  3. 打開一個文件之後,系統就以inode號碼來識別這個文件,再也不考慮文件名。所以,一般來講,系統沒法從inode號碼得知文件名。

      第3點使得軟件更新變得簡單,能夠在不關閉軟件的狀況下進行更新,不須要重啓。由於系統經過inode號碼,識別運行中的文件,不經過文件名。更新的時候,新版文件以一樣的文件名,生成一個新的inode,不會影響到運行中的文件。等到下一次運行這個軟件的時候,文件名就自動指向新版文件,舊版文件的inode則被回收。

 

九 實際問題

   在一臺配置較低的Linux服務器(內存、硬盤比較小)的/data分區內建立文件時,系統提示磁盤空間不足,用df -h命令查看了一下磁盤使用狀況,發現/data分區只使用了66%,還有12G的剩餘空間,按理說不會出現這種問題。 後來用df -i查看了一下/data分區的索引節點(inode),發現已經用滿(IUsed=100%),致使系統沒法建立新目錄和文件。 
查找緣由:

  /data/cache目錄中存在數量很是多的小字節緩存文件,佔用的Block很少,可是佔用了大量的inode。 

解決方案:  一、刪除/data/cache目錄中的部分文件,釋放出/data分區的一部分inode。  二、用軟鏈接將空閒分區/opt中的newcache目錄鏈接到/data/cache,使用/opt分區的inode來緩解/data分區inode不足的問題:  ln -s /opt/newcache /data/cache 
相關文章
相關標籤/搜索