我如何使用cloud-init在Azure中的ubuntu VM上加载数据磁盘 [英] How can i use cloud-init to load a datadisk on an ubuntu VM in azure
问题描述
我一直在尝试使用cloud-init在azure中对Ubuntu VM上的数据磁盘进行分区和挂载.大多数在线示例都显示了如何对临时磁盘执行此操作,但对数据磁盘不执行此操作.我试着运气不好,没有运气.如果我在以下代码中做错了什么,或者如果无法使用cloud-init做到这一点,请告诉我
I have been trying use cloud-init to partition and mount a datadisk on a Ubuntu VM in azure. Most of the online examples show how to do that for the temporary disk but not for data disk. I tried dabbling around with it with out much luck. Please let me know if I am doing something wrong in the below code or if its not possible to do it with cloud-init
#cloud-config
device_aliases: {'ephemeral0': '/dev/sdb','datadisk': '/dev/sdc1'}
disk_setup:
ephemeral0:
table_type: mbr
layout: True
overwrite: False
/dev/sdc1:
table_type: mbr
layout: True
overwrite: False
fs_setup:
- label: ephemeral0
filesystem: ext4
device: ephemeral0.1
replace_fs: ntfs
- cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s
label: '/dev/sdc1/'
filesystem: ext4
device: '/dev/sdc1/'
replace_fs: ntfs
mounts:
- ["ephemeral0.1", "/mnt"]
- ["/dev/sdc1/", "/datadisk"]
推荐答案
我同意-这种常见情况的例子并不多.我认为您上面遇到的问题的一部分是您引用的是分区而不是disk_setup中的磁盘.
I agree -- not many examples of this common scenario. I think part of the problem you're facing above is that you're referencing a partition instead of a disk in disk_setup.
对于Azure,附加到VM的第一个数据磁盘通常将被标识为/dev/sdc
,第二个将被标识为/dev/sdd
,依此类推.但是,这不能保证.文档此处指示:在某些情况下,可能会导致分配了不同的驱动器号.因此,我们将使用内置别名来引用磁盘.确保始终使用ARM模板(或磁盘定义)中分配的LUN映射此别名.这些别名的格式为/dev/disk/azure/scsi1/lun#
(分区的别名为/dev/disk/azure/scsi1/lun#-part#
)
With Azure, the first data disk attached to the VM will usually be identified as /dev/sdc
, the second will be /dev/sdd
, and so on. This isn't guaranteed, however. The documentation here indicates that there are circumstances which can result in a different drive letter being assigned. As a result, we'll reference the disks using a build-in alias. This alias is guarantee to always be mapped using the LUN assigned in the ARM template (or disk definition). These aliases follow the form /dev/disk/azure/scsi1/lun#
(with partitions aliased as /dev/disk/azure/scsi1/lun#-part#
).
如果您使用的是ARM,则模板将在VM定义中包含对驱动器的引用.作为该定义的一部分,您将指定LUN值.您可以在cloud-init中引用该分配的值.例如,以下ARM代码片段将创建/dev/disk/azure/scsi1/lun0
:
If you're using ARM, the template will include a reference in the VM definition to the drive. As part of that definition, you will specify the LUN value. You can reference that assigned value in your cloud-init. For example, the following ARM snippet will create /dev/disk/azure/scsi1/lun0
:
"dataDisks": [
{
"lun": 0,
"name": "[concat(variables('vmName'),'-datadisk0')]",
"createOption": "Attach",
"managedDisk":
{
"id": "[resourceId('Microsoft.Compute/disks/',
concat(variables('vmName'),'-datadisk0'))]"
}
},
知道了这一点,我们可以为cloud-config构造内容.首先,我们定义数据磁盘.我建议使用GPT作为表类型,以启用对大于2TiB的磁盘和分区的支持.
Knowing this, we can construct the contents for a cloud-config. First, we define the data disk. I suggest using GPT as the table type for to enable support for disks and partitions that are > 2TiB.
disk_setup:
/dev/disk/azure/scsi1/lun0:
table_type: gpt
layout: True
overwrite: True
接下来,我们为磁盘指定文件系统设置.我们引用每个分区并声明要使用的文件系统.
Next, we specify the file system setup for the disks. We reference each partition and declare the file system to be used.
fs_setup:
- device: /dev/disk/azure/scsi1/lun0
partition: 1
filesystem: ext4
最后,我们安装分区.cloud-init使用的过程将创建文件夹并挂载指定的分区.我正在使用推荐的 nofail
(确保VM在出现问题或驱动器分离时可以启动)以及 noexec
(这可以防止在该分区上执行二进制文件).由于我们已将文件系统放置在lun0的第一个分区上,因此我们需要挂载 lun0-part1
.
Finally, we mount the partitions. The process used by cloud-init will create the folders and mount the specified partitions. I'm using the recommended nofail
(ensures the VM can boot in the event of issues or a detached drive) along with noexec
(which prevents executing binaries on that partition). Since we've placed the file system on the first partition of lun0, we need to mount lun0-part1
.
mounts:
- ["/dev/disk/azure/scsi1/lun0-part1", "/datadisk", auto, "defaults,noexec,nofail"]
这篇关于我如何使用cloud-init在Azure中的ubuntu VM上加载数据磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!