2015年9月01日【磁盤和文件系統管理】-JY1506402-19+liuhui880818

《Linux系統管理》html

(1)磁盤和文件系統管理;node

(2)RAID, LVM; btrfs; linux

(3)程序包管理:rpm, yum(dnf)vim

(4)Linux的網絡管理; 緩存

(5)進程和做業管理:htop, glances, tsar;bash

(6)sed和awk;服務器

(7)Linux系統的開機啓動流程;網絡

(8)內核基礎知識,內核定製編譯;app

(9)系統安裝:kickstart, pxe異步


1、做業(練習)內容:

一、複習本次課程所講的內容;

二、總結整理磁盤管理及文件系統管理中設計的各類命令的使用,並附註必定的示例;

三、建立一個10G的文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄


2、完成時間:

2015年9月15日以前。


3、完成步驟:

《Linux磁盤管理和文件系統管理》


目錄:


(一)Linux磁盤管理

1.硬盤介紹

2.常見接口類型

3.如何磁盤分區

4.經常使用分區工具使用

(二)Linux文件系統管理

1.Linux文件系統介紹

2.Linux文件系統管理

 1)建立文件系統

 2)文件系統屬性查看及調整工具

 3)文件系統檢測

 4)對文件系統進行掛載和使用

 5)交換分區的使用

 6)設置開機掛載

3.文件系統的鏈接文件

練習:

建立一個10G文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄;

系統版本:CentOS 6.x/7 x86_64


(一)Linux磁盤管理

1.硬盤介紹

1)硬盤:就是計算機和服務器上用來存儲數據的。

2)硬盤組成:由許多的盤片、機械手臂、磁頭和主軸馬達所組成的。而數據的寫入基實是在盤片上面。盤片上面又可分出扇區和柱面兩種單位,其中扇區每一個爲512bytes。

3)種類:

    硬備用有固態硬盤(SSD)、機械硬盤(HDD)、混合硬盤(HDD一塊基於傳統機械硬盤誕生出來的新硬盤);SSD採用閃存顆粒來存儲,HDD採用磁性碟片來存儲,混合硬盤是把磁性硬盤和閃存集成到一塊兒的一種硬盤。

4)基本參數

  (1)容量

        硬盤容量以兆字節(MB/MiB)、千兆字節(GB/Gib)或百萬字節(TB/Tib)爲單位,

        常見換算式爲:1TB=1024,1GB=1024MB而1MB=1024KB。

  (2)轉速

        是硬盤內內電機主軸的旋轉速度,也就是硬盤盤片在一分鐘內所能完成的最大轉數。

  (3)平均訪問時間

        是指磁頭從起始位置到達目標磁道位置,而且從目標磁盤上找到要讀寫的數據扇區所需的時間。

  (4)傳輸速率

        指硬盤讀寫數據的速度,單位爲兆字節每秒(MB/s)。硬盤數據傳輸速率又包括了內部數據傳輸率和外部數據傳輸率。

        內部專輸率,也稱持續轉輸率,它反映了硬盤緩衝區未用時的性能。主要依賴於硬盤的旋轉速度。

        外部傳輸率:也稱爲突發傳輸率或接口傳輸率,它標稱的是系統總線和礙盤緩衝區之間的轉輸率,與硬盤接口類型和硬盤緩存的大小有關。

   (5)緩存

        緩存是硬盤控制器上的一塊內存芯片,具備極快的存取速度,它是硬盤內部接口和外部接口之間的緩衝器。


2.常見接口類型

ATA      是用傳統的40-pin並口數據線鏈接主板與硬盤的,外部接口速度最大爲133MB/s,將被SATA所取代。

IDE      即電子集成驅動器

SATA     採用串行鏈接方式。

SATA II  是SATA的升級版,從SATA的150MB/s提升到300MB/s.

SATA III 主要是傳輸速度翻番達到6Gbps.

SCSI     主要應用於中、高端服務器和高檔工做站中。

SAS      即串行鏈接SCSI,是新一代的SCSI技術。

(#此內容摘自百度百科:硬盤

3.磁盤分區

1)磁盤分區介紹

(1)磁盤的第一個分區主要記錄了兩個重要的信息:

  主引導分區(MBR):能夠安裝引導加載程序的地方,有446bytes。

  分區表(partion table):記錄整塊硬盤分區的狀態,有64bytes。

  分區表:

    其實所謂的「分區」只是針64bytes的分區表進行設置而已。

    硬盤默認的分區表僅能寫入四組分區信息。

    這四種分區信息咱們稱爲主(primary)或擴展(extended)分區。

    分區的最小單位爲柱面(cylinder).

  主分區、擴展分區與邏輯分區的特性:

    (1)主分區和擴展分區最多能夠有四個(硬盤的限制)

    (2)擴展分區最多隻能有一個(操做系統的限制)

    (3)邏輯分區是由擴展分區持續切割分來的分區

    (4)可以被格式化後做爲數據訪問的分區爲主分區和邏輯分區。擴展分區沒法格式化。

    (5)邏輯分區的數量依操做系統而不一樣,在Linux系統中,IDE硬盤最多有59個邏輯分區(5-63),SATA硬盤則有有11個邏輯分區(5號到15號)。

(2)識別硬盤設備:/dev/sda

    標記不一樣的硬備:/dev/sd[a-z]

    標記同一設備上的不一樣分區:/dev/sd[a-z][1-n]

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

       5+:邏輯分標區標識

最簡單的分區方法:僅分區根目錄和內存交換空間便可。


4.經常使用分區工具使用

fdisk,parted,sfdisk

1)fdisk工具的使用

fdisk -l       #查看磁盤設備

fdisk /dev/sda #對sda磁盤設備進行操做

  分區管理子命令:

p: 顯示

n: 建立

d: 刪除

t: 修改分區ID

l: 列出全部支持ID類型

w: 保存退出

q: 放棄修改並退出

m: 獲取幫助

示例:添加一個硬盤,並添加一個主分區,擴展分區,邏輯分區。

(1)使用fdisk -l 查看添加的分區
[root@localhost ~]# fdisk -l
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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 label type: dos
Disk identifier: 0x0000ef2f
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    21997567    10485760   83  Linux
/dev/sda3        21997568    42969087    10485760   83  Linux
/dev/sda4        42969088   251658239   104344576    5  Extended
/dev/sda5        42971136    47067135     2048000   82  Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
(2)對新添加的硬盤/dev/sdb進行分區
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa7225c08.
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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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): n  ##新建一個分區
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p    ##新建主分區
Partition number (1-4, default 1): 1 ##選擇第1個分區
First sector (2048-41943039, default 2048):     ##分區的起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G  ##給定分區大小爲5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): n    ##新建分區
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e    ##爲擴展分區,默認全部容量給擴展,一路回車。
Partition number (2-4, default 2): 
First sector (10487808-41943039, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Extended and of size 15 GiB is set
Command (m for help): n    ##新建分區
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l        ##建立邏輯分區
Adding logical partition 5
First sector (10489856-41943039, default 10489856): 
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-41943039, default 41943039): +5G  ##大小爲5G。
Partition 5 of type Linux and of size 5 GiB is set
Command (m for help): p        ##查看分區後的信息。
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xa7225c08
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    41943039    15727616    5  Extended
/dev/sdb5        10489856    20975615     5242880   83  Linux
Command (m for help): w    ##保存分區信息。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2)建立完成以後,查看內核是否已經識別新的分區:
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name
   8        0  125829120 sda
   8        1     512000 sda1
   8        2   10485760 sda2
   8        3   10485760 sda3
   8        4          1 sda4
   8        5    2048000 sda5
   8        6   20971520 sda6
   8        7   10485760 sda7
   8        8   20971520 sda8
   8       16   20971520 sdb
   8       17    5242880 sdb1
   8       18          1 sdb2
   8       21    5242880 sdb5
  11        0    4209664 sr0
有三個命令可讓內核重讀磁盤分區表:
CentOS 5.x:partprobe [DEVICE]
CentOS 6.x,7.x:
    partx 
    kpartx
partx 命令:告訴內核去識別、登記某個硬盤上的分區信息。
-a 登記某塊盤上的全部分區信息,若是某個分區信息有記錄,就會報錯。
-d 刪除內核中關於某磁盤的全部分區的記錄(不是卸載)
-d --nr m-n 刪除從第m-n分區的記錄
-l 列出某磁盤上的分區狀況。
[root@localhost ~]# partx -a --nr 5 /dev/sdb
[root@localhost ~]# cat /proc/partitions
[root@localhost ~]# partx -a --nr 5 /dev/sdb
partx: /dev/sdb: error adding partition 5[root@localhost ~]# partx -l /dev/sdb
# 1:      2048- 10487807 ( 10485760 sectors,   5368 MB)
# 2:  10487808- 41943039 ( 31455232 sectors,  16105 MB)
# 5:  10489856- 20975615 ( 10485760 sectors,   5368 MB)

(二)Linux文件系統管理

1.Linux文件系統介紹

   Linux文件系統中的文件是數據的集合,文件系統不只包含着文件中的數據並且還有文件系統的結構,全部Linux 用戶和程序看到的文件、目錄、軟鏈接及文件保護信息等都存儲在其中。

VFS:Virtual File System 虛擬文件系統:整個Linux的系統都是經過虛擬文件系統的內核功能去讀取文件系統的。

    Linux:ext2, ext3, ext4, reiserfs, xfs, btrfs, swap

    光盤:iso9660

    Windows: fat32(vfat), ntfs

    Unix: ffs, ufs, jfs, jfs2

    網絡文件系統:nfs, cifs

    集羣文件系統:ocfs2, gfs2

    分佈式文件系統:ceph, 

    moosefs, mogilefs, hdfs, gfs, glusterfs

    非日誌型文件系統(傳統文件系統):ext2

    日誌型文件系統:ext3


2.Linux文件系統管理

 1)建立文件系統

(1)說明:格式化,即爲建立文件系統。

    要使用某種文件系統,要知足兩個條件:    

      在內核中:要支持此種文件系統

      用戶空間:要有文件系統管理工具

(2)建立文件系統工具:

a)mkfs 命令(make filesystem)

格式:mkfs -t type DEVICE

   或:mkfs.type DEVICE

b)ext系統文件系統的專用管理工具:

mke2fs 命令

格式:mke2fs -t {ext2|ext3|ext4} DEVICE

    -b BLOCK:1024,2048,4096 #設置BLOCK大小。

    -L 'LABAL' #設定卷標


blkid 命令

格式:blkid DEVIE  #查看LABEL,UUID,TYPE

示例:添加一個分區後,建立ext4文件系統,並經過mke2fs設定block和LABEL,並查看。

[root@LiuHui ~]# mkfs.ext4 /dev/sdb1  #建立文件系統
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
655776 inodes, 2622603 blocks
131130 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@LiuHui ~]# cat /proc/partitions  #查看分區後是否被內核識別 
major minor  #blocks  name
   8        0   20971520 sda
   8        1     204800 sda1
   8        2    1024000 sda2
   8        3   19741696 sda3
   8       16   20971520 sdb
   8       17   10490413 sdb1
[root@LiuHui ~]# mke2fs -L 'MyTest1' /dev/sdb1 #設置卷標
[root@LiuHui ~]# blkid /dev/sdb1    #查看設定卷標
/dev/sdb1: LABEL="MyTest1" UUID="b5445ce5-3182-402f-b6e8-df7140c9fe6a" TYPE="ext2"

 2)文件系統屬性查看及調整工具

e2label 命令:顯示和設置新的卷標

格式:e2label DEVICE [new-label]

示例:

[root@LiuHui ~]# e2label /dev/sdb1
MyTest1
[root@LiuHui ~]# e2label /dev/sdb1 LiuHui
[root@LiuHui ~]# e2label /dev/sdb1
LiuHui

tune2fs 命令:顯示ext系列文件系統的屬性

經常使用選項:

    -l:顯示超級塊中的信息;顯示整個文件的屬性及佈局等相關信息

    -L 'LABEL':修改卷標

    -m #: 調整預留給管理員的管理空間百分比

    -j: ext2 --> ext3

    -O:文件系統屬性的啓動或關閉 

    -o:文件系統默認掛載選項的啓用或關閉

示例:

[root@LiuHui ~]# tune2fs -l /dev/sdb1  #顯示超級塊中的信息
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   LiuHui
Last mounted on:          <not available>
Filesystem UUID:          b5445ce5-3182-402f-b6e8-df7140c9fe6a
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file
Filesystem flags:         signed_directory_hash 
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              655776
Block count:              2622603
Reserved block count:     131130
Free blocks:              2575671
Free inodes:              655765
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      640
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8096
Inode blocks per group:   506
Filesystem created:       Fri Aug 28 03:38:26 2015
Last mount time:          n/a
Last write time:          Fri Aug 28 04:09:38 2015
Mount count:              0
Maximum mount count:      35
Last checked:             Fri Aug 28 03:38:26 2015
Check interval:           15552000 (6 months)
Next check after:         Wed Feb 24 03:38:26 2016
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
Default directory hash:   half_md4
Directory Hash Seed:      7ae58900-8ddc-4b19-8b10-5aaf7cf5b7fa
[root@LiuHui ~]# tune2fs -L 'MyTest' /dev/sdb1    #修改卷標
tune2fs 1.41.12 (17-May-2010)
[root@LiuHui ~]# tune2fs -l /dev/sdb1    #查看
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   MyTest
Last mounted on:          <not available>

dumpe2fs 命令

經常使用選項:

    -h :僅列出superblock的數據,不會列出其餘的區段內容。


 3)文件系統檢測

(1)fsck 命令 :用於檢查而且試圖修復文件系統中的錯誤。當文件系統發生錯誤變化,可用fsck指令嘗試加以修復。

格式:fsck [選項] [參數]

選項:

經常使用選項:

    -a :自動修復錯誤,不詢問任何問題。

    -r:交互式修改錯誤

    -f:強制檢測


-A 依照/etc/fatab配置文件的內容,檢查文件內所列的所有文件系統。

-N 不執行指令,僅列出實際執行會進行的動做。

-P 當搭配「-A」 參數使用時,則會同時檢查全部的文件系統。

-R 當搭配「-A」參數使用時,則會略過/目錄的文件系統不予檢查。

-s 依序執行檢查做業,而非同時執行。

-t <文件系統類型> 指定要檢查的文件系統類型。

-T 執行fack指令時,不顯示標題信息。

-V 顯示指令執行過程。

-y 自動回答爲 yes。

fdisk -l 查看設備號

 運行fsck -y /dev/sdb1 修復磁盤

參數

文件系統:指定要查看信息的文件系統。


(2)e2fsck 命令 :ext系列文件系統專用的檢測修復工具;

e2fsck執行後的傳回值及表明意義以下:

0 沒有任何錯誤發生。

1 文件系統發生錯誤,而且已經修正。

2 文件系統發生錯誤,而且已經修正。

4 文件系統發生錯誤,但沒有修正。


示例:檢查/dev/sdb1是否有問題,如發現問題便自動修復:

e2fsck -a -y /dev/sdb1


 4)對文件系統進行掛載和使用

(a)掛載文件統

掛載:將額外文件系統與根文件系統某現存的目錄創建起關聯關係,進而使得此目錄作爲其它文件系統訪問入口的行爲,稱爲掛載;

解除此關聯關係的過程:卸載;

注意:掛載點在掛載後,其內部原有的文件會被暫時隱藏;創建使用空目錄作爲掛載點;


mount 命令:經過讀取/etc/fstab文件來顯示當前系統全部已經掛載的設備;

  mount -a :掛載/etc/fstab 文件中的全部支持自動掛載的文件系統;

格式:mount [options] [-o options] DEVICE MOUNT-POINT

  [options]     :爲命令選項

  [-o options]  :爲掛載選項

  DEVICE:要掛載的設備

    (1)設備文件:/dev/sdb3

    (2)卷標:-L 'LABEL'

    (3)UUID: -U "UUID"

    (4)僞文件系統名稱

  MOUNT-POINT:掛載點

經常使用選項:

    -t type:文件系統類型

    -r:以「只讀」方式掛載此文件系統

    -w:以「讀寫」方式掛載此文件系統

    -n:每一個文件系統在掛載時都會自動更新/etc/mtab文件,-n 用於禁止此功能;

        此時,若是想查看掛載的全部文件系統:cat /proc/mounts

    -a:自動掛載全部支持自動掛載的設備;

    -B:綁定目錄至另外一個目錄上;

 -o 掛載選項

    async:異步模式

    sync:同步模式

    atime/noatime:是否更新訪問時間戳:

    diratime/nodiratime:是否更新目錄的訪問時間戳;

    auto/noauto:是否容許此設備被自動掛載;

    exec/noexec:是否容許執行此文件系統上應用程序;

    dev/nodev:是否支持在此設備上使用設備文件;

    suid/nosuid: 是否支持suid特殊權限

    remount :從新掛載

    ro 只讀掛載

    rw 可讀寫掛載

    user/nouser:是否容許普通掛載此設備;

    acl:是否支持此設備上使用facl;

示例1:掛載本地光盤

[root@LiuHui ~]# mount /dev/cdrom  /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@LiuHui ~]# ls /mnt
CentOS_BuildTag  p_w_picpaths    repodata
EFI              isolinux  RPM-GPG-KEY-CentOS-7
EULA             LiveOS    RPM-GPG-KEY-CentOS-Testing-7
GPL              Packages  TRANS.TBL

示例2:掛載ISO鏡像到/var/www/html/os/CentOS-6.5-x86_64目錄

mount -t iso9660 -o loop /data/CentOS-6.7-x86_64-bin-DVD1.iso  /var/www/html/os/CentOS-6.7-x86_64


示例3:掛載nfs網絡文件系統

mount -t nfs 192.268.1.168:/data /data1

示例4:掛載格式好的硬盤

[root@LiuHui ~]# mkdir /data1
[root@LiuHui ~]# mount /dev/sdb1 /data1
[root@LiuHui ~]# df -h /dev/sdb1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       4.8G   20M  4.6G   1% /data1

(b)卸載文件系統

umount: 卸載命令

  #umount DEVICE

  #umount MOUNT_POINT

示例:卸載

[root@LiuHui ~]# umount /mnt  == umount /dev/cdrom

(c)查看

查看正訪問指定掛載點進程:

 #fuser -v /data1/
[root@LiuHui data1]# fuser -v /data1/
                     USER        PID ACCESS COMMAND
/data1/:             root       2615 ..c.. bash

終止全部正在訪問指定的掛載點的進程:

 #fuser -km /data1/

 5)交換分區的使用


(a)查看交換分區方式:top,free命令

  free命令:能夠顯示當前系統末使用和已使用的內存數目,還能夠顯示被內核使用的內存緩衝區。

查看memory和swap的使用狀態

-m 以M爲單位顯示

-g 以G爲單位顯示

示例:

CentOS 7:
[root@LiuHui ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            979         119         632           6         226         693
Swap:          1999           0        1999
[root@LiuHui ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:              0           0           0           0           0           0
Swap:             1           0           1

CentOS 6.7:
[root@LiuHui sbin]# free -m
             total       used       free     shared    buffers     cached
Mem:           981        913         67          0         88        553
-/+ buffers/cache:        272        709 
Swap:         1999          0       1999
第一部分Mem行說明:
total:內存總數;
used :已經使用的內存數;
free :空閒的內存數;
shared:當前已經廢棄不用;
buffers Buffer:緩存內存數;
cached Page:緩存內存數。
關係:total = used+free
第二部分(-/+ buffers/cache)解釋: 
(-buffers/cache) used內存數:第一部分Mem行中的 used – buffers – cached 
(+buffers/cache)free內存數: 第一部分Mem行中的 free + buffers + cached

(b)如何經過swap文件擴充交換空間的大小

示例:

[root@LiuHui ~]# free -m   
             total       used       free     shared    buffers     cached
Mem:           981        913         67          0         88        553
-/+ buffers/cache:        271        709 
Swap:         1999          0       1999 
[root@LiuHui ~]# dd if=/dev/zero of=/myswap bs=1M count=1000  #劃分空間
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 33.2985 s, 31.5 MB/s
[root@LiuHui ~]# mkswap /myswap    #格式成交換分區的格式
mkswap: /myswap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=d99627aa-3d6e-4a7c-bfd9-eb584ed19b6b
[root@LiuHui ~]# swapon /myswap     #使格式的swap生效
[root@LiuHui ~]# free -m    #查看增長的swap分區
             total       used       free     shared    buffers     cached
Mem:           981        909         72          0         69        662
-/+ buffers/cache:        177        804 
Swap:         2999          0       2999

 6)設置開機掛載


文件系統相關掛載配置文件:/etc/fstab

[root@LiuHui ~]# cat /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Mon Aug 24 23:35:21 2015
#
# 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
##
/dev/mapper/vg_mytestc6-LogVol03 /                       ext4    defaults        1 1
UUID=3f0d9a05-673f-4541-9c84-0e9eed05012b /boot          ext4    defaults        1 2
/dev/mapper/vg_mytestc6-LogVol01 /usr                    ext4    defaults        1 2
/dev/mapper/vg_mytestc6-LogVol02 /var                    ext4    defaults        1 2
/dev/mapper/vg_mytestc6-LogVol00 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

第一列爲設備號或該設備的卷標 

第二列爲掛載點 

第三列爲文件系統 

第四列爲文件系統參數 

第五列爲是否能夠用demp命令備份。0:不備份,1:備份,2:備份,但比1重要性小。設置了該參數後,Linux中使用dump命令備份系統的時候就能夠備份相應設置的掛載點了。 

第六列爲是否在系統啓動的時候,用fsck檢驗分區。由於有些掛載點是不須要檢驗的,好比:虛擬內存swap、/proc等。0:不檢驗,1:要檢驗,2要檢驗,但比1晚檢驗,通常根目錄設置爲1,其餘設置爲2就能夠了


示例:在/etc/fstab中添加自動掛載

/dev/sdb1    /data1    ext4    defaults    0 0  
/dev/sdb6    swap      swap    defaults    0 0

3.文件系統的鏈接文件

硬連接(相似快照)

    兩個路徑指向同一個inode,inode號相同

    不對對目錄進行

    不能跨分區進行

    指向同一個inode 的多個不一樣路徑,建立文件的硬連接會增長indode的引用計數;刪除硬連接僅是刪除其一個訪問路徑,直到最後一個路徑被刪除;

ln 命令:添加連接

格式:ln [-s] 被鏈接源文件 鏈接目標文件

    加-s 即建立軟鏈接

示例:

[root@LiuHui tmp]# touch test.txt  #建立測試文件
[root@LiuHui tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 13 22:07 test.txt
[root@LiuHui tmp]# ln test.txt hard.test.txt    #添加一個硬連接
[root@LiuHui tmp]# ll    #查看硬鏈接數變爲2
total 0
-rw-r--r--. 2 root root 0 Sep 13 22:07 hard.test.txt
-rw-r--r--. 2 root root 0 Sep 13 22:07 test.txt
[root@LiuHui tmp]# echo 11111 > test.txt     #向源文件增長數據
[root@LiuHui tmp]# cat test.txt 
11111
[root@LiuHui tmp]# cat hard.test.txt     #查看鏈接目標文件也被寫入數據
11111
[root@LiuHui tmp]# ln test.txt hard2.test.txt #再增長一個硬連接
[root@LiuHui tmp]# ll        #查看硬連接數變爲3
total 12
-rw-r--r--. 3 root root 6 Sep 13 22:07 hard2.test.txt
-rw-r--r--. 3 root root 6 Sep 13 22:07 hard.test.txt
-rw-r--r--. 3 root root 6 Sep 13 22:07 test.txt
[root@LiuHui tmp]# rm test.txt     #刪除源文件,
rm: remove regular file ‘test.txt’? y
[root@LiuHui tmp]# cat hard2.test.txt     #查看鏈接目標文件,對數據無影響。
11111    
[root@LiuHui tmp]# cat hard.test.txt 
11111
[root@LiuHui tmp]# ll        #硬連接數減1
total 8
-rw-r--r--. 2 root root 6 Sep 13 22:07 hard2.test.txt
-rw-r--r--. 2 root root 6 Sep 13 22:07 hard.test.txt

符號鏈接(相似快捷方式),又稱軟連接

格式:ln -s 源文件 連接目標文件

    能夠跨文件系統,能夠對目錄建立符號連接。

    刪除源文件,符號連接將失效。

    源文件的inode與連接文件不一樣。

示例:

[root@LiuHui tmp]# touch test.txt  #建立測試文件
[root@LiuHui tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 13 22:20 test.txt
[root@LiuHui tmp]# ls -i
33554563 test.txt
[root@LiuHui tmp]# ln -s test.txt sym.test.txt    #建立符號連接.
[root@LiuHui tmp]# ls -i        #查看inode節點號不同
33576579 sym.test.txt  33554563 test.txt
[root@LiuHui tmp]# echo 1111 > test.txt     #添加數據
[root@LiuHui tmp]# cat test.txt     #查看數據
1111
[root@LiuHui tmp]# cat sym.test.txt 
1111
[root@LiuHui tmp]# rm -f test.txt     #刪除源文件
[root@LiuHui tmp]# ls
sym.test.txt
[root@LiuHui tmp]# ll
total 0
lrwxrwxrwx. 1 root root 8 Sep 13 22:20 sym.test.txt -> test.txt
[root@LiuHui tmp]# cat sym.test.txt     #刪除源文件後,建立的符號鏈接失效
cat: sym.test.txt: No such file or directory

練習:

1.建立一個10G文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄;

(1)建立一個分區,大小爲10G
[root@LiuHui ~]# fdisk /dev/sdb
Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (10489856-41943039, default 10489856): 
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-41943039, default 41943039): +10G
Partition 5 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xa7225c08
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    41943039    15727616    5  Extended
/dev/sdb5        10489856    31461375    10485760   83  Linux
Command (m for help): w   
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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.
(2)格式化爲ext4文件系統
[root@LiuHui ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 
(3)建立空目錄,設置開機自啓動
[root@LiuHui ~]# mkdir /mydata
[root@LiuHui ~]# vim /etc/fstab 
-bash: vim: command not found
[root@LiuHui ~]# vi /etc/fstab 
[root@LiuHui ~]# e2label /dev/sdb5
[root@LiuHui ~]# blkid /dev/sdb5    #查看UUID
/dev/sdb5: UUID="4dae0efb-204a-4428-b4ed-b6c43a8f3add" TYPE="ext4" 
(4)經過UUID爲被掛載的設備進行自動掛載
[root@LiuHui ~]# vi /etc/fstab
[root@LiuHui ~]# tail -1 /etc/fstab 
UUID=4dae0efb-204a-4428-b4ed-b6c43a8f3add /mydata       ext4  defaults  0 0
(5)驗證掛載並查看掛載的結果
[root@LiuHui ~]# mount -a
[root@LiuHui ~]# df -h /dev/sdb5
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb5       4.8G   20M  4.6G   1% /mydata
相關文章
相關標籤/搜索