這是我參與 8 月更文挑戰的第 6 天,活動詳情查看: 8月更文挑戰前端
一名致力於在技術道路上的終身學習者、實踐者、分享者,一位忙起來又偶爾偷懶的原創博主,一個偶爾無聊又偶爾幽默的少年。node
歡迎各位掘友們微信搜索「傑哥的IT之旅」關注!數據庫
原文連接:Linux磁盤和文件系統管理vim
掛接好新的硬盤設備並啓動主機後,Linux系統會自動檢測並加載該硬盤,無須額外安裝驅動。執行「fdisk -l」命令便可查看,確認新增硬盤的設備名稱和位置。做用:列出當前系統中全部硬盤設備及其分區的信息。windows
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux
複製代碼
上述輸出信息中包含了各硬盤的總體狀況和分區狀況,其中「/dev/sda」爲原有的硬盤設備,對於已有的分區,將經過列表的方式輸出如下信息;後端
右鍵單擊上圖右下角某塊磁盤,「設置」按鈕。彈出下圖對話框,「添加」按鈕。微信
選擇相應的硬件類型。點擊「下一步」按鈕。markdown
選擇磁盤類型,默認爲SCSI。運維
選擇最大磁盤大小的空間,並選擇將虛擬磁盤存儲爲單個文件,點擊「下一步」按鈕。添加完成後,重啓設備。編輯器
重啓後執行「fdisk -l」命令,便可查看是否識別新的硬盤。
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000
複製代碼
在硬盤設備中建立、刪除、更改分區等操做一樣經過fdisk命令進行,只要使用硬盤的設備文件做爲參數。例如,執行「fdisk /dev/sdb」命令,便可進入到交互式的分區管理界面中。
在該操做界面中的「Command (m for help):」提示符後,輸入特定的分區操做指令,能夠完成各項分區管理任務。輸入「m」指令後,能夠查看各類操做指令的幫助信息。
fdisk 分區工具的交互式操做界面
[root@localhost ~]# 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 0x97fe0724.
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)
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):
複製代碼
關於fdisk交互式操做指令的幫助信息
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
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):
複製代碼
使用「p」指令能夠列出詳細的分區狀況,信息顯示的格式與執行「fdisk -l」命令相同,硬盤中還沒有創建分區時,輸出的列表信息爲空。
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x97fe0724
Device Boot Start End Blocks Id System
Command (m for help):
複製代碼
使用「n」指令能夠進行建立分區的操做,包括主分區和擴展分區。根據提示繼續輸入「p」選擇建立主分區,輸入「e」選擇建立擴展分區。以後依次選擇分區序號,起始位置,結束位置或分區大小便可完成新分區的建立。
選擇分區好時,主分區和擴展分區的序號只能爲1-4.分區起始位置通常由fdisk默認識別,結束位置或大小可使用"+sizeM"或"+sizeG"的形式。
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +5G
Value out of range.
複製代碼
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x97fe0724
Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 83 Linux
複製代碼
使用「d」指令能夠刪除指定的分區,根據提示繼續輸入須要刪除的分區序號,在執行前刪除分區時必定要慎重,應首先使用p指令查看分區的序號,確認無誤後再進行刪除。
須要注意的是,若擴展分區被刪除,則擴展分區之下的邏輯分區也將被刪除。所以建議從最後一個分區開始進行刪除,以避免fdisk識別的分區序號發生混亂。
在fdisk分區工具中,新建的分區默認使用的文件系統類型爲EXT4,通常不須要更改,可是若新建的分區須要用做swap交換分區或其餘類型的文件系統時,則須要對分區類型進行變動以保持一致性,從而避免在管理分區時產生混亂。
使用「t」指令能夠變動分區的ID號,操做時須要依次指定目標分區序號,新的系統ID號。不一樣類型的文件系統對應不一樣的ID號,以16進制數表示,在fdisk交互環境中輸入「l」指令進行列表查看,最經常使用的EXT四、swap文件系統的ID號分別爲8三、82,而用於windows中的NTFS文件系統的ID號通常爲86。
完成對硬盤的分區操做之後,能夠執行w或q指令退出fdisk分區工具。其中「w」將保存分區操做,而「q」指令將不會保存對硬盤所作的分區操做。
對已包含數據的硬盤進行分區時,必定要作好數據備份,保存以前要確保操做無誤,以避免發生數據損壞,若沒法肯定本次分區操做是否正確,建議使用「q」指令不保存退出。
變動磁盤的分區設置之後,建議最好將系統重啓一次,或者執行「partprobe」命令使操做系統檢測新的分區表狀況。在某些Linux操做系統中,若不進行這些操做,可能會致使格式化分區時對硬盤中已有數據的損壞,嚴重者甚至引發系統崩潰。執行 partprobe命令檢測/dev/sdb磁盤中的分區變化。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/sdb
複製代碼
mkfs命令是一個前端工具,能夠自動加載不一樣的程序來建立各類類型的分區,然後端包括有多個與mkfs命令相關的工具程序,位於/sbin目錄中。
[root@localhost ~]# ls /sbin/mkfs*
/sbin/mkfs /sbin/mkfs.cramfs /sbin/mkfs.ext2 /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.ext4dev /sbin/mkfs.msdos /sbin/mkfs.vfat
複製代碼
基本的命令格式
mkfs -t 文件系統類型 分區設備
複製代碼
使用mkswap命令能夠在指定的分區上建立交換文件系統,目標分區先經過fdisk工具將ID號設爲82。
對於新增長的交換分區,須要使用swapon命令進行啓用,反之使用swapoff命令能夠停用指定的交換分區。
[root@localhost ~]# fdisk -l /dev/sda
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux
複製代碼
mount命令的基本使用格式
mount [-t 文件系統類型] 存儲設備 掛載點
複製代碼
光盤對應的設備文件一般使用"/dev/cdrom",其實這是一個連接文件,連接到實際的光盤設備"/dev/sr0"。使用這兩個名稱均可以表示光盤設備。因爲光盤是隻讀的存儲介質,所以在掛載時系統會出現"mounting read-only"的提示信息。
使用不帶任何參數或選項的mount命令時,將顯示出當前系統中已掛載的各個分區的相關信息,最近掛載的文件系統將顯示在最後邊。
[root@localhost ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
[root@localhost ~]#
複製代碼
須要卸載文件系統時,使用的命令爲umount,使用掛載點目錄或對應設備的文件名做爲卸載參數,Linux系統中,因爲同一個設備能夠被掛載到多個目錄下,因此通常建議經過掛載點的目錄位置來進行卸載。
[root@localhost ~]# umount /dev/cdrom
複製代碼
系統中的/etc/fstab文件能夠視爲mount命令的配置文件,其中存儲了文件系統的靜態掛載數據。Linux系統在每次開機時,會自動讀取這個文件的內容,自動掛載所指定的文件系統。默認的fatab文件中包括了根分區、/boot分區、交換分區及proc、tmpfs等僞文件系統的掛載配置。
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Jun 4 06:06:05 2016
#
# 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=793c943d-a96e-453d-8249-a8eb9ad1ec59 / ext4 defaults 1 1
UUID=758a54e1-de64-4e02-a4a9-f09f4c28fdfa /boot ext4 defaults 1 2
UUID=0535a0f7-88f2-490c-b26f-94a27e45804d 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
[root@localhost ~]#
複製代碼
在"/etc/fstab"文件中,每一行記錄對應一個分區或設備的掛載配置信息,各部分的含義以下。
第1字段:設備名或設備卷標名。
第2字段:文件系統的掛載點目錄的位置。
第3字段:文件系統類型,如ext四、swap等。
第4字段:掛載參數,mount命令"-o"選項後可以使用的參數。
(defaults、rw、ro、noexec分別表示默認參數、可寫、只讀、禁用執行程序)
第6字段:該數字用於決定在系統啓動時進行磁盤檢查的順序。0表示不進行檢查,1表示優先檢查,2表示其次檢查,對於根分區應設爲1,其餘分區設爲2。
不帶選項及參數的mount命令能夠顯示分區的掛載狀況,瞭解系統中已掛載各文件系統的磁盤使用狀況可使用df命令。
df命令使用文件或者設備做爲命令參數,經常使用的選項爲"-h","-T"。"-h"選項能夠顯示更易讀的容量單位,而"-T"選項用於顯示對應文件系統的類型。
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 18G 5.0G 12G 30% /
tmpfs tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 ext4 97M 30M 63M 32% /boot
[root@localhost ~]#
複製代碼
Linux環境下安裝及管理程序(軟件包封裝、RPM命令、源代碼編譯安裝的基本過程)
本文完。
原創不易,若是你以爲這篇文章對你有點用的話,麻煩你爲本文點個贊、評論或轉發一下,由於這將是我輸出更多優質文章的動力,感謝!
對了,掘友們記得給我點個免費的關注喲!防止你迷路下次就找不到我了。
咱們下期再見!