创建 OpenBSD 磁盘映像并将其上传到 Azure

适用于:✔️ Linux VM ✔️ 灵活规模集

本文介绍如何创建和上传包含 OpenBSD 操作系统的虚拟硬盘 (VHD)。 上传后,可将其用作自己的映像,通过 Azure CLI 在 Azure 中创建虚拟机 (VM)。

先决条件

本文假定你拥有以下项目:

  • Azure 订阅 - 如果没有帐户,只需几分钟即可创建一个。 如果有 MSDN 订阅,请参阅 Visual Studio 订户的每月 Azure 信用额度。 否则,请了解如何创建试用帐户
  • Azure CLI - 确保已安装了最新的 Azure CLI 并已使用 az login 登录到 Azure 帐户 。
  • 安装在 .vhd 文件中的 OpenBSD 操作系统 - 必须将受支持的 OpenBSD 操作系统(6.6 版 AMD64)安装到虚拟硬盘中。 可使用多种工具创建 .vhd 文件。 例如,可使用 Hyper-V 等虚拟化解决方案创建 .vhd 文件并安装操作系统。 有关如何安装和使用 Hyper-V 的说明,请参阅安装 Hyper-V 并创建虚拟机

为 Azure 准备 OpenBSD 映像

在安装了 OpenBSD 操作系统 6.1(已添加 Hyper-V 支持)的 VM上,完成以下步骤:

  1. 如果安装期间未启用 DHCP,请启用该服务,如下所示:

    doas echo dhcp > /etc/hostname.hvn0
    
  2. 设置串行控制台,如下所示:

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

    doas echo "https://ftp.openbsd.org/pub/OpenBSD" > /etc/installurl
    
  4. 默认情况下,禁止在 Azure 中的虚拟机上使用 root 用户。 用户可以对 OpenBSD VM 使用 doas 命令,通过提升的权限运行命令。 默认启用 Doas。

  5. 安装并配置 Azure 代理的先决条件,如下所示:

    doas pkg_add py-setuptools openssl git
    doas ln -sf /usr/local/bin/python2.7 /usr/local/bin/python
    doas ln -sf /usr/local/bin/python2.7-2to3 /usr/local/bin/2to3
    doas ln -sf /usr/local/bin/python2.7-config /usr/local/bin/python-config
    doas ln -sf /usr/local/bin/pydoc2.7  /usr/local/bin/pydoc
    
  6. 始终可以在 GitHub 上找到 Azure 代理的最新版本。 安装代理,如下所示:

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

    重要

    安装 Azure 代理后,最好验证它是否正在运行,如下所示:

    doas 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)
    doas cat /var/log/waagent.log
    
  7. 取消预配系统以清除系统并使其适用于取消预配。 以下命令还会删除上次预配的用户帐户和关联数据:

    doas waagent -deprovision+user -force
    

注意

如果要迁移特定的虚拟机,但不希望创建通用化映像,请跳过“取消预配”步骤。

现在可以关闭 VM。

准备 VHD

Azure 不支持 VHDX 格式,仅支持 固定大小的 VHD。 可使用 Hyper-V 管理器或 PowerShell convert-vhd cmdlet 将磁盘转换为固定 VHD 格式。 示例如下。

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

创建存储资源并上传

首先,使用 az group create 创建资源组。 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:

注意

在可以在由世纪互联运营的 Microsoft Azure 中使用 Azure CLI 之前,请先运行 az cloud set -n AzureChinaCloud 来更改云环境。 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud

az group create --name myResourceGroup --location chinaeast

若要上传 VHD,请使用 az storage account create 创建存储帐户。 存储帐户名称必须唯一,因此,请提供自己的名称。 以下示例创建一个名为 mystorageaccount 的存储帐户:

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

若要控制对存储帐户的访问,请按如下所示,使用 az storage account key list 获取存储密钥:

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 在存储帐户内创建容器:

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

最后,请使用 az storage blob upload 上传 VHD,如下所示:

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

从 VHD 创建 VM

可使用示例脚本或直接使用 az vm create 创建 VM。 若要指定上传的 OpenBSD VHD,请使用 --image 参数,如下所示:

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 地址,如下所示:

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

现在,可如常使用 SSH 连接到 OpenBSD VM:

ssh azureuser@<ip address>

后续步骤

如果要了解有关 OpenBSD6.1 上 Hyper-V 支持的详细信息,请阅读 OpenBSD 6.1

若要从托管磁盘创建 VM,请阅读 az disk