]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
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 | PVE::JSONSchema::register_standard_option('skiplock', { | |
41 | description => "Ignore locks - only root is allowed to use this option.", | |
42 | type => 'boolean', | |
43 | optional => 1, | |
44 | }); | |
45 | ||
46 | sub 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 { | |
188 | PVE::QemuServer::change_config_nolock ($vmid, {}, { lock => 1 }, 1); | |
189 | }); | |
190 | ||
191 | return undef; | |
192 | }}); | |
193 | ||
194 | __PACKAGE__->register_method ({ | |
195 | name => 'mtunnel', | |
196 | path => 'mtunnel', | |
197 | method => 'POST', | |
32f9da68 | 198 | description => "Used by qmigrate - do not use manually.", |
1e3baf05 DM |
199 | parameters => { |
200 | additionalProperties => 0, | |
201 | properties => {}, | |
202 | }, | |
203 | returns => { type => 'null'}, | |
204 | code => sub { | |
205 | my ($param) = @_; | |
206 | ||
32f9da68 DM |
207 | if (!PVE::Cluster::check_cfs_quorum(1)) { |
208 | print "no quorum\n"; | |
209 | return undef; | |
210 | } | |
211 | ||
1e3baf05 DM |
212 | print "tunnel online\n"; |
213 | *STDOUT->flush(); | |
214 | ||
215 | while (my $line = <>) { | |
216 | chomp $line; | |
217 | last if $line =~ m/^quit$/; | |
218 | } | |
219 | ||
220 | return undef; | |
221 | }}); | |
222 | ||
223 | __PACKAGE__->register_method ({ | |
224 | name => 'startall', | |
225 | path => 'startall', | |
226 | method => 'POST', | |
227 | description => "Start all virtual machines (when onboot=1).", | |
228 | parameters => { | |
229 | additionalProperties => 0, | |
230 | properties => {}, | |
231 | }, | |
232 | returns => { type => 'null'}, | |
233 | code => sub { | |
234 | my ($param) = @_; | |
235 | ||
236 | my $vzlist = PVE::QemuServer::vzlist(); | |
237 | my $storecfg = PVE::Storage::config(); | |
238 | ||
f669a2b1 DM |
239 | PVE::Cluster::check_cfs_quorum(); |
240 | ||
9db78b5e | 241 | my $count = 0; |
1e3baf05 DM |
242 | foreach my $vmid (keys %$vzlist) { |
243 | next if $vzlist->{$vmid}->{pid}; # already running | |
244 | ||
9db78b5e DM |
245 | sleep(2) if $count != 0; # reduce load |
246 | $count++; | |
247 | ||
f669a2b1 DM |
248 | PVE::Cluster::check_cfs_quorum(); # abort when we loose quorum |
249 | ||
1e3baf05 DM |
250 | eval { |
251 | my $conf = PVE::QemuServer::load_config($vmid); | |
252 | if ($conf->{onboot}) { | |
253 | print STDERR "Starting Qemu VM $vmid\n"; | |
254 | PVE::QemuServer::vm_start($storecfg, $vmid); | |
255 | } | |
256 | }; | |
257 | print STDERR $@ if $@; | |
258 | } | |
259 | ||
260 | return undef; | |
261 | }}); | |
262 | ||
263 | __PACKAGE__->register_method ({ | |
264 | name => 'stopall', | |
265 | path => 'stopall', | |
266 | method => 'POST', | |
267 | description => "Stop all virtual machines.", | |
268 | parameters => { | |
269 | additionalProperties => 0, | |
270 | properties => { | |
271 | timeout => { | |
272 | description => "Timeout in seconds. Default is to wait 3 minutes.", | |
273 | type => 'integer', | |
274 | minimum => 1, | |
275 | optional => 1, | |
276 | } | |
277 | }, | |
278 | }, | |
279 | returns => { type => 'null'}, | |
280 | code => sub { | |
281 | my ($param) = @_; | |
282 | ||
283 | my $timeout = $param->{timeout}; | |
284 | ||
285 | PVE::QemuServer::vm_stopall($timeout); | |
286 | ||
287 | return undef; | |
288 | }}); | |
289 | ||
290 | __PACKAGE__->register_method ({ | |
291 | name => 'wait', | |
292 | path => 'wait', | |
293 | method => 'GET', | |
294 | description => "Wait until the VM is stopped.", | |
295 | parameters => { | |
296 | additionalProperties => 0, | |
297 | properties => { | |
298 | vmid => get_standard_option('pve-vmid'), | |
299 | timeout => { | |
300 | description => "Timeout in seconds. Default is to wait forever.", | |
301 | type => 'integer', | |
302 | minimum => 1, | |
303 | optional => 1, | |
304 | } | |
305 | }, | |
306 | }, | |
307 | returns => { type => 'null'}, | |
308 | code => sub { | |
309 | my ($param) = @_; | |
310 | ||
311 | my $vmid = $param->{vmid}; | |
312 | my $timeout = $param->{timeout}; | |
313 | ||
314 | my $pid = PVE::QemuServer::check_running ($vmid); | |
315 | return if !$pid; | |
316 | ||
317 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
318 | ||
319 | my $count = 0; | |
320 | while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) { | |
321 | $count++; | |
322 | sleep 1; | |
323 | } | |
324 | ||
325 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); | |
326 | ||
327 | return undef; | |
328 | }}); | |
329 | ||
330 | __PACKAGE__->register_method ({ | |
331 | name => 'monitor', | |
332 | path => 'monitor', | |
333 | method => 'POST', | |
334 | description => "Enter Qemu Monitor interface.", | |
335 | parameters => { | |
336 | additionalProperties => 0, | |
337 | properties => { | |
338 | vmid => get_standard_option('pve-vmid'), | |
339 | }, | |
340 | }, | |
341 | returns => { type => 'null'}, | |
342 | code => sub { | |
343 | my ($param) = @_; | |
344 | ||
345 | my $vmid = $param->{vmid}; | |
346 | ||
347 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
348 | ||
349 | print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; | |
350 | ||
351 | my $term = new Term::ReadLine ('qm'); | |
352 | ||
353 | my $input; | |
354 | while (defined ($input = $term->readline('qm> '))) { | |
355 | chomp $input; | |
356 | ||
357 | next if $input =~ m/^\s*$/; | |
358 | ||
359 | last if $input =~ m/^\s*q(uit)?\s*$/; | |
360 | ||
361 | eval { | |
362 | print PVE::QemuServer::vm_monitor_command ($vmid, $input); | |
363 | }; | |
364 | print "ERROR: $@" if $@; | |
365 | } | |
366 | ||
367 | return undef; | |
368 | ||
369 | }}); | |
370 | ||
371 | my $cmddef = { | |
372 | list => [ "PVE::API2::Qemu", 'vmlist', [], | |
373 | { node => $nodename }, sub { | |
374 | my $vmlist = shift; | |
375 | ||
376 | exit 0 if (!scalar(@$vmlist)); | |
377 | ||
378 | printf "%10s %-20s %-10s %-10s %12s %-10s\n", | |
379 | qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); | |
380 | ||
381 | foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { | |
382 | printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, | |
383 | $rec->{status}, | |
384 | ($rec->{maxmem} || 0)/(1024*1024), | |
385 | ($rec->{maxdisk} || 0)/(1024*1024*1024), | |
386 | $rec->{pid}||0; | |
387 | } | |
388 | ||
389 | ||
390 | } ], | |
391 | ||
392 | create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename } ], | |
393 | ||
394 | destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename } ], | |
395 | ||
396 | set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], | |
397 | ||
398 | unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node => $nodename } ], | |
399 | ||
400 | config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], | |
401 | { node => $nodename }, sub { | |
402 | my $config = shift; | |
403 | foreach my $k (sort (keys %$config)) { | |
554ac7e7 | 404 | next if $k eq 'digest'; |
1e3baf05 DM |
405 | my $v = $config->{$k}; |
406 | if ($k eq 'description') { | |
407 | $v = PVE::Tools::encode_text($v); | |
408 | } | |
409 | print "$k: $v\n"; | |
410 | } | |
411 | }], | |
412 | ||
413 | showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], | |
414 | ||
415 | status => [ __PACKAGE__, 'status', ['vmid']], | |
416 | ||
417 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], | |
418 | ||
419 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
420 | ||
421 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
422 | ||
423 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], | |
424 | ||
425 | startall => [ __PACKAGE__, 'startall', []], | |
426 | ||
427 | stopall => [ __PACKAGE__, 'stopall', []], | |
428 | ||
429 | mtunnel => [ __PACKAGE__, 'mtunnel', []], | |
430 | }; | |
431 | ||
432 | sub register_vm_command { | |
433 | my ($cmd, $descr) = @_; | |
434 | ||
435 | # we create a wrapper, because we want one 'description' per command | |
436 | __PACKAGE__->register_method ({ | |
437 | name => $cmd, | |
438 | path => $cmd, | |
439 | method => 'PUT', | |
440 | description => $descr, | |
441 | parameters => { | |
442 | additionalProperties => 0, | |
443 | properties => { | |
444 | vmid => get_standard_option('pve-vmid'), | |
445 | skiplock => get_standard_option('skiplock'), | |
446 | }, | |
447 | }, | |
448 | returns => { type => 'null'}, | |
449 | code => sub { | |
450 | my ($param) = @_; | |
451 | ||
452 | $param->{command} = $cmd; | |
453 | $param->{node} = $nodename; | |
454 | ||
455 | return PVE::API2::Qemu->vm_command($param); | |
456 | }}); | |
457 | ||
458 | $cmddef->{$cmd} = [ __PACKAGE__, $cmd, ['vmid']]; | |
459 | } | |
460 | ||
461 | register_vm_command('start', "Start virtual machine."); | |
462 | register_vm_command('stop', "Stop virtual machine."); | |
463 | register_vm_command('reset', "Reset virtual machine."); | |
464 | register_vm_command('shutdown', "Shutdown virtual machine (send ACPI showdown request)"); | |
465 | register_vm_command('suspend', "Suspend virtual machine."); | |
466 | register_vm_command('resume', "Resume virtual machine."); | |
467 | register_vm_command('cad', "Send CTRL-ALT-DELETE keys."); | |
468 | ||
469 | my $cmd = shift; | |
470 | ||
471 | PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0); | |
472 | ||
473 | exit 0; | |
474 | ||
475 | __END__ | |
476 | ||
477 | =head1 NAME | |
478 | ||
479 | qm - qemu/kvm virtual machine manager | |
480 | ||
481 | =head1 SYNOPSIS | |
482 | ||
483 | =include synopsis | |
484 | ||
485 | =head1 DESCRIPTION | |
486 | ||
487 | qm is a script to manage virtual machines with qemu/kvm. You can | |
488 | create and destroy virtual machines, and control execution | |
489 | (start/stop/suspend/resume). Besides that, you can use qm to set | |
490 | parameters in the associated config file. It is also possible to | |
491 | create and delete virtual disks. | |
492 | ||
f661e766 DM |
493 | =head1 CONFIGURATION |
494 | ||
495 | All configuration files consists of lines in the form | |
496 | ||
497 | PARAMETER: value | |
498 | ||
499 | See L<vm.conf|vm.conf> for a complete list of options. | |
500 | ||
501 | Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>. | |
502 | ||
503 | The default for option 'keyboard' is read from | |
504 | F</etc/pve/datacenter.conf>. | |
505 | ||
506 | =head1 Locks | |
507 | ||
508 | Online migration and backups (vzdump) set a lock to prevent | |
509 | unintentional action on such VMs. Sometimes you need remove such lock | |
510 | manually (power failure). | |
511 | ||
512 | qm unlock <vmid> | |
513 | ||
514 | =head1 EXAMPLES | |
515 | ||
516 | # create a new VM with 4 GB ide disk | |
517 | qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso | |
518 | ||
519 | # start the new VM | |
520 | qm start 300 | |
521 | ||
522 | # send shutdown, then wait until VM is stopped | |
523 | qm shutdown 300 && qm wait 300 | |
524 | ||
525 | # same as above, but only wait for 40 seconds | |
526 | qm shutdown 300 && qm wait 300 -timeout 40 | |
527 | ||
528 | ||
1e3baf05 | 529 | =include pve_copyright |