你的浏览器不支持canvas

做你害怕做的事情,然后你会发现,不过如此。

linux挂载新磁盘

时间: 作者: 黄运鑫

本文章属原创文章,未经作者许可,禁止转载,复制,下载,以及用作商业用途。原作者保留所有解释权。


查看磁盘

  • 系统版本CentOS Linux 7
  • 执行fdisk -l查看磁盘列表,显示有两个磁盘;/dev/vda是系统盘,/dev/vdb是新的数据盘容量300G并且没有分区
[root@ngd-app1 dev]# fdisk -l

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000a2a8f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   209715166   104856559+  83  Linux

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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

磁盘分区

  • 执行fdisk /dev/vdb进入分区操作
[root@ngd-app1 dev]# fdisk /dev/vdb
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 0x16b691e9.

Command (m for help):
  • 输入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)

  • 输入p查看分区列表,新盘没有分区
Command (m for help): p

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0x16b691e9

   Device Boot      Start         End      Blocks   Id  System

  • 输入n进行分区
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
  • 输入p划分主要分区,再输入分区编号1-4,这里磁盘全部分到1
Select (default p): 
Using default response p
Partition number (1-4, default 1): 1
First sector (2048-629145599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-629145599, default 629145599): 
Using default value 629145599
Partition 1 of type Linux and of size 300 GiB is set
  • 输入w保存
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 执行mkfs.ext4 /dev/vda1格式化磁盘
[root@ngd-app1 dev]# mkfs.ext4 /dev/vdb
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
19660800 inodes, 78643200 blocks
3932160 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2227175424
2400 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
  • 这时再输入fdisk -l查看磁盘列表,可以看到新的分区/dev/vdb1,磁盘分区完成
[root@ngd-app1 dev]# fdisk -l

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000a2a8f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   209715166   104856559+  83  Linux

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0x16b691e9

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   629145599   314571776   83  Linux

挂载磁盘

手动挂载

  • 创建/data目录,执行mount /dev/vdb /data,意思是将磁盘挂载到/data目录,这是临时挂载,服务器重启后就没有了

通过/etc/fstab系统启动后自动挂载

  • 执行ls -l /dev/disk/by-uuid/查看磁盘UUID
[root@ngd-app1 dev]# ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Mar 14 22:41 19da9702-e4b3-4982-859f-691559f17007 -> ../../vda1
lrwxrwxrwx 1 root root  9 Mar 14 19:11 2022-03-14-19-10-47-00 -> ../../sr0
lrwxrwxrwx 1 root root  9 Apr 25 18:54 3cf15910-3bc3-48b0-b66a-4cd32fe30f29 -> ../../vdb
  • /etc/fstab文件中添加一行UUID=3cf15910-3bc3-48b0-b66a-4cd32fe30f29 /data ext4 defaults 1 1
[root@ngd-app1 dev]# vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Mon May 24 14:57:16 2021
#
# 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
#
UUID=19da9702-e4b3-4982-859f-691559f17007 /                       ext4    defaults        1 1
UUID=3cf15910-3bc3-48b0-b66a-4cd32fe30f29 /data                       ext4    defaults        1 1
  • 执行df -h查看是否挂载成功
[root@ngd-app1 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        7.9G     0  7.9G   0% /dev
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           7.9G  1.4M  7.9G   1% /run
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/vda1        99G  7.4G   87G   8% /
tmpfs           1.6G     0  1.6G   0% /run/user/0
tmpfs            60M     0   60M   0% /var/log/rtlog
/dev/vdb        296G   65M  281G   1% /data

对于本文内容有问题或建议的小伙伴,欢迎在文章底部留言交流讨论。