云硬盘 EVS-扩容云硬盘分区和文件系统(Linux):新增MBR分区

时间:2025-03-06 09:15:50

新增MBR分区

示例说明:数据盘“/dev/vdb”原有容量100GiB,只有一个分区“/dev/vdb1”。将数据盘容量扩大至150GiB,本示例使用fdisk工具为新增的50GiB分配新的MBR分区“/dev/vdb2”。

  1. 查看“/dev/vdb”磁盘信息

    1. 查看磁盘分区容量。
      lsblk
      [root@ecs-test-0001 ~]# lsblk
      NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      vda    253:0    0   40G  0 disk
      └vda1 253:1    0   40G  0 part /
      vdb    253:16   0  150G  0 disk
      └vdb1 253:17   0  100G  0 part /mnt/sdc

      磁盘“/dev/vdb”扩容前已有分区“/dev/vdb1”,将数据盘扩容50GiB后,新增的容量还未划分磁盘分区,因此“/dev/vdb”显示150GiB,“/dev/vdb1”显示100GiB。

    1. 查看磁盘分区形式。

      parted /dev/vdb

      p

      [root@ecs-test-0001 ~]# parted /dev/vdb
      GNU Parted 3.1
      Using /dev/vdb
      Welcome to GNU Parted! Type 'help' to view a list of commands.
      (parted) p
      Model: Virtio Block Device (virtblk)
      Disk /dev/vdb: 161GiB
      Sector size (logical/physical): 512B/512B
      Partition Table: msdos
      Disk Flags:
      
      Number  Start   End    Size   File system  Name        Flags
       1      1049kB  107GiB 107GiB ext4         /dev/vdb1
      
      (parted) 

      当前磁盘分区形式为MBR。

      查看完成后,输入“q”,按“Enter”,退出parted模式。

      • “Partition Table:msdos”表示磁盘分区形式为MBR
      • “Partition Table:gpt”表示磁盘分区形式为GPT
      • “Partition Table:loop”表示磁盘未分区(整盘分区),只在设备上创建了文件系统

  2. 为磁盘/dev/vdb使用扩容新增容量创建第二个主分区/dev/vdb2。

    1. 开始新建分区

      fdisk /dev/vdb

      n

      p
      [root@ecs-test-0001 ~]# 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 0x38717fc1.
      
      Command (m for help): n
      Partition type:
         p   primary (0 primary, 0 extended, 4 free)
         e   extended
      Select (default p): p
      Partition number (2-4, default 2): 

    “Partition type”表示磁盘有两种分区类型:“p”表示主分区,“e”表示扩展分区。

    “Partition number”表示主分区编号,可以选择2-4,由于1已被使用,此处从2开始。

    MBR分区个数最多支持4个主分区或3个主分区+1个扩展分区。

    在扩展分区中创建逻辑分区的数量没有限制,可以创建任意多个逻辑分区。如果您需要划分大于4个分区,只能使用主分区+1个扩展分区,然后在这个扩展分区中划分多个逻辑分区。

    1. 输入主分区编号“2”,查看起始磁柱。
      Partition number (2-4, default 2): 2
      First sector (83886080-209715199, default 83886080):

      “First sector”表示起始磁柱值,可以选择83886080-209715199,默认为83886080。

    2. 按“Enter”,使用默认的起始磁柱值;再按“Enter”,使用默认的截止磁柱值。
      First sector (83886080-209715199, default 83886080): 
      using default value 83886080
      Last sector, +sectors or +size{K,M,G} (83886080-209715199, default 209715199):
      using default value 209715199
      Partition 2 of type Linux and of size 40 GB is set
      
      Command (m for help):

      “Last sector”表示截止磁柱值,可以选择83886080-209715199,默认为209715199。

      如果您需要使用扩容新增容量创建两个及以上的分区,分区的起始磁柱值和截止磁柱值计算方法举例如下:

      数据盘/dev/vdb总容量为100 GB,为该数据盘创建第1个主分区/dev/vdb1(40 GB)和第2个主分区/dev/vdb2(60 GB)。

      容量 = sectors值 * 512 bytes,1 GB=1073741824 bytes
      • 数据盘/dev/vdb(100 GB)的sectors值 = 容量 / 512 bytes = 100 * 1073741824 / 512 = 209715200,则该盘截止磁柱值为209715200-1=209715199

        如上图所示:First sector (2048-209715199, default 2048),该盘起始磁柱值为2048,截止磁柱值为209715199

      • 数据盘第1个分区/dev/vdb1(40 GB)的sectors值 = 容量 / 512 bytes = 40 * 1073741824 / 512 = 83886080,则该分区的截止磁柱值为83886080-1=83886079

        该分区的起始磁柱值,使用default值即可2048

      • 数据盘第2个分区/dev/vdb2(60 GB)的sectors值 = 容量 / 512 bytes = 60 * 1073741824 / 512 = 125829120,则该分区的截止磁柱值为125829120-1=125829119

        起始磁柱值 = /dev/vdb1的截止磁柱值 + 1 = 83886079+1 = 83886080

        截止磁柱值 = 起始磁柱值 + sectors - 1 = 83886080+125829120 -1 = 209715199

  3. 查看新建分区大小、分区格式信息。

    1. 输入“p”,按“Enter”,查看新建分区/dev/vdb2的详细信息。
      Command (m for help): p
      
      Disk /dev/vdb: 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: 0x994727e5
      
         Device Boot       Start         End      Blocks   Id  System
      /dev/vdb1             2048    83886079    41942016   83  Linux
      /dev/vdb2         83886080   209715199    62914560   83  Linux
      
      Command (m for help):
    2. 输入“w”,按“Enter”,将分区结果写入分区表中。

      如果之前分区操作有误,请输入“q”,按“Enter”,则会退出fdisk分区工具,之前的分区结果将不会被保留。此时,重新执行创建分区步骤即可。

    3. 将新的分区表变更同步至操作系统。

      partprobe

    如果出现报错“-bash: partprobe: command not found”,则说明系统不识别该命令,需要执行“yum install -y parted”命令来安装该命令。安装成功后再执行上述命令。

    • 如果系统出现以下Error,请输入“Fix”。
      Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating system believes the
      disk is smaller.  Fix, by moving the backup to the end (and removing the old backup)?

      GPT分区表信息存储在磁盘开头,为了减少分区表损坏的风险,同时在磁盘末尾会备份一份。当磁盘容量扩大后,末尾位置也会随之变化,因此需要根据系统提示输入“Fix”,将分区表信息的备份文件挪到新的磁盘末尾位置。

    • 如果系统出现以下Warning,请输入“Fix”。
      Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 104857600
      blocks) or continue with the current setting?
      Fix/Ignore? Fix

      根据系统提示输入“Fix”,系统会自动将磁盘扩容部分的容量设置为GPT。

  1. 为新分区/dev/vdb2创建ext4文件系统。

    mkfs -t ext4 /dev/vdb2

    创建文件系统格式需要等待一段时间,请观察系统运行状态,不要退出。

    [root@ecs-test-0001 ~]# mkfs -t ext4 /dev/vdb2
    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
    2621440 inodes, 10485504 blocks
    524275 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2157969408
    320 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
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    执行“parted /dev/vdb”命令,再输入“p”,查看分区文件系统类型。

    [root@ecs-test-0001 ~]# parted /dev/vdb
    GNU Parted 3.1
    Using /dev/vdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 107GiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:
    
    Number  Start   End      Size     Type     File system  Flags
     1      1049kB  42.9GB   42.9GB   primary  ext4
     2      42.9GB  107GB    64.4GB   primary  ext4            
    
    (parted) q
    [root@ecs-test-0001 ~]#

    查看完成后,输入“q”,按“Enter”,退出parted模式。

    表示新分区“/dev/vdb2”的文件系统类型已设置为“ext4”

  2. 新建目录,并将新建分区挂载至新建目录。

    mkdir -p /mnt/sdd

    mount /dev/vdb2 /mnt/sdd

    lsblk

    查看挂载结果
    [root@ecs-test-0001 ~]# lsblk
    NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    vda    253:0    0  40G  0 disk
    ├vda1 253:1    0  40G  0 part /
    vdb    253:16   0 150G  0 disk
    ├vdb1 253:17   0  100G  0 part /mnt/sdc
    ├vdb2 253:18   0  50G  0 part /mnt/sdd

    表示新分区“/dev/vdb2”已挂载至“/mnt/sdd”

  3. 使用磁盘分区的UUID来设置开机自动挂载磁盘分区。

    • 如果采用在“/etc/fstab”文件中直接指定设备名(比如/dev/vdb1)的方法,会因为云中设备的顺序编码在关闭或者开启云服务器过程中可能发生改变(例如:/dev/vdb1可能会变成/dev/vdb2),可能会导致云服务器重启后不能正常运行。
    • UUID(universally unique identifier)是Linux系统为磁盘分区提供的唯一的标识字符串。
    1. 查询磁盘分区“/dev/vdb2”的UUID

      blkid /dev/vdb2

      [root@ecs-test-0001 ~]# blkid /dev/vdb2
      /dev/vdb2: UUID="0b3040e2-1367-4abb-841d-ddb0b92693df" TYPE="ext4"

      “/dev/vdb2”的UUID为0b3040e2-1367-4abb-841d-ddb0b92693df。

    2. 设置开机自动挂载磁盘分区

      vi /etc/fstab

      按“i”,进入编辑模式,将光标移至文件末尾,按“Enter”,将两个分区的如下信息添加进来。
      UUID=0b3040e2-1367-4abb-841d-ddb0b92693df    /mnt/sdd     ext4    defaults     0    2

      按“ESC”后,输入“:wq”,按“Enter”,保存设置并退出编辑器。

      表2 参数说明

      参数示例

      说明

      UUID=0b3040e2-1367-4abb-841d-ddb0b92693df

      磁盘分区的UUID

      /mnt/sdc

      磁盘分区的挂载目录

      ext4

      磁盘分区的文件系统格式

      defaults

      磁盘分区的挂载选项,此处通常设置为defaults即可

      0

      • Linux dump备份选项。
        • 0表示不使用Linux dump备份。现在通常不使用dump备份,此处设置为0即可。
        • 1表示使用Linux dump备份。

      2

      • fsck选项,即开机时是否使用fsck检查磁盘。
        • 2表示从挂载点为非根目录(/)的分区开始检验。
        • 1表示从挂载点为根目录(/)的分区开始检验。
        • 0表示不检验。

  4. 验证自动挂载功能已生效。

    umount /dev/vdb2

    mount -a

    则系统会将“/etc/fstab”文件所有内容重新加载。

    查询文件系统挂载信息。

    mount | grep /mnt/sdd

    回显类似如下信息,说明自动挂载功能生效:

    root@ecs-test-0001 ~]# mount | grep /mnt/sdd
    /dev/vdb2 on /mnt/sdd type ext4 (rw,relatime,data=ordered)

support.huaweicloud.com/usermanual-evs/evs_01_0109.html