]>
git.proxmox.com Git - qemu-server.git/blob - qm
10 use PVE
::Tools
qw(extract_param);
14 use PVE
::RPCEnvironment
;
17 use PVE
::JSONSchema
qw(get_standard_option);
22 use base
qw(PVE::CLIHandler);
24 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
28 die "please run as root\n" if $> != 0;
30 PVE
::INotify
::inotify_init
();
32 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
34 $rpcenv->init_request();
35 $rpcenv->set_language($ENV{LANG
});
36 $rpcenv->set_user('root@pam');
38 my $nodename = PVE
::INotify
::nodename
();
43 my $path = PVE
::QemuServer
::vnc_socket
($vmid);
45 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
47 die "unable to connect to socket '$path' - $!" if !$s;
49 my $select = new IO
::Select
;
51 $select->add(\
*STDIN
);
54 my $timeout = 60*15; # 15 minutes
57 while ($select->count &&
58 scalar(@handles = $select->can_read ($timeout))) {
59 foreach my $h (@handles) {
61 my $n = $h->sysread($buf, 4096);
71 syswrite(\
*STDOUT
, $buf);
81 __PACKAGE__-
>register_method ({
85 description
=> "Show command line which is used to start the VM (debug info).",
87 additionalProperties
=> 0,
89 vmid
=> get_standard_option
('pve-vmid'),
92 returns
=> { type
=> 'null'},
96 my $storecfg = PVE
::Storage
::config
();
97 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
102 __PACKAGE__-
>register_method ({
106 description
=> "Show VM status.",
108 additionalProperties
=> 0,
110 vmid
=> get_standard_option
('pve-vmid'),
112 description
=> "Verbose output format",
118 returns
=> { type
=> 'null'},
123 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
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
131 next if !defined($v);
135 my $status = $stat->{status
} || 'unknown';
136 print "status: $status\n";
142 __PACKAGE__-
>register_method ({
146 description
=> "Proxy VM VNC traffic to stdin/stdout",
148 additionalProperties
=> 0,
150 vmid
=> get_standard_option
('pve-vmid'),
153 returns
=> { type
=> 'null'},
157 my $vmid = $param->{vmid
};
159 run_vnc_proxy
($vmid);
164 __PACKAGE__-
>register_method ({
168 description
=> "Unlock the VM.",
170 additionalProperties
=> 0,
172 vmid
=> get_standard_option
('pve-vmid'),
175 returns
=> { type
=> 'null'},
179 my $vmid = $param->{vmid
};
181 PVE
::QemuServer
::lock_config
($vmid, sub {
182 PVE
::QemuServer
::change_config_nolock
($vmid, {}, { lock => 1 }, 1);
188 __PACKAGE__-
>register_method ({
192 description
=> "Used by qmigrate - do not use manually.",
194 additionalProperties
=> 0,
197 returns
=> { type
=> 'null'},
201 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
206 print "tunnel online\n";
209 while (my $line = <>) {
211 last if $line =~ m/^quit$/;
217 __PACKAGE__-
>register_method ({
221 description
=> "Start all virtual machines (when onboot=1).",
223 additionalProperties
=> 0,
226 returns
=> { type
=> 'null'},
230 my $vzlist = PVE
::QemuServer
::vzlist
();
231 my $storecfg = PVE
::Storage
::config
();
233 PVE
::Cluster
::check_cfs_quorum
();
236 foreach my $vmid (keys %$vzlist) {
237 next if $vzlist->{$vmid}->{pid
}; # already running
239 sleep(2) if $count != 0; # reduce load
242 PVE
::Cluster
::check_cfs_quorum
(); # abort when we loose quorum
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);
251 print STDERR
$@ if $@;
257 __PACKAGE__-
>register_method ({
261 description
=> "Stop all virtual machines.",
263 additionalProperties
=> 0,
266 description
=> "Timeout in seconds. Default is to wait 3 minutes.",
273 returns
=> { type
=> 'null'},
277 my $timeout = $param->{timeout
};
279 PVE
::QemuServer
::vm_stopall
($timeout);
284 __PACKAGE__-
>register_method ({
288 description
=> "Wait until the VM is stopped.",
290 additionalProperties
=> 0,
292 vmid
=> get_standard_option
('pve-vmid'),
294 description
=> "Timeout in seconds. Default is to wait forever.",
301 returns
=> { type
=> 'null'},
305 my $vmid = $param->{vmid
};
306 my $timeout = $param->{timeout
};
308 my $pid = PVE
::QemuServer
::check_running
($vmid);
311 print "waiting until VM $vmid stopps (PID $pid)\n";
314 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
319 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
324 __PACKAGE__-
>register_method ({
328 description
=> "Enter Qemu Monitor interface.",
330 additionalProperties
=> 0,
332 vmid
=> get_standard_option
('pve-vmid'),
335 returns
=> { type
=> 'null'},
339 my $vmid = $param->{vmid
};
341 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
343 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
345 my $term = new Term
::ReadLine
('qm');
348 while (defined ($input = $term->readline('qm> '))) {
351 next if $input =~ m/^\s*$/;
353 last if $input =~ m/^\s*q(uit)?\s*$/;
356 print PVE
::QemuServer
::vm_monitor_command
($vmid, $input);
358 print "ERROR: $@" if $@;
366 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
367 { node
=> $nodename }, sub {
370 exit 0 if (!scalar(@$vmlist));
372 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
373 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
375 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
376 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
378 ($rec->{maxmem
} || 0)/(1024*1024),
379 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
386 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename } ],
388 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename } ],
390 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['target', 'vmid'], { node
=> $nodename },
393 my $status = PVE
::Tools
::upid_read_status
($upid);
394 exit($status eq 'OK' ?
0 : -1);
397 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
399 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node
=> $nodename } ],
401 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
402 { node
=> $nodename }, sub {
404 foreach my $k (sort (keys %$config)) {
405 next if $k eq 'digest';
406 my $v = $config->{$k};
407 if ($k eq 'description') {
408 $v = PVE
::Tools
::encode_text
($v);
414 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
416 status
=> [ __PACKAGE__
, 'status', ['vmid']],
418 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
420 wait => [ __PACKAGE__
, 'wait', ['vmid']],
422 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
424 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
426 startall
=> [ __PACKAGE__
, 'startall', []],
428 stopall
=> [ __PACKAGE__
, 'stopall', []],
430 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
433 sub register_vm_command
{
434 my ($cmd, $descr) = @_;
436 # we create a wrapper, because we want one 'description' per command
437 __PACKAGE__-
>register_method ({
441 description
=> $descr,
443 additionalProperties
=> 0,
445 vmid
=> get_standard_option
('pve-vmid'),
446 skiplock
=> get_standard_option
('skiplock'),
447 stateuri
=> get_standard_option
('pve-qm-stateuri'),
450 returns
=> { type
=> 'null'},
454 $param->{command
} = $cmd;
455 $param->{node
} = $nodename;
457 return PVE
::API2
::Qemu-
>vm_command($param);
460 $cmddef->{$cmd} = [ __PACKAGE__
, $cmd, ['vmid']];
463 register_vm_command
('start', "Start virtual machine.");
464 register_vm_command
('stop', "Stop virtual machine.");
465 register_vm_command
('reset', "Reset virtual machine.");
466 register_vm_command
('shutdown', "Shutdown virtual machine (send ACPI showdown request)");
467 register_vm_command
('suspend', "Suspend virtual machine.");
468 register_vm_command
('resume', "Resume virtual machine.");
469 register_vm_command
('cad', "Send CTRL-ALT-DELETE keys.");
473 PVE
::CLIHandler
::handle_cmd
($cmddef, "qm", $cmd, \
@ARGV, undef, $0);
481 qm - qemu/kvm virtual machine manager
489 qm is a script to manage virtual machines with qemu/kvm. You can
490 create and destroy virtual machines, and control execution
491 (start/stop/suspend/resume). Besides that, you can use qm to set
492 parameters in the associated config file. It is also possible to
493 create and delete virtual disks.
497 All configuration files consists of lines in the form
501 See L<vm.conf|vm.conf> for a complete list of options.
503 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
505 The default for option 'keyboard' is read from
506 F</etc/pve/datacenter.conf>.
510 Online migration and backups (vzdump) set a lock to prevent
511 unintentional action on such VMs. Sometimes you need remove such lock
512 manually (power failure).
518 # create a new VM with 4 GB ide disk
519 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
524 # send shutdown, then wait until VM is stopped
525 qm shutdown 300 && qm wait 300
527 # same as above, but only wait for 40 seconds
528 qm shutdown 300 && qm wait 300 -timeout 40
531 =include pve_copyright