Linux扩容-新增磁盘分区挂载-fdisk

Linux扩容-新增磁盘分区挂载-fdisk

扩容后的云硬盘,挂载给实例后,需要对扩容的部分划分分区并初始化。本节以“CentOS 7.0 64位”操作系统为例,介绍使用fdisk工具为扩容后的磁盘创建新的分区。

一 背景信息

扩容成功后,需要为扩容部分的容量创建新的分区,或者重新创建分区替换原有分区。

  • 为扩容部分的容量创建新的分区

该场景下,不需要卸载原有的分区,而是在原有分区的基础上新增分区。该操作不会中断业务,对业务影响较小。推荐系统盘或者需要保证业务不中断的磁盘扩容场景使用。如果当前磁盘使用的是MBR格式,则要求扩容后的磁盘容量不超过2TB,并且磁盘的分区数量还未达到上限。

  • 重新创建分区替换原有分区

如果当前磁盘使用的是MBR分区,且磁盘分区数量已达到上限,无法再增加新的分区。此时需要先卸载已有分区,重新创建新的分区替换原有分区。该操作不会删除原有分区的数据,但操作过程中需预先中断业务,因此会对用户正在运行的业务产生影响。

  • MBR分区转换为GPT分区

如果当前磁盘使用的是MBR分区,且扩容后磁盘容量已超过2TB,则此时需要将MBR分区转换为GPT分区,该操作会清除磁盘上的数据,请您预先对原数据进行备份。

二 挂载操作步骤

以下步骤,举例说明如何将磁盘扩容部分的空间划分为一个新的分区,并挂载到“/opt”下。

该磁盘采用MBR分区,磁盘容量小于2TB,且分区数量未达到上限。分区方式为MBR时,可以使用fdisk工具或parted工具划分分区,本节以fdisk工具为例。

  1. 执行以下命令,查看磁盘信息。

fdisk -l

回显类似如下信息,“/dev/xvda”表示系统盘。

[root@ecs-bab9 test]# fdisk -l

 

Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad

 

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1   *        2048     2050047     1024000   83  Linux

/dev/xvda2         2050048    22530047    10240000   83  Linux

/dev/xvda3        22530048    24578047     1024000   83  Linux

/dev/xvda4        24578048    83886079    29654016    5  Extended

/dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris

  1. 执行以下命令,进入fdisk模式,以“/dev/xvda”为例。

fdisk /dev/xvda

回显类似如下信息:

[root@ecs-bab9 test]# fdisk /dev/xvda

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.

 

Command (m for help):

  1. 输入“n”,按“Enter”,开始新建分区。

本例中由于系统盘原来已经有5个分区,所以系统自动添加第6分区。

回显类似如下信息:

Command (m for help): n

All primary partitions are in use

Adding logical partition 6

First sector (26630144-83886079, default 26630144):

  1. 输入新分区的起始磁柱编号,如设置默认值,按“Enter”。

起始磁柱编号必须大于原有分区的结束磁柱编号。

回显类似如下信息:

First sector (26630144-83886079, default 26630144):

Using default value 26630144

Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079):

  1. 输入新分区的截止磁柱编号,按“Enter”。

本步骤中使用默认截止磁柱编号为例。

回显类似如下信息:

Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079):

Using default value 83886079

Partition 6 of type Linux and of size 27.3 GiB is set

 

Command (m for help):

  1. 输入“p”,按“Enter”,查看新建分区。

回显类似如下信息:

Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad

 

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1   *        2048     2050047     1024000   83  Linux

/dev/xvda2         2050048    22530047    10240000   83  Linux

/dev/xvda3        22530048    24578047     1024000   83  Linux

/dev/xvda4        24578048    83886079    29654016    5  Extended

/dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris

/dev/xvda6        26630144    83886079    28627968   83  Linux

 

Command (m for help):

  1. 输入“w”,按“Enter”。将分区结果写入分区表中,分区创建完毕。

回显类似如下信息:

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

  1. 执行partprobe命令,将新的分区表变更同步至操作系统。

partprobe

 

  1. 执行以下命令,设置新建分区文件系统格式(df -T查看原有文件系统 )。

以“ext4” 文件格式为例:

mkfs -t ext4 /dev/xvda6

回显类似如下信息:

[root@ecs-bab9 test]# mkfs -t ext4 /dev/xvda6

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

1790544 inodes, 7156992 blocks

357849 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2155872256

219 block groups

32768 blocks per group, 32768 fragments per group

8176 inodes per group

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

格式化需要等待一段时间,请观察系统运行状态,若回显中进程提示为done,则表示格式化完成。

  1. 执行以下命令,将新建分区挂载到需要增加空间的目录下,以“/opt”为例。

mount /dev/xvda6 /opt

回显类似如下信息:

[root@ecs-bab9 test]# mount /dev/xvda6 /opt

[root@ecs-bab9 test]#

 说明:

新增加的分区挂载到不为空的目录时,该目录下原本的子目录和文件会被隐藏,所以,新增的分区最好挂载到空目录或者新建目录。如确实要挂载到不为空的目录,可将该目录下的子目录和文件临时移动到其他目录下,新分区挂载成功后,再将子目录和文件移动回来。

  1. 执行以下命令,查看挂载结果。

df -TH

回显类似如下信息:

[root@ecs-bab9 test]# df -TH

Filesystem     Type      Size  Used Avail Use% Mounted on

/dev/xvda2     xfs        11G  7.4G  3.2G  71% /

devtmpfs       devtmpfs  4.1G     0  4.1G   0% /dev

tmpfs          tmpfs     4.1G   82k  4.1G   1% /dev/shm

tmpfs          tmpfs     4.1G  9.2M  4.1G   1% /run

tmpfs          tmpfs     4.1G     0  4.1G   0% /sys/fs/cgroup

/dev/xvda3     xfs       1.1G   39M  1.1G   4% /home

/dev/xvda1     xfs       1.1G  131M  915M  13% /boot

/dev/xvda6     ext4       29G   47M   28G   1% /opt

三 设置开机自动挂载磁盘

如果您需要在实例启动时自动挂载磁盘,请在上述操作之后参考本节设置开机自动挂载磁盘。设置时不能采用在 /etc/fstab直接指定 /dev/xvdb1的方法,因为实例中设备的顺序编码在关闭或者开启实例过程中可能发生改变。推荐使用UUID来配置自动挂载数据盘。

 说明:

磁盘的UUID(Universally Unique Identifier)是Linux系统为存储设备提供的唯一的标识字符串。

  1. 执行如下命令,查询磁盘分区的UUID。

blkid 磁盘分区

以查询磁盘分区“/dev/xvdb1”的UUID为例:

blkid /dev/xvdb1

回显类似如下信息,表示“/dev/xvdb1”的UUID。

[root@ecs-b656 test]# blkid /dev/xvdb1

/dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

  1. 执行以下命令,使用VI编辑器打开“fstab”文件。

vi /etc/fstab

  1. 按“i”,进入编辑模式。
  2. 将光标移至文件末尾,按“Enter”,添加如下内容(UUID)。

UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults     0   2

  1. 按“Esc”后,输入“:wq”,按“Enter”。

             保存设置并退出编辑器。

 

posted @ 2021-08-26 16:01  风飘雪落花亦起  阅读(1410)  评论(0编辑  收藏  举报