我是一個小白,學習Linux也有一段時間了,一篇文章也沒發表過,今天剛剛學到磁盤掛載,就想著寫一篇吧!
掛載,就是把當前設備(硬碟)與當前根下的某一個檔案夾建立連接關系,
因為在LInux作業系統中只有一個根,所有訪問只能從根下目錄進行,要想使用就只能關聯到根下的目錄,
掛載硬碟需要4步驟,1添加新的硬碟,2創建磁區,3劃分硬碟格式,4掛載
1.既然是掛載當然得添加一塊硬了,打開VMware在要操作的虛擬機上右鍵點擊設定

2.點擊“添加”選擇硬碟,按照指示進行設定

3.添加完成后就會多出一塊硬碟了

4.添加后得在系統下掃描出新添加的硬碟
此命令可以定義為別名,畢竟每次打這么長的命令太麻煩
[19:58:51 root@localhost ~]#echo '- - -' > /sys/class/scsi_host/host0/scan;echo '- - -' > /sys/class/scsi_host/host1/scan;echo '- - -' > /sys/class/scsi_host/host2/scan;
用lsblk命令查看是否識別到新添加硬碟,sdb就是我新添加的硬碟,如果再添加新的硬碟,會以sdc命名以此類推
[19:58:09 root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 4G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 20G 0 disk
sr0 11:0 1 1024M 0 rom
5.磁區,目前用的比較多的兩種磁區模式,MBE和GPT
1)MBR:Master Boot Record,1982年,使用32位表示扇區數,磁區不超過2TMBR:Master Boot Record,1982年,使用32位表示扇區數,磁區不超過2T
2)GPT:GUID(Globals Unique Identifiers) partition table 支持128個磁區,使用64位,支持8Z( 512Byte/block )64Z ( 4096Byte/block) 使用128位UUID(Universally Unique Identifier) 表示磁盤和磁區 GPT磁區表自動備份在頭和尾兩份, 并有CRC校驗位 UEFI (Unified Extensible Firmware Interface 統一可擴展韌體介面)硬體支持GPT,使得作業系統可以啟動
因為我新添加的硬碟只有20G所以在這里我用MBR來做磁區,用''fdisk''工具,首先用"man"命令先查看一下有沒有安裝fdisk工具,如果沒有就安裝一下“yum -y install fdisk",安裝前先查看新添加磁盤的所在位置,一般新添加的硬碟經過掃描添加后會存在“/dev/”下

知道位置后就用fdisk工具進行磁區,工具后跟上檔案路徑與檔案名,進入互動式系統(模式)
[10:43:00 root@localhost ~]#fdisk /dev/sdb
按“m”得到幫助
[10:43:00 root@localhost ~]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x63f29422.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition #創建新磁區
p print the partition table #顯示當前磁區表
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
按“p”顯示當前磁區表
Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x63f29422
按“n”創建新磁區,這里它會問,是要創建主磁區還是擴展磁區,
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
如果沒有特殊要求,就按“p”進行主磁區,這里系統會讓你給硬碟定一個編號,你可以直接給個1-4之中的一個號,也可以直接回車,系統會自動給分為1,
Select (default p): p
Partition number (1-4, default 1):
系統會讓你給指定一個扇區位置的值,在系統提示的值之內輸入有效值,在這里我直接回車,系統會從默認起始位置開始分,
First sector (2048-41943039, default 2048): #也可以自己指定起始位置,不必從2048開始
下一步是讓指定結束扇區位置,與起始同義,我在這里直接回車,接受默認值,也就是全部值,(如果想分1G 那就在此行后面寫上“+1G”)
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):
Created a new partition 1 of type 'Linux' and of size 20 GiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x63f29422
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 41943039 41940992 20G 83 Linux #此處為我新創建出的磁區,sdb1
最后保存退出!“w”
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
使用“lsblk”查看 ,(*如果用舊的硬碟創建的磁區,可能不會直接顯示出新創建的磁區,那么就要先同步磁區表,用“partprobe”進行同步,此命令對centos5、7、8通用,但是centos6有bug,需要用“partx -a /dev/'(加新創建的磁區)”進行同步,我現在用的是新添加的硬碟,所以可以直接顯示 )
[11:23:05 root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 4G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part #這就是剛剛創建好的磁區
sr0 11:0 1 1024M 0 rom
6創建檔案系統
新創建的磁盤還沒有系統檔案,可以理解成沒有格式化,使用“lsblk -f” 可以看到還沒有磁區格式與硬碟的UUID

使用“blkid”也可以查看硬碟UUID,sdb1還沒有UUID的,

創建檔案系統,有 ext2、ext3、ext4、xsf 等儲存格式需要手動創建,
[15:04:54 root@localhost ~]#mkfs.ext4 /dev/sdb1 #mkfs. 后加想創建的檔案系統格式,跟上創建的磁盤路徑,
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 5242624 4k blocks and 1310720 inodes
Filesystem UUID: e9dc87d7-26be-4f78-b33a-d43575a8ad48
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
再次使用“lsblk -f”與“blkid”再次查看檔案系統格式與UUDI就已經有了,

7掛載
[15:27:44 root@localhost ~]#mount /dev/sdb1 /mnt ##使用“mount”命令跟上磁盤所在檔案在跟上想掛載的位置 回車 (*注意空格)
[15:29:53 root@localhost ~]#lsblk ##再次查看磁區
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 4G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part /mnt ##此磁區已經掛載到“mnt”目錄下
sr0 11:0 1 1024M 0 rom
到此,磁盤的添加磁區與掛載就都已完成,希望對您有所幫助,Linux小白,文章中如果有錯誤的地方請在評論區補充^-^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276712.html
標籤:其他
