Linux之磁盤管理

1、Raid卡之初識

又叫磁盤冗餘陣列,陣列卡。他的主要做用有三點前端

一、獲取更高的容量java

二、獲取更高的性能node

三、獲取更強的安全性linux

raid卡按支持的raid級別不一樣和raid卡的緩存容量不一樣,分爲基礎raid卡和高級raid卡redis

左邊是raid基礎卡,右邊是高級卡。shell

基礎卡只支持raid0和raid1數據庫

高級卡支持0、一、三、五、十、11或者更多vim

常見的raid級別

raid0

主要是爲了應對不作raid用不了的狀況。centos

硬盤不作raid,識別不了硬盤。因此若是隻有一塊硬盤,就作個raid0吧。緩存

安全性最低,其中一個硬盤壞掉了,全部數據都會報廢。

用於安全性需求低的數據,不怕丟失的數據。優勢就是讀寫快

raid1

只能有兩塊硬盤

算是個雞肋,由於只能同時給兩個硬盤作raid1。

raid5

最少須要3塊硬盤,在寫入以前有一個校驗的過程。有一塊硬盤用來作校驗,用來保護數據的安全性,當其中一塊硬盤壞了,就可使用校驗數據恢復丟失的數據。

能夠在添加一塊熱位盤(RAID5 + spare),當壞了一塊硬盤以後,能夠啓動熱位盤來替換壞的硬盤。

三個有點都佔一點,在併發量不高的時候,可使用

raid10

讀取速度和安全性都很高,可是很奢侈,併發量很大的時候可使用

raid10的基本原理以下:

 

2、磁盤分區

磁盤分區表

所在位置:0磁頭0磁道1扇區的祕密

咱們已經知道一個扇區的大小是512字節,那麼這512字節是如何劃分的呢?

前446字節:mbr主引導記錄

64字節:分區表,分區一建立,表便生成

55AA:表示結束標記

主分區-擴展分區-邏輯分區的關係

系統的主分區最多有4個。

由於一個主分區一旦創立,就要在64字節的分區表中佔用16個字節。

鑑於此,就有了擴展分區。

因此,擴展分區就是爲了解決主分區只有4個的問題。

主分區+擴展分區最多也就是4個。

擴展分區是無法直接使用的。

要想使用,須要在擴展分區下面建立邏輯分區。

主分區

 存放數據分區,最多有4個

擴展分區

 沒法直接使用

 最多隻能有一個

 必需要在擴展分區中建立邏輯分區

邏輯分區

 存放數據

 在功能上和主分區沒有什麼區別

硬盤分區命令規則

硬盤命名

sas/sata/scsi接口,以sd開頭sd?

第一塊sas硬盤,取名sda

第三塊sas硬盤,取名sdc

分區命名

主分區/擴展分區,1-4,如sda1

邏輯分區,從5開始

例子

第一塊SAS硬盤的第一個主分區

/dev/sda1

第二塊SAS硬盤的第二個擴展分區

/dev/sdc6

第四塊SATA硬盤的第一個擴展分區

/dev/sdd5

fdisk進行磁盤分區詳解

首先建立環境

 

一直點下一步

建立2-3個新硬盤

最後點肯定

reboot

重啓了查看一下

[root@luffy-001 ~]# fdisk -l|grep 'sd[a-d]:' 
Disk /dev/sda: 8589 MB, 8589934592 bytes
Disk /dev/sdb: 106 MB, 106954752 bytes
Disk /dev/sdc: 106 MB, 106954752 bytes
Disk /dev/sdd: 106 MB, 106954752 bytes

 

分區工具的使用

小於2個T的使用fdiak,大於2T就使用parted

fdisk支持MBR

題目:建立一個100M分區,把他掛載到/mnt上面

 

[root@luffy-001 ~]# 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 0xfed34d83.
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)
# 有一個模式已通過時了,你能夠關掉。默認磁盤在分區或者是進行讀寫的時候,是按照扇區,柱面,仍是磁道來進行的...
# 而後,告訴了咱們兩個參數c和u
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顯示幫助信息
[: unknown command
Command action
   a   toggle a bootable flag   
   b   edit bsd disklabel
   c   toggle the dos compatibility flag  # 關閉DOS的兼容模式
   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)

 

100M的分區,30M的主分區,30M的邏輯分區

 

## 加上推薦的參數cu
[root@luffy-001 ~]# fdisk -cu /dev/sdb  
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1bbbfcff.
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)
## p顯示分區信息:
Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   Device Boot      Start         End      Blocks   Id  System
## 添加主分區30M:
Command (m for help): n
Command action
   e   extended   # 擴展分區
   p   primary partition (1-4)  # 主分區
p
Partition number (1-4): 1  # 編號
First sector (2048-208895, default 2048):   # 不填就是默認的2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): +30M  
## 顯示分區信息,已經建立好了
Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First sector (63488-208895, default 63488): 
Using default value 63488
Last sector, +sectors or +size{K,M,G} (63488-208895, default 208895): 
Using default value 208895

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux
/dev/sdb2           63488      208895       72704    5  Extended
## 再建立 30M擴展分區
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l  
First sector (65536-208895, default 65536): 
Using default value 65536
Last sector, +sectors or +size{K,M,G} (65536-208895, default 208895): +30M

Command (m for help): P

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux
/dev/sdb2           63488      208895       72704    5  Extended
/dev/sdb5           65536      126975       30720   83  Linux

Command (m for help): 

## 此時,並無在系統中生效,改變只是暫時放在內存中
Command (m for help): w   # 按w保存並退出
The partition table has been altered!

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

 

在w以前,檢查一次,是否是在新的磁盤上建立分區。無誤以後再按回車鍵,不然會發生沒法挽回的錯誤。

在系統中已經能夠查看建立的分區了

[root@luffy-001 ~]# ls -l /dev/sdb*
brw-rw---- 1 root disk 8, 16 Feb 17 17:00 /dev/sdb
brw-rw---- 1 root disk 8, 17 Feb 17 17:00 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Feb 17 17:00 /dev/sdb2
brw-rw---- 1 root disk 8, 21 Feb 17 17:00 /dev/sdb5

磁盤分區--格式化--掛載流程

[root@luffy-001 ~]# fdisk -cu /dev/sdb  
# 第一次的提示信息已經沒有了
## 工做中通常怎麼用? 建立一個100M分區,把他掛載到/mnt上面
### 一、分區

## 先刪除以前的分區
Command (m for help): d
Partition number (1-5): 1

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

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   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 sector (2048-208895, default 2048):    # 把空間所有分給一個分區,直接回車
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895):   # 直接回車
Using default value 208895

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bbbfcff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      208895      103424   83  Linux
Command (m for help): w
The partition table has been altered!

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



### 二、通知系統,/dev/sdb硬盤的磁盤分區表變化了
[root@luffy-001 ~]# partprobe /dev/sdb
[root@luffy-001 ~]# # 默認沒有任何反應


### 三、格式化建立文件系統make filesystem
[root@luffy-001 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25896 inodes, 103424 blocks   # 誕生了inode和block
5171 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1992 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
## 下面這段話,主要意思是講磁盤會按期自動檢查,在下一步能夠關閉它,提示中已經給出了命令
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@luffy-001 ~]# 

### 四、關閉磁盤分區的自動檢查功能
[root@luffy-001 ~]# tune2fs -c 0 -i 0 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds

-c 0 count # 關閉每掛載多少次進行磁盤檢查的功能
-i 0 interval # 關閉每180天進行磁盤檢查功能

### 五、使用mount進行磁盤分區掛載
## 先檢查是否是在使用
[root@luffy-001 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       6.9G  1.9G  4.7G  29% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/sda1       190M   40M  141M  22% /boot

## 若是在使用能夠用umount 切掉
## 使用mount 掛載
[root@luffy-001 ~]# mount /dev/sdb1 /mnt/
[root@luffy-001 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       6.9G  1.9G  4.7G  29% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/sda1       190M   40M  141M  22% /boot
/dev/sdb1        94M  1.6M   88M   2% /mnt

 

 如何進行開機自動掛載

 重啓服務器後,我掛載的磁盤不見了,這可如何是好?

[root@luffy-001 ~]# reboot
[root@luffy-001 ~]# 
Broadcast message from root@luffy-001
    (/dev/pts/0) at 17:42 ...

The system is going down for reboot NOW!
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(study_luffy001) at 09:49:19.

Type `help' to learn how to use Xshell prompt.
[D:\~]$ 

Connecting to 10.0.0.200:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Feb 17 17:43:57 2019
[root@luffy-001 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       6.9G  1.9G  4.7G  29% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/sda1       190M   40M  141M  22% /boot
[root@luffy-001 ~]# 

 

自動掛載的方法

 一、/etc/rc.local

開機自啓動的目錄

將命令/bin/mount  /dev/sdb1  /mnt/  放在   /etc/rc.local 裏面

 二、/etc/fstab

開機自動掛載的目錄

[root@luffy-001 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Jan 10 14:45:14 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# 咱們可使用 man fstab 獲得這些信息
設備或者磁盤的名字 入口/掛載點 磁盤分區的類型 掛載參數 是否備份 開機是否檢查
UUID=30ef1485-38f4-4bac-80c9-1812eddcb1f1 / ext4 defaults 1 1 UUID=94102f2f-e731-4636-81bc-7cb023db0068 /boot ext4 defaults 1 2 UUID=fac5852c-baa7-425c-b513-591de82cdb14 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0

 

按照上面的格式將咱們要掛載的磁盤寫進去

vim   /etc/fstab

加入:

/dev/sdb1             /mnt          ext4        defaults     0  0

卸載磁盤

[root@luffy-001 ~]# umount /mnt

 

 

磁盤分區工具parted和GPT分區表

parted主要是給磁盤大於2TB的進行分區,固然2TB一下的也是可使用的。

還有一點就是,parted是基於GPT分區表

GPT與MBR兼容,支持的容量級別是ZB

Parted

[root@luffy-001 ~]# parted
GNU Parted 2.1
Using /dev/sda
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) 

 

 

建立 30M 30M (由於gpt支持不少主分區,一百多個,因此擴展和邏輯分區就沒用了

[root@luffy-001 ~]# parted /dev/sdc    # 給sdc作分區
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p            # P也可使用                                                    
Error: /dev/sdc: unrecognised disk label                                  
(parted) print                                                            
Error: /dev/sdc: unrecognised disk label                                  
(parted) mklabel                                                          
New disk label type? gpt    #支持兩種格式====>gpt和msdos(mbr)                                       
(parted) p                  #能夠查看分區了                                           
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags
## 建立分區30M,默認單位就是MB 
(parted) mkpart primary 0 30    
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I       # I表示忽略                                            
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  30.0MB  30.0MB               primary

## 建立下一個30M分區
(parted) mkpart primary 30 60 
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  30.0MB  30.0MB               primary
 2      30.4MB  59.8MB  29.4MB               primary
## 使用parted的時候,是實時生效的,因此沒有保存並退出的命令
(parted) q                                                                
Information: You may need to update /etc/fstab.                           
[root@luffy-001 ~]# ls -l /dev/sdc*
brw-rw---- 1 root disk 8, 32 Feb 17 18:23 /dev/sdc
brw-rw---- 1 root disk 8, 33 Feb 17 18:22 /dev/sdc1
brw-rw---- 1 root disk 8, 34 Feb 17 18:22 /dev/sdc2

##parted命令在命令行中也能顯示和使用
[root@luffy-001 ~]# parted /dev/sdc mklabel gpt
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost.
Do you want to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.                           

[root@luffy-001 ~]# parted /dev/sdc mkpart primary 0 30 Ignore
Warning: The resulting partition is not properly aligned for best performance.
Information: You may need to update /etc/fstab.    
                       
[root@luffy-001 ~]# parted /dev/sdc mkpart primary 30 60
Warning: You requested a partition from 30.0MB to 60.0MB.                 
The closest location we can manage is 30.0MB to 30.4MB.
Is this still acceptable to you?
Yes/No? y                                                                 
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I                                                          
Information: You may need to update /etc/fstab.                           
                                     
[root@luffy-001 ~]# parted /dev/sdc p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  30.0MB  30.0MB               primary
 3      30.0MB  30.4MB  409kB                primary
 2      30.4MB  59.8MB  29.4MB               primary

## fsdisk也能夠這麼使用(非交互式應用),只是比較麻煩,能夠了解一下

 

 

 

故障案例:java環境內存不足大量使用swap

內存不足的一種解決方法是增長內存,可是增長內存須要關機,另外一種方法就是使用swap。

通常服務器會有swap空間,以防止內存使用不足。

可是有些服務器沒有設置swap,好比雲服務器。

這時候,須要咱們臨時增長swap。

一、建立一個文件塊

[root@luffy-001 ~]# dd if=/dev/zero of=/tmp/100m bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.317705 s, 330 MB/s

# dd 建立一個文件塊
# if 輸入文件,從哪裏獲取數據
# of 輸出文件,數據放在哪裏
# bs 每次複製多少數據
# count 複製多少次
## 查看文件類型
[root@luffy-001 ~]# file /tmp/100m 
/tmp/100m: data

 

 

二、變成swap

[root@luffy-001 ~]# mkswap /tmp/100m 
mkswap: /tmp/100m: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=36c6f74c-ff57-4746-be67-b9c98b575ee3
[root@luffy-001 ~]# file /tmp/100m 
/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages

 

 

三、查看swap使用狀況

[root@luffy-001 ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       284M       3.4G       252K        12M       155M
-/+ buffers/cache:       116M       3.6G
Swap:         767M         0B       767M
# 若是以前查看過,就知道,此次更該並無生效

 

 

四、swap生效

[root@luffy-001 ~]# swapon /tmp/100m 
[root@luffy-001 ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       284M       3.4G       252K        12M       155M
-/+ buffers/cache:       116M       3.6G
Swap:         867M         0B       867M

 

五、查看swap組成

[root@luffy-001 ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                               partition    786428    0    -1
/tmp/100m                               file        102396    0    -2

 

六、swap永久生效

## 方法1、/etc/rc.local
swapon /tmp/100m
## 方法2、/etc/fstab
/tmp/100m   swap       swap      defaults      0    0

 

 

Linux把個人內存吃了

linux特色:會把使用過的命令和數據臨時存放在內存中 (cache和buffer)

以方便在下次使用的時候能夠更快,提高效率

[root@luffy-001 ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          3.7G       284M       3.4G       252K        12M       155M
-/+ buffers/cache:       116M       3.6G     # 這是減去buffer和cache的容量
Swap:         867M         0B       867M

 

www.linuxatemyram.com  嘗試閱讀並翻譯

 

3、Linux文件系統

分區就是給房子打隔斷,建立文件系統就是給房子裝修,文件系統的類型就是裝修風格,

文件在磁盤上面怎麼放。

文件系統就是一種存儲和組織計算機數據文件袋額機制或者方法。

以前已經說過了,咱們很容易理解,經過fdisk命令對一個磁盤進行分區後,並不能直接掛在目錄存放數據實體,還須要格式化操做,才能夠進行掛載並存放數據。

爲何須要格式化呢?這是由於磁盤分區在沒有格式化以前,操做系統是沒法識別系統上磁盤分區格式的,也就沒法存取文件目錄屬性和權限等內容,把分區格式化成操做系統支持的某個文件系統後,再存放數據,系統就會根據這個文件系統的格式化進行存取文件了。

 

文件系統細節

咱們都知道,操做系統中的數據分爲文件內容和文件屬性兩部分,其中文件內容就是文件的實體數據(block裏),而文件屬性就是文件類型、權限、屬主、修改時間等信息。操做系統會將上述文件的屬性內容放入磁盤文件系統的inode中,而把文件的實體數據存放在對應的block中。除了inode和block信息外,操做系統還會記錄文件系統的總體信息於superblock中,這個superblock包括整個文件系統的inode和block的總的數量,已經使用的數量,剩餘數量等(稱之爲metadata元數據)。

文件系統的每個inode和block都有對應且惟一的數字編號,而superblock則存在於整個文件系統的最前面,它負責管理記錄整個文件系統的相關信息,例如:上述的整個文件系統的inode和block的總數量,已經使用的數量,剩餘數量等。

有關文件系統的inode和block知識,在linux文件屬性一節,軟連接和硬連接屬性章節

查看文件系統的內部細節

[root@luffy-001 ~]# dumpe2fs -h /dev/sda1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /boot
Filesystem UUID:          94102f2f-e731-4636-81bc-7cb023db0068
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:              154246
Free inodes:              51161
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:       Thu Jan 10 14:42:18 2019
Last mount time:          Sun Feb 17 17:43:30 2019
Last write time:          Sun Feb 17 17:43:30 2019
Mount count:              11
Maximum mount count:      -1
Last checked:             Thu Jan 10 14:42:18 2019
Check interval:           0 (<none>)
Lifetime writes:          43 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:              128
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      cbc9b2e9-0a58-4fbe-b35e-7118e5eafd96
Journal backup:           inode blocks
Journal features:         (none)
Journal size:             4096k
Journal length:           4096
Journal sequence:         0x00000034
Journal start:            0

 

一塊硬盤在文件系統角度的名詞解釋

 

常見的文件系統

openSUSE linux 的默認文件系統 ReiserFS 文件系統

IBM 的AIX使用的 jfs 日誌文件系統

Centos7 採用 XFS 文件系統

Centos6 採用 ext4 文件系統

Centos5 採用 ext3 文件系統

文件系統的小結

一、文件系統是對一個存儲設備上的數據和元數據進行組織的一種機制

二、分區必須格式化建立文件系統才能存放數據

三、一個分區只能有一種文件系統

四、linux下常見的文件系統ext二、ext三、ext四、zfs、xfs(centos7)和Reiserfs(單獨安裝)。

windos下常見的額文件系統:NTFS、FAT3二、exFAT

 

文件系統選型簡單介紹

一、SAS/SATA硬盤文件系統選擇

  • reiserfs 大量小文件業務首選reiserfs(100k之內),若是是Centos須要單獨安裝
  • xfs 有的門戶的數據庫MySQL 業務會選擇xfs,最數據庫的支持比較好
  • ext4 視頻下載,流媒體,數據庫,小文件業務也OK,默認的,通用的
  • ext2 沒有日誌(丟失了數據沒法恢復,可是數據的讀寫速度回提升),藍汛的cache業務,CDN網站加速服務(至關於緩存)

常規應用:

對於 Centos5.x 系列,常規應用就選默認的 ext3 文件系統便可

對於 Centos6.x 系列,常規應用就選默認的 ext4 文件系統便可

對於 Centos7.x 系列,常規應用就選默認的 xfs 文件系統便可

注意:若是服務器數量少,怎麼選擇均可以

若是是海量服務器,考慮維護成本和性能,選擇其中一個折中。

選擇小結:

  1. Centos 默認的文件系統都是比較優秀的。
  2. xfs、reiserfs 須要單獨安裝,默認不支持的。
  3. 海量服務器使用多種文件系統維護可能不方便
  4. 大併發作磁盤系統的優化是一方面,但更多的仍是在網站前端增長緩存服務器(squid、varnish),存儲數據庫的前端增長緩存(memcache、redis),這是架構師的能力。

二、SSD固態文件系統的選擇

Ext四、Reiserfs能夠做爲SSD文件系統,但未對SSD作優化,不能充分發揮SSD的性能,影響SSD的使用時間。

Btrfs對SSD作了優化,mount經過參數啓用。但Btrfs仍處於試驗階段,生產環境謹慎使用。

JEFS2/Nilfs2/YAFFS是經常使用的 flash file system,在嵌入式環境(手機、智能電器)普遍應用,建議使用。性能目前還未作測試評估。

在Linux中製做文件系統

看系統默認是否支持,若是支持就直接用對用命令格式化便可

磁盤部分總結與磁盤命令總結

磁盤知識體系

磁盤結構

raid

磁盤分區

磁盤格式化

文件系統

命令

 1     fdisk:磁盤分區工具                       ★★★★☆
 2     parted:磁盤分區工具                      ★★★★☆
 3     partprobe:通知系統磁盤的分區信息變化 更新內核的硬盤分區表信息            ★★★☆☆
 4     tune2fs:調整ext2/ext3/ext4文件系統參數        ★★☆☆☆
 5         -c 0 count    關閉每掛載多少次進行磁盤檢查
 6         -i 0 interval 關閉每一個多久進行磁盤檢查
 7         關閉磁盤分區的自動檢查
 8     mkfs:make filesystem 建立Linux文件系統                   ★★★☆☆
 9         -t 指定文件系統類型
10         mkfs.ext4 == mkfs -t ext4
11     dumpe2fs:顯示文件系統信息       ★★☆☆☆
12     resize2fs:調整ext2/ext3/ext4文件系統大小                       ★★☆☆☆   會影響業務
13     fsck:檢查並修復Linux文件系統                       ★★★☆☆  硬盤沒問題不要用
14         -a
15     dd:轉換或複製文件                                   ★★★☆☆
16     od  查看二進制文件的內容
17         -xa
18         dd if=/dev/sda  of=/tmp/512.bin  bs=512 count=1
19 
20         od -xa /tmp/512.bin
21 
22     mount:掛載文件系統                       ★★★★★
23     umount:卸載文件系統                       ★★★☆☆
24         umount /mnt
25     df:報告文件系統磁盤空間的使用狀況                       ★★★★★
26     mkswap:建立交換分區                       ★★☆☆☆
27     swapon:激活交換分區                       ★★☆☆☆
28     swapoff:關閉交換分區                       ★★★☆☆
29     sync:刷新文件系統緩衝區                      ★★☆☆☆
30     top :查看系統性能信息
31     iotop 查看系統的磁盤讀寫速度 顯示出進程使用swap的狀況
32         io input/output 輸入/輸出 讀寫
33     htop  top升級版
34     iftop 查詢網卡流量狀況
35         -i 指定監視網卡
相關文章
相關標籤/搜索