azure linux 挂载数据磁盘,使用 Azure CLI 将数据磁盘添加到 Linux VM - Azure Virtual Machines | Microsoft Docs...

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

将磁盘添加到 Linux VMAdd a disk to a Linux VM

08/20/2020

本文内容

本文介绍了如何将持久性磁盘附加到 VM 以便持久保存数据 - 即使 VM 由于维护或调整大小而重新预配。This article shows you how to attach a persistent disk to your VM so that you can preserve your data - even if your VM is reprovisioned due to maintenance or resizing.

将新磁盘附加到 VMAttach a new disk to a VM

如果只需要在 VM 上添加新的空数据磁盘,请使用 az vm disk attach 命令以及 --new 参数。If you want to add a new, empty data disk on your VM, use the az vm disk attach command with the --new parameter. 如果 VM 位于某个可用性区域中,则会自动在与 VM 相同的区域中创建磁盘。If your VM is in an Availability Zone, the disk is automatically created in the same zone as the VM. 有关详细信息,请参阅可用性区域概述。以下示例创建一个名为“myDataDisk”且大小为 50 GB 的磁盘:The following example creates a disk named myDataDisk that is 50 Gb in size:

az vm disk attach \

-g myResourceGroup \

--vm-name myVM \

--name myDataDisk \

--new \

--size-gb 50

附加现有磁盘Attach an existing disk

若要附加现有磁盘,请查找磁盘 ID 并将该 ID 传递到 az vm disk attach 命令。To attach an existing disk, find the disk ID and pass the ID to the az vm disk attach command. 以下示例查询 myResourceGroup 中名为 myDataDisk 的磁盘,然后将其附加到名为 myVM 的 VM:The following example queries for a disk named myDataDisk in myResourceGroup, then attaches it to the VM named myVM:

diskId=$(az disk show -g myResourceGroup -n myDataDisk --query 'id' -o tsv)

az vm disk attach -g myResourceGroup --vm-name myVM --name $diskId

格式化磁盘和装载磁盘Format and mount the disk

若要对新磁盘进行分区、格式化和装载,以便 Linux VM 可以使用它,请通过 SSH 登录到 VM。To partition, format, and mount your new disk so your Linux VM can use it, SSH into your VM. 以下示例使用用户名“azureuser”连接到使用公共 IP 地址 10.123.123.25 的 VM :The following example connects to a VM with the public IP address of 10.123.123.25 with the username azureuser:

ssh azureuser@10.123.123.25

找到磁盘Find the disk

连接到 VM 后,需要找到该磁盘。Once connected to your VM, you need to find the disk. 在此示例中,我们使用 lsblk 来列出磁盘。In this example, we are using lsblk to list the disks.

lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd"

输出类似于以下示例:The output is similar to the following example:

sda 0:0:0:0 30G

├─sda1 29.9G /

├─sda14 4M

└─sda15 106M /boot/efi

sdb 1:0:1:0 14G

└─sdb1 14G /mnt

sdc 3:0:0:0 50G

这里的 sdc 是我们所需的磁盘,因为它是 50G。Here, sdc is the disk that we want, because it is 50G. 如果只根据大小无法确定是哪块磁盘,可转到门户中的 VM 页面,选择“磁盘”,然后在“数据磁盘”下检查磁盘的 LUN 编号 。If you aren't sure which disk it is based on size alone, you can go to the VM page in the portal, select Disks, and check the LUN number for the disk under Data disks.

格式化磁盘Format the disk

请使用 parted 对磁盘进行格式化,如果磁盘大小大于等于 2TiB,必须使用 GPT 分区,如果小于 2TiB,则可以使用 MBR 或 GPT 分区。Format the disk with parted, if the disk size is 2 tebibytes (TiB) or larger then you must use GPT partitioning, if it is under 2TiB, then you can use either MBR or GPT partitioning.

备注

建议使用适用于你的发行版的最新版 parted。It is recommended that you use the latest version parted that is available for your distro.

如果磁盘大于或等于 2 TiB,必须使用 GPT 分区。If the disk size is 2 tebibytes (TiB) or larger, you must use GPT partitioning. 如果磁盘小于 2 TiB,则可以使用 MBR 或 GPT 分区。If disk size is under 2 TiB, then you can use either MBR or GPT partitioning.

以下示例在 /dev/sdc 上使用 parted,那里是大多数 VM 上第一块数据磁盘通常所在的位置。The following example uses parted on /dev/sdc, which is where the first data disk will typically be on most VMs. 将 sdc 替换为磁盘的正确选项。Replace sdc with the correct option for your disk. 我们还使用 XFS 文件系统对其进行格式设置。We are also formatting it using the XFS filesystem.

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%

sudo mkfs.xfs /dev/sdc1

sudo partprobe /dev/sdc1

请使用 partprobe 实用程序以确保内核知晓新分区和文件系统。Use the partprobe utility to make sure the kernel is aware of the new partition and filesystem. 如果无法使用 partprobe,则可能导致 blkid 或 lslbk 命令不立即返回新文件系统的 UUID。Failure to use partprobe can cause the blkid or lslbk commands to not return the UUID for the new filesystem immediately.

装载磁盘Mount the disk

现在,使用 mkdir 创建一个目录来装载文件系统。Now, create a directory to mount the file system using mkdir. 以下示例在 /datadrive 处创建一个目录:The following example creates a directory at /datadrive:

sudo mkdir /datadrive

然后,使用 mount 来装载文件系统。Use mount to then mount the filesystem. 以下示例将 /dev/sdc1 分区装载到 /datadrive 装入点:The following example mounts the /dev/sdc1 partition to the /datadrive mount point:

sudo mount /dev/sdc1 /datadrive

持久保留装载Persist the mount

若要确保在重新引导后自动重新装载驱动器,必须将其添加到 /etc/fstab 文件。To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file. 强烈建议在 /etc/fstab 中使用 UUID(全局唯一标识符)来引用驱动器而不是只使用设备名称(例如 //dev/sdc1) 。It is also highly recommended that the UUID (Universally Unique Identifier) is used in /etc/fstab to refer to the drive rather than just the device name (such as, /dev/sdc1). 如果 OS 在启动过程中检测到磁盘错误,使用 UUID 可以避免将错误的磁盘装载到给定位置。If the OS detects a disk error during boot, using the UUID avoids the incorrect disk being mounted to a given location. 然后为剩余的数据磁盘分配这些设备 ID。Remaining data disks would then be assigned those same device IDs. 若要查找新驱动器的 UUID,请使用 blkid 实用工具:To find the UUID of the new drive, use the blkid utility:

sudo blkid

输出与以下示例类似:The output looks similar to the following example:

/dev/sda1: LABEL="cloudimg-rootfs" UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4" PARTUUID="1a1b1c1d-11aa-1234-1a1a1a1a1a1a"

/dev/sda15: LABEL="UEFI" UUID="BCD7-96A6" TYPE="vfat" PARTUUID="1e1g1cg1h-11aa-1234-1u1u1a1a1u1u"

/dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4" TYPE="ext4" PARTUUID="1a2b3c4d-01"

/dev/sda14: PARTUUID="2e2g2cg2h-11aa-1234-1u1u1a1a1u1u"

/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="xfs" PARTLABEL="xfspart" PARTUUID="c1c2c3c4-1234-cdef-asdf3456ghjk"

备注

错误地编辑 /etc/fstab 文件可能会导致系统无法引导。Improperly editing the /etc/fstab file could result in an unbootable system. 如果没有把握,请参考分发的文档来获取有关如何正确编辑该文件的信息。If unsure, refer to the distribution's documentation for information on how to properly edit this file. 另外,建议在编辑前备份 /etc/fstab 文件。It is also recommended that a backup of the /etc/fstab file is created before editing.

接下来,在文本编辑器中打开 /etc/fstab 文件,如下所示:Next, open the /etc/fstab file in a text editor as follows:

sudo nano /etc/fstab

在此示例中,使用在之前的步骤中创建的 /dev/sdc1 设备的 UUID 值并使用 /datadrive 装入点。In this example, use the UUID value for the /dev/sdc1 device that was created in the previous steps, and the mountpoint of /datadrive. 请将以下行添加到 /etc/fstab文件的末尾:Add the following line to the end of the /etc/fstab file:

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,nofail 1 2

此示例中使用的是 nano 编辑器,所以在编辑完文件后,请使用 Ctrl+O 写入文件,然后使用 Ctrl+X 退出编辑器。In this example, we are using the nano editor, so when you are done editing the file, use Ctrl+O to write the file and Ctrl+X to exit the editor.

备注

之后,在不编辑 fstab 的情况下删除数据磁盘可能会导致 VM 无法启动。Later removing a data disk without editing fstab could cause the VM to fail to boot. 大多数分发版都提供 nofail 和/或 nobootwait fstab 选项。Most distributions provide either the nofail and/or nobootwait fstab options. 这些选项使系统在磁盘无法装载的情况下也能启动。These options allow a system to boot even if the disk fails to mount at boot time. 有关这些参数的详细信息,请查阅分发文档。Consult your distribution's documentation for more information on these parameters.

即使文件系统已损坏或磁盘在引导时不存在, nofail 选项也能确保 VM 启动。The nofail option ensures that the VM starts even if the filesystem is corrupt or the disk does not exist at boot time. Without this option, you may encounter behavior as described in Cannot SSH to Linux VM due to FSTAB errors

如果修改 fstab 导致启动失败,可以使用 Azure VM 串行控制台对 VM 进行控制台访问。The Azure VM Serial Console can be used for console access to your VM if modifying fstab has resulted in a boot failure. 有关更多详细信息,请参阅串行控制台文档。More details are available in the Serial Console documentation.

Azure 中对 Linux 的 TRIM/UNMAP 支持TRIM/UNMAP support for Linux in Azure

某些 Linux 内核支持 TRIM/UNMAP 操作以放弃磁盘上未使用的块。Some Linux kernels support TRIM/UNMAP operations to discard unused blocks on the disk. 此功能主要用于标准存储中,如果你创建大型文件后又将其删除,则该功能将通知 Azure 已删除的页不再有效并且可以丢弃,可以节省成本。This feature is primarily useful in standard storage to inform Azure that deleted pages are no longer valid and can be discarded, and can save money if you create large files and then delete them.

在 Linux VM 中有两种方法可以启用 TRIM 支持。There are two ways to enable TRIM support in your Linux VM. 与往常一样,有关建议的方法,请参阅分发:As usual, consult your distribution for the recommended approach:

在 /etc/fstab 中使用 discard 装载选项,例如:Use the discard mount option in /etc/fstab, for example:

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,discard 1 2

在某些情况下,discard 选项可能会影响性能。In some cases, the discard option may have performance implications. 此处,还可以从命令行手动运行 fstrim 命令,或将其添加到 crontab 以定期运行:Alternatively, you can run the fstrim command manually from the command line, or add it to your crontab to run regularly:

UbuntuUbuntu

sudo apt-get install util-linux

sudo fstrim /datadrive

RHEL/CentOSRHEL/CentOS

sudo yum install util-linux

sudo fstrim /datadrive

故障排除Troubleshooting

将数据磁盘添加到 Linux VM 时,如果 LUN 0 位置没有磁盘,则你可能会遇到错误。When adding data disks to a Linux VM, you may encounter errors if a disk does not exist at LUN 0. 如果使用 az vm disk attach -new 命令并指定 LUN (--lun) 来手动添加磁盘,而不是让 Azure 平台确定适当的 LUN,则请注意,LUN 0 已经有磁盘或者将有磁盘。If you are adding a disk manually using the az vm disk attach -new command and you specify a LUN (--lun) rather than allowing the Azure platform to determine the appropriate LUN, take care that a disk already exists / will exist at LUN 0.

请考虑以下示例,其中显示了 lsscsi 输出的代码片段:Consider the following example showing a snippet of the output from lsscsi:

[5:0:0:0] disk Msft Virtual Disk 1.0 /dev/sdc

[5:0:0:1] disk Msft Virtual Disk 1.0 /dev/sdd

两个数据磁盘位于 LUN 0 和 LUN 1(lsscsi 中的第一列输出了详细信息 [host:channel:target:lun])。The two data disks exist at LUN 0 and LUN 1 (the first column in the lsscsi output details [host:channel:target:lun]). 两个磁盘都应该可从 VM 内部访问。Both disks should be accessible from within the VM. 如果手动指定了要在 LUN 1 位置添加第一个磁盘并在 LUN 2 位置添加第二个磁盘,则可能无法从 VM 内部正常查看这些磁盘。If you had manually specified the first disk to be added at LUN 1 and the second disk at LUN 2, you may not see the disks correctly from within your VM.

备注

在这些示例中,Azure host 值为 5,但此值可能根据所选存储类型的不同而异。The Azure host value is 5 in these examples, but this may vary depending on the type of storage you select.

此磁盘行为不是 Azure 的问题,而是因为 Linux 内核遵循了 SCSI 规范。This disk behavior is not an Azure problem, but the way in which the Linux kernel follows the SCSI specifications. 当 Linux 内核在 SCSI 总线中扫描附加的设备时,必须能够在 LUN 0 位置找到设备,系统才能继续扫描是否有其他设备。When the Linux kernel scans the SCSI bus for attached devices, a device must be found at LUN 0 in order for the system to continue scanning for additional devices. 因此:As such:

在添加数据磁盘之后,请查看 lsscsi 的输出,验证 LUN 0 位置是否有磁盘。Review the output of lsscsi after adding a data disk to verify that you have a disk at LUN 0.

如果磁盘未在 VM 内正确显示,请验证 LUN 0 位置是否有磁盘。If your disk does not show up correctly within your VM, verify a disk exists at LUN 0.

后续步骤Next steps

为确保正确配置 Linux VM,请查看有关优化 Linux 计算机性能的建议。To ensure your Linux VM is configured correctly, review the Optimize your Linux machine performance recommendations.

可以添加更多的磁盘来扩展存储容量,配置 RAID 来提高性能。Expand your storage capacity by adding additional disks and configure RAID for additional performance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值