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