]>
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 | ||
1e3baf05 DM |
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', | |
32f9da68 | 192 | description => "Used by qmigrate - do not use manually.", |
1e3baf05 DM |
193 | parameters => { |
194 | additionalProperties => 0, | |
195 | properties => {}, | |
196 | }, | |
197 | returns => { type => 'null'}, | |
198 | code => sub { | |
199 | my ($param) = @_; | |
200 | ||
32f9da68 DM |
201 | if (!PVE::Cluster::check_cfs_quorum(1)) { |
202 | print "no quorum\n"; | |
203 | return undef; | |
204 | } | |
205 | ||
1e3baf05 DM |
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 | ||
f669a2b1 DM |
233 | PVE::Cluster::check_cfs_quorum(); |
234 | ||
9db78b5e | 235 | my $count = 0; |
1e3baf05 DM |
236 | foreach my $vmid (keys %$vzlist) { |
237 | next if $vzlist->{$vmid}->{pid}; # already running | |
238 | ||
9db78b5e DM |
239 | sleep(2) if $count != 0; # reduce load |
240 | $count++; | |
241 | ||
f669a2b1 DM |
242 | PVE::Cluster::check_cfs_quorum(); # abort when we loose quorum |
243 | ||
1e3baf05 DM |
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 | ||
3ea94c60 DM |
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 | ||
1e3baf05 DM |
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)) { | |
554ac7e7 | 405 | next if $k eq 'digest'; |
1e3baf05 DM |
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 | ||
5fdbe4f0 DM |
418 | start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } ], |
419 | ||
420 | stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename } ], | |
421 | ||
422 | reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename } ], | |
423 | ||
424 | shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename } ], | |
425 | ||
426 | suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename } ], | |
427 | ||
428 | resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename } ], | |
429 | ||
430 | sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], | |
431 | ||
1e3baf05 DM |
432 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], |
433 | ||
434 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
435 | ||
436 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
437 | ||
438 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], | |
439 | ||
440 | startall => [ __PACKAGE__, 'startall', []], | |
441 | ||
442 | stopall => [ __PACKAGE__, 'stopall', []], | |
443 | ||
444 | mtunnel => [ __PACKAGE__, 'mtunnel', []], | |
445 | }; | |
446 | ||
1e3baf05 DM |
447 | my $cmd = shift; |
448 | ||
449 | PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0); | |
450 | ||
451 | exit 0; | |
452 | ||
453 | __END__ | |
454 | ||
455 | =head1 NAME | |
456 | ||
457 | qm - qemu/kvm virtual machine manager | |
458 | ||
459 | =head1 SYNOPSIS | |
460 | ||
461 | =include synopsis | |
462 | ||
463 | =head1 DESCRIPTION | |
464 | ||
465 | qm is a script to manage virtual machines with qemu/kvm. You can | |
466 | create and destroy virtual machines, and control execution | |
467 | (start/stop/suspend/resume). Besides that, you can use qm to set | |
468 | parameters in the associated config file. It is also possible to | |
469 | create and delete virtual disks. | |
470 | ||
f661e766 DM |
471 | =head1 CONFIGURATION |
472 | ||
473 | All configuration files consists of lines in the form | |
474 | ||
475 | PARAMETER: value | |
476 | ||
477 | See L<vm.conf|vm.conf> for a complete list of options. | |
478 | ||
479 | Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>. | |
480 | ||
481 | The default for option 'keyboard' is read from | |
482 | F</etc/pve/datacenter.conf>. | |
483 | ||
484 | =head1 Locks | |
485 | ||
486 | Online migration and backups (vzdump) set a lock to prevent | |
487 | unintentional action on such VMs. Sometimes you need remove such lock | |
488 | manually (power failure). | |
489 | ||
490 | qm unlock <vmid> | |
491 | ||
492 | =head1 EXAMPLES | |
493 | ||
494 | # create a new VM with 4 GB ide disk | |
495 | qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso | |
496 | ||
497 | # start the new VM | |
498 | qm start 300 | |
499 | ||
500 | # send shutdown, then wait until VM is stopped | |
501 | qm shutdown 300 && qm wait 300 | |
502 | ||
503 | # same as above, but only wait for 40 seconds | |
504 | qm shutdown 300 && qm wait 300 -timeout 40 | |
505 | ||
506 | ||
1e3baf05 | 507 | =include pve_copyright |