Running CentOS 6.5, qemu-kvm.

I have a script which builds a list of running VM's. I want to pull information from those .xml files of only the VM's that are running.

cat /dev/null > /<path>/runlist virsh list --all|grep running|awk -F" " '{print $2}' > /<path>/runlist.tmp

Now that I have the list of running vm's, I want to match them to their appropriate .xml files in a directory listing that includes the .xml files of all of our vm's and pull bits of information out.

I know that I can:

grep <info> </path/file.xml>

But how do I get that middle bit that will parse the runlist file against the directory listing and only search for the information from the files that are pertinent to the running VM's.

Sample xml file:

<domain type='kvm'>
  <memory unit='KiB'>"X"</memory>
  <currentMemory unit='KiB'>"X"</currentMemory>
  <vcpu placement='static'>1</vcpu>
    <type arch='x86_64' machine='X'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <source file='/path/file.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    <interface type='bridge'>
      <mac address='X'/>
      <source bridge='br1'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <serial type='pty'>
      <target port='0'/>
    <console type='pty'>
      <target type='serial' port='0'/>
    <input type='tablet' bus='usb'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  • You'll need to show us that xml file. – glenn jackman Aug 26 '14 at 16:06
  • An update: I am using sed to append the .xml file extension to the end of each vm name in the runlist file. – Matthew Lee Allen-Goebel Aug 26 '14 at 16:18
  • @MatthewLeeAllen-Goebel: So you if runlist have a.xml and b.xml, you only want to search <info> from those files? – cuonglm Aug 26 '14 at 16:21
  • awk can do what grep does: virsh | awk '/running/ {print $2} – glenn jackman Aug 26 '14 at 16:22
  • @Gnouc - yes. I want to search a.xml and b.xml, but not c.xml or d.xml. – Matthew Lee Allen-Goebel Aug 26 '14 at 16:41

The direct answer to your question is

grep -f /path/to/runlist /path/to/file.xml

But I think this is an XY question: without thinking about how to solve the question, what are you trying to do?

Given that runlist is a file containing xml filenames, I'd use an xml processing tool (like xmlstarlet) to extract the vcpu:

$ cat runlist
$ xmlstarlet sel -t -v /domain/name -o $'\t' -v /domain/currentMemory -o $'\t' -v /domain/vcpu -nl $(< ./runlist)
"X"     "X"     1
"XX"    256     16
  • I want to grab 'vcpu', disk, and memory information to calculate and total up. The purpose is to notify system admins and users building VM's that they have enough resources left on a Host server to build a virtual guest, and to kill a build if there are not enough resources. – Matthew Lee Allen-Goebel Aug 26 '14 at 16:47
  • Thus, you need to know what is running, what resources it uses, and how much left you have. – Matthew Lee Allen-Goebel Aug 26 '14 at 16:48
  • grep is not the best tool for parsing xml. Why don't you provide a sample xml file? – glenn jackman Aug 26 '14 at 17:24
  • There are only two pieces of information I need out of this (see edited answer) XML file. The "vcpu" line and the "current memory" line. But I need those bits of information for each running virtual guest on the host machine. – Matthew Lee Allen-Goebel Aug 26 '14 at 19:08

It seems that you only want to get information from list of files in runlist. You can:

grep <info> -- $(cat runlist)
  • Thanks! command is now thus: 'grep vcpu $(cat runlist)|awk -F":" '{print $2}'|sed s,\>,\ ,g|sed s,\<,\ ,g|awk -F" " '{print $3}' > /<path>/core' – Matthew Lee Allen-Goebel Aug 26 '14 at 17:54
  • This works from the command line, but unfortunately doesn't work within a bash script. Keeps throwing an unknow file/directory error. – Matthew Lee Allen-Goebel Aug 27 '14 at 16:21
  • @MatthewLeeAllen-Goebel: You must use /path/to/runlist if use in script. – cuonglm Aug 27 '14 at 16:23
  • I tried a fully qualified path. It still threw the error. – Matthew Lee Allen-Goebel Aug 27 '14 at 16:24
  • Weird. Try the raw data link: pastebin.com/raw.php?i=6JLvgkiy – Matthew Lee Allen-Goebel Aug 27 '14 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.