创建 OpenBSD 磁盘映像并将其上传到 AzureCreate and Upload an OpenBSD disk image to Azure

本文介绍如何创建和上传包含 OpenBSD 操作系统的虚拟硬盘 (VHD)。This article shows you how to create and upload a virtual hard disk (VHD) that contains the OpenBSD operating system. 上传后,可将其用作自己的映像,通过 Azure CLI 在 Azure 中创建虚拟机 (VM)。After you upload it, you can use it as your own image to create a virtual machine (VM) in Azure through Azure CLI.

先决条件Prerequisites

本文假定你拥有以下项目:This article assumes that you have the following items:

  • Azure 订阅 - 如果没有帐户,只需几分钟即可创建一个。An Azure subscription - If you don't have an account, you can create one in just a couple of minutes. 如果有 MSDN 订阅,请参阅 Visual Studio 订户的每月 Azure 信用额度If you have an MSDN subscription, see Monthly Azure credit for Visual Studio subscribers. 否则,请了解如何创建试用帐户Otherwise, learn how to create a trial account.
  • Azure CLI - 确保已安装了最新的 Azure CLI 并已使用 az login 登录到 Azure 帐户 。Azure CLI - Make sure you have the latest Azure CLI installed and logged in to your Azure account with az login.
  • 安装在 .vhd 文件中的 OpenBSD 操作系统 - 必须将受支持的 OpenBSD 操作系统(6.6 版 AMD64)安装到虚拟硬盘中。OpenBSD operating system installed in a .vhd file - A supported OpenBSD operating system (6.6 version AMD64) must be installed to a virtual hard disk. 可使用多种工具创建 .vhd 文件。Multiple tools exist to create .vhd files. 例如,可使用 Hyper-V 等虚拟化解决方案创建 .vhd 文件并安装操作系统。For example, you can use a virtualization solution such as Hyper-V to create the .vhd file and install the operating system. 有关如何安装和使用 Hyper-V 的说明,请参阅安装 Hyper-V 并创建虚拟机For instructions about how to install and use Hyper-V, see Install Hyper-V and create a virtual machine.

为 Azure 准备 OpenBSD 映像Prepare OpenBSD image for Azure

在安装了 OpenBSD 操作系统 6.1(已添加 Hyper-V 支持)的 VM上,完成以下步骤:On the VM where you installed the OpenBSD operating system 6.1, which added Hyper-V support, complete the following procedures:

  1. 如果安装期间未启用 DHCP,请启用该服务,如下所示:If DHCP is not enabled during installation, enable the service as follows:

    echo dhcp > /etc/hostname.hvn0
    
  2. 设置串行控制台,如下所示:Set up a serial console as follows:

    echo "stty com0 115200" >> /etc/boot.conf
    echo "set tty com0" >> /etc/boot.conf
    
  3. 配置包安装,如下所示:Configure Package installation as follows:

    echo "https://ftp.openbsd.org/pub/OpenBSD" > /etc/installurl
    
  4. 默认情况下,禁止在 Azure 中的虚拟机上使用 root 用户。By default, the root user is disabled on virtual machines in Azure. 用户可以对 OpenBSD VM 使用 doas 命令,通过提升的权限运行命令。Users can run commands with elevated privileges by using the doas command on OpenBSD VM. 默认启用 Doas。Doas is enabled by default. 有关详细信息,请参阅 doas.confFor more information, see doas.conf.

  5. 安装并配置 Azure 代理的先决条件,如下所示:Install and configure prerequisites for the Azure Agent as follows:

    pkg_add py-setuptools openssl git
    ln -sf /usr/local/bin/python2.7 /usr/local/bin/python
    ln -sf /usr/local/bin/python2.7-2to3 /usr/local/bin/2to3
    ln -sf /usr/local/bin/python2.7-config /usr/local/bin/python-config
    ln -sf /usr/local/bin/pydoc2.7  /usr/local/bin/pydoc
    
  6. 始终可以在 GitHub 上找到 Azure 代理的最新版本。The latest release of the Azure agent can always be found on GitHub. 安装代理,如下所示:Install the agent as follows:

    git clone https://github.com/Azure/WALinuxAgent 
    cd WALinuxAgent
    python setup.py install
    waagent -register-service
    

    重要

    安装 Azure 代理后,最好验证它是否正在运行,如下所示:After you install Azure Agent, it's a good idea to verify that it's running as follows:

    ps auxw | grep waagent
    root     79309  0.0  1.5  9184 15356 p1  S      4:11PM    0:00.46 python /usr/local/sbin/waagent -daemon (python2.7)
    cat /var/log/waagent.log
    
  7. 取消设置系统可清除系统并使其适用于重新设置。Deprovision the system to clean it and make it suitable for reprovisioning. 以下命令还会删除上次预配的用户帐户和关联数据:The following command also deletes the last provisioned user account and the associated data:

    waagent -deprovision+user -force
    

现在可以关闭 VM。Now you can shut down your VM.

准备 VHDPrepare the VHD

Azure 不支持 VHDX 格式,仅支持 固定大小的 VHDThe VHDX format is not supported in Azure, only fixed VHD. 可使用 Hyper-V 管理器或 Powershell convert-vhd cmdlet 将磁盘转换为固定 VHD 格式。You can convert the disk to fixed VHD format using Hyper-V Manager or the Powershell convert-vhd cmdlet. 示例如下。An example is as following.

Convert-VHD OpenBSD61.vhdx OpenBSD61.vhd -VHDType Fixed

创建存储资源并上传Create storage resources and upload

首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

az group create --name myResourceGroup --location chinaeast

若要上传 VHD,请使用 az storage account create 创建存储帐户。To upload your VHD, create a storage account with az storage account create. 存储帐户名称必须唯一,因此,请提供自己的名称。Storage account names must be unique, so provide your own name. 以下示例创建一个名为 mystorageaccount 的存储帐户:The following example creates a storage account named mystorageaccount:

az storage account create --resource-group myResourceGroup \
    --name mystorageaccount \
    --location chinaeast \
    --sku Premium_LRS

若要控制对存储帐户的访问,请按如下所示,使用 az storage account key list 获取存储密钥:To control access to the storage account, obtain the storage key with az storage account keys list as follows:

STORAGE_KEY=$(az storage account keys list \
    --resource-group myResourceGroup \
    --account-name mystorageaccount \
    --query "[?keyName=='key1']  | [0].value" -o tsv)

若要从逻辑上划分上传的 VHD,请使用 az storage container create 在存储帐户内创建容器:To logically separate the VHDs you upload, create a container within the storage account with az storage container create:

az storage container create \
    --name vhds \
    --account-name mystorageaccount \
    --account-key ${STORAGE_KEY}

最后,请使用 az storage blob upload 上传 VHD,如下所示:Finally, upload your VHD with az storage blob upload as follows:

az storage blob upload \
    --container-name vhds \
    --file ./OpenBSD61.vhd \
    --name OpenBSD61.vhd \
    --account-name mystorageaccount \
    --account-key ${STORAGE_KEY}

从 VHD 创建 VMCreate VM from your VHD

可使用示例脚本或直接使用 az vm create 创建 VM。You can create a VM with a sample script or directly with az vm create. 若要指定上传的 OpenBSD VHD,请使用 --image 参数,如下所示:To specify the OpenBSD VHD you uploaded, use the --image parameter as follows:

az vm create \
    --resource-group myResourceGroup \
    --name myOpenBSD61 \
    --image "https://mystorageaccount.blob.core.chinacloudapi.cn/vhds/OpenBSD61.vhd" \
    --os-type linux \
    --admin-username azureuser \
    --ssh-key-value ~/.ssh/id_rsa.pub

请使用 az vm list-ip-addresses 获取 OpenBSD VM 的 IP 地址,如下所示:Obtain the IP address for your OpenBSD VM with az vm list-ip-addresses as follows:

az vm list-ip-addresses --resource-group myResourceGroup --name myOpenBSD61

现在,可如常使用 SSH 连接到 OpenBSD VM:Now you can SSH to your OpenBSD VM as normal:

ssh azureuser@<ip address>

后续步骤Next steps

若要深入了解 OpenBSD6.1 上的 Hyper-V 支持,请阅读 OpenBSD 6.1hyperv.4If you want to know more about Hyper-V support on OpenBSD6.1, read OpenBSD 6.1 and hyperv.4.

若要从托管磁盘创建 VM,请阅读 az diskIf you want to create a VM from managed disk, read az disk.