1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/* This is a more significant example of a tool which can grab the
* DHCP address from some types of virtual machine. Since there are
* so many possible ways to do this, without clarity on which is the
* best way, I don't want to make this into an official virt tool.
*
* For more information, see:
*
* https://rwmj.wordpress.com/2010/10/26/tip-find-the-ip-address-of-a-virtual-machine/
* https://rwmj.wordpress.com/2011/03/30/tip-another-way-to-get-the-ip-address-of-a-virtual-machine/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <guestfs.h>
static int compare_keys_len (const void *p1, const void *p2);
static size_t count_strings (char *const *argv);
static void free_strings (char **argv);
static void mount_disks (guestfs_h *g, char *root);
static void print_dhcp_address (guestfs_h *g, char *root);
static void print_dhcp_address_linux (guestfs_h *g, char *root, const char *logfile);
static void print_dhcp_address_windows (guestfs_h *g, char *root);
int
main (int argc, char *argv[])
{
guestfs_h *g;
size_t i;
char **roots, *root;
if (argc < 2) {
fprintf (stderr,
"Usage: virt-dhcp-address disk.img [disk.img [...]]\n"
"Note that all disks must come from a single virtual machine.\n");
exit (EXIT_FAILURE);
}
g = guestfs_create ();
if (g == NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
for (i = 1; i < (size_t) argc; ++i) {
/* Attach the disk image(s) read-only to libguestfs. */
if (guestfs_add_drive_opts (g, argv[i],
/* GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", */
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
-1) /* this marks end of optional arguments */
== -1)
exit (EXIT_FAILURE);
}
/* Run the libguestfs back-end. */
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
/* Ask libguestfs to inspect for operating systems. */
roots = guestfs_inspect_os (g);
if (roots == NULL)
exit (EXIT_FAILURE);
if (roots[0] == NULL) {
fprintf (stderr, "virt-dhcp-address: no operating systems found\n");
exit (EXIT_FAILURE);
}
if (count_strings (roots) > 1) {
fprintf (stderr, "virt-dhcp-address: multi-boot operating system\n");
exit (EXIT_FAILURE);
}
root = roots[0];
/* Mount up the guest's disks. */
mount_disks (g, root);
/* Print DHCP address. */
print_dhcp_address (g, root);
/* Close handle and exit. */
guestfs_close (g);
free_strings (roots);
exit (EXIT_SUCCESS);
}
static void
mount_disks (guestfs_h *g, char *root)
{
char **mountpoints;
size_t i;
/* Mount up the disks, like guestfish -i.
*
* Sort keys by length, shortest first, so that we end up
* mounting the filesystems in the correct order.
*/
mountpoints = guestfs_inspect_get_mountpoints (g, root);
if (mountpoints == NULL)
exit (EXIT_FAILURE);
qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
compare_keys_len);
for (i = 0; mountpoints[i] != NULL; i += 2) {
/* Ignore failures from this call, since bogus entries can
* appear in the guest's /etc/fstab.
*/
guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
}
free_strings (mountpoints);
}
static void
print_dhcp_address (guestfs_h *g, char *root)
{
char *guest_type, *guest_distro;
/* Depending on the guest type, try to get the DHCP address. */
guest_type = guestfs_inspect_get_type (g, root);
if (guest_type == NULL)
exit (EXIT_FAILURE);
if (strcmp (guest_type, "linux") == 0) {
guest_distro = guestfs_inspect_get_distro (g, root);
if (guest_distro == NULL)
exit (EXIT_FAILURE);
if (strcmp (guest_distro, "fedora") == 0 ||
strcmp (guest_distro, "rhel") == 0 ||
strcmp (guest_distro, "redhat-based") == 0) {
print_dhcp_address_linux (g, root, "/var/log/messages");
}
else if (strcmp (guest_distro, "debian") == 0 ||
strcmp (guest_distro, "ubuntu") == 0) {
print_dhcp_address_linux (g, root, "/var/log/syslog");
}
else {
fprintf (stderr, "virt-dhcp-address: don't know how to get DHCP address from '%s'\n",
guest_distro);
exit (EXIT_FAILURE);
}
free (guest_distro);
}
else if (strcmp (guest_type, "windows") == 0) {
print_dhcp_address_windows (g, root);
}
else {
fprintf (stderr, "virt-dhcp-address: don't know how to get DHCP address from '%s'\n",
guest_type);
exit (EXIT_FAILURE);
}
free (guest_type);
}
/* Look for dhclient messages in logfile.
*/
static void
print_dhcp_address_linux (guestfs_h *g, char *root, const char *logfile)
{
char **lines, *p;
size_t len;
lines = guestfs_grep_opts (g, "dhclient.*: bound to ", logfile,
GUESTFS_GREP_OPTS_EXTENDED, 1,
-1);
if (lines == NULL)
exit (EXIT_FAILURE);
len = count_strings (lines);
if (len == 0) {
fprintf (stderr, "virt-dhcp-address: cannot find DHCP address for this guest.\n");
exit (EXIT_FAILURE);
}
/* Only want the last message. */
p = strstr (lines[len-1], "bound to ");
assert (p);
p += 9;
len = strcspn (p, " ");
p[len] = '\0';
printf ("%s\n", p);
free_strings (lines);
}
/* Download the Windows SYSTEM hive and find DHCP configuration in there. */
static void
print_dhcp_address_windows (guestfs_h *g, char *root_fs)
{
char *system_path;
int64_t root, node, value;
struct guestfs_hivex_node_list *nodes;
char *controlset;
size_t i;
char *p;
/* Locate the SYSTEM hive. */
system_path = guestfs_inspect_get_windows_system_hive (g, root_fs);
if (!system_path)
exit (EXIT_FAILURE);
/* Open the hive to parse it. Note that before libguestfs 1.19.35
* you had to download the file and parse it using hivex(3). Since
* libguestfs 1.19.35, parts of the hivex(3) API are now exposed
* through libguestfs, and that is what we'll use here because it is
* more convenient and avoids having to download the hive.
*/
if (guestfs_hivex_open (g, system_path, -1) == -1)
exit (EXIT_FAILURE);
free (system_path);
root = guestfs_hivex_root (g);
if (root == -1)
exit (EXIT_FAILURE);
/* Get ControlSetXXX\Services\Tcpip\Parameters\Interfaces. */
controlset = guestfs_inspect_get_windows_current_control_set (g, root_fs);
if (controlset == NULL)
exit (EXIT_FAILURE);
const char *path[] = { controlset, "Services", "Tcpip", "Parameters",
"Interfaces" };
node = root;
for (i = 0; node > 0 && i < sizeof path / sizeof path[0]; ++i)
node = guestfs_hivex_node_get_child (g, node, path[i]);
if (node == -1)
exit (EXIT_FAILURE);
if (node == 0) {
fprintf (stderr, "virt-dhcp-address: HKLM\\System\\%s\\Services\\Tcpip\\Parameters\\Interfaces not found.", controlset);
exit (EXIT_FAILURE);
}
free (controlset);
/* Look for a node under here which has a "DhcpIPAddress" entry in it. */
nodes = guestfs_hivex_node_children (g, node);
if (nodes == NULL)
exit (EXIT_FAILURE);
value = 0;
for (i = 0; value == 0 && i < nodes->len; ++i) {
value = guestfs_hivex_node_get_value (g, nodes->val[i].hivex_node_h,
"DhcpIPAddress");
if (value == -1)
exit (EXIT_FAILURE);
}
if (value == 0) {
fprintf (stderr, "virt-dhcp-address: cannot find DHCP address for this guest.\n");
exit (EXIT_FAILURE);
}
guestfs_free_hivex_node_list (nodes);
/* Get the string and use libguestfs's auto-conversion to convert it
* to UTF-8 for output.
*/
p = guestfs_hivex_value_string (g, value);
if (!p)
exit (EXIT_FAILURE);
printf ("%s\n", p);
free (p);
/* Close the hive handle. */
guestfs_hivex_close (g);
}
static int
compare_keys_len (const void *p1, const void *p2)
{
const char *key1 = * (char * const *) p1;
const char *key2 = * (char * const *) p2;
return strlen (key1) - strlen (key2);
}
static size_t
count_strings (char *const *argv)
{
size_t c;
for (c = 0; argv[c]; ++c)
;
return c;
}
static void
free_strings (char **argv)
{
size_t i;
for (i = 0; argv[i]; ++i)
free (argv[i]);
free (argv);
}
|