[pve-devel] r5123 - in qemu-server/pve2: . PVE/API2

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Sep 13 12:39:03 CEST 2010


Author: dietmar
Date: 2010-09-13 10:39:03 +0000 (Mon, 13 Sep 2010)
New Revision: 5123

Added:
   qemu-server/pve2/PVE/API2/QemuServerStatus.pm
Modified:
   qemu-server/pve2/ChangeLog
   qemu-server/pve2/PVE/API2/Makefile
   qemu-server/pve2/PVE/API2/QemuServer.pm
   qemu-server/pve2/QemuServer.pm
   qemu-server/pve2/nqm
   qemu-server/pve2/qm
Log:


Modified: qemu-server/pve2/ChangeLog
===================================================================
--- qemu-server/pve2/ChangeLog	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/ChangeLog	2010-09-13 10:39:03 UTC (rev 5123)
@@ -1,5 +1,7 @@
 2010-09-13  Proxmox Support Team  <support at proxmox.com>
 
+	* PVE/API2/QemuServerStatus.pm: implement vm_command()
+
 	* nqm: implement monitor command
 
 	* QemuServer.pm (vm_monitor_command): remove $self - not needed at all.

Modified: qemu-server/pve2/PVE/API2/Makefile
===================================================================
--- qemu-server/pve2/PVE/API2/Makefile	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/PVE/API2/Makefile	2010-09-13 10:39:03 UTC (rev 5123)
@@ -2,3 +2,4 @@
 .PHONY: install
 install:
 	install -D -m 0644 QemuServer.pm ${DESTDIR}${PERLDIR}/PVE/API2/QemuServer.pm
+	install -D -m 0644 QemuServerStatus.pm ${DESTDIR}${PERLDIR}/PVE/API2/QemuServerStatus.pm

Modified: qemu-server/pve2/PVE/API2/QemuServer.pm
===================================================================
--- qemu-server/pve2/PVE/API2/QemuServer.pm	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/PVE/API2/QemuServer.pm	2010-09-13 10:39:03 UTC (rev 5123)
@@ -18,15 +18,7 @@
 #
 ## GET    /vms/
 ## GET    /vms/index  status of all VMs (cluster wide)
-# GET    /vms/qm  status of all Qemu VMs (cluster wide)
-# GET    /vms/qm/{node}/  status of all Qemu VMs (per node)
-# POST   /vms/qm/{node}/  create VM
-# GET    /vms/qm/{node}/{vmid}  get VM config
-# PUT    /vms/qm/{node}/{vmid}  set VM config/status
-# DELETE /vms/qm/{node}/{vmid}  destroy VM
 
-##or
-
 # GET    /vms/config/qm/{node}/  list of all Qemu VMs (per node)
 # POST   /vms/config/qm/{node}/  create VM
 # GET    /vms/config/qm/{node}/{vmid}  get VM config
@@ -38,39 +30,11 @@
 # GET    /vms/status/qm/{node}/{vmid}  get VM status
 # PUT    /vms/status/qm/{node}/{vmid}  set VM status
 
-
 use base qw(PVE::RESTHandler);
 
 __PACKAGE__->register_method ({
-    name => 'index', 
-    path => '', 
-    method => 'GET',
-    description => "Virtual machine index (cluster wide).",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {},
-    },
-    returns => {
-	type => 'array',
-	items => {
-	    type => "object",
-	    properties => {},
-	},
-	links => [ { rel => 'child', href => "{vmid}" } ],
-    },
-    code => sub {
-	my ($param) = @_;
-
-	# fixme: not implemented
-	die "not implemented";
-
-	return [{ vmid => 100 }];
-
-    }});
-
-__PACKAGE__->register_method ({
     name => 'vmlist', 
-    path => 'config/{node}', 
+    path => '{node}', 
     method => 'GET',
     description => "Virtual machine index.",
     parameters => {
@@ -98,7 +62,7 @@
 
 __PACKAGE__->register_method ({
     name => 'create_vm', 
-    path => 'config/{node}', 
+    path => '{node}', 
     method => 'POST',
     description => "Create new virtual machine.",
     parameters => {
@@ -178,7 +142,7 @@
 
 __PACKAGE__->register_method ({
     name => 'update_vm', 
-    path => 'config/{node}/{vmid}', 
+    path => '{node}/{vmid}', 
     method => 'PUT',
     description => "Set virtual machine options.",
     parameters => {
@@ -262,18 +226,14 @@
 
 __PACKAGE__->register_method ({
     name => 'vm_config', 
-    path => 'config/{node}/{vmid}', 
+    path => '{node}/{vmid}', 
     method => 'GET',
     description => "Get virtual machine configuration.",
     parameters => {
     	additionalProperties => 0,
 	properties => {
-	    node => { type => 'string', format => 'pve-node' },
-	    vmid => { 
-		description => "The (unique) ID of the VM.",
-		type => 'integer', format => 'pve-vmid',
-		minimum => 1,
-	    },
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid'),
 	},
     },
     returns => {},

Added: qemu-server/pve2/PVE/API2/QemuServerStatus.pm
===================================================================
--- qemu-server/pve2/PVE/API2/QemuServerStatus.pm	                        (rev 0)
+++ qemu-server/pve2/PVE/API2/QemuServerStatus.pm	2010-09-13 10:39:03 UTC (rev 5123)
@@ -0,0 +1,141 @@
+package PVE::API2::QemuServerStatus;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::INotify qw(read_file);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RESTHandler;
+use PVE::QemuServer;
+use PVE::RPCEnvironment;
+
+use Data::Dumper; # fixme: remove
+
+use base qw(PVE::RESTHandler);
+
+
+__PACKAGE__->register_method ({
+    name => 'vmlist', 
+    path => '{node}', 
+    method => 'GET',
+    description => "Virtual machine index.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => {},
+	},
+	links => [ { rel => 'child', href => "{vmid}" } ],
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmstatus =  PVE::QemuServer::vmstatus();
+
+	return PVE::RESTHandler::hash_to_array($vmstatus, 'vmid');
+
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'vm_command', 
+    path => '{node}/{vmid}', 
+    method => 'POST',
+    description => "Set virtual machine status.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => PVE::QemuServer::json_config_properties(
+	    {
+		node => get_standard_option('pve-node'),
+		vmid => get_standard_option('pve-vmid'),
+		skiplock => { 
+		    description => "Ignore locks - only root is allowed to use this option.",
+		    type => 'boolean', 
+		    optional => 1,
+		},
+		command => { type => 'string' },
+	    }),
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+
+	my $user = $rpcenv->get_user();
+
+	my $node = extract_param($param, 'node');
+
+	# fixme: proxy to correct node
+	# fixme: fork worker?
+
+	my $vmid = extract_param($param, 'vmid');
+
+	my $skiplock = extract_param($param, 'skiplock');
+	raise_param_exc({ skiplock => "Only root may use this option." }) 
+	    if $user ne 'root';
+
+	my $command = $param->{command};
+
+	my $storecfg = read_file('storagecfg');
+	
+	if ($command eq 'start') {
+	    my $statefile = undef; # fixme: --incoming parameter
+	    PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, $skiplock);
+	} elsif ($command eq 'stop') {
+	    PVE::QemuServer::vm_stop($vmid, $skiplock);
+	} elsif ($command eq 'reset') {
+	    PVE::QemuServer::vm_reset($vmid, $skiplock);
+	} elsif ($command eq 'shutdown') {
+	    PVE::QemuServer::vm_shutdown($vmid, $skiplock);
+	} elsif ($command eq 'suspend') {
+	    PVE::QemuServer::vm_suspend($vmid, $skiplock);
+	} elsif ($command eq 'resume') {
+	    PVE::QemuServer::vm_resume($vmid, $skiplock);
+	} elsif ($command eq 'cad') {
+	    PVE::QemuServer::vm_cad($vmid, $skiplock);
+	} else {
+	    raise_param_exc({ command => "unknown command '$command'" }) 
+	}
+
+	print "TESTCMD $command\n";
+
+	return undef;
+    }});
+
+
+__PACKAGE__->register_method ({
+    name => 'vm_status', 
+    path => '{node}/{vmid}', 
+    method => 'GET',
+    description => "Get virtual machine status.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid'),
+	},
+    },
+    returns => {},
+    code => sub {
+	my ($param) = @_;
+
+	my $conf = PVE::QemuServer::load_config ($param->{vmid});
+
+	# fixme: that should not be added to the config
+	delete $conf->{disksize};
+	delete $conf->{disktype};
+	delete $conf->{diskinfo};
+
+	return $conf;
+    }});
+
+1;

Modified: qemu-server/pve2/QemuServer.pm
===================================================================
--- qemu-server/pve2/QemuServer.pm	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/QemuServer.pm	2010-09-13 10:39:03 UTC (rev 5123)
@@ -907,19 +907,19 @@
 }
 
 sub print_drive_full {
-    my ($self, $vmid, $drive) = @_;
+    my ($storecfg, $vmid, $drive) = @_;
 
     my $opts = get_qemu_drive_options ($drive);
 
     my $path;
     my $volid = $drive->{file};
     if (drive_is_cdrom ($drive)) {
-	$path = get_iso_path ($self->{storecfg}, $vmid, $volid);
+	$path = get_iso_path ($storecfg, $vmid, $volid);
     } else {
 	if ($volid =~ m|^/|) {
 	    $path = $volid;
 	} else {
-	    $path = PVE::Storage::path ($self->{storecfg}, $volid);
+	    $path = PVE::Storage::path ($storecfg, $volid);
 	}
     }
 
@@ -1751,7 +1751,7 @@
 }
 
 sub config_to_command {
-    my ($self, $vmid, $conf, $defaults, $migrate_uri) = @_;
+    my ($storecfg, $vmid, $conf, $defaults, $migrate_uri) = @_;
 
     my $cmd = [];
 
@@ -1904,7 +1904,7 @@
     my $di = $conf->{diskinfo};
     foreach my $ds (sort keys %$di) {
 	$use_virtio = 1 if $ds =~ m/^virtio/;
-	my $drive = $self->print_drive_full ($vmid, $di->{$ds});
+	my $drive = print_drive_full ($storecfg, $vmid, $di->{$ds});
 	$drive .= ",boot=on" if $conf->{bootdisk} && ($conf->{bootdisk} eq $ds);
 	push @$cmd, '-drive', $drive;
     }
@@ -2034,7 +2034,7 @@
 }
 
 sub activate_volumes {
-    my ($self, $conf)  = @_;
+    my ($storecfg, $conf)  = @_;
 
     my $di = $conf->{diskinfo};
 
@@ -2047,11 +2047,11 @@
 	};
     }
 
-    PVE::Storage::activate_volumes ($self->{storecfg}, $vollist);
+    PVE::Storage::activate_volumes ($storecfg, $vollist);
 }
 
 sub vm_start {
-    my ($self, $vmid, $statefile, $skiplock) = @_;
+    my ($storecfg, $vmid, $statefile, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 	my $conf = load_config ($vmid);
@@ -2086,7 +2086,7 @@
 
 	my $defaults = load_defaults();
 
-	my $cmd = $self->config_to_command ($vmid, $conf, $defaults, $migrate_uri);
+	my $cmd = config_to_command ($storecfg, $vmid, $conf, $defaults, $migrate_uri);
 	# host pci devices
 	if (my $pcidl = $conf->{hostpci}) {
 	    my @dl = split (/,/, $pcidl);
@@ -2099,7 +2099,7 @@
 	    }
 	}
 
-	$self->activate_volumes ($conf);
+	activate_volumes ($storecfg, $conf);
 
 	eval  { run_command ($cmd, timeout => $migrate_uri ? undef : 30); };
 
@@ -2254,19 +2254,19 @@
 }
 
 sub vm_commandline {
-    my ($self, $vmid) = @_;
+    my ($storecfg, $vmid) = @_;
 
     my $conf = load_config ($vmid);
 
     my $defaults = load_defaults();
 
-    my $cmd = $self->config_to_command ($vmid, $conf, $defaults);
+    my $cmd = config_to_command ($storecfg, $vmid, $conf, $defaults);
 
     return join (' ', @$cmd);
 }
 
 sub vm_reset {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2301,7 +2301,7 @@
 }
 
 sub vm_shutdown {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2316,7 +2316,7 @@
 }
 
 sub vm_stop {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2375,7 +2375,7 @@
 }
 
 sub vm_suspend {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2390,7 +2390,7 @@
 }
 
 sub vm_resume {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2405,7 +2405,7 @@
 }
 
 sub vm_cad {
-    my ($self, $vmid, $skiplock) = @_;
+    my ($vmid, $skiplock) = @_;
 
     lock_config ($vmid, sub {
 
@@ -2523,7 +2523,7 @@
 }
 
 sub vm_stopall {
-    my ($self, $timeout) = @_;
+    my ($timeout) = @_;
 
     $timeout = 3*60 if !$timeout;
 
@@ -2542,7 +2542,7 @@
 
 	foreach my $vmid (keys %$vzlist) {
 	    next if !$vzlist->{$vmid}->{pid};
-	    eval { $self->vm_shutdown ($vmid, 1); };
+	    eval { vm_shutdown ($vmid, 1); };
 	    print STDERR $@ if $@;
 	}
 

Modified: qemu-server/pve2/nqm
===================================================================
--- qemu-server/pve2/nqm	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/nqm	2010-09-13 10:39:03 UTC (rev 5123)
@@ -9,6 +9,7 @@
 use PVE::RPCEnvironment;
 use PVE::QemuServer;
 use PVE::API2::QemuServer;
+use PVE::API2::QemuServerStatus;
 use PVE::JSONSchema qw(get_standard_option);
 use Term::ReadLine;
 
@@ -45,13 +46,67 @@
     code => sub {
 	my ($param) = @_;
 
-	my $qm = PVE::QemuServer->new();
-	print $qm->vm_commandline ($param->{vmid}) . "\n";
+	my $storecfg = read_file('storagecfg');
+	print PVE::QemuServer::vm_commandline ($storecfg, $param->{vmid}) . "\n";
 
 	return undef;
     }});
 
 __PACKAGE__->register_method ({
+    name => 'status', 
+    path => 'status', 
+    method => 'GET',
+    description => "Show VM status.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    vmid => get_standard_option('pve-vmid'),
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $status = 'unknown';
+
+	eval { 
+	    if (PVE::QemuServer::check_running($param->{vmid})) {
+		$status = 'running';
+	    } else {
+		$status = 'stopped';
+	    }
+	};
+
+	print "$status\n";
+
+	return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'unlock', 
+    path => 'unlock', 
+    method => 'PUT',
+    description => "Unlock the VM.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    vmid => get_standard_option('pve-vmid'),
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+
+	PVE::QemuServer::lock_config ($vmid, sub {
+	    PVE::QemuServer::change_config_nolock  ($vmid, {}, { lock => 1 }, 1);
+	});
+
+	return undef;
+    }});
+
+__PACKAGE__->register_method ({
     name => 'startall', 
     path => 'startall', 
     method => 'POST',
@@ -65,6 +120,7 @@
 	my ($param) = @_;
 
 	my $vzlist = PVE::QemuServer::vzlist();
+	my $storecfg = read_file('storagecfg');
 
 	foreach my $vmid (keys %$vzlist) {
 	    next if $vzlist->{$vmid}->{pid}; # already running
@@ -73,8 +129,7 @@
 		my $conf = PVE::QemuServer::load_config ($vmid);
 		if ($conf->{onboot}) {
 		    print STDERR "Starting Qemu VM $vmid\n";
-		    my $qm = PVE::QemuServer->new();
-		    $qm->vm_start ($vmid);
+		    vm_start ($storecfg, $vmid);
 		}
 	    };
 	    print STDERR $@ if $@;
@@ -84,6 +139,33 @@
     }});
 
 __PACKAGE__->register_method ({
+    name => 'stopall', 
+    path => 'stopall', 
+    method => 'POST',
+    description => "Stop all virtual machines.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    timeout => {
+		description => "Timeout in seconds. Default is to wait 3 minutes.",
+		type => 'integer',
+		minimum => 1,
+		optional => 1,
+	    }
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $timeout = $param->{timeout};
+
+	PVE::QemuServer::vm_stopall($timeout);
+
+	return undef;
+    }});
+
+__PACKAGE__->register_method ({
     name => 'wait', 
     path => 'wait', 
     method => 'GET',
@@ -163,7 +245,13 @@
     }});
 
 # fixme: unlink
+# fixme: destroy
+# fixme: vncticket
+# fixme: cdrom
 
+# fixme: vncproxy, vnc
+# fixme: mtunnel
+
 my $cmddef = {
     list => [ "PVE::API2::QemuServer", 'vmlist', [],
 	     { node => $hostname }, sub {
@@ -197,11 +285,31 @@
 	
     showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
 
+    status => [ __PACKAGE__, 'status', ['vmid']],
+
     wait => [ __PACKAGE__, 'wait', ['vmid']],
 
+    unlock => [ __PACKAGE__, 'unlock', ['vmid']],
+
     monitor  => [ __PACKAGE__, 'monitor', ['vmid']],
 
     startall => [ __PACKAGE__, 'startall', []],
+
+    stopall => [ __PACKAGE__, 'stopall', []],
+
+
+    start => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+	       { node => $hostname, command => 'start' } ],
+    stop => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+	      { node => $hostname, command => 'stop' } ],
+    reset => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+	       { node => $hostname, command => 'reset' } ],
+    suspend => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+		 { node => $hostname, command => 'suspend' } ],
+    resume => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+		{ node => $hostname, command => 'resume' } ],
+    cad => [ "PVE::API2::QemuServerStatus", 'vm_command', ['vmid'], 
+	     { node => $hostname, command => 'cad' } ],
 	
 };
 

Modified: qemu-server/pve2/qm
===================================================================
--- qemu-server/pve2/qm	2010-09-13 09:26:40 UTC (rev 5122)
+++ qemu-server/pve2/qm	2010-09-13 10:39:03 UTC (rev 5123)
@@ -466,7 +466,7 @@
 	exit (-1);
     }    
 
-    print $qm->vm_commandline ($vmid) . "\n";
+    print vm_commandline ($qm->{storecfg}, $vmid) . "\n";
 
 } elsif ($cmd eq 'start') {
     my $statefile;
@@ -477,7 +477,7 @@
 	exit (-1);
     }    
 
-    $qm->vm_start ($vmid, $statefile, $skiplock);
+    PVE::QemuServer::vm_start ($qm->{storecfg}, $vmid, $statefile, $skiplock);
 
 } elsif ($cmd eq 'startall') {
     if (scalar (@ARGV) != 0) {
@@ -493,7 +493,7 @@
 	exit (-1);
     }
     
-    $qm->vm_reset ($vmid, $skiplock);
+    PVE::QemuServer::vm_reset ($vmid, $skiplock);
 
 } elsif ($cmd eq 'shutdown') {
     if (scalar (@ARGV) != 0) {
@@ -501,7 +501,7 @@
 	exit (-1);
     }
 
-    $qm->vm_shutdown ($vmid, $skiplock);
+    vm_shutdown ($vmid, $skiplock);
 
 } elsif ($cmd eq 'wait') {
     my $timeout = shift || 0;
@@ -531,7 +531,7 @@
 	exit (-1);
     }
 
-    $qm->vm_stopall ($timeout);
+    PVE::QemuServer::vm_stopall($timeout);
 
 } elsif ($cmd eq 'stop') {
     if (scalar (@ARGV) != 0) {
@@ -539,7 +539,7 @@
 	exit (-1);
     }
 
-    $qm->vm_stop ($vmid, $skiplock);
+    vm_stop ($vmid, $skiplock);
 
 } elsif ($cmd eq 'destroy') {
     if (scalar (@ARGV) != 0) {
@@ -555,7 +555,7 @@
 	exit (-1);
     }
 
-    $qm->vm_suspend ($vmid, $skiplock);
+    PVE::QemuServer::vm_suspend ($vmid, $skiplock);
 
 } elsif ($cmd eq 'resume') {
     if (scalar (@ARGV) != 0) {
@@ -563,7 +563,7 @@
 	exit (-1);
     }
 
-    $qm->vm_resume ($vmid, $skiplock);
+    PVE::QemuServer::vm_resume ($vmid, $skiplock);
 
 } elsif ($cmd eq 'cad') {
     if (scalar (@ARGV) != 0) {
@@ -571,7 +571,7 @@
 	exit (-1);
     }
 
-    $qm->vm_cad ($vmid, $skiplock);
+    PVE::QemuServer::vm_cad ($vmid, $skiplock);
 
 } elsif ($cmd eq 'vncticket') {
     if (scalar (@ARGV) != 0) {




More information about the pve-devel mailing list