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

本文介绍如何通过 Azure 门户将新磁盘和现有磁盘附加到 Linux 虚拟机。This article shows you how to attach both new and existing disks to a Linux virtual machine through the Azure portal. 也可以在 Azure 门户中将数据磁盘附加到 Windows VMYou can also attach a data disk to a Windows VM in the Azure portal.

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

查找虚拟机Find the virtual machine

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

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

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

    打开磁盘设置

附加新磁盘Attach a new disk

  1. 在“磁盘”窗格上,单击“+ 添加数据磁盘” 。On the Disks pane, click + Add data disk.

  2. 单击“名称”的下拉列表菜单,并选择“创建磁盘”:Click the drop-down menu for Name and select Create disk:

    创建 Azure 托管磁盘

  3. 输入托管磁盘的名称。Enter a name for your managed disk. 查看默认设置,根据需要更新,并单击“创建”。Review the default settings, update as necessary, and then click Create.

    检查磁盘设置

  4. 单击“保存”以创建托管磁盘并更新 VM 配置:Click Save to create the managed disk and update the VM configuration:

    保存新的 Azure 托管磁盘

  5. 在 Azure 创建磁盘并将磁盘附加到虚拟机之后,新磁盘出现在“数据磁盘” 下的虚拟机磁盘设置中。After Azure creates the disk and attaches it to the virtual machine, the new disk is listed in the virtual machine's disk settings under Data Disks. 托管磁盘是顶级资源,因此磁盘会显示在资源组的根部:As managed disks are a top-level resource, the disk appears at the root of the resource group:

    资源组中的 Azure 托管磁盘

附加现有磁盘Attach an existing disk

  1. 在“磁盘”窗格上,单击“+ 添加数据磁盘” 。On the Disks pane, click + Add data disk.

  2. 单击“名称”的下拉列表菜单,查看 Azure 订阅可访问的现有托管磁盘列表。Click the drop-down menu for Name to view a list of existing managed disks accessible to your Azure subscription. 选择要附加的托管磁盘:Select the managed disk to attach:

    附加现有 Azure 管理磁盘

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

    保存 Azure 托管磁盘更新

  4. 在 Azure 将磁盘附加到虚拟机之后,磁盘出现在“数据磁盘” 下的虚拟机磁盘设置中。After Azure attaches the disk to the virtual machine, it's listed in the virtual machine's disk settings under Data Disks.

连接到 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. 有关详细信息,请参阅如何在 Azure 中将 SSH 用于 LinuxFor more information, see How to use SSH with Linux on Azure. 以下示例使用公共 DNS 条目 mypublicdns.chinanorth.cloudapp.chinacloudapi.cn 和用户名 azureuser 连接到一个 VM:The following example connects to a VM with the public DNS entry of mypublicdns.chinanorth.cloudapp.chinacloudapi.cn with the username azureuser:

ssh azureuser@mypublicdns.chinanorth.cloudapp.chinacloudapi.cn

连接到 VM 后就可以附加磁盘了。Once connected to your VM, you're ready to attach a disk. 首先,使用 dmesg 来查找磁盘(用于发现新磁盘的方法可能各不相同)。First, find the disk using dmesg (the method you use to discover your new disk may vary). 以下示例使用 dmesg 来筛选 SCSI 磁盘:The following example uses dmesg to filter on SCSI disks:

dmesg | grep SCSI

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

[    0.294784] SCSI subsystem initialized
[    0.573458] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 252)
[    7.110271] sd 2:0:0:0: [sda] Attached SCSI disk
[    8.079653] sd 3:0:1:0: [sdb] Attached SCSI disk
[ 1828.162306] sd 5:0:0:0: [sdc] Attached SCSI disk

此处,sdc 是我们需要的磁盘。Here, sdc is the disk that we want.

对新磁盘进行分区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.

备注

建议使用可用于你的 distro 的最新版本 fdisk 或 parted。It is recommended that you use the latest versions of fdisk or parted that are available for your distro.

使用 fdisk 对磁盘进行分区。Partition the disk with fdisk. 如果磁盘大小为 2 太字节 (TiB) 或更大,则必须使用 GPT 分区;可以使用 parted 来执行 GPT 分区。If the disk size is 2 tebibytes (TiB) or larger then you must use GPT partitioning, you can use parted to perform GPT partitioning. 如果磁盘大小在 2 TiB 以下,则可以使用 MBR 或 GPT 分区。If disk size is under 2TiB, then you can use either MBR or GPT partitioning. 将其设置为分区 1 中的主磁盘,并接受其他默认值。Make it a primary disk on partition 1, and accept the other defaults. 以下示例在 /dev/sdc 上启动 fdisk 进程:The following example starts the fdisk process on /dev/sdc:

sudo fdisk /dev/sdc

使用 n 命令添加新分区。Use the n command to add a new partition. 在此示例中,我们还选择主分区的 p 并接受其余默认值。In this example, we also choose p for a primary partition and accept the rest of the default values. 输出将类似于以下示例:The output will be similar to the following example:

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x2a59b123.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759

通过键入 p 打印分区表并使用 w 将该表写入磁盘,然后退出。Print the partition table by typing p and then use w to write the table to disk and exit. 输出应类似于以下示例:The output should look similar to the following example:

Command (m for help): p

Disk /dev/sdc: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 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 identifier: 0x2a59b123

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10485759     5241856   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

现在,使用 mkfs 命令将文件系统写入到该分区。Now, write a file system to the partition with the mkfs command. 指定文件系统类型和设备名称。Specify your filesystem type and the device name. 以下示例在通过前面的步骤创建的 /dev/sdc1 分区中创建 ext4 文件系统:The following example creates an ext4 filesystem on the /dev/sdc1 partition that was created in the preceding steps:

sudo mkfs -t ext4 /dev/sdc1

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

mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310464 blocks
65523 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

使用 parted 的替代方法Alternate method using parted

fdisk 实用程序需要交互式输入,因此不适合在自动化脚本中使用。The fdisk utility needs interactive input and hence is not ideal for use within automation scripts. 不过,parted 实用程序可以编写脚本,因此在自动化方案中更适合。However, the parted utility can be scripted and hence lends itself better in automation scenarios. parted 实用程序可用于对数据磁盘进行分区和格式化。The parted utility can be used to partition and to format a data disk. 在下面的演练中,我们将使用新的数据磁盘 /dev/sdc,并使用 XFS 文件系统将其格式化。For the walkthrough below, we use a new data disk /dev/sdc and format it using the XFS filesystem.

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
sudo mkfs.xfs /dev/sdc1
partprobe /dev/sdc1

如上所示,我们使用 partprobe 实用程序来确保内核即时了解新的分区和文件系统。As seen above, we use the partprobe utility to make sure the kernel is immediately 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 -i blkid

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

/dev/sda1: UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4"
/dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4"
/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="ext4"

备注

错误地编辑 /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 vi /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   ext4   defaults,nofail   1   2

完成后,保存 /etc/fstab 文件并重新启动系统。When done, save the /etc/fstab file and reboot the system.

备注

之后,在不编辑 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. 如果不使用此选项,可能会遇到 Cannot SSH to Linux VM due to FSTAB errorsWithout this option, you may encounter behavior as described in Cannot SSH to Linux VM due to FSTAB errors

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   ext4   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

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