Linux中的磁盘挂载与取消

2023-12-14 00:00:32

前言

今天准备给一台PVE设备添加一块磁盘,踩了点小坑:磁盘挂载后重启后发现磁盘没有被挂载,于是整理记录一番。

磁盘挂载

查看分区信息

运行fdisk -l命令来查看系统中可用的磁盘分区信息

root@pve:/mnt# fdisk -l
Disk /dev/sda: 59.63 GiB, 64023257088 bytes, 125045424 sectors
Disk model: SanDisk SSD P4 6
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: gpt
Disk identifier: 4CF9B57E-4721-4752-8C97-292DD2E20AC9

Device       Start       End   Sectors  Size Type
/dev/sda1       34      2047      2014 1007K BIOS boot
/dev/sda2     2048   1050623   1048576  512M EFI System
/dev/sda3  1050624 125045390 123994767 59.1G Linux LVM


Disk /dev/mapper/pve-swap: 4 GiB, 4294967296 bytes, 8388608 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/pve-root: 55.12 GiB, 59185823744 bytes, 115597312 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/sdc: 111.79 GiB, 120034123776 bytes, 234441648 sectors
Disk model:  Z700 120GB     
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: 0xc5957d6a

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdc1  *     2048 234440703 234438656 111.8G  7 HPFS/NTFS/exFAT
root@pve:/mnt# 

信息说明:

这里有2个磁盘:sda磁盘有3个分区,sdc磁盘有一个分区

/dev/sda: 
	/dev/sda1
	/dev/sda2
	/dev/sda3

/dev/sdc:
	/dev/sdc1

分区设置

sdc磁盘设备是使用USB连接,使用fdisk /dev/sdc命令进行分区设置

执行命令后进入分区界面,按照提示操作:

输入m:查看帮助信息

输入n:创建一个新分区

输入p:显示磁盘分区表的信息,包括分区号、起始扇区、大小等

输入w:写入保存分区设置

具体执行信息如下:

root@pve:/mnt# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

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

Command (m for help): n
All space for primary partitions is in use.

Command (m for help): p
Disk /dev/sdc: 111.79 GiB, 120034123776 bytes, 234441648 sectors
Disk model:  Z700 120GB     
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: 0xc5957d6a

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdc1  *     2048 234440703 234438656 111.8G  7 HPFS/NTFS/exFAT

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

格式化

将磁盘格式化为ext4格式

mkfs -t ext4 /dev/sdc1

磁盘挂载

创建/mnt/?sdc1目录

mkdir -p /mnt/sdc1

/mnt/?sdc1分区挂载在 /mnt/?sdc1

mount -t ext4 /dev/sdc1 /mnt/sdc1

持久化挂载点配置

在磁盘挂载后重启后发现磁盘没有被挂载,是由于挂载点配置不正确或者没有进行持久化设置导致。

/mnt/?sdc1挂载写入到系统启动项

echo /dev/sdc1 /mnt/sdc1 ext4 defaults 1 2 >> /etc/fstab

检查/etc/fstab配置文件:

/etc/fstab文件用于存储系统启动时要挂载的磁盘信息和挂载点设置

root@pve:/mnt# vi /etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/pve/root / ext4 errors=remount-ro 0 1
UUID=D11C-D304 /boot/efi vfat defaults 0 1
/dev/pve/swap none swap sw 0 0
proc /proc proc defaults 0 0
/dev/sdc1 /mnt/sdc1 ext4 defaults 0 1

取消磁盘挂载

要取消挂载磁盘,可以使用umount命令。

查看已挂载的磁盘

使用以下命令查看当前已挂载的磁盘和对应的挂载点:

root@pve:/mnt# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=1694612k,nr_inodes=423653,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=345664k,mode=755,inode64)
/dev/sda2 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
/dev/sdc1 on /mnt/sdc1 type ext4 (rw,relatime)

取消挂载

使用umount命令取消挂载指定的磁盘。将目标磁盘对应的挂载点作为命令的参数。

root@pve:/mnt# umount /mnt/sdc1

注意:

如果挂载失败,可能是因为磁盘正在被使用,可以尝试关闭使用该磁盘的应用程序或进程,然后再次运行取消挂载命令。

文章来源:https://blog.csdn.net/qq_38628046/article/details/134753766
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。