紅帽7 磁盤劃分

一、掛載硬件設備node

mount命令用於掛載文件系統,格式爲「mount 文件系統 掛載目錄」。掛載是在使用硬件設備前所執行的最後一步操做。只需使用mount命令把硬盤設備或分區與一個目錄文件進行關聯,而後就能在這個目錄中看到硬件設備中的數據了。linux

mount命令中的參數以及做用vim

參數 做用
-a 掛載全部在/etc/fstab中定義的文件系統
-t 指定文件系統的類型

 

 

 

 

 

 

 

例如,要把設備 /dev/sdb2 掛載到 /back 目錄,只須要在mount命令中填寫設備與掛載目錄參數就行,系統會自動去判斷要掛載文件的類型app

[root@localhost Desktop]# mount  /dev/sdb2  /back

 雖然按照上面的方法執行mount命令後就能當即使用文件系統了,但系統在重啓後掛載就會失效,也就是說咱們須要每次開機後都手動掛載一下。若是想讓硬件設備和目錄永久地進行自動關聯,就必須把掛載信息按照指定的填寫格式  「設備文件 掛載目錄 格式類型 權限選項 是否備份 是否自檢」  寫入到/etc/fstab文件中。這個文件中包含着掛載所需的諸多信息項目,一旦配置好以後就能一勞永逸了。async

用於掛載信息的指定填寫格式中,各字段所表示的意義ide

字段 意義
設備文件 通常爲設備的路徑+設備名稱,也能夠寫惟一識別碼(UUID,Universally Unique Identifier)
掛載目錄 指定要掛載到的目錄,需在掛載前建立好
格式類型 指定文件系統的格式,好比Ext三、Ext四、XFS、SWAP、iso9660(此爲光盤設備)等
權限選項 若設置爲defaults,則默認權限爲:rw, suid, dev, exec, auto, nouser, async
是否備份 若爲1則開機後使用dump進行磁盤備份,爲0則不備份
是否自檢 若爲1則開機後自動進行磁盤自檢,爲0則不自檢

 

 

 

 

 

 

 

 

 

 

若是想將文件系統爲ext4的硬件設備 /dev/sdb2 在開機後自動掛載到 /back 目錄上,並保持默認權限且無需開機自檢,就須要在/etc/fstab文件中寫入下面的信息,這樣在系統重啓後也會成功掛載。ui

[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb2 /back ext4 defaults 0 0

umount命令:用於撤銷已經掛載的設備文件,格式爲 「umount [掛載點/設備文件]」 。咱們掛載文件系統的目的是爲了使用硬件資源,而卸載文件系統就意味再也不使用硬件的設備資源;相對應地,掛載操做就是把硬件設備與目錄進行關聯的動做,所以卸載操做只須要說明想要取消關聯的設備文件或掛載目錄的其中一項便可,通常不須要加其餘額外的參數。spa

[root@localhost Desktop]# umount  /dev/sdb2

二、添加硬盤設備 設計

第1步:首先把虛擬機系統關機,稍等幾分鐘會自動返回到虛擬機管理主界面,而後單擊「編輯虛擬機設置」選項,在彈出的界面中單擊「添加」按鈕,新增一塊硬件設備3d

在虛擬機中模擬添加了硬盤設備後就應該能看到抽象成的硬盤設備文件了。第二個被識別的SCSI設備應該會被保存爲/dev/sdb,這個就是硬盤設備文件了。但在開始使用該硬盤以前還須要進行分區操做。

(1)fdisk命令

在Linux系統中,管理硬盤設備最經常使用的方法就當屬fdisk命令了。fdisk命令用於管理磁盤分區,格式爲「fdisk  [磁盤名稱]」,它提供了集添加、刪除、轉換分區等功能於一身的「一站式分區服務」。

fdisk命令中的參數以及做用

參數 做用
m 查看所有可用的參數
n 添加新的分區
d 刪除某個分區信息
l 列出全部可用的分區類型
t 改變某個分區的類型
p 查看分區表信息
w 保存並退出
q 不保存直接退出

 

 

 

 

 

 

 

 

 

 

 

 

 第1步:咱們首先使用fdisk命令來嘗試管理 /dev/sdb 硬盤設備。在看到提示信息後輸入參數p來查看硬盤設備內已有的分區信息,其中包括了硬盤的容量大小、扇區個數等信息

[root@localhost Desktop]# 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 0xab9c86d8.

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

   Device Boot      Start         End      Blocks   Id  System

第2步:輸入參數n嘗試添加新的分區。系統會要求您是選擇繼續輸入參數p來建立主分區,仍是輸入參數e來建立擴展分區。這裏輸入參數p來建立一個主分區

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

第3步:在確認建立一個主分區後,系統要求您先輸入主分區的編號。咱們在前文得知,主分區的編號範圍是1~4,所以這裏輸入默認的1就能夠了。接下來系統會提示定義起始的扇區位置,這不須要改動,咱們敲擊回車鍵保留默認設置便可,系統會自動計算出最靠前的空閒扇區的位置。最後,系統會要求定義分區的結束扇區位置,這其實就是要去定義整個分區的大小是多少。咱們不用去計算扇區的個數,只須要輸入+2G便可建立出一個容量爲2GB的硬盤分區

Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):    敲回車
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

第4步:再次使用參數p來查看硬盤設備中的分區信息。果真就能看到一個名稱爲 /dev/sdb一、起始扇區位置爲204八、結束扇區位置爲4196351的主分區了。這時候千萬不要直接關閉窗口,而應該敲擊參數w後回車,這樣分區信息纔是真正的寫入成功。

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

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

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

第5步:在上述步驟執行完畢以後,Linux系統會自動把這個硬盤主分區抽象成/dev/sdb1設備文件。咱們可使用file命令查看該文件的屬性,有些時候系統並無自動把分區信息同步給Linux內核,咱們能夠輸入 partprobe命令 手動將分區信息同步到內核,並且通常推薦連續兩次執行該命令,效果會更好。

[root@localhost Desktop]# file /dev/sdb1
/dev/sdb1: block special

若是硬件存儲設備沒有進行格式化,則Linux系統沒法得知怎麼在其上寫入數據。所以,在對存儲設備進行分區後還須要進行格式化操做。在Linux系統中用於格式化操做的命令是mkfs。在Shell終端中輸入mkfs名後再敲擊兩下用於補齊命令的Tab鍵

[root@localhost Desktop]# mkfs
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat    

把 /dev/sdb1 格式化爲xfs的文件系統,則命令應爲 mkfs.xfs /dev/sdb1 。

[root@localhost Desktop]# mkfs.xfs  /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

接下來就是要來掛載並使用存儲設備了。與之相關的步驟也很是簡單:首先是建立一個用於掛載設備的掛載點目錄;而後使用 mount 命令將存儲設備與掛載點進行關聯;最後

使用 df -h 命令來查看掛載狀態和硬盤使用量信息。

[root@localhost Desktop]# mkdir /newfile
[root@localhost Desktop]# mount /dev/sdb1 /newfile/
[root@localhost Desktop]# df -h Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  2.9G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
/dev/sdb1              2.0G   33M  2.0G   2% /newfile

使用mount命令掛載的設備文件會在系統下一次重啓的時候失效。若是想讓這個設備文件的掛載永久有效,則須要把掛載的信息寫入到配置文件中

[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1 /newfile xfs defaults 0 0                                              

三、添加交換分區

SWAP(交換)分區是一種經過在硬盤中預先劃分必定的空間,而後將把內存中暫時不經常使用的數據臨時存放到硬盤中,以便騰出物理內存空間讓更活躍的程序服務來使用的技術,其設計目的是爲了解決真實物理內存不足的問題。但因爲交換分區畢竟是經過硬盤設備讀寫數據的,速度確定要比物理內存慢,因此只有當真實的物理內存耗盡後纔會調用交換分區的資源。交換分區的建立過程與前文講到的掛載並使用存儲設備的過程很是類似。在對/dev/sdb存儲設備進行分區操做前,有必要先說一下交換分區的劃分建議:在生產環境中,交換分區的大小通常爲真實物理內存的1.5~2倍。

[root@localhost Desktop]# 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.


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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +2G
Partition 2 of type Linux and of size 2 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: 0xab9c86d8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   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.
View Code

使用SWAP分區專用的格式化命令 mkswap,對新建的主分區進行格式化操做

[root@localhost Desktop]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=a7bc373a-1c22-447c-8fea-b251466e7d1d

使用swapon命令把準備好的SWAP分區設備正式掛載到系統中。咱們可使用free -m命令查看交換分區的大小變化

[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:          1987        906       1081          9          1        298
-/+ buffers/cache:        606       1381 Swap:  2047          0       2047
[root@localhost Desktop]# swapon /dev/sdb2 
[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:          1987        908       1079          9          1        298
-/+ buffers/cache:        607       1380 Swap:         4095          0       4095
[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1 /newfile xfs defaults 0 0
/dev/sdb2 swap swap defaults 0 0

四、軟硬方式連接

在Linux系統中存在硬連接和軟鏈接兩種文件。

硬連接(hard link):能夠將它理解爲一個「指向原始文件inode的指針」,系統不爲它分配獨立的inode和文件。因此,硬連接文件與原始文件實際上是同一個文件,只是名字不一樣。咱們每添加一個硬連接,該文件的inode鏈接數就會增長1;並且只有當該文件的inode鏈接數爲0時,纔算完全將它刪除。換言之,因爲硬連接其實是指向原文件inode的指針,所以即使原始文件被刪除,依然能夠經過硬連接文件來訪問。須要注意的是,因爲技術的侷限性,咱們不能跨分區對目錄文件進行連接。

軟連接(也稱爲符號連接[symbolic link]):僅僅包含所連接文件的路徑名,所以能連接目錄文件,也能夠跨越文件系統進行連接。可是,當原始文件被刪除後,連接文件也將失效,從這一點上來講與Windows系統中的「快捷方式」具備同樣的性質。

ln命令

ln命令用於建立連接文件,格式爲「ln [選項] 目標」,在使用ln命令時,是否添加-s參數,將建立出性質不一樣的兩種「快捷方式」。

ln命令中可用的參數以及做用

參數 做用
-s 建立「符號連接」(若是不帶-s參數,則默認建立硬連接)
-f 強制建立文件或目錄的連接
-i 覆蓋前先詢問
-v 顯示建立連接的過程

 

 

 

 

 

 

 

 

建立一個相似於Windows系統中快捷方式的軟連接。這樣,當原始文件被刪除後,就沒法讀取新建的連接文件了。

[root@localhost Desktop]# echo "hello world" > text.txt
[root@localhost Desktop]# ln -s text.txt copy.txt [root@localhost Desktop]# cat copy.txt 
hello world
[root@localhost Desktop]# cat text.txt 
hello world
[root@localhost Desktop]# ls -l text.txt 
-rw-r--r--. 1 root root 12 Sep 15 20:09 text.txt
[root@localhost Desktop]# rm -rf text.txt [root@localhost Desktop]# cat copy.txt cat: copy.txt: No such file or directory

接下來針對一個原始文件建立一個硬連接,即至關於針對原始文件的硬盤存儲位置建立了一個指針,這樣一來,新建立的這個硬連接就再也不依賴於原始文件的名稱等信息,也不會由於原始文件的刪除而致使沒法讀取。同時能夠看到建立硬連接後,原始文件的硬盤連接數量增長到了2。

[root@localhost Desktop]# echo "hello world" > test.txt
[root@localhost Desktop]# ln test.txt copy.txt
[root@localhost Desktop]# cat copy.txt 
hello world
[root@localhost Desktop]# cat test.txt 
hello world
[root@localhost Desktop]# ls -l test.txt 
-rw-r--r--. 2 root root 12 Sep 15 20:14 test.txt
[root@localhost Desktop]# rm -rf test.txt 
[root@localhost Desktop]# cat copy.txt 
hello world
相關文章
相關標籤/搜索