使用 Azure CLI 扩展 Linux VM 上的虚拟硬盘Expand virtual hard disks on a Linux VM with the Azure CLI
本文介绍了如何使用 Azure CLI 扩展 Linux 虚拟机 (VM) 的托管磁盘。This article describes how to expand managed disks for a Linux virtual machine (VM) with the Azure CLI. 可通过添加数据磁盘来扩充存储空间,也可扩展现有的数据磁盘。You can add data disks to provide for additional storage space, and you can also expand an existing data disk. 在 Azure 中的 Linux VM 上,操作系统 (OS) 的默认虚拟硬盘大小通常为 30 GB。The default virtual hard disk size for the operating system (OS) is typically 30 GB on a Linux VM in Azure.
警告
始终确保文件系统处于正常状态,磁盘分区表类型将支持新大小,并确保在执行磁盘大小调整操作之前备份数据。Always make sure that your filesystem is in a healthy state, your disk partition table type will support the new size, and ensure your data is backed up before you perform disk resize operations. 有关详细信息,请参阅在 Azure 中备份 Linux VM。For more information, see Back up Linux VMs in Azure.
扩展 Azure 托管磁盘Expand an Azure Managed Disk
确保已安装了最新的 Azure CLI 并已使用 az login 登录到 Azure 帐户。Make sure that you have the latest Azure CLI installed and are signed in to an Azure account by using az login.
本文需要 Azure 中的现有 VM 已附加至少一个数据磁盘并且该磁盘已准备就绪。This article requires an existing VM in Azure with at least one data disk attached and prepared. 如果尚无可用的 VM,请参阅使用数据磁盘创建和准备 VM。If you do not already have a VM that you can use, see Create and prepare a VM with data disks.
备注
请先运行 az cloud set -n AzureChinaCloud
更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud
first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud
。If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud
again.
在以下示例中,请将示例参数名称(例如 myResourceGroup 和 myVM)替换成自己的值。In the following samples, replace example parameter names such as myResourceGroup and myVM with your own values.
当 VM 正在运行时,无法在虚拟硬盘上执行操作。Operations on virtual hard disks can't be performed with the VM running. 使用 az vm deallocate 解除分配 VM。Deallocate your VM with az vm deallocate. 以下示例在名为 myResourceGroup 的资源组中解除分配名为 myVM 的 VM: The following example deallocates the VM named myVM in the resource group named myResourceGroup:
az vm deallocate --resource-group myResourceGroup --name myVM
备注
只有释放 VM 才能扩展虚拟硬盘。The VM must be deallocated to expand the virtual hard disk. 使用
az vm stop
停止 VM 不会释放计算资源。Stopping the VM withaz vm stop
does not release the compute resources. 若要释放计算资源,请使用az vm deallocate
。To release compute resources, useaz vm deallocate
.使用 az disk list 查看资源组中的托管磁盘列表。View a list of managed disks in a resource group with az disk list. 以下示例显示名为 myResourceGroup 的资源组中的托管磁盘列表:The following example displays a list of managed disks in the resource group named myResourceGroup:
az disk list \ --resource-group myResourceGroup \ --query '[*].{Name:name,Gb:diskSizeGb,Tier:accountType}' \ --output table
使用 az disk update 扩展所需磁盘。Expand the required disk with az disk update. 以下示例将名为 myDataDisk 的托管磁盘扩展为 200 GB:The following example expands the managed disk named myDataDisk to 200 GB:
az disk update \ --resource-group myResourceGroup \ --name myDataDisk \ --size-gb 200
备注
扩展托管磁盘时,更新的大小将向上舍入到最接近的托管磁盘大小。When you expand a managed disk, the updated size is rounded up to the nearest managed disk size. 有关可用托管磁盘大小和层的表,请参阅 Azure 托管磁盘概述 - 定价和计费。For a table of the available managed disk sizes and tiers, see Azure Managed Disks Overview - Pricing and Billing.
使用 az vm start 启动 VM。Start your VM with az vm start. 以下示例在名为 myResourceGroup 的资源组中启动名为 myVM 的 VM: The following example starts the VM named myVM in the resource group named myResourceGroup:
az vm start --resource-group myResourceGroup --name myVM
扩展磁盘分区和文件系统Expand a disk partition and filesystem
若要使用扩展的磁盘,请扩展基础分区和文件系统。To use an expanded disk, expand the underlying partition and filesystem.
使用相应的凭据通过 SSH 连接到 VM。SSH to your VM with the appropriate credentials. 可以使用 az vm show 查看 VM 的 公共 IP 地址:You can see the public IP address of your VM with az vm show:
az vm show --resource-group myResourceGroup --name myVM -d --query [publicIps] --output tsv
扩展基础分区和文件系统。Expand the underlying partition and filesystem.
a.a. 如果磁盘已装载,请将其卸载:If the disk is already mounted, unmount it:
sudo umount /dev/sdc1
b.b. 使用
parted
查看磁盘信息并重设分区大小:Useparted
to view disk information and resize the partition:sudo parted /dev/sdc
使用
print
查看有关现有分区布局的信息。View information about the existing partition layout withprint
. 其输出类似于以下示例,该示例显示基础磁盘大小为 215 GB:The output is similar to the following example, which shows the underlying disk is 215 GB:GNU Parted 3.2 Using /dev/sdc1 Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print Model: Unknown Msft Virtual Disk (scsi) Disk /dev/sdc1: 215GB Sector size (logical/physical): 512B/4096B Partition Table: loop Disk Flags: Number Start End Size File system Flags 1 0.00B 107GB 107GB ext4
c.c. 使用
resizepart
展开分区。Expand the partition withresizepart
. 输入分区号 1 以及新分区的大小:Enter the partition number, 1, and a size for the new partition:(parted) resizepart Partition number? 1 End? [107GB]? 215GB
d.d. 若要退出,请输入
quit
。To exit, enterquit
.重设分区大小后,请使用
e2fsck
验证分区一致性:With the partition resized, verify the partition consistency withe2fsck
:sudo e2fsck -f /dev/sdc1
使用
resize2fs
重设文件系统大小:Resize the filesystem withresize2fs
:sudo resize2fs /dev/sdc1
将分区安装到目标位置,例如
/datadrive
:Mount the partition to the desired location, such as/datadrive
:sudo mount /dev/sdc1 /datadrive
若要验证是否已调整数据磁盘的大小,请使用
df -h
。To verify the data disk has been resized, usedf -h
. 以下示例输出显示数据驱动器 /dev/sdc1 现在为 200 GB:The following example output shows the data drive /dev/sdc1 is now 200 GB:Filesystem Size Used Avail Use% Mounted on /dev/sdc1 197G 60M 187G 1% /datadrive
后续步骤Next steps
- 如需更多存储,还可以向 Linux VM 添加数据磁盘。If you need additional storage, you can also add data disks to a Linux VM.
- 有关磁盘加密的详细信息,请参阅适用于 Linux VM 的 Azure 磁盘加密。For more information about disk encryption, see Azure Disk Encryption for Linux VMs.