当前位置:主页 > 查看内容

Linux磁盘分区挂载

发布时间:2021-07-13 00:00| 位朋友查看

简介:我是一个小白学习Linux也有一段时间了一篇文章也没发表过今天刚刚学到磁盘挂载就想着写一篇吧 挂载就是把当前设备硬盘与当前根下的某一个文件夹建立连接关系。 因为在LInux操作系统中只有一个根所有访问只能从根下目录进行要想使用就只能关联到根下的目录。……

我是一个小白,学习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://blog.csdn.net/qq_52164327/article/details/115707466
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:【Linux 4】定时任务调度与进程服务管理 下一篇:没有了

推荐图文


随机推荐