1、磁盤和文件系統node
計算機的外部存儲器中也採用了相似磁帶的裝置,比較經常使用的一種叫磁盤,將圓形的磁性盤片裝在一個方的密封盒子裏,這樣作的目的是爲了防止磁盤表面劃傷,致使數據丟失。linux
將圓形的磁性盤片裝在一個方形的密封盒子裏。有了磁盤以後,人們使用計算機就方便多了,不但能夠把數據處理結果存放在磁盤中,還能夠把不少輸入到計算機中的數據存儲到磁盤中,這樣這些數據能夠反覆使用,避免了重複勞動。但是不久以後,人們又發現了另外一個問題:人們要存儲到磁盤上的內容愈來愈多,衆多的信息存儲在一塊兒,很不方便。這樣就致使了文件系統的產生。bash
文件系統是操做系統用於明確存儲設備(常見的是磁盤,也有基於NAND Flash的固態硬盤)或分區上的文件的方法和數據結構;即在存儲設備上組織文件的方法。操做系統中負責管理和存儲文件信息的軟件機構稱爲文件管理系統,簡稱文件系統。文件系統由三部分組成:文件系統的接口,對對象操縱和管理的軟件集合,對象及屬性。從系統角度來看,文件系統是對文件存儲設備的空間進行組織和分配,負責文件存儲並對存入的文件進行保護和檢索的系統。具體地說,它負責爲用戶創建文件,存入、讀出、修改、轉儲文件,控制文件的存取,當用戶再也不使用時撤銷文件等數據結構
2、磁盤管理異步
機械式硬盤:secotr: 扇區(512bytes),track:磁道,cylinder: 磁柱(磁盤以磁柱爲單位進行分區)ide
零磁道處於硬盤上一個很是重要的位置,硬盤的主引導記錄區(MBR)就在這個位置上。MBR位於硬盤的0磁頭0柱面1扇區,其中存放着硬盤主引導程序和硬盤分區表。在總共512字節的硬盤主引導記錄扇區中,446字節屬於硬盤主引導程序,64字節屬於硬盤分區表(DPT),兩個字節(55 AA)屬於分區結束標誌。工具
MBR: Master Boot Record(446bytes: boot loader,64bytes:分區表、16bytes: 標識一個分區,2bytes: 55AA分區結束標誌)flex
因爲磁盤分區表的限制,一塊磁盤僅能有4個主分區;3主分區+1擴展(N個邏輯分區,邏輯分區個數由操做系統限制)ui
對於一個系統管理者( root )而言,磁盤的的管理是至關重要的一環,尤爲近來硬盤已經漸漸的被當成是消耗品了 ..... 若是咱們想要在系統裏面新增一顆硬盤時,應該有哪些動做須要作的呢:this
①、對磁盤進行分割,以建立可用的 partition ;
②、對該 partition 進行格式化( format ),以建立系統可用的 filesystem;
③、若想要仔細一點,則可對剛剛建立好的 filesystem 進行檢驗;
④、在 Linux 系統上,須要建立掛載點 ( 亦便是目錄 ),並將他掛載上來;
fdisk -l [-u] [device...]
[root@wlw dev]# fdisk -l /dev/sda Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0x00091c83 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 1437 10485760 83 Linux /dev/sda3 1437 1567 1048576 82 Linux swap / Solaris #磁盤設備的設備文件命名:IDE: /dev/hd。SCSI, SATA, SAS, USB: /dev/sd #不一樣磁盤設備設備:a-z,例如:/dev/sda, /dev/sdb, ... #同一磁盤設備上的不一樣分區:1,2, ...,例如:/dev/sda1, /dev/sda5 #fdisk -l能夠顯示磁盤的各分區詳細信息,這裏咱們能夠看到一共有6527 cylinders,而咱們最後一個分區僅使用了 #1567 cylinders,因此表明此磁盤還有4960 cylinders可用
獲取磁盤分區工具fdisk幫助信息,這裏不要使用/dev/sda1,這個爲分區名而不是磁盤設備名,磁盤設備名爲/dev/sda
[root@wlw dev]# 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): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition <==刪除一個partition l list known partition types <==列出全部partition id m print this menu <==列出幫助信息 n add a new partition <==新增一個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 <==調整partition id u change display/entry units v verify the partition table w write table to disk and exit <==將剛剛的動做寫入分割表 x extra functionality (experts only) #進入磁盤分區界面能夠發送m指令獲取相關指令幫助,上面列出參見的一些指令信息
添加磁盤分區
[root@wlw dev]# 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 (1567-6527, default 1567): Using default value 1567 Last cylinder, +cylinders or +size{K,M,G} (1567-6527, default 6527): +10G Command (m for help): p Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0x00091c83 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 1437 10485760 83 Linux /dev/sda3 1437 1567 1048576 82 Linux swap / Solaris /dev/sda4 1567 2872 10485404 83 Linux #能夠看到咱們新添加的大小爲10G的主分區/dev/sda4,每次添加完分區操做須要發送w指令保存才能生效,若是是直接 #Ctrl+c或q退出則表明不保存當前分區操做
刪除磁盤分區
[root@wlw dev]# 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): d Partition number (1-4): 4 Command (m for help): p Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0x00091c83 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 1437 10485760 83 Linux /dev/sda3 1437 1567 1048576 82 Linux swap / Solaris #能夠看到剛剛新添加的磁盤分區已經沒有了
當咱們添加完磁盤分區保存退出後,能夠查看內核是否已經識別新的分區,由於只有內核識別了咱們添加的分區才能格式化使用
[root@wlw dev]# cat /proc/partitions major minor #blocks name 8 0 52428800 sda 8 1 1048576 sda1 8 2 10485760 sda2 8 3 1048576 sda3 #這裏只看到了sda1,sda2,sda3分區,並無咱們剛剛添加的新的磁盤分區,這是由於開機的時候內核已經識別好分區了 #因此咱們新添加的分區須要通知內核從新識別,能夠重啓讓內核從新識別,或經過命令通知內核從新識別
經過命令讓內核從新識別分區表
partx -a /dev/DEVICE (Centos 6建議使用此命令)
partprobe [/dev/DEVICE] (Centos 5使用此命令)
[root@wlw dev]# 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 [root@wlw dev]# 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 [root@wlw dev]# cat /proc/partitions major minor #blocks name 8 0 52428800 sda 8 1 1048576 sda1 8 2 10485760 sda2 8 3 1048576 sda3 8 4 20975849 sda4 #第一次執行partx -a /dev/sda並無識別到sda4,此時咱們能夠重複執行多一次,能夠看到內核已經識別到sda4
三、文件系統管理
磁盤分區後須要對分區進行文件系統格式化後才能掛載使用。
Linux文件系統: ext2, ext3, ext4, xfs, btrfs, reiserfs, jfs, swap(交換分區)
根據其是否支持"journal"功能:
日誌型文件系統: ext3, ext4, xfs, ...
非日誌型文件系統: ext2, vfat
文件系統的建立
mkfs.FS_TYPE(ext4,xfs,btrfs) /dev/DEVICE
mkfs -t FS_TYPE /dev/DEVICE
這兩個命令爲通用的文件系統建立工具,調用的接口是同樣的
[root@wlw dev]# mkfs [tab][tab] mkfs mkfs.ext2 mkfs.ext4 mkfs.msdos mkfs.cramfs mkfs.ext3 mkfs.ext4dev mkfs.vfat #這裏能夠看到當前系統可以支持建立的文件系統,若是須要其餘文件系統須要加載對應的模塊。
對剛剛分區的/dev/sda4進行文件ext4的文件系統建立
[root@wlw ~]# mkfs.ext4 /dev/sda4 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1311184 inodes, 5243962 blocks 262198 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 161 block groups 32768 blocks per group, 32768 fragments per group 8144 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
因爲如今大多數Linux上使用的仍是ext系列的文件系統,因此下面詳細介紹一下mke2fs,ext系列文件系統專用管理工具。
mkfs.ext3 = mkfs -t ext3 = mke2fs -t ext3
mke2fs OPTION /dev/DEVICE
-t {ext2|ext3|ext4}:指定文件系統
-b {1024|2048|4096}:指定塊大小
-L 'LABEL':指定卷標名
-j: 至關於 -t ext3
-i #: 爲數據空間中每多少個字節建立一個inode;此大小不該該小於block的大小;
-N #:爲數據空間建立多少個inode;
-m #: 爲管理人員預留的空間佔據的百分比;
-O FEATURE[,...]:啓用指定特性
-O ^FEATURE:關閉指定特性
[root@wlw ~]# mke2fs -t ext4 -L "sda4" -b 2048 -i 8192 /dev/sda4 mke2fs 1.41.12 (17-May-2010) Filesystem label=sda4 OS type: Linux Block size=2048 (log=1) Fragment size=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 2625536 inodes, 10487924 blocks 524396 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=548405248 641 block groups 16384 blocks per group, 16384 fragments per group 4096 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000, 3981312, 5619712, 10240000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
其實 mke2fs 所使用的各項選項/參數也能夠用在『 mkfs -t ext3 ... 』後面,由於最終使用的公用程序是相同的!若是沒有特殊需求的話,使用『 mkfs -t ext3....』不但容易記憶,並且就很是好用。
mkswap:建立交換分區
mkswap [options] device:指明要掛載的設備;`
前提:調整其分區的ID爲82 Linux swap / So;
[root@wlw ~]# 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): t Partition number (1-4): 4 Hex code (type L to list codes): 82 Changed system type of partition 4 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0x00091c83 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 1437 10485760 83 Linux /dev/sda3 1437 1567 1048576 82 Linux swap / Solaris /dev/sda4 1567 2872 10485404 82 Linux swap / Solaris #把/dev/sda4的分區格式調整爲Linux swap / So [root@wlw ~]# mkswap -L 'sd4swap' /dev/sda4 Setting up swapspace version 1, size = 20975844 KiB LABEL=sd4swap, UUID=67f662c9-7ac7-4ac2-8b07-e0e989caee39
4、文件系統常見相關工具
①、blkid:塊設備屬性信息查看
blkid [OPTION]... [DEVICE]
-U UUID: 根據指定的UUID來查找對應的設備
-L LABEL:根據指定的LABEL來查找對應的設備
[root@wlw ~]# blkid /dev/sda1: UUID="4c49beb0-6125-44ba-a1a4-9cba332717ee" TYPE="ext4" /dev/sda2: UUID="a503d00c-a6f8-4045-bb53-1e22aef42561" TYPE="ext4" /dev/sda3: UUID="fc7c9f09-7741-41bf-832b-ab55bba6031a" TYPE="swap" /dev/sda4: LABEL="sd4swap" UUID="67f662c9-7ac7-4ac2-8b07-e0e989caee39" TYPE="swap" #查看全部文件格式化分區的設備屬性信息 [root@wlw ~]# blkid /dev/sda4 /dev/sda4: LABEL="sd4swap" UUID="67f662c9-7ac7-4ac2-8b07-e0e989caee39" TYPE="swap" #查看指定文件格式化分區的設備屬性信息 [root@wlw ~]# blkid -U "4c49beb0-6125-44ba-a1a4-9cba332717ee" /dev/sda1 [root@wlw ~]# blkid -L sd4swap /dev/sda4
②、e2label:查看ext系列文件系統的LABEL
e2label DEVICE [LABEL]
[root@wlw ~]# e2label /dev/sda4 sda4
③、tune2fs:從新設定ext系列文件系統可調整參數的值
-l:查看指定文件系統超級塊信息;super block
-L 'LABEL':修改卷標
-m #:修預留給管理員的空間百分比
-j: 將ext2升級爲ext3
-O: 文件系統屬性啓用或禁用
-o: 調整文件系統的默認掛載選項
-U UUID: 修改UUID號;
[root@wlw ~]# tune2fs -l /dev/sda1 tune2fs 1.41.12 (17-May-2010) Filesystem volume name: <none> Last mounted on: /boot Filesystem UUID: 4c49beb0-6125-44ba-a1a4-9cba332717ee 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 large_file 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: 65536 Block count: 262144 Reserved block count: 13107 Free blocks: 240829 Free inodes: 65499 First block: 0 Block size: 4096 Fragment size: 4096 Reserved GDT blocks: 63 Blocks per group: 32768 Fragments per group: 32768 Inodes per group: 8192 Inode blocks per group: 512 Flex block group size: 16 Filesystem created: Wed Sep 23 18:58:45 2015 Last mount time: Sun Sep 27 20:59:48 2015 Last write time: Sun Sep 27 20:59:48 2015 Mount count: 3 Maximum mount count: -1 Last checked: Wed Sep 23 18:58:45 2015 Check interval: 0 (<none>) Lifetime writes: 83 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: b7e87d5f-01a5-4e06-9d14-723d1c050b35 Journal backup: inode blocks
④、dumpe2fs -h [DEVICE]:查看超級塊信息
五、文件系統檢測工具
因爲系統在運行時誰也說不許啥時硬件或者是電源會有問題,因此『死機』多是不免的狀況(不論是硬件仍是軟件)。 如今咱們知道文件系統運行時會有硬盤與內存數據異步的情況發生,所以莫名其妙的死機很是可能致使文件系統的錯亂。此時就須要文件系統檢測工具進行修復。
fsck: File System CheCk
fsck.FS_TYPE [DEVICE]
fsck -t FS_TYPE [DEVICE]
-a: 自動修復錯誤
-r: 交互式修復錯誤
-c:顯示修復的詳細進度
-f:強制修復
Note: FS_TYPE必定要與分區上已經文件類型相同;
[root@wlw ~]# fsck -C -f -t ext4 /dev/sda4 fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sda4: 11/1311184 files (0.0% non-contiguous), 126320/5243962 bloc
e2fsck:ext系列文件專用的檢測修復工具
-y:自動回答爲yes;
-f:強制修復;
[root@wlw ~]# e2fsck -y -f /dev/sda4 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sda4: 11/1311184 files (0.0% non-contiguous), 126320/5243962 blocks