立足于核心技术的平台研发

教科书式分区挂载空磁盘

场景:磁盘已连接到centos 或 阿里云Linux服务器,并且已经运行,磁盘是未曾使用过的空磁盘。

先看一下文件系统,确认现有文件系统只有/dev/vda1:

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        986M     0  986M   0% /dev
tmpfs           996M     0  996M   0% /dev/shm
tmpfs           996M  472K  996M   1% /run
tmpfs           996M     0  996M   0% /sys/fs/cgroup
/dev/vda1        40G  2.0G   36G   6% /
tmpfs           200M     0  200M   0% /run/user/0

运行如下命令查看分区情况,可以看到磁盘vda只有一个分区vda1,而vdb磁盘没有分区。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c2bef

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886046    41941999+  83  Linux

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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/vdb执行分区命令创建分区,这里是一张20G的虚拟磁盘,只想作为数据盘,

不想分多个区,故输入p创建主分区,输入n创建信分区,输入1创建一个分区,输入

默认的开始扇区号和默认的结束扇区号,输入p查看数据盘规划分区情况,输入w开

始分区,完成后自动退出。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]#
[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# fdisk -u /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 0x92b0be56.

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x92b0be56

Device Boot      Start         End      Blocks   Id  System

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

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x92b0be56

Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

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

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

查看新分区,输出vdb1的相关信息,说明分区创建成功。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# fdisk -lu /dev/vdb

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x92b0be56

Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

上面的一波操作只是创建了当前操作系统下的一个磁盘分区,还需要在新分区

上创建文件系统,也就是常说的磁盘格式化,这里格式为ext4格式,也可以格式

为其他文件格式,比如xfs。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# mkfs -t ext4 /dev/vdb1
mke2fs 1.43.5 (04-Aug-2017)
Creating filesystem with 5242624 4k blocks and 1310720 inodes
Filesystem UUID: dbd30f47-7b9f-45b1-a157-1b45b82977c1
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

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

创建好了文件系统,还需要把当前分区信息写入系统分区表,以便重启时自动挂载分区。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# cp /etc/fstab /etc/fstab.bak
[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Sep  4 09:36:47 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
#
UUID=dcbdbcd3-f78c-4739-8cc7-fd949850da3b /                       ext4    defaults        1 1
~

将新分区写入系统分区表,利用blkid获取新分区的全局唯一标识符UUID,这是分区表/etc/fstab要求

的规范。

[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# echo `blkid /dev/vdb1 | awk ‘{print $2}’ | sed ‘s/\”//g’` /mnt ext4 defaults 0 0 >> /etc/fstab

查看创建的新分区表,验证完成。
[root@iZbsdfrgfdfgfrtbg2ml6otjZ ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Sep  4 09:36:47 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
#
UUID=dcbdbcd3-f78c-4739-8cc7-fd949850da3b /                       ext4    defaults        1 1
UUID=dbd30f47-7b9f-45b1-a157-1b45b82977c1 /mnt ext4 defaults 0 0

上一篇

相关文章

在线留言

*

*

  1. 你好

    你好

    • 兔子

      写的不错。

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

电话:18660802028