linux系統查看硬盤命令:linux怎麼加載硬盤

1檢測並確認新硬盤

掛接好新的硬盤設備並啟動主機後,Linux系統會自動檢測並加載該硬盤,無須額外安裝驅動。執行“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
[root@localhost ~]#

上述輸出信息中包含了各硬盤的整體情況和分區情況,其中“/dev/sda”為原有的硬盤設備,對於已有的分區,將通過列表的方式輸出以下信息;

  • Device:分區的設備文件名稱。
  • Boot:是否是引導分區。是,則有“*”標識。
  • Start:該分區在硬盤中的起始位置。
  • End:該分區在硬盤中的結束位置。
  • Blocks:分區的大小,以Blocks(塊)為單位,默認的塊大小為1024字節。
  • Id:分區對應的系統ID號。83表示Linux中的EXT4分區,8e表示LVM 邏輯卷。
  • System:分區類型。

2添加新的硬盤

Linux 磁盤和文件系統管理

右鍵單擊上圖右下角某塊磁盤,“設置”按鈕。彈出下圖對話框,“添加”按鈕。

Linux 磁盤和文件系統管理

選擇相應的硬件類型。點擊“下一步”按鈕。

Linux 磁盤和文件系統管理

選擇磁盤類型,默認為SCSI。

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

選擇最大磁盤大小的空間,並選擇將虛擬磁盤存儲為單個文件,點擊“下一步”按鈕。添加完成後,重啟設備。

Linux 磁盤和文件系統管理

重啟後執行“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

[root@localhost ~]#

3規劃硬盤中的分區

在硬盤設備中創建、刪除、更改分區等操作同樣通過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):

C/C++Linux服務器開發/後台架構師【零聲教育】-學習視頻教程-騰訊課堂

【文章福利】:小編整理了一些個人覺得比較好的學習書籍、視頻資料共享在群文件裡面,有需要的可以自行添加哦!~點擊加入(832218493需要自取)

Linux 磁盤和文件系統管理

關於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指令——列出硬盤中的分區情況

使用“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指令——新建分區

使用“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.

完成後可以輸入“p”指令查看分區情況;

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指令——刪除分區

使用“d”指令可以刪除指定的分區,根據提示繼續輸入需要刪除的分區序號,在執行前刪除分區時一定要慎重,應首先使用p指令查看分區的序號,確認無誤後再進行刪除。

需要注意的是,若擴展分區被刪除,則擴展分區之下的邏輯分區也將被刪除。因此建議從最後一個分區開始進行刪除,以免fdisk識別的分區序號發生混亂。

t指令——變更分區的類型

在fdisk分區工具中,新建的分區默認使用的文件系統類型為EXT4,一般不需要更改,但是若新建的分區需要用作swap交換分區或其他類型的文件系統時,則需要對分區類型進行變更以保持一致性,從而避免在管理分區時產生混亂。

使用“t”指令可以變更分區的ID號,操作時需要依次指定目標分區序號,新的系統ID號。不同類型的文件系統對應不同的ID號,以16進制數表示,在fdisk交互環境中輸入“l”指令進行列表查看,最常用的EXT4、swap文件系統的ID號分別為83、82,而用於windows中的NTFS文件系統的ID號一般為86。

w和q指令——退出fdisk分區工具

完成對硬盤的分區操作以後,可以執行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
[root@localhost ~]#

4管理文件系統

mkfs命令的使用

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

基本的命令格式

mkfs -t 文件系統類型 分區設備

mkswap命令的使用

使用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

5掛載、卸載文件系統

掛載文件系統

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

設置文件系統的自動掛載

系統中的/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字段:文件系統類型,如ext4、swap等。

第4字段:掛載參數,mount命令”-o”選項後可使用的參數。

(defaults、rw、ro、noexec分別表示默認參數、可寫、只讀、禁用執行程序)

第5字段:表示文件系統是否需要dump備份,為1時表示需要,為0時將被dump所忽略。

第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 ~]#

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/217659.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 00:28
下一篇 2024-12-09 00:28

相關推薦

發表回復

登錄後才能評論