你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure VM 映像生成器创建 Windows VM

适用于:✔️ Windows VM

本文介绍如何使用 Azure VM 映像生成器创建自定义 Windows 映像。 本文中的示例使用自定义程序来自定义映像:

  • PowerShell (ScriptUri):下载并运行 PowerShell 脚本
  • Windows 重启:重启 VM。
  • PowerShell(内联):运行特定的命令。 本示例通过使用 mkdir c:\\buildActions 在 VM 上创建一个目录。
  • 文件:将 GitHub 中的文件复制到 VM。 本示例将 index.md 复制到 VM 上的 c:\buildArtifacts\index.html 中。
  • buildTimeoutInMinutes:指定生成时间(以分钟为单位)。 默认值为 240 分钟,可增加该值使长时间运行的生成能够完成。 允许的最小值为 6 分钟。 小于 6 分钟的值将导致错误。
  • vmProfile:指定 vmSize 和网络属性。
  • osDiskSizeGB:可用于增大映像的大小。
  • identity。 提供 VM 映像生成器在生成期间使用的标识。

使用以下示例 JSON 模板配置映像:helloImageTemplateWin.json

注意

Windows 用户可使用 Bash 在 Azure Cloud Shell 上运行以下 Azure CLI 示例。

注册提供程序

若要使用 VM 映像生成器,则需要注册此功能。 运行以下命令来检查注册:

az provider show -n Microsoft.VirtualMachineImages | grep registrationState
az provider show -n Microsoft.KeyVault | grep registrationState
az provider show -n Microsoft.Compute | grep registrationState
az provider show -n Microsoft.Storage | grep registrationState
az provider show -n Microsoft.Network | grep registrationState
az provider show -n Microsoft.ContainerInstance -o json | grep registrationState

如果输出未显示“已注册”,请运行以下命令:

az provider register -n Microsoft.VirtualMachineImages
az provider register -n Microsoft.Compute
az provider register -n Microsoft.KeyVault
az provider register -n Microsoft.Storage
az provider register -n Microsoft.Network
az provider register -n Microsoft.ContainerInstance

设置变量

因为你将重复使用某些信息,所以要创建一些变量来存储这些信息:

# Resource group name - we're using myImageBuilderRG in this example
imageResourceGroup='myWinImgBuilderRG'
# Region location
location='WestUS2'
# Run output name
runOutputName='aibWindows'
# The name of the image to be created
imageName='aibWinImage'

为你的订阅 ID 创建变量:

subscriptionID=$(az account show --query id --output tsv)

创建资源组

若要存储映像配置模板项目和映像,请使用以下资源组:

az group create -n $imageResourceGroup -l $location

创建用户分配的标识,并在资源组上设置权限

VM 映像生成器使用提供的用户标识将映像注入到资源组中。 在此示例中将创建一个 Azure 角色定义,该定义具有分发映像的特定权限。 然后将角色定义分配给用户标识。

创建一个用户分配的托管标识并授予权限

创建一个用户分配的标识,以便 VM 映像生成器可以访问存储脚本的存储帐户。

identityName=aibBuiUserId$(date +'%s')
az identity create -g $imageResourceGroup -n $identityName

# Get the identity ID
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)

# Get the user identity URI that's needed for the template
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName

# Download the preconfigured role definition example
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json

imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')

# Update the definition
sed -i -e "s%<subscriptionID>%$subscriptionID%g" aibRoleImageCreation.json
sed -i -e "s%<rgName>%$imageResourceGroup%g" aibRoleImageCreation.json
sed -i -e "s%Azure Image Builder Service Image Creation Role%$imageRoleDefName%g" aibRoleImageCreation.json

# Create role definitions
az role definition create --role-definition ./aibRoleImageCreation.json

# Grant a role definition to the user-assigned identity
az role assignment create \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup

下载映像配置模板

我们已经创建了一个参数化映像配置模板供你试用。 请下载示例 JSON 文件,然后使用先前设置的变量对其进行配置。

curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/0_Creating_a_Custom_Windows_Managed_Image/helloImageTemplateWin.json -o helloImageTemplateWin.json

sed -i -e "s%<subscriptionID>%$subscriptionID%g" helloImageTemplateWin.json
sed -i -e "s%<rgName>%$imageResourceGroup%g" helloImageTemplateWin.json
sed -i -e "s%<region>%$location%g" helloImageTemplateWin.json
sed -i -e "s%<imageName>%$imageName%g" helloImageTemplateWin.json
sed -i -e "s%<runOutputName>%$runOutputName%g" helloImageTemplateWin.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateWin.json

可以使用 vi 等文本编辑器在终端中修改此示例。

vi helloImageTemplateWin.json

注意

对于源映像,始终指定一个版本。 不能指定 latest 为版本。

如果添加或更改映像所分发到的资源组,请确保对资源组设置权限

创建映像

通过运行以下命令,将映像配置提交到 VM 映像生成器服务:

az resource create \
    --resource-group $imageResourceGroup \
    --properties @helloImageTemplateWin.json \
    --is-full-object \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateWin01

完成后,会将成功消息返回到控制台,并在 $imageResourceGroup 中创建 VM 映像生成器配置模板。 若要查看资源组中的此资源,请转到 Azure 门户,然后启用“显示隐藏类型”。

在后台,VM 映像生成器还会在你的订阅中创建一个暂存资源组。 此资源组用于按以下格式生成映像:IT_<DestinationResourceGroup>_<TemplateName>

注意

请勿直接删除暂存资源组。 请先删除映像模板项目,这样就会删除暂存资源组。

如果服务在提交映像配置模板时报告失败,请执行以下操作:

az resource delete \
    --resource-group $imageResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateWin01

启动映像生成

使用 az resource invoke-action 启动映像生成过程。

az resource invoke-action \
     --resource-group $imageResourceGroup \
     --resource-type  Microsoft.VirtualMachineImages/imageTemplates \
     -n helloImageTemplateWin01 \
     --action Run

等待生成完成。

如果遇到任何错误,请参阅排查 Azure VM 映像生成器服务的问题

创建 VM

使用已生成的映像创建 VM。 在以下代码中,将密码替换为 VM 上用于 aibuser 的你自己的密码。<>

az vm create \
  --resource-group $imageResourceGroup \
  --name aibImgWinVm00 \
  --admin-username aibuser \
  --admin-password <password> \
  --image $imageName \
  --location $location

验证自定义

使用创建 VM 时设置的用户名和密码创建与 VM 的远程桌面连接。 在 VM 中,打开命令提示符窗口,然后键入:

dir c:\

在映像自定义期间会创建以下两个目录:

  • buildActions
  • buildArtifacts

清理资源

完成后,请删除已创建的资源。

  1. 删除 VM 映像生成器模板。

    az resource delete \
        --resource-group $imageResourceGroup \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateWin01
    
  2. 删除角色分配、角色定义和用户标识。

    az role assignment delete \
        --assignee $imgBuilderCliId \
        --role "$imageRoleDefName" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
    
    az role definition delete --name "$imageRoleDefName"
    
    az identity delete --ids $imgBuilderId
    
  3. 删除映像资源组。

    az group delete -n $imageResourceGroup
    

后续步骤

若要详细了解本文中使用的 JSON 文件的组件,请参阅 Azure VM 映像生成器模板参考