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