linux vm 磁盘,将数据磁盘附加到 Linux VM - Azure Virtual Machines | Microsoft Docs

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

使用门户将数据磁盘附加到 Linux VMUse the portal to attach a data disk to a Linux VM

08/28/2020

本文内容

本文介绍如何通过 Azure 门户将新磁盘和现有磁盘附加到 Linux 虚拟机。This article shows you how to attach both new and existing disks to a Linux virtual machine through the Azure portal.

将磁盘附加到 VM 前,请查看以下提示:Before you attach disks to your VM, review these tips:

虚拟机的大小决定了可以附加多少个磁盘。The size of the virtual machine controls how many data disks you can attach. 有关详细信息,请参阅虚拟机大小。

附加到虚拟机的磁盘实际上是存储在 Azure 中的 .vhd 文件。Disks attached to virtual machines are actually .vhd files stored in Azure. 有关详细信息,请查看托管磁盘简介。

查找虚拟机Find the virtual machine

转到 Azure 门户以找到 VM。Go to the Azure portal to find the VM. 搜索并选择“虚拟机”。Search for and select Virtual machines.

从列表中选择 VM。Choose the VM from the list.

在“虚拟机”页面的“设置”下,选择“磁盘”。In the Virtual machines page, under Settings, choose Disks.

附加新磁盘Attach a new disk

在“磁盘”窗格的“数据磁盘”下,选择“创建并附加新磁盘” 。On the Disks pane, under Data disks, select Create and attach a new disk.

输入托管磁盘的名称。Enter a name for your managed disk. 查看默认设置,并根据需要更新“存储类型”、“大小(GiB)”、“加密”和“主机缓存” 。Review the default settings, and update the Storage type, Size (GiB), Encryption and Host caching as necessary.

034e7ad5ad68f5cc8419196d54c1bb9f.png

完成后,选择页面顶部的“保存”,以创建托管磁盘并更新 VM 配置。When you are done, select Save at the top of the page to create the managed disk and update the VM configuration.

附加现有磁盘Attach an existing disk

在“磁盘”窗格的“数据磁盘”下,选择“附加现有磁盘” 。On the Disks pane, under Data disks, select Attach existing disks.

单击“磁盘名称”的下拉菜单,然后从可用托管磁盘列表中选择一个磁盘。Click the drop-down menu for Disk name and select a disk from the list of available managed disks.

单击“保存”以附加现有托管磁盘并更新 VM 配置:Click Save to attach the existing managed disk and update the VM configuration:

连接到 Linux VM 以装入新磁盘Connect to the Linux VM to mount the new 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 4G

在此示例中,添加的磁盘是 sdc。In this example, the disk that I added is sdc. 它的 LUN 为 0,大小为 4 GB。It is a LUN 0 and is 4GB.

对于更复杂的示例,多个数据磁盘在门户中显示如下:For a more complex example, here is what multiple data disks looks like in the portal:

034e7ad5ad68f5cc8419196d54c1bb9f.png

在图片中,可以看到有 3 个数据磁盘:LUN 0 为 4 GB,LUN 1 为 16 GB,LUN 2 为 32G。In the image, you can see that there are 3 data disks: 4 GB on LUN 0, 16GB at LUN 1, and 32G at LUN 2.

下面是使用 lsblk 时可能显示的内容:Here is what that might look like using lsblk:

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 4G

sdd 3:0:0:1 16G

sde 3:0:0:2 32G

从 lsblk 的输出可以看到,LUN 0 处的 4GB 磁盘为 sdc,LUN 1 处的 16 GB 磁盘为 sdd,LUN 2 处的 32G 磁盘为 sde。From the output of lsblk you can see that the 4GB disk at LUN 0 is sdc, the 16GB disk at LUN 1 is sdd, and the 32G disk at LUN 2 is sde.

对新磁盘进行分区Partition a new disk

如果使用包含数据的现有磁盘,请跳到装载磁盘。If you are using an existing disk that contains data, skip to mounting the disk. 如果附加新磁盘,需要对磁盘进行分区。If you are attaching a new disk, you need to partition the disk.

parted 实用程序可用于对数据磁盘进行分区和格式设置。The parted utility can be used to partition and to format a data disk.

备注

建议使用适用于你的发行版的最新版 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 创建一个目录来装载文件系统。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

若要确保在重新引导后自动重新装载驱动器,必须将其添加到 /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 退出该编辑器。We used 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

验证磁盘Verify the disk

现在可以再次使用 lsblk 来查看磁盘和装入点。You can now use lsblk again to see the disk and the mountpoint.

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

输出将如下所示:The output will look something like this:

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 4G

└─sdc1 4G /datadrive

可以看到 sdc 现在已装载到 /datadrive。You can see that sdc is now mounted at /datadrive.

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

后续步骤Next steps

有关详细信息,以及为了帮助排查磁盘问题,请参阅 排查 LINUX VM 设备名称更改问题。For more information, and to help troubleshoot disk issues, see Troubleshoot Linux VM device name changes.

还可使用 Azure CLI 附加数据磁盘。You can also attach a data disk using the Azure CLI.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值