专属企业存储服务-Linux(CentOS)磁盘扩容后处理(fdisk):替换原有分区

时间:2023-11-01 16:13:34

替换原有分区

本操作以该场景为例,裸金属服务器上已挂载一块磁盘,分区“/dev/xvdb1”,挂载目录“/mnt/sdc”,需要替换原有分区“/dev/xvdb1”,将新增容量加到该分区内,此时需要中断业务。

扩容后的新增空间是添加在磁盘末尾的,对具有多个分区的的磁盘扩容时,只支持替换排在末尾的分区。

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

    fdisk -l

    回显类似如下信息:
    [root@ecs-b656 test]# fdisk -lDisk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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 / SolarisDisk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xb00005bd    Device Boot      Start         End      Blocks   Id  System/dev/xvdb1            2048    20971519    10484736   83  Linux

    表示当前数据盘“/dev/xvdb”总容量为21.5 GB,数据盘当前只有一个分区“dev/xvdb1”,该分区的初始磁柱值为2048,截止磁柱值为20971519。

    查看回显中数据盘“/dev/xvdb”的容量,扩容的容量是否已经包含在容量总和中。
    • 若扩容的容量未在数据盘容量总和中,请参考Linux(SLES)磁盘扩容后处理(fdisk)章节刷新系统内容量。
    • 若扩容的容量已在数据盘容量总和中,请记录待替换分区“dev/xvdb1”的初始和截止磁柱值,这些值在后续重新创建分区时需要使用,记录完成后执行2

  2. 执行以下命令,卸载磁盘分区。

    umount /mnt/sdc

  3. 执行以下命令之后,进入fdisk分区工具,并输入“d”,删除原来的分区“/dev/xvdb1”

    fdisk /dev/xvdb

    屏幕回显如下:
    [root@ecs-b656 test]# fdisk /dev/xvdbWelcome 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): dSelected partition 1Partition 1 is deletedCommand (m for help):

    删除分区后,请参考以下操作步骤替换原有分区,则不会导致数据盘内数据的丢失。

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

    输入“n”表示新增一个分区。

    回显类似如下信息:
    Command (m for help): nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extended
    表示磁盘有两种分区类型:
    • “p”表示主要分区。
    • “e”表示延伸分区。

  5. 此处分区类型需要与原分区保持一致,以原分区类型是主要分区为例,输入“p”,按“Enter”,开始重新创建一个主分区。

    回显类似如下信息
    Select (default p): pPartition number (1-4, default 1):

    “Partition number”表示主分区编号。

  6. 此处分区编号需要与原分区保持一致,以原分区编号是“1”为例,输入分区编号“1”,按“Enter”。

    回显类似如下信息:
    Partition number (1-4, default 1): 1First sector (2048-41943039, default 2048):

    “First sector”表示初始磁柱值。

    以下操作会导致数据丢失:
    • 选择的初始磁柱值与原分区的不一致。
    • 选择的截止磁柱值小于原分区的值。

  7. 此处必须与原分区保持一致,以1中记录的初始磁柱值2048为例,按“Enter”。

    回显类似如下信息:
    First sector (2048-41943039, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):

    “Last sector”表示截止磁柱值。

  8. 此处截止磁柱值应大于等于1中记录的截止磁柱值20971519,以选择默认截止磁柱值41943039为例,按“Enter”。

    回显类似如下信息:
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):Using default value 41943039Partition 1 of type Linux and of size 20 GiB is setCommand (m for help):

    表示分区完成。

  9. 输入“p”,按“Enter”,查看新建分区的详细信息。

    回显类似如下信息:
    Command (m for help): pDisk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xb00005bd    Device Boot      Start         End      Blocks   Id  System/dev/xvdb1            2048    41943039    20970496   83  LinuxCommand (m for help): 

    表示新建分区“/dev/xvdb1”的详细信息。

  10. 输入“w”,按“Enter”,将分区结果写入分区表中。

    回显类似如下信息:
    Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

    表示分区创建完成。

    如果之前分区操作有误,请输入“q”,则会退出fdisk分区工具,之前的分区结果将不会被保留。

  11. 执行以下命令,检查“/dev/xvdb1”文件系统的正确性。

    e2fsck -f /dev/xvdb1

    回显类似如下信息:
    [root@ecs-b656 test]# e2fsck -f /dev/xvdb1e2fsck 1.42.9 (28-Dec-2013)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/xvdb1: 11/655360 files (0.0% non-contiguous), 83137/2621184 blocks

  12. 执行以下命令,扩展“/dev/xvdb1”文件系统的大小。

    resize2fs /dev/xvdb1

    回显类似如下信息:
    [root@ecs-b656 test]# resize2fs /dev/xvdb1resize2fs 1.42.9 (28-Dec-2013)Resizing the filesystem on /dev/xvdb1 to 5242624 (4k) blocks.The filesystem on /dev/xvdb1 is now 5242624 blocks long.

  13. 执行以下命令,将新建分区挂载到“/mnt/sdc”目录下。

    mount /dev/xvdb1 /mnt/sdc

  14. 执行以下命令,查看“/dev/xvdb2”分区挂载结果。

    df -TH

support.huaweicloud.com/usermanual-dess/hw_ppeo_linux_fdisk.html