Linux 磁盤分區和掛載


Linux 磁盤分區和掛載

windows 下的分區

查看源圖像

磁盤管理

Linux系統中磁盤管理就是將硬盤通過掛載的方式掛載到Linux文件系統中

相關命令

  • lsblk:用於列出所有可用塊設備的信息,而且還能顯示他們之間的依賴關系數據來源-/sys/dev/block
  • df -h:df可顯示磁盤的文件系統與使用情形,-h是格式化輸出
  • 分區命令:
    • fdisk:分區2TB以下的磁盤,最多可以分4個分區
    • gdisk:分區2TB以上的磁盤,最多可以分128個分區
    • 注.fdisk和gdisk,2TB不是限制,有時候超過2TB分區不穩定所以建議使用gdisk
  • 格式化文件系統:mkfs.xfs
  • 掛載(卸載)命令:mount / umount

分區及掛載實現步驟

  • 添加硬盤(虛擬機編輯設置)
  • 創建分區(fdisk/gdisk)
  • 格式化文件系統(mkfs.xfs)
  • 掛載(mount)

詳細步驟如下操作👇


添加硬盤

1、 打開VMware,選擇編輯虛擬機設置

image

2、選擇添加按鍵

image

3、點擊硬盤,選擇下一步

image

4、選擇SCSI,下一步

補充:Linux硬盤分IDE硬盤和SCSI硬盤,目前基本上都是SCSI硬盤

對於IDE硬盤,驅動標識為“hdx~”,其中“hd”分區所在的設備類型,這里指IDE硬盤了。“x”為盤號(a為基本盤,b為基本從屬盤,c為輔助盤,d為輔助從屬盤),“~”代表分區,前4個分區用數字1~4表示,他們是主分區或擴展分區,從5開始就是邏輯分區。如:hda3表示為第一個IDE硬盤上的第三個主分區或擴展分區

對於SCSI硬盤則標識為“sdx~”,SCSI硬盤是用“sd”來表示分區所在設備的類型的,其余則和IDE硬盤的表示方法一樣

image

5、選擇創建新虛擬硬盤,下一步

image

6、設置磁盤大小

image

7、選擇位置存放

image

添加磁盤完成!

image


通過lsblk查看本機磁盤及分區情況

image


分區步驟

1、創建分區fdisk /dev/sdbgdisk /dev/sdb

  • 補充:磁盤分區命令操作:
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
   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)
# 用啥大家翻譯一下吧,鄙人也得翻譯
# 常用的如下:
	n : 新建一個分區
	p : 打印分區表
	w : 寫入磁盤並退出
	q : 退出
	d : 刪除一個分區

步驟

[root@localhost ~]# fdisk /dev/sdb

Command (m for help): n   # ---添加新分區

Select (default p): p    # ---默認為p,可以不輸入

Partition number (3,4, default 3):   # 默認值為3,因為前面分了兩個了

First sector (314574848-4294967295, default 314574848):   # 起始區,這里默認值就行了

Last sector, +sectors or +size{K,M,G} (314574848-4294967294, default 4294967294): +10G
Partition 3 of type Linux and of size 10 GiB is set           
# 終止分區,自己添加,這里我添加10G作為例子

Command (m for help): w   # ---寫入磁盤並退出
The partition table has been altered!

通過lsblk查看分區情況

image

掛載步驟

1、格式化文件系統

[root@localhost ~]# mkfs.xfs /dev/sdb3
meta-data=/dev/sdb3              isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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

2、掛載

[root@localhost ~]# mount /dev/sdb3 /root/sdb3
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 223M     0  223M   0% /dev
tmpfs                    235M     0  235M   0% /dev/shm
tmpfs                    235M  5.5M  229M   3% /run
tmpfs                    235M     0  235M   0% /sys/fs/cgroup
/dev/mapper/centos-root  100G  3.4G   97G   4% /
/dev/sda1                509M  142M  368M  28% /boot
tmpfs                     47M     0   47M   0% /run/user/0
/dev/sdb3                 10G   33M   10G   1% /root/sdb3

image


3、不想要可以卸載

[root@localhost ~]# umount /dev/sdb3 
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 223M     0  223M   0% /dev
tmpfs                    235M     0  235M   0% /dev/shm
tmpfs                    235M  5.5M  229M   3% /run
tmpfs                    235M     0  235M   0% /sys/fs/cgroup
/dev/mapper/centos-root  100G  3.4G   97G   4% /
/dev/sda1                509M  142M  368M  28% /boot
tmpfs                     47M     0   47M   0% /run/user/0

注:卸載掛載易錯點:

1、卸載的時候不能進入掛載的路徑,也就是說,如果你現在掛載到/root/sdb3里面,但是你進到/root/sdb3里面的時候卸載時也會報錯。

2、卸載光寫卸載的目錄路徑就行,不用寫掛載時的路徑,例如卸載/root/sdb3,光寫umount /root/sdb3


gdisk和fdisk一樣,步驟如下:

[root@localhost ~]# gdisk /dev/sdb
# 分區命令說明
Command (? for help): ?
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu
# 添加新分區
Command (? for help): n

# 這兩步默認就好
Partition number (4-128, default 4): 
First sector (34-8388607966, default = 335546368) or {+-}size{KMGTP}: 

# +30個G
Last sector (335546368-8388607966, default = 8388607966) or {+-}size{KMGTP}: +30G

# 默認回車
Hex code or GUID (L to show codes, Enter = 8300): 
Command (? for help): w   # 寫入
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

image


卸載分區步驟

# 查看
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0  512M  0 part /boot
└─sda2            8:2    0 99.5G  0 part 
  └─centos-root 253:0    0 99.5G  0 lvm  /
sdb               8:16   0  3.9T  0 disk 
├─sdb1            8:17   0  100G  0 part 
├─sdb2            8:18   0   50G  0 part 
├─sdb3            8:19   0   10G  0 part 
└─sdb4            8:20   0   30G  0 part 
sr0              11:0    1  4.4G  0 rom 
# 卸載sdb4,主要內容粘貼如下:
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): d    # ---刪除
Partition number (1-4, default 4): 4   # 刪除sdb4
Command (m for help): w     # 寫入,保存

# 查看
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0  512M  0 part /boot
└─sda2            8:2    0 99.5G  0 part 
  └─centos-root 253:0    0 99.5G  0 lvm  /
sdb               8:16   0  3.9T  0 disk 
├─sdb1            8:17   0  100G  0 part 
├─sdb2            8:18   0   50G  0 part 
└─sdb3            8:19   0   10G  0 part 
sr0              11:0    1  4.4G  0 rom 

# 成功卸載!


補充:

lsblk和df的區別:

  • lsblk 查看的是block device,也就是邏輯磁盤大小。
  • df查看的是file system, 也就是文件系統層的磁盤大小。

永久掛載:

mount是臨時掛載,重啟就沒了

  • 命令 fdisk-l,查看未掛載硬盤

  • 硬盤uuid查看命令:blkid [路徑]

  • 查看到UUID寫入 /etc/fstab文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM