# HG changeset patch # User Heiko Schlittermann # Date 1251311822 -7200 # Node ID 94dcabb84ca8fae51cd848d23877024291ff1d5f # Parent 20a7c2277b670f5707a757f0732f6b90897f944e code: implemented a „list“ command diff -r 20a7c2277b67 -r 94dcabb84ca8 kvmtool --- a/kvmtool Wed Aug 26 17:48:45 2009 +0200 +++ b/kvmtool Wed Aug 26 20:37:02 2009 +0200 @@ -29,12 +29,13 @@ sub cmd_start(@); sub cmd_kill(@); +sub cmd_list(@); sub cmd_monitor(@); sub _import(@); sub _base_dir($) { "$opt_base/vm/$_[0]" } -sub _config_running_file($) { "$opt_base/vm/$_[0]/running_config" } -sub _config_user_file($) { "$opt_base/vm/$_[0]/config" } +sub _running_config_file($) { "$opt_base/vm/$_[0]/running_config" } +sub _user_config_file($) { "$opt_base/vm/$_[0]/config" } sub _monitor_link($) { "$opt_base/vm/$_[0]/monitor" } MAIN: { @@ -58,6 +59,7 @@ } when ("monitor") { cmd_monitor(@ARGV) } when ("kill") { cmd_kill(@ARGV) } + when ("list") { cmd_list(@ARGV) } default { pod2usage() }; } @@ -70,17 +72,17 @@ -d _base_dir $kvm or mkpath _base_dir $kvm; my $config = config->load_config_user($kvm, - defined $opt_config ? $opt_config : _config_user_file $kvm, @kvm_opts); + defined $opt_config ? $opt_config : _user_config_file $kvm, @kvm_opts); if (defined(my $pidfile = $config->get("-pidfile"))) { -d dirname $pidfile or die "$ME: directory for pidfile ($pidfile): $!\n"; } - $config->save_config_user(_config_user_file $kvm) + $config->save_user_config(_user_config_file $kvm) if defined $opt_config and defined $opt_import; - $config->save_config_running(_config_running_file $kvm, @kvm_opts); + $config->save_running_config(_running_config_file $kvm, @kvm_opts); defined(my $pid = open(VM, "-|")) or die "Can't fork: $!\n"; @@ -171,16 +173,37 @@ system("stty $termio"); } +sub cmd_list(@) { + foreach my $dir (glob(_base_dir "*")) { + my $kvm = basename $dir; + print "$kvm: "; + my ($config, $pid); + eval { + $config = config->load_running_config(_running_config_file $kvm); + $pid = slurp $config->get("-pidfile"), { chomp => 1 }; + }; + if ($@ or !kill 0 => $pid) { + print "not running"; + next; + } + + print "running ($pid)"; + + } continue { + print "\n"; + } +} + sub cmd_kill(@) { my ($kvm) = @_; pod2usage() if not defined $kvm; - my $config = config->load_config_running(_config_running_file $kvm); + my $config = config->load_running_config(_running_config_file $kvm); my $pidfile = $config->get("-pidfile"); my $pid = slurp $pidfile, { chomp => 1 }; kill $KILL => $pid; waitpid($pid, 0); - unlink $pidfile, _monitor_link $kvm, _config_running_file $kvm; + unlink $pidfile, _monitor_link $kvm, _running_config_file $kvm; } @@ -215,12 +238,12 @@ return $self; } - sub save_config_running { + sub save_running_config { my ($self, $file) = @_; DumpFile($file, $data{$self}); } - sub save_config_user { + sub save_user_config { my ($self, $file) = @_; my $fh = new IO::File ">$file" or die "$file: $!\n"; $fh->print( @@ -231,7 +254,7 @@ ); } - sub load_config_running { + sub load_running_config { my ($class, $file) = @_; my $self = bless do { \my $x } => $class; @@ -269,6 +292,7 @@ kvmtool [options] start [kvm-options] kvmtool [options] monitor kvmtool [options] kill + kvmtool [options] list =head1 DESCRITPTION