]>
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 | ||
2372d114 DM |
38 | my $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 |
44 | my $nodename = PVE::INotify::nodename(); |
45 | ||
1e3baf05 DM |
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 { | |
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 | ||
1e3baf05 DM |
225 | __PACKAGE__->register_method ({ |
226 | name => 'wait', | |
227 | path => 'wait', | |
228 | method => 'GET', | |
229 | description => "Wait until the VM is stopped.", | |
230 | parameters => { | |
231 | additionalProperties => 0, | |
232 | properties => { | |
233 | vmid => get_standard_option('pve-vmid'), | |
234 | timeout => { | |
235 | description => "Timeout in seconds. Default is to wait forever.", | |
236 | type => 'integer', | |
237 | minimum => 1, | |
238 | optional => 1, | |
239 | } | |
240 | }, | |
241 | }, | |
242 | returns => { type => 'null'}, | |
243 | code => sub { | |
244 | my ($param) = @_; | |
245 | ||
246 | my $vmid = $param->{vmid}; | |
247 | my $timeout = $param->{timeout}; | |
248 | ||
249 | my $pid = PVE::QemuServer::check_running ($vmid); | |
250 | return if !$pid; | |
251 | ||
252 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
253 | ||
254 | my $count = 0; | |
255 | while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) { | |
256 | $count++; | |
257 | sleep 1; | |
258 | } | |
259 | ||
260 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); | |
261 | ||
262 | return undef; | |
263 | }}); | |
264 | ||
265 | __PACKAGE__->register_method ({ | |
266 | name => 'monitor', | |
267 | path => 'monitor', | |
268 | method => 'POST', | |
269 | description => "Enter Qemu Monitor interface.", | |
270 | parameters => { | |
271 | additionalProperties => 0, | |
272 | properties => { | |
273 | vmid => get_standard_option('pve-vmid'), | |
274 | }, | |
275 | }, | |
276 | returns => { type => 'null'}, | |
277 | code => sub { | |
278 | my ($param) = @_; | |
279 | ||
280 | my $vmid = $param->{vmid}; | |
281 | ||
282 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
283 | ||
284 | print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; | |
285 | ||
286 | my $term = new Term::ReadLine ('qm'); | |
287 | ||
288 | my $input; | |
289 | while (defined ($input = $term->readline('qm> '))) { | |
290 | chomp $input; | |
291 | ||
292 | next if $input =~ m/^\s*$/; | |
293 | ||
294 | last if $input =~ m/^\s*q(uit)?\s*$/; | |
295 | ||
296 | eval { | |
9df5cbcc | 297 | print PVE::QemuServer::vm_human_monitor_command ($vmid, $input); |
1e3baf05 DM |
298 | }; |
299 | print "ERROR: $@" if $@; | |
300 | } | |
301 | ||
302 | return undef; | |
303 | ||
304 | }}); | |
305 | ||
c21536ab DM |
306 | __PACKAGE__->register_method ({ |
307 | name => 'rescan', | |
308 | path => 'rescan', | |
309 | method => 'POST', | |
310 | description => "Rescan all storages and update disk sizes and unused disk images.", | |
311 | parameters => { | |
312 | additionalProperties => 0, | |
313 | properties => { | |
314 | vmid => get_standard_option('pve-vmid', {optional => 1}), | |
315 | }, | |
316 | }, | |
317 | returns => { type => 'null'}, | |
318 | code => sub { | |
319 | my ($param) = @_; | |
320 | ||
321 | my $cfg = PVE::Cluster::cfs_read_file("storage.cfg"); | |
322 | ||
323 | my $info = PVE::Storage::vdisk_list($cfg, undef, $param->{vmid}); | |
324 | ||
325 | my $volid_hash = {}; | |
326 | foreach my $storeid (keys %$info) { | |
327 | foreach my $item (@{$info->{$storeid}}) { | |
328 | next if !($item->{volid} && $item->{size}); | |
329 | $volid_hash->{$item->{volid}} = $item; | |
330 | } | |
331 | } | |
332 | ||
333 | my $updatefn = sub { | |
334 | my ($vmid) = @_; | |
335 | ||
336 | my $conf = PVE::QemuServer::load_config($vmid); | |
337 | ||
338 | PVE::QemuServer::check_lock($conf); | |
339 | ||
340 | my $changes; | |
341 | ||
342 | my $used = {}; | |
343 | ||
344 | # update size info | |
345 | foreach my $opt (keys %$conf) { | |
346 | if (PVE::QemuServer::valid_drivename($opt)) { | |
347 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
348 | my $volid = $drive->{file}; | |
349 | next if !$volid; | |
350 | ||
351 | $used->{$volid} = 1; | |
352 | ||
353 | next if PVE::QemuServer::drive_is_cdrom($drive); | |
354 | next if !$volid_hash->{$volid}; | |
355 | ||
356 | $drive->{size} = $volid_hash->{$volid}->{size}; | |
357 | $changes = 1; | |
358 | $conf->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
359 | } | |
360 | } | |
361 | ||
362 | # add unused volumes | |
363 | foreach my $storeid (keys %$info) { | |
364 | foreach my $item (@{$info->{$storeid}}) { | |
365 | next if !($item->{volid} && $item->{vmid}); | |
366 | next if $item->{vmid} ne $vmid; | |
367 | next if $used->{$item->{volid}}; | |
368 | $changes = 1; | |
369 | PVE::QemuServer::add_unused_volume($conf, $item->{volid}); | |
370 | } | |
371 | } | |
372 | ||
373 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1) if $changes; | |
374 | }; | |
375 | ||
376 | if (defined($param->{vmid})) { | |
377 | PVE::QemuServer::lock_config($param->{vmid}, $updatefn, $param->{vmid}); | |
378 | } else { | |
379 | my $vmlist = PVE::QemuServer::config_list(); | |
380 | foreach my $vmid (keys %$vmlist) { | |
381 | PVE::QemuServer::lock_config($vmid, $updatefn, $vmid); | |
382 | } | |
383 | } | |
384 | ||
385 | return undef; | |
386 | }}); | |
387 | ||
1e3baf05 DM |
388 | my $cmddef = { |
389 | list => [ "PVE::API2::Qemu", 'vmlist', [], | |
390 | { node => $nodename }, sub { | |
391 | my $vmlist = shift; | |
392 | ||
393 | exit 0 if (!scalar(@$vmlist)); | |
394 | ||
395 | printf "%10s %-20s %-10s %-10s %12s %-10s\n", | |
396 | qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); | |
397 | ||
398 | foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { | |
399 | printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, | |
400 | $rec->{status}, | |
401 | ($rec->{maxmem} || 0)/(1024*1024), | |
402 | ($rec->{maxdisk} || 0)/(1024*1024*1024), | |
403 | $rec->{pid}||0; | |
404 | } | |
405 | ||
406 | ||
407 | } ], | |
408 | ||
2372d114 | 409 | create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ], |
1e3baf05 | 410 | |
2372d114 | 411 | destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], |
1e3baf05 | 412 | |
ac021d2e | 413 | migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ], |
3ea94c60 | 414 | |
1e3baf05 DM |
415 | set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], |
416 | ||
2f48a4f5 | 417 | resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], |
0d02881c | 418 | |
1e3baf05 DM |
419 | unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node => $nodename } ], |
420 | ||
421 | config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], | |
422 | { node => $nodename }, sub { | |
423 | my $config = shift; | |
424 | foreach my $k (sort (keys %$config)) { | |
554ac7e7 | 425 | next if $k eq 'digest'; |
1e3baf05 DM |
426 | my $v = $config->{$k}; |
427 | if ($k eq 'description') { | |
428 | $v = PVE::Tools::encode_text($v); | |
429 | } | |
430 | print "$k: $v\n"; | |
431 | } | |
432 | }], | |
433 | ||
434 | showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], | |
435 | ||
9dbd1ee4 AD |
436 | snapshot => [ "PVE::API2::Qemu", 'snapshot_vm', ['vmid', 'action', 'snapname'], { node => $nodename } ], |
437 | ||
1e3baf05 DM |
438 | status => [ __PACKAGE__, 'status', ['vmid']], |
439 | ||
2372d114 | 440 | start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ], |
5fdbe4f0 | 441 | |
2372d114 | 442 | stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ], |
5fdbe4f0 | 443 | |
2372d114 | 444 | reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ], |
5fdbe4f0 | 445 | |
2372d114 | 446 | shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], |
5fdbe4f0 | 447 | |
2372d114 | 448 | suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], |
5fdbe4f0 | 449 | |
2372d114 | 450 | resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], |
5fdbe4f0 DM |
451 | |
452 | sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], | |
453 | ||
1e3baf05 DM |
454 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], |
455 | ||
456 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
457 | ||
458 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
459 | ||
c21536ab DM |
460 | rescan => [ __PACKAGE__, 'rescan', []], |
461 | ||
1e3baf05 DM |
462 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], |
463 | ||
1e3baf05 DM |
464 | mtunnel => [ __PACKAGE__, 'mtunnel', []], |
465 | }; | |
466 | ||
1e3baf05 DM |
467 | my $cmd = shift; |
468 | ||
f91b2e45 DM |
469 | # Note: disable '+' prefix for Getopt::Long (for resize command) |
470 | use Getopt::Long qw(:config no_getopt_compat); | |
471 | ||
1e3baf05 DM |
472 | PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0); |
473 | ||
474 | exit 0; | |
475 | ||
476 | __END__ | |
477 | ||
478 | =head1 NAME | |
479 | ||
480 | qm - qemu/kvm virtual machine manager | |
481 | ||
482 | =head1 SYNOPSIS | |
483 | ||
484 | =include synopsis | |
485 | ||
486 | =head1 DESCRIPTION | |
487 | ||
488 | qm is a script to manage virtual machines with qemu/kvm. You can | |
489 | create and destroy virtual machines, and control execution | |
490 | (start/stop/suspend/resume). Besides that, you can use qm to set | |
491 | parameters in the associated config file. It is also possible to | |
492 | create and delete virtual disks. | |
493 | ||
f661e766 DM |
494 | =head1 CONFIGURATION |
495 | ||
496 | All configuration files consists of lines in the form | |
497 | ||
498 | PARAMETER: value | |
499 | ||
500 | See L<vm.conf|vm.conf> for a complete list of options. | |
501 | ||
502 | Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>. | |
503 | ||
504 | The default for option 'keyboard' is read from | |
505 | F</etc/pve/datacenter.conf>. | |
506 | ||
507 | =head1 Locks | |
508 | ||
509 | Online migration and backups (vzdump) set a lock to prevent | |
510 | unintentional action on such VMs. Sometimes you need remove such lock | |
511 | manually (power failure). | |
512 | ||
513 | qm unlock <vmid> | |
514 | ||
515 | =head1 EXAMPLES | |
516 | ||
517 | # create a new VM with 4 GB ide disk | |
518 | qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso | |
519 | ||
520 | # start the new VM | |
521 | qm start 300 | |
522 | ||
523 | # send shutdown, then wait until VM is stopped | |
524 | qm shutdown 300 && qm wait 300 | |
525 | ||
526 | # same as above, but only wait for 40 seconds | |
527 | qm shutdown 300 && qm wait 300 -timeout 40 | |
528 | ||
529 | ||
1e3baf05 | 530 | =include pve_copyright |