menu E4b9a6's blog
rss_feed
E4b9a6's blog
有善始者实繁,能克终者盖寡。

Linux挂载硬盘

作者:E4b9a6, 创建:2021-06-16, 字数:3723, 已阅:82, 最后更新:2024-03-19

这篇文章更新于 217 天前,文中部分信息可能失效,请自行甄别无效内容。

硬盘挂载步骤

  • 查找所有硬盘硬件信息
  • 选中需要挂载的硬盘进行分区
  • 分区完成后格式化硬盘分区
  • 将挂载硬盘的信息写入系统文件/etc/fstab

下面以格式化一个新增加的1G硬盘为例子

Bash
# 打印所有硬盘硬件信息
❯ sudo fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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

Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 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
Disk label type: dos
Disk identifier: 0x00087949

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM

Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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

Disk /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 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


# 此处要初始化的盘是1G的硬盘,一般通过容量跟分区信息就可以判断出是哪一个硬盘,通过上面的输出信息可以得知是/dev/sdb
❯ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0xd01a716f.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 
Using default value 2097151
Partition 1 of type Linux and of size 1023 MiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


# 硬盘创建分区之后还需要格式化
❯ sudo mkfs.ext4 /dev/sdb1 
sudo mkfs.ext4 /dev/sdb1 
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Ubuntu1804
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

# 将硬盘挂载信息写入硬盘文件/etc/fstab,然后尝试重启,如若重启有问题可以修改/etc/fstab来修复启动
❯ sudo echo '/dev/sdb1 /mnt/sdb1/ ext4 defaults 0 0' >> /etc/fstab
❯ sudo reboot

/etc/fstab文件格式

  • <filesystem> <mountpoint> <type> <opts> <dump> <pass>

格式解释

  • filesystem:挂载设备的位置或者UUID,例如/dev/sdb1,也可以写其唯一的UUID,通过/dev/disk/by-uuid/来确认,相对而言,/dev/sd[x]的位置是不会变化的,使用UUID则是更为保险的方法
  • mountpoint:挂载点,即这个硬盘挂载到哪一个文件夹下
  • type:文件系统类型,Linux支持非常多的文件系统挂载,包括但不限于ext2/ext3/ext4/xfs/ntfs/swap,也可以使用auto来代替手动指定,让系统自动判断文件系统类型
  • opts:这是挂载的时候最重要的参数,涉及到许多硬盘的参数,一般若无特殊要求使用 defauls即可
  • dump:备份命令,需要备份是1,无需备份是0,备份需要联合dump untility使用,较少使用,一般情况下都是使用0
  • pass:在系统启动时使用fsck工具检查扇区,0表示不检查,1表示尽快检查,2表示需要检查但无需过急,对于特殊分区如交换空间是不需要使用检查的

实际例子

  • UUID=96ddc5ee-78f7-43e4-abbd-8359cf73bdfc /mnt/sda/ ext4 defaults 0 0
  • /dev/sdb1 /mnt/sdb1/ ext4 defaults 0 0

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]