磁碟分割槽格式化檢驗掛載
一.磁碟分割槽:fdisk
fdisk 【-l】 裝置名稱
-l:輸出後面接的裝置的所有的分割槽內容。若僅由fdisl -l時,則系統會把整個系統能夠找到的裝置的分割槽均列出來
例:
[[email protected] mnt]# df *先通過df找出可用磁碟名
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda3 20243456 3307500 16935956 17% /
[[email protected] mnt]# fdisk /dev/vda
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): *等待輸入
Command (m for help): m *輸入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 *不儲存,直接離開fdisk程式
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/vda: 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: 0x000cce79
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 411647 204800 83 Linux
/dev/vda2 411648 1435647 512000 82 Linux swap / Solaris
/dev/vda3 1435648 41943039 20253696 83 Linux
Command (m for help): q *如果不想儲存,按q即可離開
介紹一下分割槽表主要列出的每個分割槽的資訊專案
a.Device:裝置檔名,依據不同的磁碟介面/分割槽位置而改變
b.Boot:表示是否為開機引導模組
c.Start,End:表示這個分割槽在那個柱面號碼之間,可以決定此分割槽的大小
d.Blocks:就是以1k為單位的容量。
e.Id System:代表這個分割槽內的檔案系統時什麼
二.增加磁碟分割槽
例:
a,增加主分割槽1,2,3
[[email protected] mnt]# fdisk /dev/vdb
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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p *選擇primary分割槽,即主分割槽
Partition number (1-4, default 1): *預設為1號,可自己改變
First sector (2048-41943039, default 2048): *直接安Enter決定
Using default value 2048 *起始柱面
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +500M
###此處可直接輸入柱面號碼,但需要你自己計算分割槽大小
###也可直接用+..M(如上),讓系統自己分配柱面號碼
Partition 1 of type Linux and of size 500 MiB is set
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 (1026048-41943039, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-41943039, default 41943039): +500M
Partition 2 of type Linux and of size 500 MiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (2050048-41943039, default 2050048):
Using default value 2050048
Last sector, +sectors or +size{K,M,G} (2050048-41943039, default 41943039): +500M
Partition 3 of type Linux and of size 500 MiB is set
b.增加擴充套件分割槽
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): *因前三個分割槽都為主分割槽,第四個預設為擴充套件分割槽
Using default response e
Selected partition 4
First sector (3074048-41943039, default 3074048):
Using default value 3074048
Last sector, +sectors or +size{K,M,G} (3074048-41943039, default 41943039):
###直接Enter,將剩下的所有分割槽都分給擴充套件分割槽
Using default value 41943039
Partition 4 of type Extended and of size 18.5 GiB is set
Command (m for help): p *檢視分割槽資訊
Disk /dev/vdb: 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: 0x7afa732b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 1026047 512000 83 Linux
/dev/vdb2 1026048 2050047 512000 83 Linux
/dev/vdb3 2050048 3074047 512000 83 Linux
/dev/vdb4 3074048 41943039 19434496 5 Extended
c.增加邏輯分割槽
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (3076096-41943039, default 3076096):
Using default value 3076096
Last sector, +sectors or +size{K,M,G} (3076096-41943039, default 41943039): +1024M
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/vdb: 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: 0x7afa732b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 1026047 512000 83 Linux
/dev/vdb2 1026048 2050047 512000 83 Linux
/dev/vdb3 2050048 3074047 512000 83 Linux
/dev/vdb4 3074048 41943039 19434496 5 Extended
/dev/vdb5 3076096 5173247 1048576 83 Linux
此時/dev/vdb5,便是邏輯分割槽
之後,可以按q(不儲存)離開此介面,注意,不要儲存,否則會損壞系統。(自己新增的虛擬磁碟請任性)
一般來說,新建分割槽形式會有下面幾種情況:
*1-4號尚有剩餘,且系統未由擴充套件分割槽:
此時會讓你選擇primary/extended的選項,且你可以指定1-4號碼
*1-4號尚有剩餘,且系統由擴充套件分割槽:
此時會讓你選擇primary/logical的選項;若選擇p則你還需要指定1-4之間的號碼;若選擇l(L)則不需要設定號碼,因為系統會自動指定邏輯分割槽的檔名號碼。
*1-4沒有剩餘,且系統由擴充套件分割槽;
此時不會讓你挑選分割槽型別,直接進入logical的分割槽形式
三.刪除磁碟分割槽
例:
[[email protected] mnt]# fdisk -l
Disk /dev/vdb: 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: 0x7afa732b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 1026047 512000 83 Linux
/dev/vdb2 1026048 2050047 512000 83 Linux
/dev/vdb3 2050048 3074047 512000 83 Linux
/dev/vdb4 3074048 41943039 19434496 5 Extended
/dev/vdb5 3076096 5173247 1048576 83 Linux
[[email protected] mnt]# fdisk /dev/vdb
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): d
Partition number (1-5, default 5): 5
Partition 5 is deleted
Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/vdb: 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: 0x7afa732b
Device Boot Start End Blocks Id System
四.磁碟格式化
mkfs
格式: mkfs 【-t 檔案系統格式】 裝置檔名
-t :可接檔案系統格式
例:
[[email protected] mnt]# mkfs 【Tab】【Tab】
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
[[email protected] mnt]# mkfs.ext4 /dev/vdb2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label= *指分割槽的名稱
OS type: Linux
Block size=1024 (log=0) *block的大小設定為1K
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks *由此設定決定的inode/block數量
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done 有日誌記錄
Writing superblocks and filesystem accounting information: done
至此,ext4格式化完成
mke2fs:
mke2fs 【-b block 大小】 【-i block 大小】【-L 卷標】 【-cj】裝置
-b:可以設定每個block的大小,目前支援1024,2048,4096bytes三種
-i:多少容量給予一個inode呢
-c:檢查磁碟錯誤,僅下達一次-c時會進行快速讀取測試;如果下達兩次-c -c的話,會測試讀寫
-L:後面可以接卷標名稱
-j:本來mke2fs時ext2,加上-j後,會主動加入journal而成為ext3
五.磁碟檢驗:fsck,badblocks
fsck
格式:fsck [-t 檔案系統] [-ACay] 裝置名稱
-t:指定檔案系統,不過現在的linux會自動通過super block去分辨檔案系統,因此通常時不需要這個引數的
-A:依據/etc/fstab的內容,將需要的裝置掃描一次
-a:自動修復檢查到的有問題的扇區,不需要一直按y
-y:與-a類似,但是某些檔案僅支援-y這個引數
-C:可以在檢測的過程中使用一個直方圖來顯示目前的進度
badblocks
格式:badblocks -【svw】 裝置名稱
-s:在螢幕上列出進度
-v:可以在螢幕上看到進度
-w:使用寫入的方式來測試,建議不要使用此引數,尤其是待檢查的裝置已有檔案時
六.磁碟的掛載與解除安裝
在掛載前,先確定幾件事:
*單一檔案系統不應該被重複掛載在不同的掛載點(目錄)中
*單一目錄不應該重複掛載多個檔案系統
*作為掛載點的目錄理論上應該都是空目錄才是
掛載命令:mount
格式:
mount -a
mount 【-l(L的小寫)】
mount 【-t 檔案系統】 【-L Label名】 【-o 額外選項】 【-n】 裝置檔名 掛載點
引數:
-a:依照配置檔案/etc/fstab的資料將所有未掛載的磁碟都掛載上來
-l:單純輸入mount會顯示目前的掛載資訊,加上-l可增列Labekl名稱
-t:可以加上檔案系統的種類來指定欲掛載的型別。
-n:在預設情況下,系統會將實際掛載的情況寫入/etc/fstab中,以利其他程式的執行。但在某些某些情況下為了避免問題,會刻意不寫入,此時就要使用-n這個引數了
-L:系統除了利用裝置檔名之外,還可以利用檔案系統的卷標名稱進行掛載。
-o:後面可以接一些掛載時額外加上的引數,例如帳號。密碼讀取許可權等:
*ro,rw:掛載檔案系統為只讀(ro)或可讀寫(rw)
*async,sync:此檔案系統是否使用同步寫入(sync)或非同步(async)的記憶體機制,預設為async
*auto,noauto:允許此分割槽被mount -a自動掛載
*dev,nodev:是否允許此分割槽上可建立裝置檔案。dev為允許
*suid,nosuid:是否允許此分割槽含有suid/sgid的檔案格式
*exec,noexec:是否允許此分割槽上擁有可執行的binary檔案
*user,nouser:是否允許此分割槽讓任何使用者執行mount。一般來說,mount只有root可以執行,但使用user引數,冊可以讓一般使用者也能夠對此分割槽執行mount
*defaults:預設值為rw,suid,dev,exec,auto,nouser,async
*remount:重新掛載,這在系統出錯,或重新更改引數時,很有用
umount(將裝置檔案解除安裝)
格式:umount 【-fn】 裝置檔名或掛載點
-f:遷至解除安裝,可以用在網路檔案系統(NFS)無法讀取到的情況下
-n:不更新/etc/mtab的情況下解除安裝
注:只有掛載的裝置檔案解除安裝了,才可以退出光碟,U盤等裝置。
七.設定開機掛載:/etc/fstab
在開機的時候就將我們需要的檔案系統掛載好,這就是fstab檔案的作用,在說這個檔案之前,首先說一下系統掛載的限制:
*根目錄時必須掛載的,而且一定要先於其它的掛載點被掛載起來
*其他掛載點必須為已新建的目錄,可以人以指定,但一定要遵守必須的系統目錄架構規則
*所有掛載點在同一時間內,只能掛載一次
*所有分割槽在同一時間內,只能掛載一次
*如若進行解除安裝,你必須先將工作目錄移到到掛載點(及其子目錄)之外
接下來日那個我們看一下fstab這個檔案的內容:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Oct 23 20:41:40 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
#
UUID=02014ad0-eefa-419a-b5a2-69fe78f17e48 / xfs defaults 0 0
UUID=f87158fa-2d5a-4509-8a2e-98f193d1c1bf /boot xfs defaults 0 0
UUID=49de3e9a-8594-40df-8182-da3a2b2164eb swap swap defaults 0 0
*第一列:磁碟裝置檔名或該裝置的Label
*第二列:掛載點
*第三列:掛載分割槽的檔案系統
*第四列:檔案系統引數,參考mount -o後面的引數
*第五列:能否被dump備份命令作用,0代表不備份,1代表備份
*第六列:是否以fsck檢驗扇區,0時不檢驗,1是檢驗