]> git.proxmox.com Git - qemu-server.git/blob - qm
move code from qmigrate into PVE/QemuMigrate.pm
[qemu-server.git] / qm
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5 use Fcntl ':flock';
6 use File::Path;
7 use IO::Socket::UNIX;
8 use IO::Select;
9
10 use PVE::Tools qw(extract_param);
11 use PVE::Cluster;
12 use PVE::SafeSyslog;
13 use PVE::INotify;
14 use PVE::RPCEnvironment;
15 use PVE::QemuServer;
16 use PVE::API2::Qemu;
17 use PVE::JSONSchema qw(get_standard_option);
18 use Term::ReadLine;
19
20 use PVE::CLIHandler;
21
22 use base qw(PVE::CLIHandler);
23
24 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
25
26 initlog('qm');
27
28 die "please run as root\n" if $> != 0;
29
30 PVE::INotify::inotify_init();
31
32 my $rpcenv = PVE::RPCEnvironment->init('cli');
33
34 $rpcenv->init_request();
35 $rpcenv->set_language($ENV{LANG});
36 $rpcenv->set_user('root@pam');
37
38 my $nodename = PVE::INotify::nodename();
39
40 sub run_vnc_proxy {
41 my ($vmid) = @_;
42
43 my $path = PVE::QemuServer::vnc_socket($vmid);
44
45 my $s = IO::Socket::UNIX->new(Peer => $path, Timeout => 120);
46
47 die "unable to connect to socket '$path' - $!" if !$s;
48
49 my $select = new IO::Select;
50
51 $select->add(\*STDIN);
52 $select->add($s);
53
54 my $timeout = 60*15; # 15 minutes
55
56 my @handles;
57 while ($select->count &&
58 scalar(@handles = $select->can_read ($timeout))) {
59 foreach my $h (@handles) {
60 my $buf;
61 my $n = $h->sysread($buf, 4096);
62
63 if ($h == \*STDIN) {
64 if ($n) {
65 syswrite($s, $buf);
66 } else {
67 exit(0);
68 }
69 } elsif ($h == $s) {
70 if ($n) {
71 syswrite(\*STDOUT, $buf);
72 } else {
73 exit(0);
74 }
75 }
76 }
77 }
78 exit(0);
79 }
80
81 __PACKAGE__->register_method ({
82 name => 'showcmd',
83 path => 'showcmd',
84 method => 'GET',
85 description => "Show command line which is used to start the VM (debug info).",
86 parameters => {
87 additionalProperties => 0,
88 properties => {
89 vmid => get_standard_option('pve-vmid'),
90 },
91 },
92 returns => { type => 'null'},
93 code => sub {
94 my ($param) = @_;
95
96 my $storecfg = PVE::Storage::config();
97 print PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}) . "\n";
98
99 return undef;
100 }});
101
102 __PACKAGE__->register_method ({
103 name => 'status',
104 path => 'status',
105 method => 'GET',
106 description => "Show VM status.",
107 parameters => {
108 additionalProperties => 0,
109 properties => {
110 vmid => get_standard_option('pve-vmid'),
111 verbose => {
112 description => "Verbose output format",
113 type => 'boolean',
114 optional => 1,
115 }
116 },
117 },
118 returns => { type => 'null'},
119 code => sub {
120 my ($param) = @_;
121
122 # test if VM exists
123 my $conf = PVE::QemuServer::load_config ($param->{vmid});
124
125 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
126 my $stat = $vmstatus->{$param->{vmid}};
127 if ($param->{verbose}) {
128 foreach my $k (sort (keys %$stat)) {
129 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
130 my $v = $stat->{$k};
131 next if !defined($v);
132 print "$k: $v\n";
133 }
134 } else {
135 my $status = $stat->{status} || 'unknown';
136 print "status: $status\n";
137 }
138
139 return undef;
140 }});
141
142 __PACKAGE__->register_method ({
143 name => 'vncproxy',
144 path => 'vncproxy',
145 method => 'PUT',
146 description => "Proxy VM VNC traffic to stdin/stdout",
147 parameters => {
148 additionalProperties => 0,
149 properties => {
150 vmid => get_standard_option('pve-vmid'),
151 },
152 },
153 returns => { type => 'null'},
154 code => sub {
155 my ($param) = @_;
156
157 my $vmid = $param->{vmid};
158
159 run_vnc_proxy ($vmid);
160
161 return undef;
162 }});
163
164 __PACKAGE__->register_method ({
165 name => 'unlock',
166 path => 'unlock',
167 method => 'PUT',
168 description => "Unlock the VM.",
169 parameters => {
170 additionalProperties => 0,
171 properties => {
172 vmid => get_standard_option('pve-vmid'),
173 },
174 },
175 returns => { type => 'null'},
176 code => sub {
177 my ($param) = @_;
178
179 my $vmid = $param->{vmid};
180
181 PVE::QemuServer::lock_config ($vmid, sub {
182 PVE::QemuServer::change_config_nolock ($vmid, {}, { lock => 1 }, 1);
183 });
184
185 return undef;
186 }});
187
188 __PACKAGE__->register_method ({
189 name => 'mtunnel',
190 path => 'mtunnel',
191 method => 'POST',
192 description => "Used by qmigrate - do not use manually.",
193 parameters => {
194 additionalProperties => 0,
195 properties => {},
196 },
197 returns => { type => 'null'},
198 code => sub {
199 my ($param) = @_;
200
201 if (!PVE::Cluster::check_cfs_quorum(1)) {
202 print "no quorum\n";
203 return undef;
204 }
205
206 print "tunnel online\n";
207 *STDOUT->flush();
208
209 while (my $line = <>) {
210 chomp $line;
211 last if $line =~ m/^quit$/;
212 }
213
214 return undef;
215 }});
216
217 __PACKAGE__->register_method ({
218 name => 'startall',
219 path => 'startall',
220 method => 'POST',
221 description => "Start all virtual machines (when onboot=1).",
222 parameters => {
223 additionalProperties => 0,
224 properties => {},
225 },
226 returns => { type => 'null'},
227 code => sub {
228 my ($param) = @_;
229
230 my $vzlist = PVE::QemuServer::vzlist();
231 my $storecfg = PVE::Storage::config();
232
233 PVE::Cluster::check_cfs_quorum();
234
235 my $count = 0;
236 foreach my $vmid (keys %$vzlist) {
237 next if $vzlist->{$vmid}->{pid}; # already running
238
239 sleep(2) if $count != 0; # reduce load
240 $count++;
241
242 PVE::Cluster::check_cfs_quorum(); # abort when we loose quorum
243
244 eval {
245 my $conf = PVE::QemuServer::load_config($vmid);
246 if ($conf->{onboot}) {
247 print STDERR "Starting Qemu VM $vmid\n";
248 PVE::QemuServer::vm_start($storecfg, $vmid);
249 }
250 };
251 print STDERR $@ if $@;
252 }
253
254 return undef;
255 }});
256
257 __PACKAGE__->register_method ({
258 name => 'stopall',
259 path => 'stopall',
260 method => 'POST',
261 description => "Stop all virtual machines.",
262 parameters => {
263 additionalProperties => 0,
264 properties => {
265 timeout => {
266 description => "Timeout in seconds. Default is to wait 3 minutes.",
267 type => 'integer',
268 minimum => 1,
269 optional => 1,
270 }
271 },
272 },
273 returns => { type => 'null'},
274 code => sub {
275 my ($param) = @_;
276
277 my $timeout = $param->{timeout};
278
279 PVE::QemuServer::vm_stopall($timeout);
280
281 return undef;
282 }});
283
284 __PACKAGE__->register_method ({
285 name => 'wait',
286 path => 'wait',
287 method => 'GET',
288 description => "Wait until the VM is stopped.",
289 parameters => {
290 additionalProperties => 0,
291 properties => {
292 vmid => get_standard_option('pve-vmid'),
293 timeout => {
294 description => "Timeout in seconds. Default is to wait forever.",
295 type => 'integer',
296 minimum => 1,
297 optional => 1,
298 }
299 },
300 },
301 returns => { type => 'null'},
302 code => sub {
303 my ($param) = @_;
304
305 my $vmid = $param->{vmid};
306 my $timeout = $param->{timeout};
307
308 my $pid = PVE::QemuServer::check_running ($vmid);
309 return if !$pid;
310
311 print "waiting until VM $vmid stopps (PID $pid)\n";
312
313 my $count = 0;
314 while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) {
315 $count++;
316 sleep 1;
317 }
318
319 die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
320
321 return undef;
322 }});
323
324 __PACKAGE__->register_method ({
325 name => 'monitor',
326 path => 'monitor',
327 method => 'POST',
328 description => "Enter Qemu Monitor interface.",
329 parameters => {
330 additionalProperties => 0,
331 properties => {
332 vmid => get_standard_option('pve-vmid'),
333 },
334 },
335 returns => { type => 'null'},
336 code => sub {
337 my ($param) = @_;
338
339 my $vmid = $param->{vmid};
340
341 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
342
343 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
344
345 my $term = new Term::ReadLine ('qm');
346
347 my $input;
348 while (defined ($input = $term->readline('qm> '))) {
349 chomp $input;
350
351 next if $input =~ m/^\s*$/;
352
353 last if $input =~ m/^\s*q(uit)?\s*$/;
354
355 eval {
356 print PVE::QemuServer::vm_monitor_command ($vmid, $input);
357 };
358 print "ERROR: $@" if $@;
359 }
360
361 return undef;
362
363 }});
364
365 my $cmddef = {
366 list => [ "PVE::API2::Qemu", 'vmlist', [],
367 { node => $nodename }, sub {
368 my $vmlist = shift;
369
370 exit 0 if (!scalar(@$vmlist));
371
372 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
373 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
374
375 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
376 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name},
377 $rec->{status},
378 ($rec->{maxmem} || 0)/(1024*1024),
379 ($rec->{maxdisk} || 0)/(1024*1024*1024),
380 $rec->{pid}||0;
381 }
382
383
384 } ],
385
386 create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename } ],
387
388 destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename } ],
389
390 migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['target', 'vmid'], { node => $nodename },
391 sub {
392 my $upid = shift;
393 my $status = PVE::Tools::upid_read_status($upid);
394 exit($status eq 'OK' ? 0 : -1);
395 }],
396
397 set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ],
398
399 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node => $nodename } ],
400
401 config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
402 { node => $nodename }, sub {
403 my $config = shift;
404 foreach my $k (sort (keys %$config)) {
405 next if $k eq 'digest';
406 my $v = $config->{$k};
407 if ($k eq 'description') {
408 $v = PVE::Tools::encode_text($v);
409 }
410 print "$k: $v\n";
411 }
412 }],
413
414 showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
415
416 status => [ __PACKAGE__, 'status', ['vmid']],
417
418 vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']],
419
420 wait => [ __PACKAGE__, 'wait', ['vmid']],
421
422 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
423
424 monitor => [ __PACKAGE__, 'monitor', ['vmid']],
425
426 startall => [ __PACKAGE__, 'startall', []],
427
428 stopall => [ __PACKAGE__, 'stopall', []],
429
430 mtunnel => [ __PACKAGE__, 'mtunnel', []],
431 };
432
433 sub register_vm_command {
434 my ($cmd, $descr) = @_;
435
436 # we create a wrapper, because we want one 'description' per command
437 __PACKAGE__->register_method ({
438 name => $cmd,
439 path => $cmd,
440 method => 'PUT',
441 description => $descr,
442 parameters => {
443 additionalProperties => 0,
444 properties => {
445 vmid => get_standard_option('pve-vmid'),
446 skiplock => get_standard_option('skiplock'),
447 stateuri => get_standard_option('pve-qm-stateuri'),
448 },
449 },
450 returns => { type => 'null'},
451 code => sub {
452 my ($param) = @_;
453
454 $param->{command} = $cmd;
455 $param->{node} = $nodename;
456
457 return PVE::API2::Qemu->vm_command($param);
458 }});
459
460 $cmddef->{$cmd} = [ __PACKAGE__, $cmd, ['vmid']];
461 }
462
463 register_vm_command('start', "Start virtual machine.");
464 register_vm_command('stop', "Stop virtual machine.");
465 register_vm_command('reset', "Reset virtual machine.");
466 register_vm_command('shutdown', "Shutdown virtual machine (send ACPI showdown request)");
467 register_vm_command('suspend', "Suspend virtual machine.");
468 register_vm_command('resume', "Resume virtual machine.");
469 register_vm_command('cad', "Send CTRL-ALT-DELETE keys.");
470
471 my $cmd = shift;
472
473 PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0);
474
475 exit 0;
476
477 __END__
478
479 =head1 NAME
480
481 qm - qemu/kvm virtual machine manager
482
483 =head1 SYNOPSIS
484
485 =include synopsis
486
487 =head1 DESCRIPTION
488
489 qm is a script to manage virtual machines with qemu/kvm. You can
490 create and destroy virtual machines, and control execution
491 (start/stop/suspend/resume). Besides that, you can use qm to set
492 parameters in the associated config file. It is also possible to
493 create and delete virtual disks.
494
495 =head1 CONFIGURATION
496
497 All configuration files consists of lines in the form
498
499 PARAMETER: value
500
501 See L<vm.conf|vm.conf> for a complete list of options.
502
503 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
504
505 The default for option 'keyboard' is read from
506 F</etc/pve/datacenter.conf>.
507
508 =head1 Locks
509
510 Online migration and backups (vzdump) set a lock to prevent
511 unintentional action on such VMs. Sometimes you need remove such lock
512 manually (power failure).
513
514 qm unlock <vmid>
515
516 =head1 EXAMPLES
517
518 # create a new VM with 4 GB ide disk
519 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
520
521 # start the new VM
522 qm start 300
523
524 # send shutdown, then wait until VM is stopped
525 qm shutdown 300 && qm wait 300
526
527 # same as above, but only wait for 40 seconds
528 qm shutdown 300 && qm wait 300 -timeout 40
529
530
531 =include pve_copyright