主题
Linux如何挂载磁盘
数据一旦丢失恢复成本远高于您的备份操作,数据的拥有者是数据安全的第一责任人,对于数据的操作必须慎之又慎
由于 NTFS 为微软私有协议,目前 linux 支持存在困难,贸然使用可能导致磁盘文件损坏,强烈不建议您在 linux 下使用 NTFS
事前检查
首先检查系统内是否已经识别到需要挂载的磁盘,这里的数据盘为 1T
shell
root@balabala# fdisk -l
Disk /dev/sdb: 1000 GiB, 1073741824000 bytes, 2097152000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xdf29ee9a
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2097151999 2097149952 1000G 83 Linux
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x4f08e586
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 41943006 41940959 20G 83 Linux
可以发现数据盘为 /dev/sdb
,并且有一个分区 /dev/sdb1
磁盘分区、格式化
如果发现您的磁盘没有分区,则需要进行分区后格式化
shell
root@balabala# 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 0x06194404.
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): n #输入n 创建新分区
Command action
e extended
p primary partition (1-4) #这里可以选择是作为扩展分区还是主分区。这里作为主分区,则选择p
p
Partition number (1-4): 1 #做第一块主分区
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +1000G #分区大小为K,M,G。制作分区的大小,这里选择第一块分区大小为500M
Command (m for help): p #输入p可以查看刚才分区的情况
Disk /dev/sdb: 1000 GiB, 1073741824000 bytes, 2097152000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xdf29ee9a
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2097151999 2097149952 83 Linux
Command (m for help): w #这里是将设置写入硬盘
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
确认已经分区成功后进行格式化
shell
root@balabala# fdisk -l
Disk /dev/sdb: 1000 GiB, 1073741824000 bytes, 2097152000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xdf29ee9a
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2097151999 2097149952 1000G 83 Linux
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x4f08e586
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 41943006 41940959 20G 83 Linux
root@balabala# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
......
等待格式化完成后即可进行后续挂载操作
挂载磁盘
直接使用 /dev/sdb
挂载可能会存在开机后盘位变化的情况,这里推荐使用 uuid 进行挂载
所以需要对磁盘的 uuid 进行查询
shell
root@balabala# ls -ll /dev/disk/by-uuid | grep sdb
lrwxrwxrwx 1 root root 10 Jul 5 11:56 d36f0c2e-c229-4ca4-ad69-508d94d3ed3f -> ../../sdb1
可以看到 /dev/sdb
的 uuid 对应为:d36f0c2e-c229-4ca4-ad69-508d94d3ed3f
挂载的目标一定是一个目录,所以我们首先得创建文件夹,通常会把磁盘挂载在 /mnt
路径下
创建好目录后就可以进行挂载操作了
shell
root@balabala# mkdir /mnt/my_data_disk
root@balabala# mount /dev/disk/by-uuid/d36f0c2e-c229-4ca4-ad69-508d94d3ed3f /mnt/my_data_disk
root@balabala# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
`-sda1 8:1 0 20G 0 part /
sdb 8:16 0 1000G 0 disk
`-sdb1 8:17 0 1000G 0 part /mnt/my_data_disk
sr0 11:0 1 428K 0 rom
配置持久化
如果您不想每次开机都手动执行一次 mount
,可以将磁盘挂载写入 fstab
文件,开机时会自动进行挂载
首先检查下文件中的配置
shell
root@balabala# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
/dev/sda1 / ext4 errors=remount-ro 0 1
使用 nano 打开配置文件,并在行尾写入您的配置
text
/dev/disk/by-uuid/d36f0c2e-c229-4ca4-ad69-508d94d3ed3f /mnt/my_data_disk ext4 defaults 0 2
卸载磁盘
您可以使用 umount
对磁盘挂载的路径进行卸载
卸载时请保证该路径没有活跃的读写操作
shell
# umount /mnt/my_data_disk
之后需要对 /etc/fstab
中的自动挂载配置进行注释【注释后的代码不再执行】,以免再次启动系统时出现卡磁盘挂载的问题
text
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
/dev/sda1 / ext4 errors=remount-ro 0 1
# 这是您挂载的数据盘,在行首加上一个#号即可保存退出
#/dev/disk/by-uuid/d36f0c2e-c229-4ca4-ad69-508d94d3ed3f /mnt/my_data_disk ext4 defaults 0 2