调整具有 GPT 分区的 OS 磁盘的大小Resize an OS disk that has a GPT partition

备注

此方案仅适用于具有 GUID 分区表 (GPT) 分区的 OS 磁盘。This scenario applies only to OS disks that have a GUID Partition Table (GPT) partition.

本文介绍如何在 Linux 中增加具有 GPT 分区的 OS 磁盘的大小。This article describes how to increase the size of an OS disk that has a GPT partition in Linux.

确定 OS 磁盘是否具有 MBR 或 GPT 分区Identify whether the OS disk has an MBR or GPT partition

使用 parted 命令确定创建的磁盘分区是否具有主启动记录 (MBR) 分区或 GPT 分区。Use the parted command to identify if the disk partition has been created with either a master boot record (MBR) partition or a GPT partition.

MBR 分区MBR partition

在下面的输出中,分区表显示了一个 msdos 值 。In the following output, Partition Table shows a value of msdos. 此值标识 MBR 分区。This value identifies an MBR partition.

[user@myvm ~]# parted -l /dev/sda
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type     File system  Flags
1       1049kB  525MB   524MB   primary  ext4         boot
2       525MB   34.4GB  33.8GB  primary  ext4
[user@myvm ~]#

GPT 分区GPT partition

在下面的输出中,分区表显示了一个 gpt 值 。In the following output, Partition Table shows a value of gpt. 此值标识 GPT 分区。This value identifies a GPT partition.

[user@myvm ~]# parted -l /dev/sda
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 68.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name                  Flags
1       1049kB  525MB   524MB   fat16        EFI System Partition  boot
2       525MB   1050MB  524MB   xfs
3       1050MB  1052MB  2097kB                                     bios_grub
4       1052MB  68.7GB  67.7GB                                     lvm

如果虚拟机 (VM) 在 OS 磁盘上具有 GPT 分区,请增加 OS 磁盘的大小。If your virtual machine (VM) has a GPT partition on your OS disk, increase the size of the OS disk.

增加 OS 磁盘的大小Increase the size of the OS disk

以下说明适用于 Linux 支持的分发版。The following instructions apply to Linux-endorsed distributions.

备注

在继续操作之前,为 VM 创建备份副本,或者拍摄 OS 磁盘的快照。Before you proceed, make a backup copy of your VM, or take a snapshot of your OS disk.

UbuntuUbuntu

在 Ubuntu 16.x 和 18.x 中增加 OS 磁盘的大小:To increase the size of the OS disk in Ubuntu 16.x and 18.x:

  1. 停止 VM。Stop the VM.
  2. 从门户增加 OS 磁盘的大小。Increase the size of the OS disk from the portal.
  3. 重新启动 VM,然后以根用户身份登录到 VM。Restart the VM, and then log in to the VM as a root user.
  4. 验证 OS 磁盘现在是否显示增加后的文件系统大小。Verify that the OS disk now displays an increased file system size.

如以下示例中所示,已通过门户将 OS 磁盘的大小调整为 100 GB。As shown in the following example, the OS disk has been resized from the portal to 100 GB. / 上装载的 /dev/sda1 文件系统现在显示为 97 GB 。The /dev/sda1 file system mounted on / now displays 97 GB.

user@myvm:~# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  314M     0  314M   0% /dev
tmpfs          tmpfs      65M  2.3M   63M   4% /run
/dev/sda1      ext4       97G  1.8G   95G   2% /
tmpfs          tmpfs     324M     0  324M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     324M     0  324M   0% /sys/fs/cgroup
/dev/sda15     vfat      105M  3.6M  101M   4% /boot/efi
/dev/sdb1      ext4       20G   44M   19G   1% /mnt
tmpfs          tmpfs      65M     0   65M   0% /run/user/1000
user@myvm:~#

SUSESUSE

增加 SUSE 12 SP4、SUSE SLES 12 for SAP、SUSE SLES 15 和 SUSE SLES 15 for SAP 中的 OS 磁盘大小:To increase the size of the OS disk in SUSE 12 SP4, SUSE SLES 12 for SAP, SUSE SLES 15, and SUSE SLES 15 for SAP:

  1. 停止 VM。Stop the VM.
  2. 从门户增加 OS 磁盘的大小。Increase the size of the OS disk from the portal.
  3. 重启 VM。Restart the VM.

重新启动 VM 后,请执行以下步骤:When the VM has restarted, perform the following steps:

  1. 通过使用以下命令,以根用户身份访问 VM:Access your VM as a root user by using the following command:

    #sudo su

  2. 使用以下命令安装 gptfdisk 包,增加 OS 磁盘大小需要此包:Use the following command to install the gptfdisk package, which is required for increasing the size of the OS disk:

    #zypper install gptfdisk -y

  3. 若要查看磁盘上可用的最大扇区,请运行以下命令:To view the largest sector available on the disk, run the following command:

    #sgdisk -e /dev/sda

  4. 使用以下命令,在不删除分区的情况下调整其大小。Resize the partition without deleting it by using the following command. Parted 命令具有一个名为“resizepart”的选项,用于在不删除分区的情况下调整其大小 。The parted command has an option named resizepart to resize the partition without deleting it. Resizepart 后面的数字 4 表示调整第四个分区的大小。The number 4 after resizepart indicates resizing the fourth partition.

    #parted -s /dev/sda "resizepart 4 -1" quit

  5. 运行 #lsblk 命令以检查是否已增加分区大小。Run the #lsblk command to check whether the partition has been increased.

    下面的输出显示已将 /dev/sda4 分区的大小调整为 98.5 GB。The following output shows that the /dev/sda4 partition has been resized to 98.5 GB.

    user@myvm:~ # lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  100G  0 disk
    ├─sda1   8:1    0    2M  0 part
    ├─sda2   8:2    0  512M  0 part /boot/efi
    └─sda4   8:4    0 98.5G  0 part /
    sdb      8:16   0   20G  0 disk
    └─sdb1   8:17   0   20G  0 part /mnt/resource
    
  6. 使用以下命令确定 OS 磁盘上的文件系统类型:Identify the type of file system on the OS disk by using the following command:

    blkid

    示例输出:Example output:

    #blkid
    
    user@myvm:~ # blkid
    /dev/sda1: PARTLABEL="p.legacy" PARTUUID="0122fd4c-0069-4a45-bfd4-98b97ccb6e8c"
    /dev/sda2: SEC_TYPE="msdos" LABEL_FATBOOT="EFI" LABEL="EFI" UUID="00A9-D170" TYPE="vfat" PARTLABEL="p.UEFI" PARTUUID="abac3cd8-949b-4e83-81b1-9636493388c7"
    /dev/sda3: LABEL="BOOT" UUID="aa2492db-f9ed-4f5a-822a-1233c06d57cc" TYPE="xfs" PARTLABEL="p.lxboot" PARTUUID="dfb36c61-b15f-4505-8e06-552cf1589cf7"
    /dev/sda4: LABEL="ROOT" UUID="26104965-251c-4e8d-b069-5f5323d2a9ba" TYPE="xfs" PARTLABEL="p.lxroot" PARTUUID="50fecee0-f22b-4406-94c3-622507e2dbce"
    /dev/sdb1: UUID="95239fce-ca97-4f03-a077-4e291588afc9" TYPE="ext4" PARTUUID="953afef3-01"
    
  7. 根据文件系统类型,使用相应的命令调整文件系统的大小。Based on the file system type, use the appropriate commands to resize the file system.

    对于 xfs,使用以下命令:For xfs, use the following command:

    #xfs_growfs /

    示例输出:Example output:

    user@myvm:~ # xfs_growfs /
    meta-data=/dev/sda4              isize=512    agcount=4, agsize=1867583 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1 spinodes=0 rmapbt=0
             =                       reflink=0
    data     =                       bsize=4096   blocks=7470331, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=3647, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 7470331 to 25820172
    

    对于 ext4,使用以下命令:For ext4, use the following command:

    #resize2fs /dev/sda4

  8. 使用以下命令验证增加后的用于 df -Th 的文件系统的大小:Verify the increased file system size for df -Th, by using the following command:

    #df -Th

    示例输出:Example output:

    user@myvm:~ # df -Th
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs  306M  4.0K  306M   1% /dev
    tmpfs          tmpfs     320M     0  320M   0% /dev/shm
    tmpfs          tmpfs     320M  8.8M  311M   3% /run
    tmpfs          tmpfs     320M     0  320M   0% /sys/fs/cgroup
    /dev/sda4      xfs        99G  1.8G   97G   2% /
    /dev/sda3      xfs      1014M   88M  927M   9% /boot
    /dev/sda2      vfat      512M  1.1M  511M   1% /boot/efi
    /dev/sdb1      ext4       20G   45M   19G   1% /mnt/resource
    tmpfs          tmpfs      64M     0   64M   0% /run/user/1000
    user@myvm:~ #
    

在前面的示例中,可以看到 OS 磁盘的文件系统大小已增加。In the preceding example, we can see that the file system size for the OS disk has been increased.

RHELRHEL

在带有 LVM 的 RHEL 7.x 中增加 OS 磁盘的大小:To increase the size of the OS disk in RHEL 7.x with LVM:

  1. 停止 VM。Stop the VM.
  2. 从门户增加 OS 磁盘的大小。Increase the size of the OS disk from the portal.
  3. 启动 VM。Start the VM.

重新启动 VM 后,请执行以下步骤:When the VM has restarted, perform the following steps:

  1. 通过使用以下命令,以根用户身份访问 VM:Access your VM as a root user by using the following command:

    #sudo su

  2. 安装 gptfdisk 包,增加 OS 磁盘大小需要此包。Install the gptfdisk package, which is required to increase the size of the OS disk.

    #yum install gdisk -y

  3. 若要查看磁盘上可用的最大扇区,请运行以下命令:To see the largest sector available on the disk, run the following command:

    #sgdisk -e /dev/sda

  4. 使用以下命令,在不删除分区的情况下调整其大小。Resize the partition without deleting it by using the following command. Parted 命令具有一个名为“resizepart”的选项,用于在不删除分区的情况下调整其大小 。The parted command has an option named resizepart to resize the partition without deleting it. Resizepart 后面的数字 4 表示调整第四个分区的大小。The number 4 after resizepart indicates resizing the fourth partition.

    #parted -s /dev/sda "resizepart 4 -1" quit

  5. 运行以下命令,以验证分区大小是否已增加:Run the following command to verify that the partition has been increased:

    #lsblk

    下面的输出显示已将 /dev/sda4 分区的大小调整为 99 GB。The following output shows that the /dev/sda4 partition has been resized to 99 GB.

    [user@myvm ~]# lsblk
    NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    fd0                 2:0    1    4K  0 disk
    sda                 8:0    0  100G  0 disk
    ├─sda1              8:1    0  500M  0 part /boot/efi
    ├─sda2              8:2    0  500M  0 part /boot
    ├─sda3              8:3    0    2M  0 part
    └─sda4              8:4    0   99G  0 part
    ├─rootvg-tmplv    253:0    0    2G  0 lvm  /tmp
    ├─rootvg-usrlv    253:1    0   10G  0 lvm  /usr
    ├─rootvg-optlv    253:2    0    2G  0 lvm  /opt
    ├─rootvg-homelv   253:3    0    1G  0 lvm  /home
    ├─rootvg-varlv    253:4    0    8G  0 lvm  /var
    └─rootvg-rootlv   253:5    0    2G  0 lvm  /
    sdb                 8:16   0   50G  0 disk
    └─sdb1              8:17   0   50G  0 part /mnt/resource
    
  6. 使用以下命令调整物理卷 (PV) 大小:Use the following command to resize the physical volume (PV):

    #pvresize /dev/sda4

    下面的输出显示已将 PV 的大小调整为 99.02 GB。The following output shows that the PV has been resized to 99.02 GB.

    [user@myvm ~]# pvresize /dev/sda4
    Physical volume "/dev/sda4" changed
    1 physical volume(s) resized or updated / 0 physical volume(s) not resized
    
    [user@myvm ~]# pvs
    PV         VG     Fmt  Attr PSize   PFree
    /dev/sda4  rootvg lvm2 a--  <99.02g <74.02g
    
  7. 在下面的示例中,通过以下命令将 /dev/mapper/rootvg-rootlv 的大小从 2 GB 调整到了 12 GB(增加了 10 GB)。In the following example, /dev/mapper/rootvg-rootlv is being resized from 2 GB to 12 GB (an increase of 10 GB) through the following command. 此命令还将调整文件系统的大小。This command will also resize the file system.

    #lvresize -r -L +10G /dev/mapper/rootvg-rootlv

    示例输出:Example output:

    [user@myvm ~]# lvresize -r -L +10G /dev/mapper/rootvg-rootlv
    Size of logical volume rootvg/rootlv changed from 2.00 GiB (512 extents) to 12.00 GiB (3072 extents).
    Logical volume rootvg/rootlv successfully resized.
    meta-data=/dev/mapper/rootvg-rootlv isize=512    agcount=4, agsize=131072 blks
             =                       sectsz=4096  attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
             =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 524288 to 3145728
    
  8. 使用以下命令验证是否已增加 /dev/mapper/rootvg-rootlv 的文件系统大小:Verify whether /dev/mapper/rootvg-rootlv has an increased file system size by using the following command:

    #df -Th /

    示例输出:Example output:

    [user@myvm ~]# df -Th /
    Filesystem                Type  Size  Used Avail Use% Mounted on
    /dev/mapper/rootvg-rootlv xfs    12G   71M   12G   1% /
    [user@myvm ~]#
    

备注

若要使用相同的过程来调整任何其他逻辑卷的大小,请更改步骤 7 中的 lv 名称。To use the same procedure to resize any other logical volume, change the lv name in step 7.

后续步骤Next steps