15.7.2. 使用 Cloud-Init Action 启动虚拟机
Cloud-Init 是自动化虚拟机初始设置的工具。您可以使用 工具配置主机名、网络接口、DNS 服务、授权密钥以及设置用户名和密码。您还可以使用
custom_script 标签指定要在启动时在虚拟机上运行的自定义脚本。
注意
cloud-init 元素只能用于启动安装有 cloud-init 软件包的虚拟机。使用 cloud-init 元素时,initialization 元素中的任何元素都会被忽略,但 cloud-init 元素以外的任何元素都会被忽略。
例 15.48. 使用 Cloud-Init 启动虚拟机的操作
本例演示了如何使用 Cloud-Init 工具启动虚拟机,以设置主机名,更改 root 密码,为
eth0 接口设置静态 IP,配置 DNS,并为 root 用户添加 SSH 密钥。
POST /ovirt-engine/api/vms/5114bb3e-a4e6-44b2-b783-b3eea7d84720/start HTTP/1.1
Accept: application/xml
Content-type: application/xml
<action>
<vm>
<initialization>
<cloud_init>
<host>
<address>MyHost.MyDomain.com</address>
</host>
<users>
<user>
<user_name>root</user_name>
<password>p@55w0rd!</password>
</user>
</users>
<network_configuration>
<nics>
<nic>
<name>eth0</name>
<boot_protocol>static</boot_protocol>
<network>
<ip address="192.168.122.31" netmask="255.255.255.0" gateway="192.168.122.1"/>
</network>
<on_boot>true</on_boot>
</nic>
</nics>
<dns>
<servers>
<host>
<address>192.168.122.1</address>
</host>
</servers>
<search_domains>
<host>
<address>MyDomain.com</address>
</host>
</search_domains>
</dns>
</network_configuration>
<authorized_keys>
<authorized_key>
<user>
<user_name>root</user_name>
</user>
<key>ssh-rsa AAAAB3Nza[...]75zkdD root@MyDomain.com</key>
</authorized_key>
</authorized_keys>
</cloud_init>
<custom_script><![CDATA[your script]]></custom_script>
</initialization>
</vm>
</action>
POST /ovirt-engine/api/vms/5114bb3e-a4e6-44b2-b783-b3eea7d84720/start HTTP/1.1
Accept: application/xml
Content-type: application/xml
<action>
<vm>
<initialization>
<cloud_init>
<host>
<address>MyHost.MyDomain.com</address>
</host>
<users>
<user>
<user_name>root</user_name>
<password>p@55w0rd!</password>
</user>
</users>
<network_configuration>
<nics>
<nic>
<name>eth0</name>
<boot_protocol>static</boot_protocol>
<network>
<ip address="192.168.122.31" netmask="255.255.255.0" gateway="192.168.122.1"/>
</network>
<on_boot>true</on_boot>
</nic>
</nics>
<dns>
<servers>
<host>
<address>192.168.122.1</address>
</host>
</servers>
<search_domains>
<host>
<address>MyDomain.com</address>
</host>
</search_domains>
</dns>
</network_configuration>
<authorized_keys>
<authorized_key>
<user>
<user_name>root</user_name>
</user>
<key>ssh-rsa AAAAB3Nza[...]75zkdD root@MyDomain.com</key>
</authorized_key>
</authorized_keys>
</cloud_init>
<custom_script><![CDATA[your script]]></custom_script>
</initialization>
</vm>
</action>