KVM在启动时自动更改映像-d的guest虚拟机主机名

我从closures的虚拟机创build了一个映像。 从新的XML模板中,我可以使用virsh create newvm.xml创build一个新的VM。 基本上是通过复制从Ubuntu操作系统映像构build的closures虚拟机的映像来创build映像 – 显然在安装过程中我必须指定主机名。 现在我closures虚拟机并复制映像,将其用作我的新VM guest虚拟机的基础映像。 问题是,我不知道如何自动更改该死的主机名。 现在,eache新创build的虚拟机与创build映像的计算机的主机名相同。 有没有办法如何处理这个?

<domain type='kvm' id='10'> <name>sensu.gc.example.com</name> <uuid>3d638021-1fd5-96c4-5b7b-a5c11d69c314</uuid> <memory>1048576</memory> <currentMemory>1048576</currentMemory> <vcpu>1</vcpu> <os> <type arch='x86_64' machine='pc-1.0'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/kvm/sensu/tmpZ2yf6n.qcow2'/> <target dev='hda' bus='ide'/> <alias name='ide0-0-0'/> <address type='drive' controller='0' bus='0' unit='0'/> </disk> <controller type='ide' index='0'> <alias name='ide0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <interface type='bridge'> <mac address='52:54:00:50:89:7b'/> <source bridge='br0'/> <target dev='vnet0'/> <model type='virtio'/> <alias name='net0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <source path='/dev/pts/0'/> <target port='0'/> <alias name='serial0'/> </serial> <console type='pty' tty='/dev/pts/0'> <source path='/dev/pts/0'/> <target type='serial' port='0'/> <alias name='serial0'/> </console> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'> <listen type='address' address='127.0.0.1'/> </graphics> <video> <model type='cirrus' vram='9216' heads='1'/> <alias name='video0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <alias name='balloon0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </memballoon> </devices> <seclabel type='dynamic' model='apparmor' relabel='yes'> <label>libvirt-3d638021-1fd5-96c4-5b7b-a5c11d69c314</label> <imagelabel>libvirt-3d638021-1fd5-96c4-5b7b-a5c11d69c314</imagelabel> </seclabel> </domain> 

我认为,通过指定标签中的主机名,它将在创build虚拟机时自动更改。 有什么办法可以实现我想要的吗? 干杯。

您不能从虚拟机的XML文件或KVM中更改主机名。 这个XML文件只列出VM的属性,主机名不是这样的属性。 在启动过程中,主机名被设置在虚拟机操作系统(在你的情况下为Ubuntu)。

要更改主机名,您应该查看Ubuntu服务器中的/etc/hostname文件(请参阅详细示例 )。

如果你使用dhcp来设置你的服务器的IP地址(我build议), dhcp-client可以在接收到IP地址时使用一些钩子来运行脚本。 这样,您可以在引导过程中dynamic更改主机名。 这里是一个如何做到这一点的例子 。

另一个选项,就像佩德罗build议的那样,是在每个新的XML文件中设置服务器的MAC地址(你可以这样做,因为它是一个VM属性),并且有一个脚本在启动时运行,它将分配一个IP地址从此MAC地址派生的主机名。

我这样做是为了在创build虚拟机时设置MAC地址,并调整/etc/rc.local来查看MAC并根据其值设置IP /主机名。 问候,佩德罗