LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-14-2011, 09:05 PM   #1
chucklesp9
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Rep: Reputation: Disabled
Getting KVM guest names and writing them to an array


I have looked all over and am unable to find a good answer.

I need to get the names of all KVM guests running and write them to an array so I can validate the names against a database.

Thanks for the help!
 
Old 12-15-2011, 09:05 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What, where, what?

Get the lists of guests from where? What program do you use to get the info you want, and in what format is the output? And put them into what kind of array? What language are we talking about? A shell script? And most important, what have you tried to do so far?

The more details you provide, the greater the chance someone will be able to help you.

And when you do, please remember to use [code][/code] tags around your code and data, to preserve formatting and to improve readability.
 
Old 12-15-2011, 08:12 PM   #3
chucklesp9
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I am running Fedora 16 with QEMU-KVM installed running 4 VMs. I have to automate the testing for checking that the names of the VMs match the names given in a MySQL database.

I know that the following will display the running VMs in the terminal:
Code:
virsh list
I then tried using the above command as a function and reading the guest names from it. Here is the code I am using:

Code:
function name()
{ virsh list
}
arr=name
echo ${arr[*]}
I tried this with no success as it only writes the word "name" to the array. I have tried adding a $ before the function name with no success.

I have also tired gathering the names from the following file:
/etc/libvirt/qemu

This is the file where all the .xml config files are stored for each VM. The only problem I have with writing these to an array is that I have exclude anything that does not have VM in the name (eg. VM7.xml).
 
Old 12-16-2011, 08:27 AM   #4
chucklesp9
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Sorry I forgot to post the output of the virsh list command and mention that you have to be logged in as root in the terminal.

Code:
[root@linux /]# virsh list
 Id Name                 State
----------------------------------
  1 VM2                  running
  2 VM3                  running
  3 VM6                  running
  4 VM1                  running
 
Old 12-16-2011, 08:57 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
arr=name
This is not the proper syntax for setting an array. All this does is set the simple variable "arr" to the string value "name".

See here for the correct forms for setting and using arrays:

http://mywiki.wooledge.org/BashGuide/Arrays
http://mywiki.wooledge.org/BashFAQ/005

In addition, a function acts as a command, just like any other, which means it only executes if it's the first word in a command string. To use the output of one command inside another, you generally have to use a command or process substitution, or similar nested command form.

http://mywiki.wooledge.org/BashGuide...ands#Functions
http://mywiki.wooledge.org/CommandSubstitution
http://mywiki.wooledge.org/ProcessSubstitution

BTW, function definitions properly use either "function foo", or "foo()", but not "function foo()".

http://mywiki.wooledge.org/BashPitfa...tion_foo.28.29


From the output of your command, you have to do some post-processing in order to extract only the second field. In this case awk is probably the best choice to use. So overall, you'd probably want to do something like this:

Code:
name(){
	virsh list | awk '( $1 ~ /^[0-9]+$/ ) { print $2 }'
}

arr=( $( name ) )
echo "${arr[*]}"
...Or perhaps put the whole thing in the function, with something like this:

Code:
name(){
	mapfile -t arr < <( virsh list | awk '( $1 ~ /^[0-9]$/ ) { print $2 }' )
}

name
echo "${arr[*]}"
Or even just use the command stand-alone, of course.
 
1 members found this post helpful.
Old 12-16-2011, 08:20 PM   #6
chucklesp9
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
This worked perfectly. Thanks for the help. I really appreciate it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting a KVM guest on boot up dman777 Linux - Virtualization and Cloud 3 09-13-2011 08:02 AM
LXer: Set up Spicevmc Channel on Ubuntu 11.04 as KVM Server and spice-vdagent as a KVM guest LXer Syndicated Linux News 0 06-15-2011 07:10 PM
KVM with Ubuntu guest - guest crashes when some apps are 'full screened' TonyRogers Linux - Virtualization and Cloud 0 05-15-2011 12:18 PM
Assign IP to Guest VM in KVM vzxen Linux - Virtualization and Cloud 9 09-28-2010 04:14 AM
KVM: Mouse under Windows Guest performs way better than under Centos Guest martdj Linux - Virtualization and Cloud 3 03-29-2010 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration