Linux之挂载新的硬盘

邰昀
2023-12-01

1.查看磁盘的信息

1.1在命令行输入lsblk (记忆:老师不离开的首字母)

[root@ZH07-TXN-2085 ~]# lsblk -f
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
fd0                                                                      
sda                                                                      
├─sda1          xfs               82d922cb-2c9e-4166-b67c-7a1dc20f91a1   /boot
└─sda2          LVM2_member       WpJ9CZ-d0Eb-4iFO-rkjM-cKlZ-Igxi-JA0Gre 
  ├─centos-root xfs               b27dd445-e537-4c56-8d62-43ca3325ee87   /
  └─centos-swap swap              5d58bc3f-0b7f-4296-b004-f525a8c19af6   [SWAP]
sdb                                                                      
sr0          
  • 其中的sdb就是我们新添加的磁盘,sda就是我们在第一次安装linux操作系统的时候设置的硬盘
  • 磁盘/dev/sda已经分区,并格式化后挂载在了对应的MOUNTPOINT下面

1.2 通过fdisk -l 也可以看到磁盘的信息

[root@ZH07-TXN-2085 ~]# fdisk -l

Disk /dev/sdb: 536.9 GB, 536870912000 bytes, 1048576000 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: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000b03fe

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

Disk /dev/mapper/centos-root: 48.4 GB, 48444211200 bytes, 94617600 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: 4160 MB, 4160749568 bytes, 8126464 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

可以看到磁盘/dev/sda已经分区了

2.对磁盘的进行分区设置

2.1 命令行输入 ​​fdisk /dev/sdb [sdb为你新添磁盘名称]​

[root@ZH07-TXN-2085 ~]# 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 0x15fe3583.

Command (m for help): 
  • 输入n 添加分区,然后输入 p 设置当前分区为主分区。
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
  •  Partition number 代表这是该磁盘的第n个分区,我们这里输入1
  • First sector 代表磁盘的开始扇区
  • Last sector 代表结束扇区
Partition number (1-4, default 1): 1                     //划分为一个分区
First sector (2048-1048575999, default 2048):            //默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999):    //默认
Using default value 1048575999
Partition 1 of type Linux and of size 500 GiB is set
  • 这里我们就划分一个扇区,所以都选默认的
Command (m for help): m            //输入m进去命令选择行
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)

Command (m for help): w                        //保存之前的操作
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 设置完后输入w保存当前的操作

2.2 此时我们再输入lsblk -f 查看当前的磁盘信息。我们可以看到sdb下面的分支上出现了sdb1节点。这说明我们的分区成功了!

3.对分区格式化

3.1 在命令行输入 ​​mkfs.ext4 /dev/​sdb1

[root@ecs-iot-0004 ~]# mkfs.ext4 /dev​/sdb1
mke2fs 1.45.3 (14-Jul-2019)
The file /dev​/vdb1 does not exist and no size was specified.

上面的报错很有可能是打错了,看不出来的编码,如果错了不要复制粘贴,一定要手敲

[root@ZH07-TXN-2085 ~]# mkfs.ext4 /dev/sdb1        //格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768000 inodes, 131071744 blocks
6553587 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2279604224
4000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000

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

输入完上述命令后,就可以完成格式化了

3.格式化完成后,可以通过lsblk -f 查看是否成功!

[root@ZH07-TXN-2085 ~]# lsblk -f 
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
fd0                                                                      
sda                                                                      
├─sda1          xfs               82d922cb-2c9e-4166-b67c-7a1dc20f91a1   /boot
└─sda2          LVM2_member       WpJ9CZ-d0Eb-4iFO-rkjM-cKlZ-Igxi-JA0Gre 
  ├─centos-root xfs               b27dd445-e537-4c56-8d62-43ca3325ee87   /
  └─centos-swap swap              5d58bc3f-0b7f-4296-b004-f525a8c19af6   [SWAP]
sdb                                                                      
└─sdb1          ext4              34f5215e-4b2c-4b27-8a73-8bbb17c6da24   
sr0   

4.挂载磁盘

在linux中,用户不能直接访问硬件设备,需要将硬件设备挂载到系统目录上,硬件才可以让用户访问到。因此我们需要将格式化后的分区挂载到具体的目录下。

4.1.创建一个系统目录,​​mkdir -p /data

mkdir -p /data

4.2 临时挂载

[root@ZH07-TXN-2085 /]# mount /dev/sdb1 /data
[root@ZH07-TXN-2085 /]# lsblk -f
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
fd0                                                                      
sda                                                                      
├─sda1          xfs               82d922cb-2c9e-4166-b67c-7a1dc20f91a1   /boot
└─sda2          LVM2_member       WpJ9CZ-d0Eb-4iFO-rkjM-cKlZ-Igxi-JA0Gre 
  ├─centos-root xfs               b27dd445-e537-4c56-8d62-43ca3325ee87   /
  └─centos-swap swap              5d58bc3f-0b7f-4296-b004-f525a8c19af6   [SWAP]
sdb                                                                      
└─sdb1          ext4              34f5215e-4b2c-4b27-8a73-8bbb17c6da24   /data
sr0       
  • mount /dev/sdb1 /data  将 /dev/sdb1 挂载到了 /data  目录,完成了临时挂载

4.3 永久挂载

永久挂载时需要修改 /etc/fstab 文件,进行永久挂载

$ vi /etc/fstab         # 编辑 fstab 文件
# 在文件中加入下面代码
/dev/sdb1               /data                   ext4     defaults        0 0
[root@ZH07-TXN-2085 /]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Sep 18 10:57:59 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=82d922cb-2c9e-4166-b67c-7a1dc20f91a1 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /data                   ext4     defaults        0 0



[root@ZH07-TXN-2085 /]# mount -a
  • 在修改/etc/fstab文件后,运行mount -a命令验证一下配置是否正确
  • 执行mount -a命令后,用df -h查看会发现磁盘已经挂载成功,说明输入没有错误。
[root@ZH07-TXN-2085 /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G   12M  7.8G   1% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root   46G  1.6G   44G   4% /
/dev/sda1               1014M  149M  866M  15% /boot
tmpfs                    1.6G     0  1.6G   0% /run/user/0
/dev/sdb1                493G   73M  467G   1% /data
  •  下次重启的时候系统就可以自动进行挂载了

总结

        linux磁盘挂载分为安装磁盘,分区,格式化,挂载四个步骤。



mkfs.xfs 命令找不到的解决方法

 对硬盘进行格式化:

[root@ZH07-TXN-2086 ~]# ​​mkfs.ext4 /dev/​sdb1
-bash: ​​mkfs.ext4: command not found

可能是系统不完全安装

运行 which mkfs  查看mkfs的命令目录

[root@ZH07-TXN-2086 ~]# which mkfs
/usr/sbin/mkfs

查看目录 

[root@ZH07-TXN-2086 ~]# ls -la /sbin/mkfs*
-rwxr-xr-x. 1 root root  11520 Feb  3  2021 /sbin/mkfs
-rwxr-xr-x. 1 root root 375240 Aug  7  2017 /sbin/mkfs.btrfs
-rwxr-xr-x. 1 root root  37024 Feb  3  2021 /sbin/mkfs.cramfs
-rwxr-xr-x. 4 root root  96336 Sep 30  2020 /sbin/mkfs.ext2
-rwxr-xr-x. 4 root root  96336 Sep 30  2020 /sbin/mkfs.ext3
-rwxr-xr-x. 4 root root  96336 Sep 30  2020 /sbin/mkfs.ext4
-rwxr-xr-x. 1 root root  28720 Oct 31  2018 /sbin/mkfs.fat
-rwxr-xr-x. 1 root root  37136 Feb  3  2021 /sbin/mkfs.minix
lrwxrwxrwx. 1 root root      8 Jun 27 11:14 /sbin/mkfs.msdos -> mkfs.fat
lrwxrwxrwx. 1 root root      8 Jun 27 11:14 /sbin/mkfs.vfat -> mkfs.fat
-rwxr-xr-x. 1 root root 368424 Aug  9  2019 /sbin/mkfs.xfs

没有 mkfs.xfs需要安装  xfsprogs  rpm包

 #yum -y install xfsprogs

有的话就说明直接不行,需要

[root@ZH07-TXN-2086 ~]# /usr/sbin/mkfs.ext4 /dev/sdb1

 类似资料: