Is there an API under KVM to obtain a ZoneID?
When using Zoning under Solaris, the hostid doesn't change by zone, but you also obtain (via a function call, getzoneid()) a unique zoneid for your zone.
Using KVM under Linux, the hostid changes (increments) by zone - which is pretty good, but it's not perfect - you might collide with someone elses hostid.
It there an API provided by KVM which enables me to开发者_运维百科 get a zoneid?
Regarding your concerns about duplicate hostids:
Unlike Solaris, the Linux kernel does not provide the "gethostid" call. Instead, "gethostid" (used by the /usr/bin/hostid
program) is implemented by glibc, which tries to:
- Determine if a fixed file (
/etc/hostid
on my system) exists; if so, uses the 4-byte value in there; - Failing that, attempts to create a hostid based on the system's IP address;
- Failing that, uses a hostid of
0
.
This means that if every system has a unique IP address they will also have a unique hostid.
If your systems do not have unique IP addresses, you can still override the hostid by using the sethostid
glibc library call (which writes to the file read by step (1) of glibc's gethostid
algorithm above).
Regarding fetching the zoneid of a KVM instance:
Unlike Solaris zones (where all instances share the same kernel), each instance of KVM runs its own copy of the Linux kernel, all of which are oblivious to other instances running. As far as I am aware, there is no direct equivalent of a Solaris zoneid, as each Linux instance has no way of collaborating with other Linux instances.
If you need a unique identifier for each running KVM instance, some options are:
Just settling for the IP Address / hostid, as described above;
When setting up / booting up your instances, generate a UUID for the system using
uuidgen
and save it in a safe location on the filesystem. Such generated UUIDs will never match any other UUID with very high probability;When booting each instance of your system, pass in on the kernel command line a manually-constructed unique identifier for the instance (using the
-append
KVM command line argument). This can be fetched later from/proc/cmdline
.
精彩评论