Kashyap Chamarthy
2014-Jun-14 06:11 UTC
[Libguestfs] Script to read systemd journal of a guest to find its IP address
Heya,
Thanks Rich for the hints on IRC about journal bindings for libguestfs.
The below script is what I ended up with. At least I learnt how to
iterate over the systemd journal entries.
$ sudo python find-ip.py
192.162.122.118
Obviously, this only searches for the _first_ occurance of the IP
address and doesn't take into account the guest may have acquired a new
IP on a later reboot. (I guess I'd have to do some thing like
$ journalctl --lines=500 | egrep -i 'dhclient.*bound').
-------------------------------------------------------------------
#!/usr/bin/python
#-----------------------------------------------------------------
# PURPOSE: To find what IP address a guest has acquired from DHCP
#-----------------------------------------------------------------
image = "/var/lib/libvirt/images/ostack-controller.qcow2"
root_filesystem = "/dev/fedora/root"
import os
import sys
import re
import guestfs
g = guestfs.GuestFS ()
g.add_drive (image, format='qcow2')
g.launch ()
#print g.list_partitions()
# Set trace
#g.set_trace (1)
g.mount_options ("ro", root_filesystem, "/")
# Open the journal
g.journal_open ("/var/log/journal")
# Loop over the journal to find a specific string
count = 0
prog = re.compile("dhclient.*bound")
while g.journal_next():
count += 1
output = g.journal_get()
#print output
flag = 0
for i in output:
if i['attrname']=="MESSAGE" and
i['attrval'].startswith("bound"):
print i['attrval'].split()[2]
flag = 1
break
if flag == 1:
break
# Close the journal handle
g.journal_close ()
# Unmount all the file system(s)
g.umount_all ()
g.sync ()
sys.exit(0)
-------------------------------------------------------------------
--
/kashyap
Richard W.M. Jones
2014-Jun-14 09:37 UTC
Re: [Libguestfs] Script to read systemd journal of a guest to find its IP address
On Sat, Jun 14, 2014 at 11:41:03AM +0530, Kashyap Chamarthy wrote:> g.add_drive (image, format='qcow2')You must use: g.add_drive (image, readonly=1) otherwise you risk disk corruption if the guest is live (eg. if the mount command has to replay the journal).> g.launch () > #print g.list_partitions() > > # Set trace > #g.set_trace (1) > > g.mount_options ("ro", root_filesystem, "/")What I'd do differently here is to use inspection to detect the root (and other) filesystems. You never know if /var is on a separate partition ... Here is the example code (see call to "g.inspect_os" and the code with the comment "Mount up the disks like guestfish -i"). http://libguestfs.org/guestfs-python.3.html#example-2:-inspect-a-virtual-machine-disk-image> g.sync ()No need to sync if you've added the drive readonly. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Kashyap Chamarthy
2014-Jun-14 10:13 UTC
Re: [Libguestfs] Script to read systemd journal of a guest to find its IP address
On Sat, Jun 14, 2014 at 10:37:12AM +0100, Richard W.M. Jones wrote:> On Sat, Jun 14, 2014 at 11:41:03AM +0530, Kashyap Chamarthy wrote: > > g.add_drive (image, format='qcow2') > > You must use: > > g.add_drive (image, readonly=1) > > otherwise you risk disk corruption if the guest is live (eg. if the > mount command has to replay the journal).Ah. I did use 'readonly=1' while testing, but forgot to mention it while writing this. Thanks for catching.> > g.launch () > > #print g.list_partitions() > > > > # Set trace > > #g.set_trace (1) > > > > g.mount_options ("ro", root_filesystem, "/") > > What I'd do differently here is to use inspection to detect the root > (and other) filesystems. You never know if /var is on a separate > partition ... > > Here is the example code (see call to "g.inspect_os" and the code with > the comment "Mount up the disks like guestfish -i"). > > http://libguestfs.org/guestfs-python.3.html#example-2:-inspect-a-virtual-machine-disk-image > > > g.sync () > > No need to sync if you've added the drive readonly.Noted. Thanks for these useful review comments, I'll test with these changes. -- /kashyap
Richard W.M. Jones
2014-Jun-14 13:25 UTC
Re: [Libguestfs] Script to read systemd journal of a guest to find its IP address
I've pushed a new tool called "virt-log" upstream, so you can now just do: virt-log -d Guest or: virt-log -a disk.img It will automatically fetch the journal or traditional syslog file as appropriate, and we can add Windows support in future. Unfortunately the journal_* API calls are rather slow because they have to fetch each single line from the journal. So there's some work to be done to make that fast. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html